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