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.portlet.PortletPreferencesFactoryUtil;
021    import com.liferay.portal.kernel.upgrade.BaseUpgradePortletPreferences;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import javax.portlet.PortletPreferences;
025    
026    /**
027     * @author Alexander Chow
028     */
029    public class UpgradeSearch extends BaseUpgradePortletPreferences {
030    
031            @Override
032            protected String[] getPortletIds() {
033                    return new String[] {"3"};
034            }
035    
036            protected JSONObject upgradeDataJSONObject(JSONObject dataJSONObject)
037                    throws Exception {
038    
039                    JSONArray valuesJSONArray = dataJSONObject.getJSONArray("values");
040    
041                    boolean hasBookmarksEntry = false;
042                    boolean hasDLFileEntry = false;
043                    boolean hasJournalArticle = false;
044    
045                    for (int i = 0; i < valuesJSONArray.length(); i++) {
046                            String value = valuesJSONArray.getString(i);
047    
048                            if (value.equals(
049                                            "com.liferay.portlet.bookmarks.model.BookmarksEntry")) {
050    
051                                    hasBookmarksEntry = true;
052                            }
053    
054                            if (value.equals(
055                                            "com.liferay.portlet.documentlibrary.model.DLFileEntry")) {
056    
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(
078                                    "com.liferay.portlet.documentlibrary.model.DLFolder");
079                    }
080    
081                    if (hasJournalArticle) {
082                            valuesJSONArray.put(
083                                    "com.liferay.portlet.journal.model.JournalFolder");
084                    }
085    
086                    dataJSONObject.put("values", valuesJSONArray);
087    
088                    return dataJSONObject;
089            }
090    
091            @Override
092            protected String upgradePreferences(
093                            long companyId, long ownerId, int ownerType, long plid,
094                            String portletId, String xml)
095                    throws Exception {
096    
097                    PortletPreferences portletPreferences =
098                            PortletPreferencesFactoryUtil.fromXML(
099                                    companyId, ownerId, ownerType, plid, portletId, xml);
100    
101                    String searchConfiguration = portletPreferences.getValue(
102                            "searchConfiguration", null);
103    
104                    if (Validator.isNull(searchConfiguration)) {
105                            return null;
106                    }
107    
108                    JSONObject searchConfigurationJSONObject =
109                            JSONFactoryUtil.createJSONObject(searchConfiguration);
110    
111                    JSONArray oldFacetsJSONArray =
112                            searchConfigurationJSONObject.getJSONArray("facets");
113    
114                    if (oldFacetsJSONArray == null) {
115                            return null;
116                    }
117    
118                    JSONArray newFacetsJSONArray = JSONFactoryUtil.createJSONArray();
119    
120                    for (int i = 0; i < oldFacetsJSONArray.length(); i++) {
121                            JSONObject oldFacetJSONObject = oldFacetsJSONArray.getJSONObject(i);
122    
123                            String fieldName = oldFacetJSONObject.getString("fieldName");
124    
125                            if (fieldName.equals("entryClassName")) {
126                                    JSONObject oldDataJSONObject = oldFacetJSONObject.getJSONObject(
127                                            "data");
128    
129                                    JSONObject newDataJSONObject = upgradeDataJSONObject(
130                                            oldDataJSONObject);
131    
132                                    if (newDataJSONObject == null) {
133                                            return null;
134                                    }
135    
136                                    oldFacetJSONObject.put("data", newDataJSONObject);
137                            }
138    
139                            newFacetsJSONArray.put(oldFacetJSONObject);
140                    }
141    
142                    searchConfigurationJSONObject.put("facets", newFacetsJSONArray);
143    
144                    portletPreferences.setValue(
145                            "searchConfiguration", searchConfigurationJSONObject.toString());
146    
147                    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
148            }
149    
150    }