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.AutoBatchPreparedStatementUtil;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.util.LoggingTimer;
020    import com.liferay.portal.kernel.xml.Document;
021    import com.liferay.portal.kernel.xml.Element;
022    import com.liferay.portal.kernel.xml.SAXReaderUtil;
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(
057                                            "com.liferay.portlet.kernel.staging.Staging")) {
058    
059                                    newRootElement.add(preferenceElement.createCopy());
060                            }
061                    }
062    
063                    return XMLUtil.formatXML(newDocument);
064            }
065    
066            @Override
067            protected void doUpgrade() throws Exception {
068                    upgradeStagingPortalPreferences();
069            }
070    
071            protected void upgradeStagingPortalPreferences() throws Exception {
072                    try (LoggingTimer loggingTimer = new LoggingTimer();
073                            PreparedStatement ps1 = connection.prepareStatement(
074                                    "select portalPreferencesId, preferences from " +
075                                            "PortalPreferences");
076                            ResultSet rs = ps1.executeQuery();
077                            PreparedStatement ps2 =
078                                    AutoBatchPreparedStatementUtil.concurrentAutoBatch(
079                                            connection,
080                                            "update PortalPreferences set preferences = ? " +
081                                                    "where portalPreferencesId = ?")) {
082    
083                            while (rs.next()) {
084                                    long portalPreferencesId = rs.getLong("portalPreferencesId");
085    
086                                    String preferences = rs.getString("preferences");
087    
088                                    ps2.setString(1, convertStagingPreferencesToJSON(preferences));
089                                    ps2.setLong(2, portalPreferencesId);
090    
091                                    ps2.addBatch();
092                            }
093    
094                            ps2.executeBatch();
095                    }
096            }
097    
098    }