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.v6_1_0;
016    
017    import com.liferay.portal.kernel.upgrade.CamelCaseUpgradePortletPreferences;
018    import com.liferay.portal.kernel.util.LoggingTimer;
019    import com.liferay.portal.kernel.util.PortletKeys;
020    import com.liferay.portal.kernel.util.StringBundler;
021    
022    import java.sql.PreparedStatement;
023    import java.sql.ResultSet;
024    
025    /**
026     * @author Julio Camarero
027     * @author Douglas Wong
028     */
029    public class UpgradePortletPreferences
030            extends CamelCaseUpgradePortletPreferences {
031    
032            protected void addPortalPreferences(
033                            long ownerId, int ownerType, String preferences)
034                    throws Exception {
035    
036                    try (PreparedStatement ps = connection.prepareStatement(
037                                    "insert into PortalPreferences (portalPreferencesId, " +
038                                            "ownerId, ownerType, preferences) values (?, ?, ?, ?)")) {
039    
040                            ps.setLong(1, increment());
041                            ps.setLong(2, ownerId);
042                            ps.setInt(3, ownerType);
043                            ps.setString(4, preferences);
044    
045                            ps.executeUpdate();
046                    }
047            }
048    
049            protected void addPortletPreferences(
050                            long ownerId, int ownerType, long plid, String portletId,
051                            String preferences)
052                    throws Exception {
053    
054                    try (PreparedStatement ps = connection.prepareStatement(
055                                    "insert into PortletPreferences (portletPreferencesId, " +
056                                            "ownerId, ownerType, plid, portletId, preferences) " +
057                                                    "values (?, ?, ?, ?, ?, ?)")) {
058    
059                            ps.setLong(1, increment());
060                            ps.setLong(2, ownerId);
061                            ps.setInt(3, ownerType);
062                            ps.setLong(4, plid);
063                            ps.setString(5, portletId);
064                            ps.setString(6, preferences);
065    
066                            ps.executeUpdate();
067                    }
068            }
069    
070            protected void createIndex() throws Exception {
071                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
072                            runSQL(
073                                    "create index IX_D1F795F1 on PortalPreferences (ownerId, " +
074                                            "ownerType)");
075                    }
076            }
077    
078            @Override
079            protected void doUpgrade() throws Exception {
080                    super.doUpgrade();
081    
082                    updatePortalPreferences();
083                    updatePortletPreferencesOwner();
084    
085                    upgrade(UpgradeCommunityProperties.class);
086    
087                    createIndex();
088            }
089    
090            protected long getOwnerId(long plid) throws Exception {
091                    try (PreparedStatement ps = connection.prepareStatement(
092                                    "select groupId from Layout where plid = " + plid);
093                            ResultSet rs = ps.executeQuery()) {
094    
095                            if (rs.next()) {
096                                    return rs.getLong("groupId");
097                            }
098                    }
099    
100                    return 0;
101            }
102    
103            @Override
104            protected String[] getPortletIds() {
105                    return _CAMEL_CASE_UPGRADE_PORTLET_IDS;
106            }
107    
108            protected long getPortletPreferencesId(
109                            long ownerId, int ownerType, long plid, String portletId)
110                    throws Exception {
111    
112                    try (PreparedStatement ps = connection.prepareStatement(
113                                    "select portletPreferencesId from PortletPreferences where " +
114                                            "ownerId = ? and ownerType = ? and plid = ? and " +
115                                                    "portletId = ?")) {
116    
117                            ps.setLong(1, ownerId);
118                            ps.setInt(2, ownerType);
119                            ps.setLong(3, plid);
120                            ps.setString(4, portletId);
121    
122                            try (ResultSet rs = ps.executeQuery()) {
123                                    if (rs.next()) {
124                                            return rs.getLong("portletPreferencesId");
125                                    }
126                            }
127                    }
128    
129                    return 0;
130            }
131    
132            protected void updatePortalPreferences() throws Exception {
133                    try (LoggingTimer loggingTimer = new LoggingTimer();
134                            PreparedStatement ps = connection.prepareStatement(
135                                    "select ownerId, ownerType, preferences from " +
136                                            "PortletPreferences where portletId = ?")) {
137    
138                            ps.setString(1, PortletKeys.LIFERAY_PORTAL);
139    
140                            try (ResultSet rs = ps.executeQuery()) {
141                                    while (rs.next()) {
142                                            long ownerId = rs.getLong("ownerId");
143                                            int ownerType = rs.getInt("ownerType");
144                                            String preferences = rs.getString("preferences");
145    
146                                            addPortalPreferences(ownerId, ownerType, preferences);
147                                    }
148    
149                                    runSQL(
150                                            "delete from PortletPreferences where portletId = '" +
151                                                    PortletKeys.LIFERAY_PORTAL + "'");
152                            }
153                    }
154            }
155    
156            protected void updatePortletPreferencesOwner() throws Exception {
157                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
158                            StringBundler sb = new StringBundler(6);
159    
160                            sb.append("select portletPreferencesId, plid, portletId, ");
161                            sb.append("preferences from PortletPreferences where ownerId = ");
162                            sb.append(PortletKeys.PREFS_OWNER_ID_DEFAULT);
163                            sb.append(" and ownerType = ");
164                            sb.append(PortletKeys.PREFS_OWNER_TYPE_LAYOUT);
165                            sb.append(" and portletId in ('8', '19', '33')");
166    
167                            try (PreparedStatement ps = connection.prepareStatement(
168                                            sb.toString());
169                                    ResultSet rs = ps.executeQuery()) {
170    
171                                    while (rs.next()) {
172                                            long plid = rs.getLong("plid");
173                                            String portletId = rs.getString("portletId");
174                                            String preferences = rs.getString("preferences");
175    
176                                            long ownerId = getOwnerId(plid);
177    
178                                            if (ownerId == 0) {
179                                                    continue;
180                                            }
181    
182                                            long portletPreferencesId = getPortletPreferencesId(
183                                                    ownerId, PortletKeys.PREFS_OWNER_TYPE_GROUP,
184                                                    PortletKeys.PREFS_PLID_SHARED, portletId);
185    
186                                            if (portletPreferencesId != 0) {
187                                                    continue;
188                                            }
189    
190                                            addPortletPreferences(
191                                                    ownerId, PortletKeys.PREFS_OWNER_TYPE_GROUP,
192                                                    PortletKeys.PREFS_PLID_SHARED, portletId, preferences);
193                                    }
194                            }
195                    }
196            }
197    
198            private static final String[] _CAMEL_CASE_UPGRADE_PORTLET_IDS = {
199                    "15", "19", "20", "33", "34", "36", "39_INSTANCE_%", "47_INSTANCE_%",
200                    "48_INSTANCE_%", "54_INSTANCE_%", "56_INSTANCE_%", "59_INSTANCE_%",
201                    "62_INSTANCE_%", "71_INSTANCE_%", "73_INSTANCE_%", "77",
202                    "82_INSTANCE_%", "85_INSTANCE_%", "100", "101_INSTANCE_%",
203                    "102_INSTANCE_%", "114", "115", "118_INSTANCE_%", "122_INSTANCE_%"
204            };
205    
206    }