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