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