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 false));
453
454 LayoutBranch layoutBranch =
455 layoutBranchLocalService.addLayoutBranch(
456 layoutSetBranch.getLayoutSetBranchId(),
457 layoutRevision.getPlid(), layoutBranchName, sb.toString(),
458 false, serviceContext);
459
460 layoutRevisionLocalService.addLayoutRevision(
461 layoutRevision.getUserId(),
462 layoutSetBranch.getLayoutSetBranchId(),
463 layoutBranch.getLayoutBranchId(),
464 LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID,
465 false, layoutRevision.getPlid(),
466 layoutRevision.getLayoutRevisionId(),
467 layoutRevision.isPrivateLayout(), layoutRevision.getName(),
468 layoutRevision.getTitle(), layoutRevision.getDescription(),
469 layoutRevision.getKeywords(), layoutRevision.getRobots(),
470 layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
471 layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
472 layoutRevision.getColorSchemeId(),
473 layoutRevision.getWapThemeId(),
474 layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
475 serviceContext);
476 }
477
478 return layoutSetBranch;
479 }
480
481 @Override
482 public LayoutSetBranch updateLayoutSetBranch(
483 long layoutSetBranchId, String name, String description,
484 ServiceContext serviceContext)
485 throws PortalException, SystemException {
486
487 LayoutSetBranch layoutSetBranch =
488 layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
489
490 validate(
491 layoutSetBranch.getLayoutSetBranchId(),
492 layoutSetBranch.getGroupId(), layoutSetBranch.getPrivateLayout(),
493 name, layoutSetBranch.isMaster());
494
495 layoutSetBranch.setName(name);
496 layoutSetBranch.setDescription(description);
497
498 layoutSetBranchPersistence.update(layoutSetBranch);
499
500 return layoutSetBranch;
501 }
502
503 protected String getLayoutBranchName(
504 long layoutSetBranchId, Locale locale, String mergeBranchName,
505 String mergeLayoutSetBranchName, long plid)
506 throws SystemException {
507
508 LayoutBranch layoutBranch = layoutBranchPersistence.fetchByL_P_N(
509 layoutSetBranchId, plid, mergeBranchName);
510
511 if (layoutBranch == null) {
512 return mergeBranchName;
513 }
514
515 StringBundler sb = new StringBundler(5);
516
517 sb.append(LanguageUtil.get(locale, mergeBranchName));
518 sb.append(StringPool.SPACE);
519 sb.append(StringPool.OPEN_PARENTHESIS);
520 sb.append(LanguageUtil.get(locale, mergeLayoutSetBranchName));
521 sb.append(StringPool.CLOSE_PARENTHESIS);
522
523 String layoutBranchName = sb.toString();
524
525 for (int i = 1;; i++) {
526 layoutBranch = layoutBranchPersistence.fetchByL_P_N(
527 layoutSetBranchId, plid, layoutBranchName);
528
529 if (layoutBranch == null) {
530 break;
531 }
532
533 layoutBranchName = sb.toString() + StringPool.SPACE + i;
534 }
535
536 return layoutBranchName;
537 }
538
539 protected void validate(
540 long layoutSetBranchId, long groupId, boolean privateLayout,
541 String name, boolean master)
542 throws PortalException, SystemException {
543
544 if (Validator.isNull(name) || (name.length() < 4)) {
545 throw new LayoutSetBranchNameException(
546 LayoutSetBranchNameException.TOO_SHORT);
547 }
548
549 if (name.length() > 100) {
550 throw new LayoutSetBranchNameException(
551 LayoutSetBranchNameException.TOO_LONG);
552 }
553
554 try {
555 LayoutSetBranch layoutSetBranch =
556 layoutSetBranchPersistence.findByG_P_N(
557 groupId, privateLayout, name);
558
559 if (layoutSetBranch.getLayoutSetBranchId() != layoutSetBranchId) {
560 throw new LayoutSetBranchNameException(
561 LayoutSetBranchNameException.DUPLICATE);
562 }
563 }
564 catch (NoSuchLayoutSetBranchException nslsbe) {
565 }
566
567 if (master) {
568 try {
569 LayoutSetBranch masterLayoutSetBranch =
570 layoutSetBranchPersistence.findByG_P_M_First(
571 groupId, privateLayout, true, null);
572
573 if (layoutSetBranchId !=
574 masterLayoutSetBranch.getLayoutSetBranchId()) {
575
576 throw new LayoutSetBranchNameException(
577 LayoutSetBranchNameException.MASTER);
578 }
579 }
580 catch (NoSuchLayoutSetBranchException nslsbe) {
581 }
582 }
583 }
584
585 }