001
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.UpgradeProcess;
019 import com.liferay.portal.kernel.util.LocaleUtil;
020 import com.liferay.portal.kernel.util.StringBundler;
021 import com.liferay.portal.kernel.util.StringPool;
022 import com.liferay.portal.model.LayoutSetPrototype;
023 import com.liferay.portal.model.ResourceConstants;
024 import com.liferay.portal.model.RoleConstants;
025 import com.liferay.portal.security.permission.ActionKeys;
026 import com.liferay.portal.upgrade.v6_1_0.util.UserGroupTemplateInfo;
027 import com.liferay.portal.util.PropsValues;
028
029 import java.sql.Connection;
030 import java.sql.PreparedStatement;
031 import java.sql.ResultSet;
032
033
036 public class UpgradeUserGroup extends UpgradeProcess {
037
038 protected long addGroup(
039 long companyId, long creatorUserId, String groupName,
040 long layoutSetPrototypeId)
041 throws Exception {
042
043 long layoutSetPrototypeClassNameId = getClassNameId(
044 LayoutSetPrototype.class.getName());
045
046 long groupId = getGroupId(
047 companyId, layoutSetPrototypeClassNameId, layoutSetPrototypeId);
048
049 if (groupId > 0) {
050 return groupId;
051 }
052
053 Connection con = null;
054 PreparedStatement ps = null;
055 ResultSet rs = null;
056
057 try {
058 con = DataAccess.getConnection();
059
060 StringBundler sb = new StringBundler(5);
061
062 sb.append("insert into Group_ (groupId, companyId, ");
063 sb.append("creatorUserId, classNameId, classPK, parentGroupId, ");
064 sb.append("liveGroupId, name, description, type_, typeSettings, ");
065 sb.append("friendlyURL, site, active_) values (?, ?, ?, ?, ?, 0, ");
066 sb.append("0, ?, ?, 0, ?, ?, ?, ?)");
067
068 String sql = sb.toString();
069
070 ps = con.prepareStatement(sql);
071
072 groupId = increment();
073
074 ps.setLong(1, groupId);
075 ps.setLong(2, companyId);
076 ps.setLong(3, creatorUserId);
077 ps.setLong(4, layoutSetPrototypeClassNameId);
078 ps.setLong(5, layoutSetPrototypeId);
079 ps.setString(6, groupId + StringPool.MINUS + groupName);
080 ps.setString(7, StringPool.BLANK);
081 ps.setString(8, StringPool.BLANK);
082 ps.setString(9, "/template-" + layoutSetPrototypeId);
083 ps.setBoolean(10, false);
084 ps.setBoolean(11, true);
085
086 ps.execute();
087
088 return groupId;
089 }
090 finally {
091 DataAccess.cleanUp(con, ps, rs);
092 }
093 }
094
095 protected long addLayoutSet(
096 long layoutSetId, long companyId, long groupId,
097 long layoutSetPrototypeId,
098 UserGroupTemplateInfo userGroupTemplateInfo)
099 throws Exception {
100
101 Connection con = null;
102 PreparedStatement ps = null;
103 ResultSet rs = null;
104
105 try {
106 con = DataAccess.getConnection();
107
108 StringBundler sb = new StringBundler(5);
109
110 sb.append("insert into LayoutSet (layoutSetId, groupId, ");
111 sb.append("companyId, privateLayout, logo, logoId, themeId, ");
112 sb.append("colorSchemeId, wapThemeId, wapColorSchemeId, css, ");
113 sb.append("pageCount, settings_, layoutSetPrototypeId) values ");
114 sb.append("(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
115
116 String sql = sb.toString();
117
118 ps = con.prepareStatement(sql);
119
120 ps.setLong(1, layoutSetId);
121 ps.setLong(2, groupId);
122 ps.setLong(3, companyId);
123 ps.setBoolean(4, userGroupTemplateInfo.isPrivateLayout());
124 ps.setShort(5, userGroupTemplateInfo.getLogo());
125 ps.setLong(6, userGroupTemplateInfo.getLogoId());
126 ps.setString(7, userGroupTemplateInfo.getThemeId());
127 ps.setString(8, userGroupTemplateInfo.getColorSchemeId());
128 ps.setString(9, userGroupTemplateInfo.getWapThemeId());
129 ps.setString(10, userGroupTemplateInfo.getWapColorSchemeId());
130 ps.setString(11, userGroupTemplateInfo.getCss());
131 ps.setLong(12, userGroupTemplateInfo.getPageCount());
132 ps.setString(13, userGroupTemplateInfo.getSettings());
133 ps.setLong(14, layoutSetPrototypeId);
134
135 ps.execute();
136
137 return layoutSetId;
138
139 }
140 finally {
141 DataAccess.cleanUp(con, ps, rs);
142 }
143 }
144
145 protected void addLayoutSetPrototype(
146 long layoutSetPrototypeId, long companyId, String name)
147 throws Exception {
148
149 Connection con = null;
150 PreparedStatement ps = null;
151 ResultSet rs = null;
152
153 try {
154 con = DataAccess.getConnection();
155
156 ps = con.prepareStatement(
157 "insert into LayoutSetPrototype (layoutSetPrototypeId, " +
158 "companyId, name, description, active_) values (?, ?, ?, " +
159 "?, ?)");
160
161 ps.setLong(1, layoutSetPrototypeId);
162 ps.setLong(2, companyId);
163 ps.setString(3, getNameXML(name));
164 ps.setString(4, name);
165 ps.setBoolean(5, true);
166
167 ps.execute();
168 }
169 finally {
170 DataAccess.cleanUp(con, ps, rs);
171 }
172 }
173
174 protected void addPermission(
175 long permissionId, long companyId, String actionId, long resourceId)
176 throws Exception {
177
178 Connection con = null;
179 PreparedStatement ps = null;
180
181 try {
182 con = DataAccess.getConnection();
183
184 ps = con.prepareStatement(
185 "insert into Permission_ (permissionId, companyId, actionId, " +
186 "resourceId) values (?, ?, ?, ?)");
187
188 ps.setLong(1, permissionId);
189 ps.setLong(2, companyId);
190 ps.setString(3, actionId);
191 ps.setLong(4, resourceId);
192
193 ps.executeUpdate();
194 }
195 finally {
196 DataAccess.cleanUp(con, ps);
197 }
198 }
199
200 protected void addPermissions(
201 long companyId, long userId, long layoutSetPrototypeId)
202 throws Exception {
203
204 if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
205 addResourcePermission(
206 increment(), companyId, LayoutSetPrototype.class.getName(),
207 layoutSetPrototypeId, userId);
208 }
209 else if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
210 addResource(increment(), increment(), companyId);
211
212 long resourceId = increment();
213
214 addResource(resourceId, increment(), layoutSetPrototypeId);
215
216 String[] actionIds = {
217 ActionKeys.DELETE, ActionKeys.PERMISSIONS, ActionKeys.UPDATE,
218 ActionKeys.VIEW
219 };
220
221 for (String actionId : actionIds) {
222 addPermission(increment(), companyId, actionId, resourceId);
223 }
224 }
225 }
226
227 protected long addResource(long resourceId, long codeId, long primKey)
228 throws Exception {
229
230 Connection con = null;
231 PreparedStatement ps = null;
232
233 try {
234 con = DataAccess.getConnection();
235
236 ps = con.prepareStatement(
237 "insert into Resource_ (resourceId, codeId, primKey) values " +
238 "(?, ?, ?)");
239
240 ps.setLong(1, resourceId);
241 ps.setLong(2, codeId);
242 ps.setString(3, String.valueOf(primKey));
243
244 ps.executeUpdate();
245 }
246 finally {
247 DataAccess.cleanUp(con, ps);
248 }
249
250 return resourceId;
251 }
252
253 protected void addResourcePermission(
254 long resourcePermissionId, long companyId, String name,
255 long primKey, long ownerId)
256 throws Exception {
257
258 Connection con = null;
259 PreparedStatement ps = null;
260 ResultSet rs = null;
261
262 try {
263 con = DataAccess.getConnection();
264
265 ps = con.prepareStatement(
266 "insert into ResourcePermission (resourcePermissionId, " +
267 "companyId, name, scope, primKey, roleId, ownerId, " +
268 "actionIds) values (?, ?, ?, ?, ?, ?, ?, ?)");
269
270 ps.setLong(1, resourcePermissionId);
271 ps.setLong(2, companyId);
272 ps.setString(3, name);
273 ps.setLong(4, ResourceConstants.SCOPE_INDIVIDUAL);
274 ps.setString(5, String.valueOf(primKey));
275 ps.setLong(6, getRoleId(companyId, RoleConstants.OWNER));
276 ps.setLong(7, ownerId);
277 ps.setLong(8, getActionIds(name));
278
279 ps.executeUpdate();
280
281 }
282 finally {
283 DataAccess.cleanUp(con, ps, rs);
284 }
285 }
286
287 protected UserGroupTemplateInfo buildUserGroupsTemplatesInfo(ResultSet rs)
288 throws Exception {
289
290 UserGroupTemplateInfo userGroupTemplateInfo =
291 new UserGroupTemplateInfo();
292
293 userGroupTemplateInfo.setColorSchemeId(rs.getString("colorSchemeId"));
294 userGroupTemplateInfo.setCompanyId(rs.getLong("companyId"));
295 userGroupTemplateInfo.setCss(rs.getString("css"));
296 userGroupTemplateInfo.setGroupId(rs.getLong("groupId"));
297 userGroupTemplateInfo.setLayoutSetId(rs.getLong("layoutSetId"));
298 userGroupTemplateInfo.setLayoutSetPrototypeId(
299 rs.getLong("layoutSetPrototypeId"));
300 userGroupTemplateInfo.setLogo(rs.getShort("logo"));
301 userGroupTemplateInfo.setLogoId(rs.getLong("logoId"));
302 userGroupTemplateInfo.setPageCount(rs.getLong("pageCount"));
303 userGroupTemplateInfo.setPrivateLayout(rs.getBoolean("privateLayout"));
304 userGroupTemplateInfo.setSettings(rs.getString("settings"));
305 userGroupTemplateInfo.setThemeId(rs.getString("themeId"));
306 userGroupTemplateInfo.setUserGroupId(rs.getLong("userGroupId"));
307 userGroupTemplateInfo.setWapColorSchemeId(
308 rs.getString("wapColorSchemeId"));
309 userGroupTemplateInfo.setWapThemeId(rs.getString("wapThemeId"));
310
311 return userGroupTemplateInfo;
312 }
313
314 @Override
315 protected void doUpgrade() throws Exception {
316 updateUserGroup();
317 upgrade(UpgradeLayoutSet.class);
318 }
319
320 protected void updateUserGroup() throws Exception {
321 Connection con = null;
322 PreparedStatement ps = null;
323 ResultSet rs = null;
324
325 try {
326 con = DataAccess.getConnection();
327
328 StringBundler sb = new StringBundler(17);
329
330 sb.append("select UserGroup.userGroupId as userGroupId, ");
331 sb.append("UserGroup.companyId as companyId, UserGroup.name as ");
332 sb.append("userGroupName, Group_.groupId as groupId, ");
333 sb.append("Group_.creatorUserId as creatorUserId, ");
334 sb.append("LayoutSet.layoutSetId as layoutSetId, ");
335 sb.append("LayoutSet.privateLayout as privateLayout, ");
336 sb.append("LayoutSet.logo as logo, LayoutSet.logoId as logoId, ");
337 sb.append("LayoutSet.themeId as themeId, LayoutSet.colorSchemeId ");
338 sb.append("as colorSchemeId, LayoutSet.wapThemeId as wapThemeId, ");
339 sb.append("LayoutSet.wapColorSchemeId as wapColorSchemeId, ");
340 sb.append("LayoutSet.css as css, LayoutSet.pageCount as ");
341 sb.append("pageCount, LayoutSet.settings_ as settings, ");
342 sb.append("LayoutSet.layoutSetPrototypeId as ");
343 sb.append("layoutSetPrototypeId from UserGroup inner join ");
344 sb.append("Group_ on Group_.classPK = UserGroup.userGroupId ");
345 sb.append("inner join LayoutSet on (LayoutSet.groupId = ");
346 sb.append("Group_.groupId) and (LayoutSet.pageCount > 0)");
347
348 String sql = sb.toString();
349
350 ps = con.prepareStatement(sql);
351
352 rs = ps.executeQuery();
353
354 while (rs.next()) {
355 long userGroupId = rs.getLong("userGroupId");
356 long companyId = rs.getLong("companyId");
357 String userGroupName = rs.getString("userGroupName");
358 long groupId = rs.getLong("groupId");
359 long creatorUserId = rs.getLong("creatorUserId");
360 boolean privateLayout = rs.getBoolean("privateLayout");
361
362 long layoutSetPrototypeId = increment();
363
364 addLayoutSetPrototype(
365 layoutSetPrototypeId, companyId, userGroupName);
366
367 long layoutSetPrototypeGroupId = addGroup(
368 companyId, creatorUserId, userGroupName,
369 layoutSetPrototypeId);
370
371 addPermissions(companyId, creatorUserId, layoutSetPrototypeId);
372
373 UserGroupTemplateInfo privateUserGroupTemplateInfo =
374 buildUserGroupsTemplatesInfo(rs);
375
376 boolean oldPrivateLayout =
377 privateUserGroupTemplateInfo.isPrivateLayout();
378
379 privateUserGroupTemplateInfo.setPrivateLayout(true);
380
381 addLayoutSet(
382 increment(), companyId, layoutSetPrototypeGroupId,
383 layoutSetPrototypeId, privateUserGroupTemplateInfo);
384
385 updateLayout(
386 groupId, layoutSetPrototypeGroupId, oldPrivateLayout);
387
388 UserGroupTemplateInfo publicUserGroupTemplateInfo =
389 new UserGroupTemplateInfo();
390
391 publicUserGroupTemplateInfo.setPrivateLayout(false);
392
393 addLayoutSet(
394 increment(), companyId, layoutSetPrototypeGroupId,
395 layoutSetPrototypeId, publicUserGroupTemplateInfo);
396
397 updateUserGroup(
398 userGroupId, privateLayout, layoutSetPrototypeId);
399 }
400 }
401 finally {
402 DataAccess.cleanUp(con, ps, rs);
403 }
404 }
405
406 protected long getActionIds(String name) throws Exception {
407 Connection con = null;
408 PreparedStatement ps = null;
409 ResultSet rs = null;
410
411 try {
412 con = DataAccess.getConnection();
413
414 ps = con.prepareStatement(
415 "select sum(bitwiseValue) as actionIds from ResourceAction " +
416 "where name = ?");
417
418 ps.setString(1, name);
419
420 rs = ps.executeQuery();
421
422 if (rs.next()) {
423 return rs.getLong("actionIds");
424 }
425 }
426 finally {
427 DataAccess.cleanUp(con, ps, rs);
428 }
429
430 return 0;
431 }
432
433 protected long getClassNameId(String className) throws Exception {
434 Connection con = null;
435 PreparedStatement ps = null;
436 ResultSet rs = null;
437
438 try {
439 con = DataAccess.getConnection();
440
441 ps = con.prepareStatement(
442 "select classNameId from ClassName_ where value = ?");
443
444 ps.setString(1, className);
445
446 rs = ps.executeQuery();
447
448 if (rs.next()) {
449 return rs.getLong("classNameId");
450 }
451 }
452 finally {
453 DataAccess.cleanUp(con, ps, rs);
454 }
455
456 return 0;
457 }
458
459 protected long getGroupId(long companyId, long classNameId, long classPK)
460 throws Exception {
461
462 Connection con = null;
463 PreparedStatement ps = null;
464 ResultSet rs = null;
465
466 long groupId = 0;
467
468 try {
469 con = DataAccess.getConnection();
470
471 ps = con.prepareStatement(
472 "select groupId from Group_ where (companyId = ?) and " +
473 "(classNameId = ?) and (classPK = ?)");
474
475 ps.setLong(1, companyId);
476 ps.setLong(2, classNameId);
477 ps.setLong(3, classPK);
478
479 rs = ps.executeQuery();
480
481 if (rs.next()) {
482 groupId = rs.getLong("groupId");
483 }
484 }
485 finally {
486 DataAccess.cleanUp(con, ps, rs);
487 }
488
489 return groupId;
490 }
491
492 protected String getNameXML(String name) {
493 StringBundler sb = new StringBundler(8);
494
495 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
496 sb.append("<root default-locale=\"");
497 sb.append(LocaleUtil.getDefault());
498 sb.append("\"><Name language-id=\"");
499 sb.append(LocaleUtil.getDefault());
500 sb.append("\">");
501 sb.append(name);
502 sb.append("</Name></root>");
503
504 return sb.toString();
505 }
506
507 protected long getRoleId(long companyId, String name)
508 throws Exception {
509
510 Connection con = null;
511 PreparedStatement ps = null;
512 ResultSet rs = null;
513
514 try {
515 con = DataAccess.getConnection();
516
517 ps = con.prepareStatement(
518 "select roleId from Role_ where (companyId = ?) and (name = " +
519 "?)");
520
521 ps.setLong(1, companyId);
522 ps.setString(2, name);
523
524 rs = ps.executeQuery();
525
526 if (rs.next()) {
527 return rs.getLong("roleId");
528 }
529 }
530 finally {
531 DataAccess.cleanUp(con, ps, rs);
532 }
533
534 return 0;
535 }
536
537 protected void updateLayout(
538 long oldGroupId, long newGroupId, boolean privateLayout)
539 throws Exception {
540
541 Connection con = null;
542 PreparedStatement ps = null;
543 ResultSet rs = null;
544
545 try {
546 con = DataAccess.getConnection();
547
548 ps = con.prepareStatement(
549 "update Layout set groupId = ?, privateLayout = ? where " +
550 "(groupId = ?) and (privateLayout = ?)");
551
552 ps.setLong(1, newGroupId);
553 ps.setBoolean(2, true);
554 ps.setLong(3, oldGroupId);
555 ps.setBoolean(4, privateLayout);
556
557 ps.executeUpdate();
558 }
559 finally {
560 DataAccess.cleanUp(con, ps, rs);
561 }
562 }
563
564 protected void updateUserGroup(
565 long userGroupId, boolean privateLayout, long layoutSetPrototypeId)
566 throws Exception {
567
568 Connection con = null;
569 PreparedStatement ps = null;
570 ResultSet rs = null;
571
572 try {
573 con = DataAccess.getConnection();
574
575 String layoutSetPrototypeIdColumnName = null;
576
577 if (privateLayout) {
578 layoutSetPrototypeIdColumnName = "privateLayoutSetPrototypeId";
579 }
580 else {
581 layoutSetPrototypeIdColumnName = "publicLayoutSetPrototypeId";
582 }
583
584 ps = con.prepareStatement(
585 "update UserGroup set " + layoutSetPrototypeIdColumnName +
586 " = ? where userGroupId = ?");
587
588 ps.setLong(1, layoutSetPrototypeId);
589 ps.setLong(2, userGroupId);
590
591 ps.executeUpdate();
592 }
593 finally {
594 DataAccess.cleanUp(con, ps, rs);
595 }
596 }
597
598 }