001    /**
002     * Copyright (c) 2000-2013 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.bookmarks.model.BookmarksEntry;
025    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
026    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
027    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
028    import com.liferay.portlet.journal.model.JournalArticle;
029    import com.liferay.portlet.journal.model.JournalFolder;
030    
031    import javax.portlet.PortletPreferences;
032    
033    /**
034     * @author Alexander Chow
035     */
036    public class UpgradeSearch extends BaseUpgradePortletPreferences {
037    
038            @Override
039            protected String[] getPortletIds() {
040                    return new String[] {PortletKeys.SEARCH};
041            }
042    
043            protected JSONObject upgradeDataJSONObject(JSONObject dataJSONObject)
044                    throws Exception {
045    
046                    JSONArray valuesJSONArray = dataJSONObject.getJSONArray("values");
047    
048                    boolean hasBookmarksEntry = false;
049                    boolean hasDLFileEntry = false;
050                    boolean hasJournalArticle = false;
051    
052                    for (int i = 0; i < valuesJSONArray.length(); i++) {
053                            String value = valuesJSONArray.getString(i);
054    
055                            if (value.equals(BookmarksEntry.class.getName())) {
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(BookmarksFolder.class.getName());
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    }