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