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