001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.LayoutSetBranchNameException;
018 import com.liferay.portal.NoSuchLayoutSetBranchException;
019 import com.liferay.portal.RequiredLayoutSetBranchException;
020 import com.liferay.portal.kernel.dao.orm.QueryUtil;
021 import com.liferay.portal.kernel.exception.PortalException;
022 import com.liferay.portal.kernel.exception.SystemException;
023 import com.liferay.portal.kernel.language.LanguageUtil;
024 import com.liferay.portal.kernel.staging.StagingUtil;
025 import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
026 import com.liferay.portal.kernel.util.StringBundler;
027 import com.liferay.portal.kernel.util.StringPool;
028 import com.liferay.portal.kernel.util.Validator;
029 import com.liferay.portal.kernel.workflow.WorkflowConstants;
030 import com.liferay.portal.model.Image;
031 import com.liferay.portal.model.Layout;
032 import com.liferay.portal.model.LayoutBranch;
033 import com.liferay.portal.model.LayoutBranchConstants;
034 import com.liferay.portal.model.LayoutConstants;
035 import com.liferay.portal.model.LayoutRevision;
036 import com.liferay.portal.model.LayoutRevisionConstants;
037 import com.liferay.portal.model.LayoutSet;
038 import com.liferay.portal.model.LayoutSetBranch;
039 import com.liferay.portal.model.LayoutSetBranchConstants;
040 import com.liferay.portal.model.ResourceConstants;
041 import com.liferay.portal.model.User;
042 import com.liferay.portal.service.ServiceContext;
043 import com.liferay.portal.service.base.LayoutSetBranchLocalServiceBaseImpl;
044 import com.liferay.portal.util.comparator.LayoutSetBranchCreateDateComparator;
045
046 import java.text.Format;
047
048 import java.util.Date;
049 import java.util.List;
050 import java.util.Locale;
051
052
057 public class LayoutSetBranchLocalServiceImpl
058 extends LayoutSetBranchLocalServiceBaseImpl {
059
060 @Override
061 public LayoutSetBranch addLayoutSetBranch(
062 long userId, long groupId, boolean privateLayout, String name,
063 String description, boolean master, long copyLayoutSetBranchId,
064 ServiceContext serviceContext)
065 throws PortalException, SystemException {
066
067
068
069 User user = userPersistence.findByPrimaryKey(userId);
070 Date now = new Date();
071
072 validate(0, groupId, privateLayout, name, master);
073
074 boolean logo = false;
075 long logoId = 0;
076 String themeId = null;
077 String colorSchemeId = null;
078 String wapThemeId = null;
079 String wapColorSchemeId = null;
080 String css = null;
081 String settings = null;
082
083 if (copyLayoutSetBranchId > 0) {
084 LayoutSetBranch copyLayoutSetBranch = getLayoutSetBranch(
085 copyLayoutSetBranchId);
086
087 logo = copyLayoutSetBranch.getLogo();
088 logoId = copyLayoutSetBranch.getLogoId();
089 themeId = copyLayoutSetBranch.getThemeId();
090 colorSchemeId = copyLayoutSetBranch.getColorSchemeId();
091 wapThemeId = copyLayoutSetBranch.getWapThemeId();
092 wapColorSchemeId = copyLayoutSetBranch.getWapColorSchemeId();
093 css = copyLayoutSetBranch.getCss();
094 settings = copyLayoutSetBranch.getSettings();
095 }
096 else {
097 LayoutSet layoutSet = layoutSetLocalService.getLayoutSet(
098 groupId, privateLayout);
099
100 logo = layoutSet.getLogo();
101 logoId = layoutSet.getLogoId();
102 themeId = layoutSet.getThemeId();
103 colorSchemeId = layoutSet.getColorSchemeId();
104 wapThemeId = layoutSet.getWapThemeId();
105 wapColorSchemeId = layoutSet.getWapColorSchemeId();
106 css = layoutSet.getCss();
107 settings = layoutSet.getSettings();
108 }
109
110 long layoutSetBranchId = counterLocalService.increment();
111
112 LayoutSetBranch layoutSetBranch = layoutSetBranchPersistence.create(
113 layoutSetBranchId);
114
115 layoutSetBranch.setGroupId(groupId);
116 layoutSetBranch.setCompanyId(user.getCompanyId());
117 layoutSetBranch.setUserId(user.getUserId());
118 layoutSetBranch.setUserName(user.getFullName());
119 layoutSetBranch.setCreateDate(serviceContext.getCreateDate(now));
120 layoutSetBranch.setModifiedDate(serviceContext.getModifiedDate(now));
121 layoutSetBranch.setPrivateLayout(privateLayout);
122 layoutSetBranch.setName(name);
123 layoutSetBranch.setDescription(description);
124 layoutSetBranch.setMaster(master);
125 layoutSetBranch.setLogo(logo);
126 layoutSetBranch.setLogoId(logoId);
127
128 if (logo) {
129 Image logoImage = imageLocalService.getImage(logoId);
130
131 long layoutSetBranchLogoId = counterLocalService.increment();
132
133 imageLocalService.updateImage(
134 layoutSetBranchLogoId, logoImage.getTextObj(),
135 logoImage.getType(), logoImage.getHeight(),
136 logoImage.getWidth(), logoImage.getSize());
137
138 layoutSetBranch.setLogoId(layoutSetBranchLogoId);
139 }
140
141 layoutSetBranch.setThemeId(themeId);
142 layoutSetBranch.setColorSchemeId(colorSchemeId);
143 layoutSetBranch.setWapThemeId(wapThemeId);
144 layoutSetBranch.setWapColorSchemeId(wapColorSchemeId);
145 layoutSetBranch.setCss(css);
146 layoutSetBranch.setSettings(settings);
147
148 layoutSetBranchPersistence.update(layoutSetBranch);
149
150
151
152 resourceLocalService.addResources(
153 user.getCompanyId(), layoutSetBranch.getGroupId(), user.getUserId(),
154 LayoutSetBranch.class.getName(),
155 layoutSetBranch.getLayoutSetBranchId(), false, true, false);
156
157
158
159 if (layoutSetBranch.isMaster() ||
160 (copyLayoutSetBranchId == LayoutSetBranchConstants.ALL_BRANCHES)) {
161
162 List<Layout> layouts = layoutPersistence.findByG_P(
163 layoutSetBranch.getGroupId(),
164 layoutSetBranch.getPrivateLayout());
165
166 for (Layout layout : layouts) {
167 LayoutBranch layoutBranch =
168 layoutBranchLocalService.addLayoutBranch(
169 layoutSetBranchId, layout.getPlid(),
170 LayoutBranchConstants.MASTER_BRANCH_NAME,
171 LayoutBranchConstants.MASTER_BRANCH_DESCRIPTION, true,
172 serviceContext);
173
174 LayoutRevision lastLayoutRevision =
175 layoutRevisionLocalService.fetchLastLayoutRevision(
176 layout.getPlid(), true);
177
178 if (lastLayoutRevision != null) {
179 layoutRevisionLocalService.addLayoutRevision(
180 userId, layoutSetBranchId,
181 layoutBranch.getLayoutBranchId(),
182 LayoutRevisionConstants.
183 DEFAULT_PARENT_LAYOUT_REVISION_ID,
184 true, lastLayoutRevision.getPlid(),
185 lastLayoutRevision.getLayoutRevisionId(),
186 lastLayoutRevision.getPrivateLayout(),
187 lastLayoutRevision.getName(),
188 lastLayoutRevision.getTitle(),
189 lastLayoutRevision.getDescription(),
190 lastLayoutRevision.getKeywords(),
191 lastLayoutRevision.getRobots(),
192 lastLayoutRevision.getTypeSettings(),
193 lastLayoutRevision.isIconImage(),
194 lastLayoutRevision.getIconImageId(),
195 lastLayoutRevision.getThemeId(),
196 lastLayoutRevision.getColorSchemeId(),
197 lastLayoutRevision.getWapThemeId(),
198 lastLayoutRevision.getWapColorSchemeId(),
199 lastLayoutRevision.getCss(), serviceContext);
200 }
201 else {
202 layoutRevisionLocalService.addLayoutRevision(
203 userId, layoutSetBranchId,
204 layoutBranch.getLayoutBranchId(),
205 LayoutRevisionConstants.
206 DEFAULT_PARENT_LAYOUT_REVISION_ID,
207 false, layout.getPlid(), LayoutConstants.DEFAULT_PLID,
208 layout.getPrivateLayout(), layout.getName(),
209 layout.getTitle(), layout.getDescription(),
210 layout.getKeywords(), layout.getRobots(),
211 layout.getTypeSettings(), layout.isIconImage(),
212 layout.getIconImageId(), layout.getThemeId(),
213 layout.getColorSchemeId(), layout.getWapThemeId(),
214 layout.getWapColorSchemeId(), layout.getCss(),
215 serviceContext);
216 }
217 }
218 }
219 else if (copyLayoutSetBranchId > 0) {
220 List<LayoutRevision> layoutRevisions =
221 layoutRevisionLocalService.getLayoutRevisions(
222 copyLayoutSetBranchId, true);
223
224 for (LayoutRevision layoutRevision : layoutRevisions) {
225 LayoutBranch layoutBranch =
226 layoutBranchLocalService.addLayoutBranch(
227 layoutSetBranchId, layoutRevision.getPlid(),
228 LayoutBranchConstants.MASTER_BRANCH_NAME,
229 LayoutBranchConstants.MASTER_BRANCH_DESCRIPTION, true,
230 serviceContext);
231
232 layoutRevisionLocalService.addLayoutRevision(
233 userId, layoutSetBranchId, layoutBranch.getLayoutBranchId(),
234 LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID,
235 true, layoutRevision.getPlid(),
236 layoutRevision.getLayoutRevisionId(),
237 layoutRevision.getPrivateLayout(), layoutRevision.getName(),
238 layoutRevision.getTitle(), layoutRevision.getDescription(),
239 layoutRevision.getKeywords(), layoutRevision.getRobots(),
240 layoutRevision.getTypeSettings(),
241 layoutRevision.isIconImage(),
242 layoutRevision.getIconImageId(),
243 layoutRevision.getThemeId(),
244 layoutRevision.getColorSchemeId(),
245 layoutRevision.getWapThemeId(),
246 layoutRevision.getWapColorSchemeId(),
247 layoutRevision.getCss(), serviceContext);
248 }
249 }
250
251 LayoutSet layoutSet = layoutSetBranch.getLayoutSet();
252
253 StagingUtil.setRecentLayoutSetBranchId(
254 user, layoutSet.getLayoutSetId(),
255 layoutSetBranch.getLayoutSetBranchId());
256
257 return layoutSetBranch;
258 }
259
260 @Override
261 public LayoutSetBranch deleteLayoutSetBranch(
262 LayoutSetBranch layoutSetBranch)
263 throws PortalException, SystemException {
264
265 return deleteLayoutSetBranch(layoutSetBranch, false);
266 }
267
268 @Override
269 public LayoutSetBranch deleteLayoutSetBranch(
270 LayoutSetBranch layoutSetBranch, boolean includeMaster)
271 throws PortalException, SystemException {
272
273
274
275 if (!includeMaster && layoutSetBranch.isMaster()) {
276 throw new RequiredLayoutSetBranchException();
277 }
278
279 layoutSetBranchPersistence.remove(layoutSetBranch);
280
281
282
283 resourceLocalService.deleteResource(
284 layoutSetBranch.getCompanyId(), LayoutSetBranch.class.getName(),
285 ResourceConstants.SCOPE_INDIVIDUAL,
286 layoutSetBranch.getLayoutSetBranchId());
287
288
289
290 layoutBranchLocalService.deleteLayoutSetBranchLayoutBranches(
291 layoutSetBranch.getLayoutSetBranchId());
292
293
294
295 layoutRevisionLocalService.deleteLayoutSetBranchLayoutRevisions(
296 layoutSetBranch.getLayoutSetBranchId());
297
298 return layoutSetBranch;
299 }
300
301 @Override
302 public LayoutSetBranch deleteLayoutSetBranch(long layoutSetBranchId)
303 throws PortalException, SystemException {
304
305 LayoutSetBranch layoutSetBranch =
306 layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
307
308 return deleteLayoutSetBranch(layoutSetBranch, false);
309 }
310
311 @Override
312 public void deleteLayoutSetBranches(long groupId, boolean privateLayout)
313 throws PortalException, SystemException {
314
315 deleteLayoutSetBranches(groupId, privateLayout, false);
316 }
317
318 @Override
319 public void deleteLayoutSetBranches(
320 long groupId, boolean privateLayout, boolean includeMaster)
321 throws PortalException, SystemException {
322
323 List<LayoutSetBranch> layoutSetBranches =
324 layoutSetBranchPersistence.findByG_P(groupId, privateLayout);
325
326 for (LayoutSetBranch layoutSetBranch : layoutSetBranches) {
327 deleteLayoutSetBranch(layoutSetBranch, includeMaster);
328 }
329 }
330
331 @Override
332 public LayoutSetBranch fetchLayoutSetBranch(
333 long groupId, boolean privateLayout, String name)
334 throws SystemException {
335
336 return layoutSetBranchPersistence.fetchByG_P_N(
337 groupId, privateLayout, name);
338 }
339
340 @Override
341 public LayoutSetBranch getLayoutSetBranch(
342 long groupId, boolean privateLayout, String name)
343 throws PortalException, SystemException {
344
345 return layoutSetBranchPersistence.findByG_P_N(
346 groupId, privateLayout, name);
347 }
348
349 @Override
350 public List<LayoutSetBranch> getLayoutSetBranches(
351 long groupId, boolean privateLayout)
352 throws SystemException {
353
354 return layoutSetBranchPersistence.findByG_P(
355 groupId, privateLayout, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
356 new LayoutSetBranchCreateDateComparator(true));
357 }
358
359 @Override
360 public LayoutSetBranch getMasterLayoutSetBranch(
361 long groupId, boolean privateLayout)
362 throws PortalException, SystemException {
363
364 return layoutSetBranchPersistence.findByG_P_M_First(
365 groupId, privateLayout, true, null);
366 }
367
368
372 @Override
373 public LayoutSetBranch getUserLayoutSetBranch(
374 long userId, long groupId, boolean privateLayout,
375 long layoutSetBranchId)
376 throws PortalException, SystemException {
377
378 return getUserLayoutSetBranch(
379 userId, groupId, privateLayout, 0, layoutSetBranchId);
380 }
381
382 @Override
383 public LayoutSetBranch getUserLayoutSetBranch(
384 long userId, long groupId, boolean privateLayout, long layoutSetId,
385 long layoutSetBranchId)
386 throws PortalException, SystemException {
387
388 if (layoutSetBranchId <= 0) {
389 User user = userPersistence.findByPrimaryKey(userId);
390
391 if (layoutSetId <= 0) {
392 LayoutSet layoutSet = layoutSetLocalService.getLayoutSet(
393 groupId, privateLayout);
394
395 layoutSetId = layoutSet.getLayoutSetId();
396 }
397
398 layoutSetBranchId = StagingUtil.getRecentLayoutSetBranchId(
399 user, layoutSetId);
400 }
401
402 if (layoutSetBranchId > 0) {
403 LayoutSetBranch layoutSetBranch = fetchLayoutSetBranch(
404 layoutSetBranchId);
405
406 if (layoutSetBranch != null) {
407 return layoutSetBranch;
408 }
409 }
410
411 return getMasterLayoutSetBranch(groupId, privateLayout);
412 }
413
414 @Override
415 public LayoutSetBranch mergeLayoutSetBranch(
416 long layoutSetBranchId, long mergeLayoutSetBranchId,
417 ServiceContext serviceContext)
418 throws PortalException, SystemException {
419
420 LayoutSetBranch layoutSetBranch =
421 layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
422 LayoutSetBranch mergeLayoutSetBranch =
423 layoutSetBranchPersistence.findByPrimaryKey(mergeLayoutSetBranchId);
424
425 Locale locale = serviceContext.getLocale();
426
427 Format dateFormatDateTime = FastDateFormatFactoryUtil.getDateTime(
428 locale);
429
430 String nowString = dateFormatDateTime.format(new Date());
431
432 serviceContext.setWorkflowAction(WorkflowConstants.STATUS_DRAFT);
433
434 List<LayoutRevision> layoutRevisions =
435 layoutRevisionLocalService.getLayoutRevisions(
436 mergeLayoutSetBranchId, true);
437
438 for (LayoutRevision layoutRevision : layoutRevisions) {
439 String layoutBranchName = getLayoutBranchName(
440 layoutSetBranch.getLayoutSetBranchId(), locale,
441 layoutRevision.getLayoutBranch().getName(),
442 mergeLayoutSetBranch.getName(), layoutRevision.getPlid());
443
444 StringBundler sb = new StringBundler(3);
445
446 sb.append(mergeLayoutSetBranch.getDescription());
447 sb.append(StringPool.SPACE);
448 sb.append(
449 LanguageUtil.format(
450 locale, "merged-from-x-x",
451 new String[] {mergeLayoutSetBranch.getName(), nowString}));
452
453 LayoutBranch layoutBranch =
454 layoutBranchLocalService.addLayoutBranch(
455 layoutSetBranch.getLayoutSetBranchId(),
456 layoutRevision.getPlid(), layoutBranchName, sb.toString(),
457 false, serviceContext);
458
459 layoutRevisionLocalService.addLayoutRevision(
460 layoutRevision.getUserId(),
461 layoutSetBranch.getLayoutSetBranchId(),
462 layoutBranch.getLayoutBranchId(),
463 LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID,
464 false, layoutRevision.getPlid(),
465 layoutRevision.getLayoutRevisionId(),
466 layoutRevision.isPrivateLayout(), layoutRevision.getName(),
467 layoutRevision.getTitle(), layoutRevision.getDescription(),
468 layoutRevision.getKeywords(), layoutRevision.getRobots(),
469 layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
470 layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
471 layoutRevision.getColorSchemeId(),
472 layoutRevision.getWapThemeId(),
473 layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
474 serviceContext);
475 }
476
477 return layoutSetBranch;
478 }
479
480 @Override
481 public LayoutSetBranch updateLayoutSetBranch(
482 long layoutSetBranchId, String name, String description,
483 ServiceContext serviceContext)
484 throws PortalException, SystemException {
485
486 LayoutSetBranch layoutSetBranch =
487 layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
488
489 validate(
490 layoutSetBranch.getLayoutSetBranchId(),
491 layoutSetBranch.getGroupId(), layoutSetBranch.getPrivateLayout(),
492 name, layoutSetBranch.isMaster());
493
494 layoutSetBranch.setName(name);
495 layoutSetBranch.setDescription(description);
496
497 layoutSetBranchPersistence.update(layoutSetBranch);
498
499 return layoutSetBranch;
500 }
501
502 protected String getLayoutBranchName(
503 long layoutSetBranchId, Locale locale, String mergeBranchName,
504 String mergeLayoutSetBranchName, long plid)
505 throws SystemException {
506
507 LayoutBranch layoutBranch = layoutBranchPersistence.fetchByL_P_N(
508 layoutSetBranchId, plid, mergeBranchName);
509
510 if (layoutBranch == null) {
511 return mergeBranchName;
512 }
513
514 StringBundler sb = new StringBundler(5);
515
516 sb.append(LanguageUtil.get(locale, mergeBranchName));
517 sb.append(StringPool.SPACE);
518 sb.append(StringPool.OPEN_PARENTHESIS);
519 sb.append(LanguageUtil.get(locale, mergeLayoutSetBranchName));
520 sb.append(StringPool.CLOSE_PARENTHESIS);
521
522 String layoutBranchName = sb.toString();
523
524 for (int i = 1;; i++) {
525 layoutBranch = layoutBranchPersistence.fetchByL_P_N(
526 layoutSetBranchId, plid, layoutBranchName);
527
528 if (layoutBranch == null) {
529 break;
530 }
531
532 layoutBranchName = sb.toString() + StringPool.SPACE + i;
533 }
534
535 return layoutBranchName;
536 }
537
538 protected void validate(
539 long layoutSetBranchId, long groupId, boolean privateLayout,
540 String name, boolean master)
541 throws PortalException, SystemException {
542
543 if (Validator.isNull(name) || (name.length() < 4)) {
544 throw new LayoutSetBranchNameException(
545 LayoutSetBranchNameException.TOO_SHORT);
546 }
547
548 if (name.length() > 100) {
549 throw new LayoutSetBranchNameException(
550 LayoutSetBranchNameException.TOO_LONG);
551 }
552
553 try {
554 LayoutSetBranch layoutSetBranch =
555 layoutSetBranchPersistence.findByG_P_N(
556 groupId, privateLayout, name);
557
558 if (layoutSetBranch.getLayoutSetBranchId() != layoutSetBranchId) {
559 throw new LayoutSetBranchNameException(
560 LayoutSetBranchNameException.DUPLICATE);
561 }
562 }
563 catch (NoSuchLayoutSetBranchException nslsbe) {
564 }
565
566 if (master) {
567 try {
568 LayoutSetBranch masterLayoutSetBranch =
569 layoutSetBranchPersistence.findByG_P_M_First(
570 groupId, privateLayout, true, null);
571
572 if (layoutSetBranchId !=
573 masterLayoutSetBranch.getLayoutSetBranchId()) {
574
575 throw new LayoutSetBranchNameException(
576 LayoutSetBranchNameException.MASTER);
577 }
578 }
579 catch (NoSuchLayoutSetBranchException nslsbe) {
580 }
581 }
582 }
583
584 }