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