001
014
015 package com.liferay.portal.kernel.upgrade;
016
017 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018 import com.liferay.portal.kernel.util.GetterUtil;
019 import com.liferay.portal.kernel.util.StringBundler;
020 import com.liferay.portal.kernel.util.StringPool;
021 import com.liferay.portal.kernel.util.Validator;
022 import com.liferay.portal.util.PortletKeys;
023
024 import java.sql.Connection;
025 import java.sql.PreparedStatement;
026 import java.sql.ResultSet;
027
028
032 public abstract class BaseUpgradePortletPreferences extends UpgradeProcess {
033
034 protected void deletePortletPreferences(long portletPreferencesId)
035 throws Exception {
036
037 runSQL(
038 "delete from PortletPreferences where portletPreferencesId = " +
039 portletPreferencesId);
040 }
041
042 @Override
043 protected void doUpgrade() throws Exception {
044 updatePortletPreferences();
045 }
046
047 protected long getCompanyId(long userId) throws Exception {
048 long companyId = 0;
049
050 Connection con = null;
051 PreparedStatement ps = null;
052 ResultSet rs = null;
053
054 try {
055 con = DataAccess.getUpgradeOptimizedConnection();
056
057 ps = con.prepareStatement(_GET_USER);
058
059 ps.setLong(1, userId);
060
061 rs = ps.executeQuery();
062
063 while (rs.next()) {
064 companyId = rs.getLong("companyId");
065 }
066 }
067 finally {
068 DataAccess.cleanUp(con, ps, rs);
069 }
070
071 return companyId;
072 }
073
074 protected Object[] getGroup(long groupId) throws Exception {
075 Object[] group = null;
076
077 Connection con = null;
078 PreparedStatement ps = null;
079 ResultSet rs = null;
080
081 try {
082 con = DataAccess.getUpgradeOptimizedConnection();
083
084 ps = con.prepareStatement(_GET_COMPANY_ID);
085
086 ps.setLong(1, groupId);
087
088 rs = ps.executeQuery();
089
090 while (rs.next()) {
091 long companyId = rs.getLong("companyId");
092
093 group = new Object[] {groupId, companyId};
094 }
095 }
096 finally {
097 DataAccess.cleanUp(con, ps, rs);
098 }
099
100 return group;
101 }
102
103 protected Object[] getLayout(long plid) throws Exception {
104 Object[] layout = null;
105
106 Connection con = null;
107 PreparedStatement ps = null;
108 ResultSet rs = null;
109
110 try {
111 con = DataAccess.getUpgradeOptimizedConnection();
112
113 ps = con.prepareStatement(_GET_LAYOUT);
114
115 ps.setLong(1, plid);
116
117 rs = ps.executeQuery();
118
119 while (rs.next()) {
120 long groupId = rs.getLong("groupId");
121 long companyId = rs.getLong("companyId");
122 boolean privateLayout = rs.getBoolean("privateLayout");
123 long layoutId = rs.getLong("layoutId");
124
125 layout = new Object[] {
126 groupId, companyId, privateLayout, layoutId};
127 }
128 }
129 finally {
130 DataAccess.cleanUp(con, ps, rs);
131 }
132
133 return layout;
134 }
135
136 protected String getLayoutUuid(long plid, long layoutId) throws Exception {
137 Object[] layout = getLayout(plid);
138
139 if (layout == null) {
140 return null;
141 }
142
143 String uuid = null;
144
145 Connection con = null;
146 PreparedStatement ps = null;
147 ResultSet rs = null;
148
149 try {
150 con = DataAccess.getUpgradeOptimizedConnection();
151
152 ps = con.prepareStatement(_GET_LAYOUT_UUID);
153
154 long groupId = (Long)layout[0];
155 boolean privateLayout = (Boolean)layout[2];
156
157 ps.setLong(1, groupId);
158 ps.setBoolean(2, privateLayout);
159 ps.setLong(3, layoutId);
160
161 rs = ps.executeQuery();
162
163 if (rs.next()) {
164 uuid = rs.getString("uuid_");
165 }
166 }
167 finally {
168 DataAccess.cleanUp(con, ps, rs);
169 }
170
171 return uuid;
172 }
173
174 protected String[] getPortletIds() {
175 return new String[0];
176 }
177
178 protected String getUpdatePortletPreferencesWhereClause() {
179 String[] portletIds = getPortletIds();
180
181 if (portletIds.length == 0) {
182 throw new IllegalArgumentException(
183 "Subclasses must override getPortletIds or " +
184 "getUpdatePortletPreferencesWhereClause");
185 }
186
187 StringBundler sb = new StringBundler(portletIds.length * 5 - 1);
188
189 for (int i = 0; i < portletIds.length; i++) {
190 String portletId = portletIds[i];
191
192 sb.append("portletId ");
193
194 if (portletId.contains(StringPool.PERCENT)) {
195 sb.append(" like '");
196 sb.append(portletId);
197 sb.append("'");
198 }
199 else {
200 sb.append(" = '");
201 sb.append(portletId);
202 sb.append("'");
203 }
204
205 if ((i + 1) < portletIds.length) {
206 sb.append(" or ");
207 }
208 }
209
210 return sb.toString();
211 }
212
213 protected void updatePortletPreferences() throws Exception {
214 Connection con = null;
215 PreparedStatement ps = null;
216 ResultSet rs = null;
217
218 try {
219 con = DataAccess.getUpgradeOptimizedConnection();
220
221 StringBundler sb = new StringBundler(4);
222
223 sb.append("select portletPreferencesId, ownerId, ownerType, ");
224 sb.append("plid, portletId, preferences from PortletPreferences");
225
226 String whereClause = getUpdatePortletPreferencesWhereClause();
227
228 if (Validator.isNotNull(whereClause)) {
229 sb.append(" where ");
230 sb.append(whereClause);
231 }
232
233 String sql = sb.toString();
234
235 ps = con.prepareStatement(sql);
236
237 rs = ps.executeQuery();
238
239 while (rs.next()) {
240 long portletPreferencesId = rs.getLong("portletPreferencesId");
241 long ownerId = rs.getLong("ownerId");
242 int ownerType = rs.getInt("ownerType");
243 long plid = rs.getLong("plid");
244 String portletId = rs.getString("portletId");
245 String preferences = GetterUtil.getString(
246 rs.getString("preferences"));
247
248 long companyId = 0;
249
250 if (ownerType == PortletKeys.PREFS_OWNER_TYPE_COMPANY) {
251 companyId = ownerId;
252 }
253 else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) {
254 Object[] group = getGroup(ownerId);
255
256 if (group != null) {
257 companyId = (Long)group[1];
258 }
259 }
260 else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_LAYOUT) {
261 Object[] layout = getLayout(plid);
262
263 if (layout != null) {
264 companyId = (Long)layout[1];
265 }
266 }
267 else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
268 companyId = getCompanyId(ownerId);
269 }
270
271 if (companyId > 0) {
272 String newPreferences = upgradePreferences(
273 companyId, ownerId, ownerType, plid, portletId,
274 preferences);
275
276 if (!preferences.equals(newPreferences)) {
277 updatePortletPreferences(
278 portletPreferencesId, newPreferences);
279 }
280 }
281 else {
282 deletePortletPreferences(portletPreferencesId);
283 }
284 }
285 }
286 finally {
287 DataAccess.cleanUp(con, ps, rs);
288 }
289 }
290
291 protected void updatePortletPreferences(
292 long portletPreferencesId, String preferences)
293 throws Exception {
294
295 Connection con = null;
296 PreparedStatement ps = null;
297
298 try {
299 con = DataAccess.getUpgradeOptimizedConnection();
300
301 ps = con.prepareStatement(
302 "update PortletPreferences set preferences = ? where " +
303 "portletPreferencesId = " + portletPreferencesId);
304
305 ps.setString(1, preferences);
306
307 ps.executeUpdate();
308 }
309 finally {
310 DataAccess.cleanUp(con, ps);
311 }
312 }
313
314 protected abstract String upgradePreferences(
315 long companyId, long ownerId, int ownerType, long plid,
316 String portletId, String xml)
317 throws Exception;
318
319 private static final String _GET_COMPANY_ID =
320 "select companyId from Group_ where groupId = ?";
321
322 private static final String _GET_LAYOUT =
323 "select groupId, companyId, privateLayout, layoutId from Layout " +
324 "where plid = ?";
325
326 private static final String _GET_LAYOUT_UUID =
327 "select uuid_ from Layout where groupId = ? and privateLayout = ? " +
328 "and layoutId = ?";
329
330 private static final String _GET_USER =
331 "select * from User_ where userId = ?";
332
333 }