001    /**
002     * Copyright (c) 2000-2012 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                    updatePortalPreferences();
092                    updatePortletPreferences();
093                    updatePortletPreferencesOwner();
094                    upgrade(UpgradeCommunityProperties.class);
095    
096                    runSQL(
097                            "create index IX_D1F795F1 on PortalPreferences (ownerId, " +
098                                    "ownerType)");
099            }
100    
101            protected long getOwnerId(long plid) throws Exception {
102                    Connection con = null;
103                    PreparedStatement ps = null;
104                    ResultSet rs = null;
105    
106                    try {
107                            con = DataAccess.getUpgradeOptimizedConnection();
108    
109                            ps = con.prepareStatement(
110                                    "select groupId from Layout where plid = " + plid);
111    
112                            rs = ps.executeQuery();
113    
114                            if (rs.next()) {
115                                    return rs.getLong("groupId");
116                            }
117                    }
118                    finally {
119                            DataAccess.cleanUp(con, ps, rs);
120                    }
121    
122                    return 0;
123            }
124    
125            @Override
126            protected String[] getPortletIds() {
127                    return _CAMEL_CASE_UPGRADE_PORTLET_IDS;
128            }
129    
130            protected long getPortletPreferencesId(
131                            long ownerId, int ownerType, long plid, String portletId)
132                    throws Exception {
133    
134                    Connection con = null;
135                    PreparedStatement ps = null;
136                    ResultSet rs = null;
137    
138                    try {
139                            con = DataAccess.getUpgradeOptimizedConnection();
140    
141                            ps = con.prepareStatement(
142                                    "select portletPreferencesId from PortletPreferences where " +
143                                            "ownerId = ? and ownerType = ? and plid = ? and " +
144                                                    "portletId = ?");
145    
146                            ps.setLong(1, ownerId);
147                            ps.setInt(2, ownerType);
148                            ps.setLong(3, plid);
149                            ps.setString(4, portletId);
150    
151                            rs = ps.executeQuery();
152    
153                            if (rs.next()) {
154                                    return rs.getLong("portletPreferencesId");
155                            }
156                    }
157                    finally {
158                            DataAccess.cleanUp(con, ps, rs);
159                    }
160    
161                    return 0;
162            }
163    
164            protected void updatePortalPreferences() throws Exception {
165                    Connection con = null;
166                    PreparedStatement ps = null;
167                    ResultSet rs = null;
168    
169                    try {
170                            con = DataAccess.getUpgradeOptimizedConnection();
171    
172                            ps = con.prepareStatement(
173                                    "select ownerId, ownerType, preferences from " +
174                                            "PortletPreferences where portletId = ?");
175    
176                            ps.setString(1, PortletKeys.LIFERAY_PORTAL);
177    
178                            rs = ps.executeQuery();
179    
180                            while (rs.next()) {
181                                    long ownerId = rs.getLong("ownerId");
182                                    int ownerType = rs.getInt("ownerType");
183                                    String preferences = rs.getString("preferences");
184    
185                                    addPortalPreferences(ownerId, ownerType, preferences);
186                            }
187    
188                            runSQL(
189                                    "delete from PortletPreferences where portletId = '" +
190                                            PortletKeys.LIFERAY_PORTAL + "'");
191                    }
192                    finally {
193                            DataAccess.cleanUp(con, ps, rs);
194                    }
195            }
196    
197            protected void updatePortletPreferencesOwner() throws Exception {
198                    Connection con = null;
199                    PreparedStatement ps = null;
200                    ResultSet rs = null;
201    
202                    try {
203                            con = DataAccess.getUpgradeOptimizedConnection();
204    
205                            StringBundler sb = new StringBundler(8);
206    
207                            sb.append("select portletPreferencesId, plid, portletId, ");
208                            sb.append("preferences from PortletPreferences where ownerId = ");
209                            sb.append(PortletKeys.PREFS_OWNER_ID_DEFAULT);
210                            sb.append(" and ownerType = ");
211                            sb.append(PortletKeys.PREFS_OWNER_TYPE_LAYOUT);
212                            sb.append(" and portletId in ('8', '19', '33')");
213    
214                            String sql = sb.toString();
215    
216                            ps = con.prepareStatement(sql);
217    
218                            rs = ps.executeQuery();
219    
220                            while (rs.next()) {
221                                    long plid = rs.getLong("plid");
222                                    String portletId = rs.getString("portletId");
223                                    String preferences = rs.getString("preferences");
224    
225                                    long ownerId = getOwnerId(plid);
226    
227                                    if (ownerId == 0) {
228                                            continue;
229                                    }
230    
231                                    long portletPreferencesId = getPortletPreferencesId(
232                                            ownerId, PortletKeys.PREFS_OWNER_TYPE_GROUP,
233                                            PortletKeys.PREFS_PLID_SHARED, portletId);
234    
235                                    if (portletPreferencesId != 0) {
236                                            continue;
237                                    }
238    
239                                    addPortletPreferences(
240                                            ownerId, PortletKeys.PREFS_OWNER_TYPE_GROUP,
241                                            PortletKeys.PREFS_PLID_SHARED, portletId, preferences);
242                            }
243                    }
244                    finally {
245                            DataAccess.cleanUp(con, ps, rs);
246                    }
247            }
248    
249            private static final String[] _CAMEL_CASE_UPGRADE_PORTLET_IDS = {
250                    "8", "15", "19", "20", "33", "34", "36", "39_INSTANCE_%",
251                    "47_INSTANCE_%", "48_INSTANCE_%", "54_INSTANCE_%", "56_INSTANCE_%",
252                    "59_INSTANCE_%", "62_INSTANCE_%", "71_INSTANCE_%", "73_INSTANCE_%",
253                    "77", "82_INSTANCE_%", "85_INSTANCE_%", "100", "101_INSTANCE_%",
254                    "102_INSTANCE_%", "114", "115", "118_INSTANCE_%", "122_INSTANCE_%"
255            };
256    
257    }