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