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.portlet.PortletPreferencesFactoryUtil;
023    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
024    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
025    
026    import javax.portlet.PortletPreferences;
027    
028    /**
029     * @author Alexander Chow
030     */
031    public class UpgradeSearch extends BaseUpgradePortletPreferences {
032    
033            @Override
034            protected String[] getPortletIds() {
035                    return new String[] {"3"};
036            }
037    
038            protected JSONObject upgradeDataJSONObject(JSONObject dataJSONObject)
039                    throws Exception {
040    
041                    JSONArray valuesJSONArray = dataJSONObject.getJSONArray("values");
042    
043                    boolean hasBookmarksEntry = false;
044                    boolean hasDLFileEntry = false;
045                    boolean hasJournalArticle = false;
046    
047                    for (int i = 0; i < valuesJSONArray.length(); i++) {
048                            String value = valuesJSONArray.getString(i);
049    
050                            if (value.equals(
051                                            "com.liferay.portlet.bookmarks.model.BookmarksEntry")) {
052    
053                                    hasBookmarksEntry = true;
054                            }
055    
056                            if (value.equals(DLFileEntryConstants.getClassName())) {
057                                    hasDLFileEntry = true;
058                            }
059    
060                            if (value.equals(
061                                            "com.liferay.portlet.journal.model.JournalArticle")) {
062    
063                                    hasJournalArticle = true;
064                            }
065                    }
066    
067                    if (!hasBookmarksEntry && !hasDLFileEntry && !hasJournalArticle) {
068                            return null;
069                    }
070    
071                    if (hasBookmarksEntry) {
072                            valuesJSONArray.put(
073                                    "com.liferay.portlet.bookmarks.model.BookmarksFolder");
074                    }
075    
076                    if (hasDLFileEntry) {
077                            valuesJSONArray.put(DLFolderConstants.getClassName());
078                    }
079    
080                    if (hasJournalArticle) {
081                            valuesJSONArray.put(
082                                    "com.liferay.portlet.journal.model.JournalFolder");
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    }