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