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.v7_0_0;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.json.JSONArray;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.xml.Document;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.kernel.xml.SAXReaderUtil;
026    import com.liferay.portlet.exportimport.staging.Staging;
027    import com.liferay.portlet.exportimport.staging.StagingConstants;
028    import com.liferay.util.xml.XMLUtil;
029    
030    import java.sql.PreparedStatement;
031    import java.sql.ResultSet;
032    
033    import java.util.HashMap;
034    import java.util.Iterator;
035    import java.util.Map;
036    
037    /**
038     * @author Joshua Gok
039     */
040    public class UpgradePortalPreferences extends UpgradeProcess {
041    
042            protected String convertStagingPreferencesToJSON(String preferences)
043                    throws Exception {
044    
045                    Document newDocument = SAXReaderUtil.createDocument();
046    
047                    Element newRootElement = SAXReaderUtil.createElement(
048                            "portlet-preferences");
049    
050                    newDocument.add(newRootElement);
051    
052                    Document document = SAXReaderUtil.read(preferences);
053    
054                    Element rootElement = document.getRootElement();
055    
056                    Iterator<Element> iterator = rootElement.elementIterator();
057    
058                    Map<String, String> stagingPreferencesMap = new HashMap<>();
059    
060                    while (iterator.hasNext()) {
061                            Element preferenceElement = iterator.next();
062    
063                            String preferenceName = preferenceElement.elementText("name");
064    
065                            if (preferenceName.contains(Staging.class.getName())) {
066                                    String preferenceValue = preferenceElement.elementText("value");
067    
068                                    int index = preferenceName.indexOf(StringPool.POUND);
069    
070                                    stagingPreferencesMap.put(
071                                            preferenceName.substring(index + 1), preferenceValue);
072                            }
073                            else {
074                                    newRootElement.add(preferenceElement.createCopy());
075                            }
076                    }
077    
078                    JSONArray stagingPreferencesJsonArray =
079                            JSONFactoryUtil.createJSONArray();
080    
081                    for (String key : stagingPreferencesMap.keySet()) {
082                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
083    
084                            jsonObject.put(key, stagingPreferencesMap.get(key));
085    
086                            stagingPreferencesJsonArray.put(jsonObject);
087                    }
088    
089                    if (stagingPreferencesJsonArray.length() > 0) {
090                            Element preferenceElement = SAXReaderUtil.createElement(
091                                    "preference");
092    
093                            Element nameElement = SAXReaderUtil.createElement("name");
094    
095                            String stagingPreferencesName =
096                                    Staging.class.getName() + StringPool.POUND +
097                                    StagingConstants.STAGING_RECENT_LAYOUT_IDS_MAP;
098    
099                            nameElement.setText(stagingPreferencesName);
100    
101                            Element valueElement = SAXReaderUtil.createElement("value");
102    
103                            valueElement.setText(stagingPreferencesJsonArray.toString());
104    
105                            preferenceElement.add(nameElement);
106                            preferenceElement.add(valueElement);
107    
108                            newRootElement.add(preferenceElement);
109                    }
110    
111                    return XMLUtil.formatXML(newDocument);
112            }
113    
114            @Override
115            protected void doUpgrade() throws Exception {
116                    upgradePortalPreferences();
117            }
118    
119            protected void upgradePortalPreferences() throws Exception {
120                    PreparedStatement ps = null;
121                    ResultSet rs = null;
122    
123                    try {
124                            ps = connection.prepareStatement(
125                                    "select portalPreferencesId, preferences from " +
126                                            "PortalPreferences");
127    
128                            rs = ps.executeQuery();
129    
130                            while (rs.next()) {
131                                    long portalPreferencesId = rs.getLong("portalPreferencesId");
132    
133                                    String preferences = rs.getString("preferences");
134    
135                                    upgradeUserStagingPreferences(portalPreferencesId, preferences);
136                            }
137                    }
138                    finally {
139                            DataAccess.cleanUp(ps, rs);
140                    }
141            }
142    
143            protected void upgradeUserStagingPreferences(
144                            long portalPreferencesId, String preferences)
145                    throws Exception {
146    
147                    PreparedStatement ps = null;
148    
149                    try {
150                            ps = connection.prepareStatement(
151                                    "update PortalPreferences set preferences = ? where " +
152                                            "portalPreferencesId = ?");
153    
154                            ps.setString(1, convertStagingPreferencesToJSON(preferences));
155                            ps.setLong(2, portalPreferencesId);
156                            ps.executeUpdate();
157                    }
158                    finally {
159                            DataAccess.cleanUp(ps);
160                    }
161            }
162    
163    }