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