001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.upgrade.v6_2_0;
016    
017    import com.liferay.portal.kernel.json.JSONArray;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.upgrade.BaseUpgradePortletPreferences;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.util.PortletKeys;
023    import com.liferay.portlet.PortletPreferencesFactoryUtil;
024    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
025    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
026    import com.liferay.portlet.journal.model.JournalArticle;
027    import com.liferay.portlet.journal.model.JournalFolder;
028    
029    import javax.portlet.PortletPreferences;
030    
031    /**
032     * @author Alexander Chow
033     */
034    public class UpgradeSearch extends BaseUpgradePortletPreferences {
035    
036            @Override
037            protected String[] getPortletIds() {
038                    return new String[] {PortletKeys.SEARCH};
039            }
040    
041            protected JSONObject upgradeDataJSONObject(JSONObject dataJSONObject)
042                    throws Exception {
043    
044                    JSONArray valuesJSONArray = dataJSONObject.getJSONArray("values");
045    
046                    boolean hasBookmarksEntry = false;
047                    boolean hasDLFileEntry = false;
048                    boolean hasJournalArticle = false;
049    
050                    for (int i = 0; i < valuesJSONArray.length(); i++) {
051                            String value = valuesJSONArray.getString(i);
052    
053                            if (value.equals(
054                                            "com.liferay.portlet.bookmarks.model.BookmarksEntry")) {
055    
056                                    hasBookmarksEntry = true;
057                            }
058    
059                            if (value.equals(DLFileEntryConstants.getClassName())) {
060                                    hasDLFileEntry = true;
061                            }
062    
063                            if (value.equals(JournalArticle.class.getName())) {
064                                    hasJournalArticle = true;
065                            }
066                    }
067    
068                    if (!hasBookmarksEntry && !hasDLFileEntry && !hasJournalArticle) {
069                            return null;
070                    }
071    
072                    if (hasBookmarksEntry) {
073                            valuesJSONArray.put(
074                                    "com.liferay.portlet.bookmarks.model.BookmarksFolder");
075                    }
076    
077                    if (hasDLFileEntry) {
078                            valuesJSONArray.put(DLFolderConstants.getClassName());
079                    }
080    
081                    if (hasJournalArticle) {
082                            valuesJSONArray.put(JournalFolder.class.getName());
083                    }
084    
085                    dataJSONObject.put("values", valuesJSONArray);
086    
087                    return dataJSONObject;
088            }
089    
090            @Override
091            protected String upgradePreferences(
092                            long companyId, long ownerId, int ownerType, long plid,
093                            String portletId, String xml)
094                    throws Exception {
095    
096                    PortletPreferences portletPreferences =
097                            PortletPreferencesFactoryUtil.fromXML(
098                                    companyId, ownerId, ownerType, plid, portletId, xml);
099    
100                    String searchConfiguration = portletPreferences.getValue(
101                            "searchConfiguration", null);
102    
103                    if (Validator.isNull(searchConfiguration)) {
104                            return null;
105                    }
106    
107                    JSONObject searchConfigurationJSONObject =
108                            JSONFactoryUtil.createJSONObject(searchConfiguration);
109    
110                    JSONArray oldFacetsJSONArray =
111                            searchConfigurationJSONObject.getJSONArray("facets");
112    
113                    if (oldFacetsJSONArray == null) {
114                            return null;
115                    }
116    
117                    JSONArray newFacetsJSONArray = JSONFactoryUtil.createJSONArray();
118    
119                    for (int i = 0; i < oldFacetsJSONArray.length(); i++) {
120                            JSONObject oldFacetJSONObject = oldFacetsJSONArray.getJSONObject(i);
121    
122                            String fieldName = oldFacetJSONObject.getString("fieldName");
123    
124                            if (fieldName.equals("entryClassName")) {
125                                    JSONObject oldDataJSONObject = oldFacetJSONObject.getJSONObject(
126                                            "data");
127    
128                                    JSONObject newDataJSONObject = upgradeDataJSONObject(
129                                            oldDataJSONObject);
130    
131                                    if (newDataJSONObject == null) {
132                                            return null;
133                                    }
134    
135                                    oldFacetJSONObject.put("data", newDataJSONObject);
136                            }
137    
138                            newFacetsJSONArray.put(oldFacetJSONObject);
139                    }
140    
141                    searchConfigurationJSONObject.put("facets", newFacetsJSONArray);
142    
143                    portletPreferences.setValue(
144                            "searchConfiguration", searchConfigurationJSONObject.toString());
145    
146                    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
147            }
148    
149    }