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.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.xml.Document;
020    import com.liferay.portal.kernel.xml.Element;
021    import com.liferay.portal.kernel.xml.SAXReaderUtil;
022    import com.liferay.portlet.exportimport.staging.Staging;
023    import com.liferay.util.xml.XMLUtil;
024    
025    import java.sql.PreparedStatement;
026    import java.sql.ResultSet;
027    
028    import java.util.Iterator;
029    
030    /**
031     * @author Joshua Gok
032     */
033    public class UpgradePortalPreferences extends UpgradeProcess {
034    
035            protected String convertStagingPreferencesToJSON(String preferences)
036                    throws Exception {
037    
038                    Document newDocument = SAXReaderUtil.createDocument();
039    
040                    Element newRootElement = SAXReaderUtil.createElement(
041                            "portlet-preferences");
042    
043                    newDocument.add(newRootElement);
044    
045                    Document document = SAXReaderUtil.read(preferences);
046    
047                    Element rootElement = document.getRootElement();
048    
049                    Iterator<Element> iterator = rootElement.elementIterator();
050    
051                    while (iterator.hasNext()) {
052                            Element preferenceElement = iterator.next();
053    
054                            String preferenceName = preferenceElement.elementText("name");
055    
056                            if (!preferenceName.contains(Staging.class.getName())) {
057                                    newRootElement.add(preferenceElement.createCopy());
058                            }
059                    }
060    
061                    return XMLUtil.formatXML(newDocument);
062            }
063    
064            @Override
065            protected void doUpgrade() throws Exception {
066                    upgradePortalPreferences();
067            }
068    
069            protected void upgradePortalPreferences() throws Exception {
070                    PreparedStatement ps = null;
071                    ResultSet rs = null;
072    
073                    try {
074                            ps = connection.prepareStatement(
075                                    "select portalPreferencesId, preferences from " +
076                                            "PortalPreferences");
077    
078                            rs = ps.executeQuery();
079    
080                            while (rs.next()) {
081                                    long portalPreferencesId = rs.getLong("portalPreferencesId");
082    
083                                    String preferences = rs.getString("preferences");
084    
085                                    upgradeUserStagingPreferences(portalPreferencesId, preferences);
086                            }
087                    }
088                    finally {
089                            DataAccess.cleanUp(ps, rs);
090                    }
091            }
092    
093            protected void upgradeUserStagingPreferences(
094                            long portalPreferencesId, String preferences)
095                    throws Exception {
096    
097                    PreparedStatement ps = null;
098    
099                    try {
100                            ps = connection.prepareStatement(
101                                    "update PortalPreferences set preferences = ? where " +
102                                            "portalPreferencesId = ?");
103    
104                            ps.setString(1, convertStagingPreferencesToJSON(preferences));
105                            ps.setLong(2, portalPreferencesId);
106                            ps.executeUpdate();
107                    }
108                    finally {
109                            DataAccess.cleanUp(ps);
110                    }
111            }
112    
113    }