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.verify;
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.staging.Staging;
022    import com.liferay.portal.kernel.staging.StagingConstants;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.xml.Document;
025    import com.liferay.portal.kernel.xml.Element;
026    import com.liferay.portal.kernel.xml.SAXReaderUtil;
027    import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
028    
029    import java.sql.Connection;
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 VerifyPortalPreferences extends VerifyProcess {
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 =
059                            new HashMap<String, String>();
060    
061                    while (iterator.hasNext()) {
062                            Element preferenceElement = iterator.next();
063    
064                            String preferenceName = preferenceElement.elementText("name");
065    
066                            if (preferenceName.contains(Staging.class.getName())) {
067                                    String preferenceValue = preferenceElement.elementText("value");
068    
069                                    int index = preferenceName.indexOf(StringPool.POUND);
070    
071                                    stagingPreferencesMap.put(
072                                            preferenceName.substring(index + 1), preferenceValue);
073                            }
074                            else {
075                                    newRootElement.add(preferenceElement.createCopy());
076                            }
077                    }
078    
079                    JSONArray stagingPreferencesJsonArray =
080                            JSONFactoryUtil.createJSONArray();
081    
082                    for (String key : stagingPreferencesMap.keySet()) {
083                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
084    
085                            jsonObject.put(key, stagingPreferencesMap.get(key));
086    
087                            stagingPreferencesJsonArray.put(jsonObject);
088                    }
089    
090                    if (stagingPreferencesJsonArray.length() > 0) {
091                            Element preferenceElement = SAXReaderUtil.createElement(
092                                    "preference");
093    
094                            Element nameElement = SAXReaderUtil.createElement("name");
095    
096                            String stagingPreferencesName =
097                                    Staging.class.getName() + StringPool.POUND +
098                                    StagingConstants.STAGING_RECENT_LAYOUT_IDS_MAP;
099    
100                            nameElement.setText(stagingPreferencesName);
101    
102                            Element valueElement = SAXReaderUtil.createElement("value");
103    
104                            valueElement.setText(stagingPreferencesJsonArray.toString());
105    
106                            preferenceElement.add(nameElement);
107                            preferenceElement.add(valueElement);
108    
109                            newRootElement.add(preferenceElement);
110                    }
111    
112                    return DDMXMLUtil.formatXML(newDocument);
113            }
114    
115            @Override
116            protected void doVerify() throws Exception {
117                    updatePortalPreferences();
118            }
119    
120            protected void updatePortalPreferences() throws Exception {
121                    Connection con = null;
122                    PreparedStatement ps = null;
123                    ResultSet rs = null;
124    
125                    try {
126                            con = DataAccess.getUpgradeOptimizedConnection();
127    
128                            ps = con.prepareStatement(
129                                    "select portalPreferencesId, preferences from " +
130                                            "PortalPreferences");
131    
132                            rs = ps.executeQuery();
133    
134                            while (rs.next()) {
135                                    long portalPreferencesId = rs.getLong("portalPreferencesId");
136    
137                                    String preferences = rs.getString("preferences");
138    
139                                    updateUserStagingPreferences(portalPreferencesId, preferences);
140                            }
141                    }
142                    finally {
143                            DataAccess.cleanUp(con, ps, rs);
144                    }
145            }
146    
147            protected void updateUserStagingPreferences(
148                            long portalPreferencesId, String preferences)
149                    throws Exception {
150    
151                    Connection con = null;
152                    PreparedStatement ps = null;
153    
154                    try {
155                            con = DataAccess.getUpgradeOptimizedConnection();
156    
157                            ps = con.prepareStatement(
158                                    "update PortalPreferences set preferences = ? where " +
159                                            "portalPreferencesId = ?");
160    
161                            ps.setString(1, convertStagingPreferencesToJSON(preferences));
162                            ps.setLong(2, portalPreferencesId);
163                            ps.executeUpdate();
164                    }
165                    finally {
166                            DataAccess.cleanUp(con, ps);
167                    }
168            }
169    
170    }