001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.LayoutFriendlyURLException;
018 import com.liferay.portal.LayoutHiddenException;
019 import com.liferay.portal.LayoutNameException;
020 import com.liferay.portal.LayoutParentLayoutIdException;
021 import com.liferay.portal.LayoutTypeException;
022 import com.liferay.portal.NoSuchLayoutException;
023 import com.liferay.portal.RequiredLayoutException;
024 import com.liferay.portal.kernel.exception.PortalException;
025 import com.liferay.portal.kernel.exception.SystemException;
026 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
027 import com.liferay.portal.kernel.language.LanguageUtil;
028 import com.liferay.portal.kernel.log.Log;
029 import com.liferay.portal.kernel.log.LogFactoryUtil;
030 import com.liferay.portal.kernel.util.FileUtil;
031 import com.liferay.portal.kernel.util.GetterUtil;
032 import com.liferay.portal.kernel.util.ListUtil;
033 import com.liferay.portal.kernel.util.LocaleUtil;
034 import com.liferay.portal.kernel.util.StringBundler;
035 import com.liferay.portal.kernel.util.StringPool;
036 import com.liferay.portal.kernel.util.Validator;
037 import com.liferay.portal.kernel.workflow.WorkflowConstants;
038 import com.liferay.portal.lar.LayoutExporter;
039 import com.liferay.portal.lar.LayoutImporter;
040 import com.liferay.portal.lar.PortletExporter;
041 import com.liferay.portal.lar.PortletImporter;
042 import com.liferay.portal.model.Group;
043 import com.liferay.portal.model.Layout;
044 import com.liferay.portal.model.LayoutConstants;
045 import com.liferay.portal.model.LayoutReference;
046 import com.liferay.portal.model.LayoutTypePortlet;
047 import com.liferay.portal.model.PortletConstants;
048 import com.liferay.portal.model.Resource;
049 import com.liferay.portal.model.ResourceConstants;
050 import com.liferay.portal.model.User;
051 import com.liferay.portal.model.impl.LayoutImpl;
052 import com.liferay.portal.service.ServiceContext;
053 import com.liferay.portal.service.base.LayoutLocalServiceBaseImpl;
054 import com.liferay.portal.util.FriendlyURLNormalizer;
055 import com.liferay.portal.util.PortalUtil;
056 import com.liferay.portal.util.PortletKeys;
057 import com.liferay.portal.util.PropsValues;
058 import com.liferay.portal.util.comparator.LayoutComparator;
059 import com.liferay.portal.util.comparator.LayoutPriorityComparator;
060 import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
061 import com.liferay.portlet.documentlibrary.NoSuchFolderException;
062 import com.liferay.portlet.documentlibrary.model.DLFolder;
063 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
064 import com.liferay.portlet.expando.model.ExpandoBridge;
065
066 import java.io.File;
067 import java.io.IOException;
068 import java.io.InputStream;
069
070 import java.util.ArrayList;
071 import java.util.Date;
072 import java.util.HashMap;
073 import java.util.HashSet;
074 import java.util.LinkedHashSet;
075 import java.util.List;
076 import java.util.Locale;
077 import java.util.Map;
078 import java.util.Set;
079
080
088 public class LayoutLocalServiceImpl extends LayoutLocalServiceBaseImpl {
089
090 public static String getCounterName(long groupId, boolean privateLayout) {
091 StringBundler sb = new StringBundler();
092
093 sb.append(Layout.class.getName());
094 sb.append(StringPool.POUND);
095 sb.append(groupId);
096 sb.append(StringPool.POUND);
097 sb.append(privateLayout);
098
099 return sb.toString();
100 }
101
102 public Layout addLayout(
103 long userId, long groupId, boolean privateLayout,
104 long parentLayoutId, Map<Locale, String> localeNamesMap,
105 Map<Locale, String> localeTitlesMap, String description,
106 String type, boolean hidden, String friendlyURL, long dlFolderId,
107 ServiceContext serviceContext)
108 throws PortalException, SystemException {
109
110
111
112 User user = userPersistence.findByPrimaryKey(userId);
113 long layoutId = getNextLayoutId(groupId, privateLayout);
114 parentLayoutId = getParentLayoutId(
115 groupId, privateLayout, parentLayoutId);
116 String name = localeNamesMap.get(LocaleUtil.getDefault());
117 friendlyURL = getFriendlyURL(
118 groupId, privateLayout, layoutId, name, friendlyURL);
119 int priority = getNextPriority(groupId, privateLayout, parentLayoutId);
120
121 validate(
122 groupId, privateLayout, layoutId, parentLayoutId, name, type,
123 hidden, friendlyURL);
124
125 long plid = counterLocalService.increment();
126
127 Layout layout = layoutPersistence.create(plid);
128
129 layout.setUuid(serviceContext.getUuid());
130 layout.setGroupId(groupId);
131 layout.setCompanyId(user.getCompanyId());
132 layout.setPrivateLayout(privateLayout);
133 layout.setLayoutId(layoutId);
134 layout.setParentLayoutId(parentLayoutId);
135 layout.setDescription(description);
136 layout.setType(type);
137 layout.setHidden(hidden);
138 layout.setFriendlyURL(friendlyURL);
139 layout.setPriority(priority);
140 layout.setDlFolderId(dlFolderId);
141
142 setLocalizedAttributes(layout, localeNamesMap, localeTitlesMap);
143
144 if (type.equals(LayoutConstants.TYPE_PORTLET)) {
145 LayoutTypePortlet layoutTypePortlet =
146 (LayoutTypePortlet)layout.getLayoutType();
147
148 layoutTypePortlet.setLayoutTemplateId(
149 0, PropsValues.LAYOUT_DEFAULT_TEMPLATE_ID, false);
150 }
151
152 layoutPersistence.update(layout, false);
153
154
155
156 resourceLocalService.addResources(
157 user.getCompanyId(), groupId, user.getUserId(),
158 Layout.class.getName(), layout.getPlid(), false, true, true);
159
160
161
162 layoutSetLocalService.updatePageCount(groupId, privateLayout);
163
164
165
166 ExpandoBridge expandoBridge = layout.getExpandoBridge();
167
168 expandoBridge.setAttributes(serviceContext);
169
170
171
172 if (PropsValues.LAYOUT_COMMENTS_ENABLED) {
173 mbMessageLocalService.addDiscussionMessage(
174 userId, user.getFullName(), groupId, Layout.class.getName(),
175 plid, WorkflowConstants.ACTION_PUBLISH);
176 }
177
178 return layout;
179 }
180
181 public Layout addLayout(
182 long userId, long groupId, boolean privateLayout,
183 long parentLayoutId, Map<Locale, String> localeNamesMap,
184 Map<Locale, String> localeTitlesMap, String description,
185 String type, boolean hidden, String friendlyURL,
186 ServiceContext serviceContext)
187 throws PortalException, SystemException {
188
189 return addLayout(
190 userId, groupId, privateLayout, parentLayoutId, localeNamesMap,
191 localeTitlesMap, description, type, hidden, friendlyURL,
192 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, serviceContext);
193 }
194
195 public Layout addLayout(
196 long userId, long groupId, boolean privateLayout,
197 long parentLayoutId, String name, String title, String description,
198 String type, boolean hidden, String friendlyURL, long dlFolderId,
199 ServiceContext serviceContext)
200 throws PortalException, SystemException {
201
202 Map<Locale, String> localeNamesMap = new HashMap<Locale, String>();
203
204 Locale defaultLocale = LocaleUtil.getDefault();
205
206 localeNamesMap.put(defaultLocale, name);
207
208 return addLayout(
209 userId, groupId, privateLayout, parentLayoutId, localeNamesMap,
210 null, description, type, hidden, friendlyURL, dlFolderId,
211 serviceContext);
212 }
213
214 public Layout addLayout(
215 long userId, long groupId, boolean privateLayout,
216 long parentLayoutId, String name, String title, String description,
217 String type, boolean hidden, String friendlyURL,
218 ServiceContext serviceContext)
219 throws PortalException, SystemException {
220
221 Map<Locale, String> localeNamesMap = new HashMap<Locale, String>();
222
223 Locale defaultLocale = LocaleUtil.getDefault();
224
225 localeNamesMap.put(defaultLocale, name);
226
227 return addLayout(
228 userId, groupId, privateLayout, parentLayoutId, localeNamesMap,
229 new HashMap<Locale, String>(), description, type, hidden,
230 friendlyURL, serviceContext);
231 }
232
233 public void deleteLayout(Layout layout, boolean updateLayoutSet)
234 throws PortalException, SystemException {
235
236
237
238 List<Layout> childLayouts = layoutPersistence.findByG_P_P(
239 layout.getGroupId(), layout.isPrivateLayout(),
240 layout.getLayoutId());
241
242 for (Layout childLayout : childLayouts) {
243 deleteLayout(childLayout, updateLayoutSet);
244 }
245
246
247
248 portletPreferencesLocalService.deletePortletPreferences(
249 PortletKeys.PREFS_OWNER_ID_DEFAULT,
250 PortletKeys.PREFS_OWNER_TYPE_LAYOUT, layout.getPlid());
251
252
253
254 tasksProposalLocalService.deleteProposal(
255 Layout.class.getName(), String.valueOf(layout.getPlid()));
256
257
258
259 ratingsStatsLocalService.deleteStats(
260 Layout.class.getName(), layout.getPlid());
261
262
263
264 mbMessageLocalService.deleteDiscussionMessages(
265 Layout.class.getName(), layout.getPlid());
266
267
268
269 journalContentSearchLocalService.deleteLayoutContentSearches(
270 layout.getGroupId(), layout.isPrivateLayout(),
271 layout.getLayoutId());
272
273
274
275 expandoValueLocalService.deleteValues(
276 Layout.class.getName(), layout.getPlid());
277
278
279
280 imageLocalService.deleteImage(layout.getIconImageId());
281
282
283
284 Group scopeGroup = layout.getScopeGroup();
285
286 if (scopeGroup != null) {
287 groupLocalService.deleteGroup(scopeGroup.getGroupId());
288 }
289
290
291
292 String primKey =
293 layout.getPlid() + PortletConstants.LAYOUT_SEPARATOR + "%";
294
295 List<Resource> resources = resourceFinder.findByC_P(
296 layout.getCompanyId(), primKey);
297
298 for (Resource resource : resources) {
299 resourceLocalService.deleteResource(resource);
300 }
301
302 resourceLocalService.deleteResource(
303 layout.getCompanyId(), Layout.class.getName(),
304 ResourceConstants.SCOPE_INDIVIDUAL, layout.getPlid());
305
306
307
308 layoutPersistence.remove(layout);
309
310
311
312 if (updateLayoutSet) {
313 layoutSetLocalService.updatePageCount(
314 layout.getGroupId(), layout.isPrivateLayout());
315 }
316 }
317
318 public void deleteLayout(long plid)
319 throws PortalException, SystemException {
320
321 Layout layout = layoutPersistence.findByPrimaryKey(plid);
322
323 deleteLayout(layout, true);
324 }
325
326 public void deleteLayout(long groupId, boolean privateLayout, long layoutId)
327 throws PortalException, SystemException {
328
329 Layout layout = layoutPersistence.findByG_P_L(
330 groupId, privateLayout, layoutId);
331
332 deleteLayout(layout, true);
333 }
334
335 public void deleteLayouts(long groupId, boolean privateLayout)
336 throws PortalException, SystemException {
337
338
339
340 List<Layout> layouts = layoutPersistence.findByG_P_P(
341 groupId, privateLayout, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
342
343 for (Layout layout : layouts) {
344 try {
345 deleteLayout(layout, false);
346 }
347 catch (NoSuchLayoutException nsle) {
348 }
349 }
350
351
352
353 layoutSetLocalService.updatePageCount(groupId, privateLayout);
354
355
356
357 counterLocalService.reset(getCounterName(groupId, privateLayout));
358 }
359
360 public byte[] exportLayouts(
361 long groupId, boolean privateLayout, long[] layoutIds,
362 Map<String, String[]> parameterMap, Date startDate, Date endDate)
363 throws PortalException, SystemException {
364
365 File file = exportLayoutsAsFile(
366 groupId, privateLayout, layoutIds, parameterMap, startDate,
367 endDate);
368
369 try {
370 return FileUtil.getBytes(file);
371 }
372 catch (IOException ioe) {
373 throw new SystemException(ioe);
374 }
375 finally {
376 file.delete();
377 }
378 }
379
380 public byte[] exportLayouts(
381 long groupId, boolean privateLayout,
382 Map<String, String[]> parameterMap, Date startDate, Date endDate)
383 throws PortalException, SystemException {
384
385 return exportLayouts(
386 groupId, privateLayout, null, parameterMap, startDate, endDate);
387 }
388
389 public File exportLayoutsAsFile(
390 long groupId, boolean privateLayout, long[] layoutIds,
391 Map<String, String[]> parameterMap, Date startDate, Date endDate)
392 throws PortalException, SystemException {
393
394 try {
395 LayoutExporter layoutExporter = new LayoutExporter();
396
397 return layoutExporter.exportLayoutsAsFile(
398 groupId, privateLayout, layoutIds, parameterMap, startDate,
399 endDate);
400 }
401 catch (PortalException pe) {
402 throw pe;
403 }
404 catch (SystemException se) {
405 throw se;
406 }
407 catch (Exception e) {
408 throw new SystemException(e);
409 }
410 }
411
412 public byte[] exportPortletInfo(
413 long plid, long groupId, String portletId,
414 Map<String, String[]> parameterMap, Date startDate, Date endDate)
415 throws PortalException, SystemException {
416
417 File file = exportPortletInfoAsFile(
418 plid, groupId, portletId, parameterMap, startDate, endDate);
419
420 try {
421 return FileUtil.getBytes(file);
422 }
423 catch (IOException ioe) {
424 throw new SystemException(ioe);
425 }
426 finally {
427 file.delete();
428 }
429 }
430
431 public File exportPortletInfoAsFile(
432 long plid, long groupId, String portletId,
433 Map<String, String[]> parameterMap, Date startDate, Date endDate)
434 throws PortalException, SystemException {
435
436 try {
437 PortletExporter portletExporter = new PortletExporter();
438
439 return portletExporter.exportPortletInfoAsFile(
440 plid, groupId, portletId, parameterMap, startDate, endDate);
441 }
442 catch (PortalException pe) {
443 throw pe;
444 }
445 catch (SystemException se) {
446 throw se;
447 }
448 catch (Exception e) {
449 throw new SystemException(e);
450 }
451 }
452
453 public long getDefaultPlid(long groupId) throws SystemException {
454 if (groupId > 0) {
455 List<Layout> layouts = layoutPersistence.findByGroupId(
456 groupId, 0, 1);
457
458 if (layouts.size() > 0) {
459 Layout layout = layouts.get(0);
460
461 return layout.getPlid();
462 }
463 }
464
465 return LayoutConstants.DEFAULT_PLID;
466 }
467
468 public long getDefaultPlid(long groupId, boolean privateLayout)
469 throws SystemException {
470
471 if (groupId > 0) {
472 List<Layout> layouts = layoutPersistence.findByG_P(
473 groupId, privateLayout, 0, 1);
474
475 if (layouts.size() > 0) {
476 Layout layout = layouts.get(0);
477
478 return layout.getPlid();
479 }
480 }
481
482 return LayoutConstants.DEFAULT_PLID;
483 }
484
485 public long getDefaultPlid(
486 long groupId, boolean privateLayout, String portletId)
487 throws PortalException, SystemException {
488
489 if (groupId > 0) {
490 List<Layout> layouts = layoutPersistence.findByG_P(
491 groupId, privateLayout);
492
493 for (Layout layout : layouts) {
494 if (layout.isTypePortlet()) {
495 LayoutTypePortlet layoutTypePortlet =
496 (LayoutTypePortlet)layout.getLayoutType();
497
498 if (layoutTypePortlet.hasPortletId(portletId)) {
499 return layout.getPlid();
500 }
501 }
502 }
503 }
504
505 return LayoutConstants.DEFAULT_PLID;
506 }
507
508 public Layout getDLFolderLayout(long dlFolderId)
509 throws PortalException, SystemException {
510
511 return layoutPersistence.findByDLFolderId(dlFolderId);
512 }
513
514 public Layout getFriendlyURLLayout(
515 long groupId, boolean privateLayout, String friendlyURL)
516 throws PortalException, SystemException {
517
518 if (Validator.isNull(friendlyURL)) {
519 throw new NoSuchLayoutException();
520 }
521
522 friendlyURL = getFriendlyURL(friendlyURL);
523
524 Layout layout = layoutPersistence.fetchByG_P_F(
525 groupId, privateLayout, friendlyURL);
526
527 if ((layout == null) &&
528 (friendlyURL.startsWith(StringPool.SLASH)) &&
529 (Validator.isNumber(friendlyURL.substring(1)))) {
530
531 long layoutId = GetterUtil.getLong(friendlyURL.substring(1));
532
533 layout = layoutPersistence.fetchByG_P_L(
534 groupId, privateLayout, layoutId);
535 }
536
537 if (layout == null) {
538 throw new NoSuchLayoutException();
539 }
540
541 return layout;
542 }
543
544 public Layout getLayout(long plid)
545 throws PortalException, SystemException {
546
547 return layoutPersistence.findByPrimaryKey(plid);
548 }
549
550 public Layout getLayout(long groupId, boolean privateLayout, long layoutId)
551 throws PortalException, SystemException {
552
553 return layoutPersistence.findByG_P_L(groupId, privateLayout, layoutId);
554 }
555
556 public Layout getLayoutByIconImageId(long iconImageId)
557 throws PortalException, SystemException {
558
559 return layoutPersistence.findByIconImageId(iconImageId);
560 }
561
562 public Layout getLayoutByUuidAndGroupId(String uuid, long groupId)
563 throws PortalException, SystemException {
564
565 return layoutPersistence.findByUUID_G(uuid, groupId);
566 }
567
568 public List<Layout> getLayouts(long groupId, boolean privateLayout)
569 throws SystemException {
570
571 return layoutPersistence.findByG_P(groupId, privateLayout);
572 }
573
574 public List<Layout> getLayouts(
575 long groupId, boolean privateLayout, long parentLayoutId)
576 throws SystemException {
577
578 return layoutPersistence.findByG_P_P(
579 groupId, privateLayout, parentLayoutId);
580 }
581
582 public List<Layout> getLayouts(
583 long groupId, boolean privateLayout, long parentLayoutId, int start,
584 int end)
585 throws SystemException {
586
587 return layoutPersistence.findByG_P_P(
588 groupId, privateLayout, parentLayoutId, start, end);
589 }
590
591 public List<Layout> getLayouts(
592 long groupId, boolean privateLayout, long[] layoutIds)
593 throws PortalException, SystemException {
594
595 List<Layout> layouts = new ArrayList<Layout>();
596
597 for (long layoutId : layoutIds) {
598 Layout layout = getLayout(groupId, privateLayout, layoutId);
599
600 layouts.add(layout);
601 }
602
603 return layouts;
604 }
605
606 public List<Layout> getLayouts(
607 long groupId, boolean privateLayout, String type)
608 throws SystemException {
609
610 return layoutPersistence.findByG_P_T(groupId, privateLayout, type);
611 }
612
613 public LayoutReference[] getLayouts(
614 long companyId, String portletId, String preferencesKey,
615 String preferencesValue)
616 throws SystemException {
617
618 List<LayoutReference> layoutReferences = layoutFinder.findByC_P_P(
619 companyId, portletId, preferencesKey, preferencesValue);
620
621 return layoutReferences.toArray(
622 new LayoutReference[layoutReferences.size()]);
623 }
624
625 public long getNextLayoutId(long groupId, boolean privateLayout)
626 throws SystemException {
627
628 long nextLayoutId = counterLocalService.increment(
629 getCounterName(groupId, privateLayout));
630
631 if (nextLayoutId == 1) {
632 List<Layout> layouts = layoutPersistence.findByG_P(
633 groupId, privateLayout, 0, 1, new LayoutComparator());
634
635 if (!layouts.isEmpty()) {
636 Layout layout = layouts.get(0);
637
638 nextLayoutId = layout.getLayoutId() + 1;
639
640 counterLocalService.reset(
641 getCounterName(groupId, privateLayout), nextLayoutId);
642 }
643 }
644
645 return nextLayoutId;
646 }
647
648 public List<Layout> getNullFriendlyURLLayouts() throws SystemException {
649 return layoutFinder.findByNullFriendlyURL();
650 }
651
652 public boolean hasLayouts(
653 long groupId, boolean privateLayout, long parentLayoutId)
654 throws SystemException {
655
656 return layoutPersistence.countByG_P_P(
657 groupId, privateLayout, parentLayoutId) > 0;
658 }
659
660 public void importLayouts(
661 long userId, long groupId, boolean privateLayout,
662 Map<String, String[]> parameterMap, byte[] bytes)
663 throws PortalException, SystemException {
664
665 importLayouts(
666 userId, groupId, privateLayout, parameterMap,
667 new UnsyncByteArrayInputStream(bytes));
668 }
669
670 public void importLayouts(
671 long userId, long groupId, boolean privateLayout,
672 Map<String, String[]> parameterMap, File file)
673 throws PortalException, SystemException {
674
675 try {
676 LayoutImporter layoutImporter = new LayoutImporter();
677
678 layoutImporter.importLayouts(
679 userId, groupId, privateLayout, parameterMap, file);
680 }
681 catch (PortalException pe) {
682 throw pe;
683 }
684 catch (SystemException se) {
685 throw se;
686 }
687 catch (Exception e) {
688 throw new SystemException(e);
689 }
690 }
691
692 public void importLayouts(
693 long userId, long groupId, boolean privateLayout,
694 Map<String, String[]> parameterMap, InputStream is)
695 throws PortalException, SystemException {
696
697 try {
698 File file = FileUtil.createTempFile("lar");
699
700 FileUtil.write(file, is);
701
702 importLayouts(userId, groupId, privateLayout, parameterMap, file);
703 }
704 catch (IOException e) {
705 throw new SystemException(e);
706 }
707 }
708
709 public void importPortletInfo(
710 long userId, long plid, long groupId, String portletId,
711 Map<String, String[]> parameterMap, File file)
712 throws PortalException, SystemException {
713
714 PortletImporter portletImporter = new PortletImporter();
715
716 portletImporter.importPortletInfo(
717 userId, plid, groupId, portletId, parameterMap, file);
718 }
719
720 public void importPortletInfo(
721 long userId, long plid, long groupId, String portletId,
722 Map<String, String[]> parameterMap, InputStream is)
723 throws PortalException, SystemException {
724
725 try {
726 File file = FileUtil.createTempFile("lar");
727
728 FileUtil.write(file, is);
729
730 importPortletInfo(
731 userId, plid, groupId, portletId, parameterMap, file);
732 }
733 catch (IOException e) {
734 throw new SystemException(e);
735 }
736 }
737
738 public void setLayouts(
739 long groupId, boolean privateLayout, long parentLayoutId,
740 long[] layoutIds)
741 throws PortalException, SystemException {
742
743 if (layoutIds == null) {
744 return;
745 }
746
747 if (parentLayoutId == LayoutConstants.DEFAULT_PARENT_LAYOUT_ID) {
748 if (layoutIds.length < 1) {
749 throw new RequiredLayoutException(
750 RequiredLayoutException.AT_LEAST_ONE);
751 }
752
753 Layout layout = layoutPersistence.findByG_P_L(
754 groupId, privateLayout, layoutIds[0]);
755
756 if (!PortalUtil.isLayoutFirstPageable(layout.getType())) {
757 throw new RequiredLayoutException(
758 RequiredLayoutException.FIRST_LAYOUT_TYPE);
759 }
760
761 if (layout.isHidden()) {
762 throw new RequiredLayoutException(
763 RequiredLayoutException.FIRST_LAYOUT_HIDDEN);
764 }
765 }
766
767 Set<Long> layoutIdsSet = new LinkedHashSet<Long>();
768
769 for (long layoutId : layoutIds) {
770 layoutIdsSet.add(layoutId);
771 }
772
773 Set<Long> newLayoutIdsSet = new HashSet<Long>();
774
775 List<Layout> layouts = layoutPersistence.findByG_P_P(
776 groupId, privateLayout, parentLayoutId);
777
778 for (Layout layout : layouts) {
779 if (!layoutIdsSet.contains(layout.getLayoutId())) {
780 deleteLayout(layout, true);
781 }
782 else {
783 newLayoutIdsSet.add(layout.getLayoutId());
784 }
785 }
786
787 int priority = 0;
788
789 for (long layoutId : layoutIdsSet) {
790 Layout layout = layoutPersistence.findByG_P_L(
791 groupId, privateLayout, layoutId);
792
793 layout.setPriority(priority++);
794
795 layoutPersistence.update(layout, false);
796 }
797
798 layoutSetLocalService.updatePageCount(groupId, privateLayout);
799 }
800
801 public Layout updateFriendlyURL(long plid, String friendlyURL)
802 throws PortalException, SystemException {
803
804 Layout layout = layoutPersistence.findByPrimaryKey(plid);
805
806 friendlyURL = getFriendlyURL(
807 layout.getGroupId(), layout.getPrivateLayout(),
808 layout.getLayoutId(), StringPool.BLANK, friendlyURL);
809
810 validateFriendlyURL(
811 layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
812 friendlyURL);
813
814 layout.setFriendlyURL(friendlyURL);
815
816 layoutPersistence.update(layout, false);
817
818 return layout;
819 }
820
821 public Layout updateLayout(
822 long groupId, boolean privateLayout, long layoutId,
823 long parentLayoutId, Map<Locale, String> localeNamesMap,
824 Map<Locale, String> localeTitlesMap, String description,
825 String type, boolean hidden, String friendlyURL, Boolean iconImage,
826 byte[] iconBytes, ServiceContext serviceContext)
827 throws PortalException, SystemException {
828
829
830
831 parentLayoutId = getParentLayoutId(
832 groupId, privateLayout, parentLayoutId);
833 String name = localeNamesMap.get(LocaleUtil.getDefault());
834 friendlyURL = getFriendlyURL(
835 groupId, privateLayout, layoutId, StringPool.BLANK, friendlyURL);
836
837 validate(
838 groupId, privateLayout, layoutId, parentLayoutId, name, type,
839 hidden, friendlyURL);
840
841 validateParentLayoutId(
842 groupId, privateLayout, layoutId, parentLayoutId);
843
844 Layout layout = layoutPersistence.findByG_P_L(
845 groupId, privateLayout, layoutId);
846
847 if (parentLayoutId != layout.getParentLayoutId()) {
848 layout.setPriority(
849 getNextPriority(groupId, privateLayout, parentLayoutId));
850 }
851
852 layout.setParentLayoutId(parentLayoutId);
853 layout.setDescription(description);
854 layout.setType(type);
855 layout.setHidden(hidden);
856 layout.setFriendlyURL(friendlyURL);
857
858 setLocalizedAttributes(layout, localeNamesMap, localeTitlesMap);
859
860 if (iconImage != null) {
861 layout.setIconImage(iconImage.booleanValue());
862
863 if (iconImage.booleanValue()) {
864 long iconImageId = layout.getIconImageId();
865
866 if (iconImageId <= 0) {
867 iconImageId = counterLocalService.increment();
868
869 layout.setIconImageId(iconImageId);
870 }
871 }
872 }
873
874 layoutPersistence.update(layout, false);
875
876
877
878 if (iconImage != null) {
879 if (!iconImage.booleanValue()) {
880 imageLocalService.deleteImage(layout.getIconImageId());
881 }
882 else if ((iconBytes != null) && (iconBytes.length > 0)) {
883 imageLocalService.updateImage(
884 layout.getIconImageId(), iconBytes);
885 }
886 }
887
888
889
890 try {
891 if (layout.getDlFolderId() > 0) {
892 DLFolder folder = dlFolderLocalService.getFolder(
893 layout.getDlFolderId());
894
895 if (!name.equals(folder.getName())) {
896 dlFolderLocalService.updateFolder(
897 folder.getFolderId(), folder.getParentFolderId(), name,
898 folder.getDescription(), new ServiceContext());
899 }
900 }
901 }
902 catch (DuplicateFolderNameException dfne) {
903 if (_log.isDebugEnabled()) {
904 _log.debug(dfne);
905 }
906 }
907 catch (NoSuchFolderException nsfe) {
908 }
909
910
911
912 ExpandoBridge expandoBridge = layout.getExpandoBridge();
913
914 expandoBridge.setAttributes(serviceContext);
915
916 return layout;
917 }
918
919 public Layout updateLayout(
920 long groupId, boolean privateLayout, long layoutId,
921 long parentLayoutId, Map<Locale, String> localeNamesMap,
922 Map<Locale, String> localeTitlesMap, String description,
923 String type, boolean hidden, String friendlyURL,
924 ServiceContext serviceContext)
925 throws PortalException, SystemException {
926
927 return updateLayout(
928 groupId, privateLayout, layoutId, parentLayoutId, localeNamesMap,
929 localeTitlesMap, description, type, hidden, friendlyURL, null,
930 null, serviceContext);
931 }
932
933 public Layout updateLayout(
934 long groupId, boolean privateLayout, long layoutId,
935 String typeSettings)
936 throws PortalException, SystemException {
937
938 Layout layout = layoutPersistence.findByG_P_L(
939 groupId, privateLayout, layoutId);
940
941 layout.setTypeSettings(typeSettings);
942
943 layoutPersistence.update(layout, false);
944
945 return layout;
946 }
947
948 public Layout updateLookAndFeel(
949 long groupId, boolean privateLayout, long layoutId, String themeId,
950 String colorSchemeId, String css, boolean wapTheme)
951 throws PortalException, SystemException {
952
953 Layout layout = layoutPersistence.findByG_P_L(
954 groupId, privateLayout, layoutId);
955
956 if (wapTheme) {
957 layout.setWapThemeId(themeId);
958 layout.setWapColorSchemeId(colorSchemeId);
959 }
960 else {
961 layout.setThemeId(themeId);
962 layout.setColorSchemeId(colorSchemeId);
963 layout.setCss(css);
964 }
965
966 layoutPersistence.update(layout, false);
967
968 return layout;
969 }
970
971 public Layout updateName(Layout layout, String name, String languageId)
972 throws PortalException, SystemException {
973
974 validateName(name, languageId);
975
976 layout.setName(name, LocaleUtil.fromLanguageId(languageId));
977
978 layoutPersistence.update(layout, false);
979
980 try {
981 if (layout.getDlFolderId() > 0) {
982 DLFolder folder = dlFolderLocalService.getFolder(
983 layout.getDlFolderId());
984
985 ServiceContext serviceContext = new ServiceContext();
986
987 dlFolderLocalService.updateFolder(
988 folder.getFolderId(), folder.getParentFolderId(),
989 layout.getName(LocaleUtil.getDefault()),
990 folder.getDescription(), serviceContext);
991 }
992 }
993 catch (NoSuchFolderException nsfe) {
994 }
995
996 return layout;
997 }
998
999 public Layout updateName(
1000 long groupId, boolean privateLayout, long layoutId, String name,
1001 String languageId)
1002 throws PortalException, SystemException {
1003
1004 Layout layout = layoutPersistence.findByG_P_L(
1005 groupId, privateLayout, layoutId);
1006
1007 return updateName(layout, name, languageId);
1008 }
1009
1010 public Layout updateName(long plid, String name, String languageId)
1011 throws PortalException, SystemException {
1012
1013 Layout layout = layoutPersistence.findByPrimaryKey(plid);
1014
1015 return updateName(layout, name, languageId);
1016 }
1017
1018 public Layout updateParentLayoutId(
1019 long groupId, boolean privateLayout, long layoutId,
1020 long parentLayoutId)
1021 throws PortalException, SystemException {
1022
1023 parentLayoutId = getParentLayoutId(
1024 groupId, privateLayout, parentLayoutId);
1025
1026 validateParentLayoutId(
1027 groupId, privateLayout, layoutId, parentLayoutId);
1028
1029 Layout layout = layoutPersistence.findByG_P_L(
1030 groupId, privateLayout, layoutId);
1031
1032 if (parentLayoutId != layout.getParentLayoutId()) {
1033 layout.setPriority(
1034 getNextPriority(groupId, privateLayout, parentLayoutId));
1035 }
1036
1037 layout.setParentLayoutId(parentLayoutId);
1038
1039 layoutPersistence.update(layout, false);
1040
1041 return layout;
1042 }
1043
1044 public Layout updateParentLayoutId(long plid, long parentPlid)
1045 throws PortalException, SystemException {
1046
1047 Layout layout = layoutPersistence.findByPrimaryKey(plid);
1048
1049 long parentLayoutId = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;
1050
1051 if (parentPlid > 0) {
1052 Layout parentLayout = layoutPersistence.fetchByPrimaryKey(
1053 parentPlid);
1054
1055 if (parentLayout != null) {
1056 parentLayoutId = parentLayout.getLayoutId();
1057 }
1058 }
1059
1060 parentLayoutId = getParentLayoutId(
1061 layout.getGroupId(), layout.isPrivateLayout(), parentLayoutId);
1062
1063 validateParentLayoutId(
1064 layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
1065 parentLayoutId);
1066
1067 if (parentLayoutId != layout.getParentLayoutId()) {
1068 int priority = getNextPriority(
1069 layout.getGroupId(), layout.isPrivateLayout(), parentLayoutId);
1070
1071 layout.setPriority(priority);
1072 }
1073
1074 layout.setParentLayoutId(parentLayoutId);
1075
1076 layoutPersistence.update(layout, false);
1077
1078 return layout;
1079 }
1080
1081 public Layout updatePriority(Layout layout, int priority)
1082 throws SystemException {
1083
1084 if (layout.getPriority() == priority) {
1085 return layout;
1086 }
1087
1088 boolean lessThan = false;
1089
1090 if (layout.getPriority() < priority) {
1091 lessThan = true;
1092 }
1093
1094 layout.setPriority(priority);
1095
1096 layoutPersistence.update(layout, false);
1097
1098 priority = 0;
1099
1100 List<Layout> layouts = layoutPersistence.findByG_P_P(
1101 layout.getGroupId(), layout.isPrivateLayout(),
1102 layout.getParentLayoutId());
1103
1104 layouts = ListUtil.sort(
1105 layouts, new LayoutPriorityComparator(layout, lessThan));
1106
1107 for (Layout curLayout : layouts) {
1108 curLayout.setPriority(priority++);
1109
1110 layoutPersistence.update(curLayout, false);
1111
1112 if (curLayout.equals(layout)) {
1113 layout = curLayout;
1114 }
1115 }
1116
1117 return layout;
1118 }
1119
1120 public Layout updatePriority(
1121 long groupId, boolean privateLayout, long layoutId, int priority)
1122 throws PortalException, SystemException {
1123
1124 Layout layout = layoutPersistence.findByG_P_L(
1125 groupId, privateLayout, layoutId);
1126
1127 return updatePriority(layout, priority);
1128 }
1129
1130 public Layout updatePriority(long plid, int priority)
1131 throws PortalException, SystemException {
1132
1133 Layout layout = layoutPersistence.findByPrimaryKey(plid);
1134
1135 return updatePriority(layout, priority);
1136 }
1137
1138 protected String getFriendlyURL(
1139 long groupId, boolean privateLayout, long layoutId,
1140 String name, String friendlyURL)
1141 throws PortalException, SystemException {
1142
1143 friendlyURL = getFriendlyURL(friendlyURL);
1144
1145 if (Validator.isNull(friendlyURL)) {
1146 friendlyURL = StringPool.SLASH + getFriendlyURL(name);
1147
1148 String originalFriendlyURL = friendlyURL;
1149
1150 for (int i = 1;; i++) {
1151 try {
1152 validateFriendlyURL(
1153 groupId, privateLayout, layoutId, friendlyURL);
1154
1155 break;
1156 }
1157 catch (LayoutFriendlyURLException lfurle) {
1158 int type = lfurle.getType();
1159
1160 if (type == LayoutFriendlyURLException.DUPLICATE) {
1161 friendlyURL = originalFriendlyURL + i;
1162 }
1163 else {
1164 friendlyURL = StringPool.SLASH + layoutId;
1165
1166 break;
1167 }
1168 }
1169 }
1170 }
1171
1172 return friendlyURL;
1173 }
1174
1175 protected String getFriendlyURL(String friendlyURL) {
1176 return FriendlyURLNormalizer.normalize(friendlyURL);
1177 }
1178
1179 protected int getNextPriority(
1180 long groupId, boolean privateLayout, long parentLayoutId)
1181 throws SystemException {
1182
1183 List<Layout> layouts = layoutPersistence.findByG_P_P(
1184 groupId, privateLayout, parentLayoutId);
1185
1186 if (layouts.size() == 0) {
1187 return 0;
1188 }
1189
1190 Layout layout = layouts.get(layouts.size() - 1);
1191
1192 return layout.getPriority() + 1;
1193 }
1194
1195 protected long getParentLayoutId(
1196 long groupId, boolean privateLayout, long parentLayoutId)
1197 throws SystemException {
1198
1199 if (parentLayoutId != LayoutConstants.DEFAULT_PARENT_LAYOUT_ID) {
1200
1201
1202
1203 Layout parentLayout = layoutPersistence.fetchByG_P_L(
1204 groupId, privateLayout, parentLayoutId);
1205
1206 if (parentLayout == null) {
1207 parentLayoutId = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;
1208 }
1209 }
1210
1211 return parentLayoutId;
1212 }
1213
1214 protected boolean isDescendant(Layout layout, long layoutId)
1215 throws PortalException, SystemException {
1216
1217 if (layout.getLayoutId() == layoutId) {
1218 return true;
1219 }
1220 else {
1221 for (Layout childLayout : layout.getChildren()) {
1222 if (isDescendant(childLayout, layoutId)) {
1223 return true;
1224 }
1225 }
1226
1227 return false;
1228 }
1229 }
1230
1231 protected void setLocalizedAttributes(
1232 Layout layout, Map<Locale, String> localeNamesMap,
1233 Map<Locale, String> localeTitlesMap) {
1234
1235 if (localeNamesMap == null) {
1236 return;
1237 }
1238
1239 if (localeTitlesMap == null) {
1240 return;
1241 }
1242
1243 Locale[] locales = LanguageUtil.getAvailableLocales();
1244
1245 for (Locale locale : locales) {
1246 String name = localeNamesMap.get(locale);
1247 String title = localeTitlesMap.get(locale);
1248
1249 layout.setName(name, locale);
1250 layout.setTitle(title, locale);
1251 }
1252 }
1253
1254 protected void validate(
1255 long groupId, boolean privateLayout, long layoutId,
1256 long parentLayoutId, String name, String type, boolean hidden,
1257 String friendlyURL)
1258 throws PortalException, SystemException {
1259
1260 validateName(name);
1261
1262 boolean firstLayout = false;
1263
1264 if (parentLayoutId == LayoutConstants.DEFAULT_PARENT_LAYOUT_ID) {
1265 List<Layout> layouts = layoutPersistence.findByG_P_P(
1266 groupId, privateLayout, parentLayoutId, 0, 1);
1267
1268 if (layouts.size() == 0) {
1269 firstLayout = true;
1270 }
1271 else {
1272 long firstLayoutId = layouts.get(0).getLayoutId();
1273
1274 if (firstLayoutId == layoutId) {
1275 firstLayout = true;
1276 }
1277 }
1278 }
1279
1280 if (firstLayout) {
1281 validateFirstLayout(type, hidden);
1282 }
1283
1284 if (!PortalUtil.isLayoutParentable(type)) {
1285 if (layoutPersistence.countByG_P_P(
1286 groupId, privateLayout, layoutId) > 0) {
1287
1288 throw new LayoutTypeException(
1289 LayoutTypeException.NOT_PARENTABLE);
1290 }
1291 }
1292
1293 validateFriendlyURL(groupId, privateLayout, layoutId, friendlyURL);
1294 }
1295
1296 protected void validateFirstLayout(String type, boolean hidden)
1297 throws PortalException {
1298
1299 if (!PortalUtil.isLayoutFirstPageable(type)) {
1300 throw new LayoutTypeException(LayoutTypeException.FIRST_LAYOUT);
1301 }
1302
1303 if (hidden) {
1304 throw new LayoutHiddenException();
1305 }
1306 }
1307
1308 protected void validateFriendlyURL(
1309 long groupId, boolean privateLayout, long layoutId,
1310 String friendlyURL)
1311 throws PortalException, SystemException {
1312
1313 if (Validator.isNull(friendlyURL)) {
1314 return;
1315 }
1316
1317 int exceptionType = LayoutImpl.validateFriendlyURL(friendlyURL);
1318
1319 if (exceptionType != -1) {
1320 throw new LayoutFriendlyURLException(exceptionType);
1321 }
1322
1323 Layout layout = layoutPersistence.fetchByG_P_F(
1324 groupId, privateLayout, friendlyURL);
1325
1326 if ((layout != null) && (layout.getLayoutId() != layoutId)) {
1327 throw new LayoutFriendlyURLException(
1328 LayoutFriendlyURLException.DUPLICATE);
1329 }
1330
1331 LayoutImpl.validateFriendlyURLKeyword(friendlyURL);
1332
1333
1347
1348 String layoutIdFriendlyURL = friendlyURL.substring(1);
1349
1350 if (Validator.isNumber(layoutIdFriendlyURL) &&
1351 !layoutIdFriendlyURL.equals(String.valueOf(layoutId))) {
1352
1353 LayoutFriendlyURLException lfurle = new LayoutFriendlyURLException(
1354 LayoutFriendlyURLException.POSSIBLE_DUPLICATE);
1355
1356 lfurle.setKeywordConflict(layoutIdFriendlyURL);
1357
1358 throw lfurle;
1359 }
1360 }
1361
1362 protected void validateName(String name) throws PortalException {
1363 if (Validator.isNull(name)) {
1364 throw new LayoutNameException();
1365 }
1366 }
1367
1368 protected void validateName(String name, String languageId)
1369 throws PortalException {
1370
1371 String defaultLanguageId = LocaleUtil.toLanguageId(
1372 LocaleUtil.getDefault());
1373
1374 if (defaultLanguageId.equals(languageId)) {
1375 validateName(name);
1376 }
1377 }
1378
1379 protected void validateParentLayoutId(
1380 long groupId, boolean privateLayout, long layoutId,
1381 long parentLayoutId)
1382 throws PortalException, SystemException {
1383
1384 Layout layout = layoutPersistence.findByG_P_L(
1385 groupId, privateLayout, layoutId);
1386
1387 if (parentLayoutId != layout.getParentLayoutId()) {
1388
1389
1390
1391 if (parentLayoutId == LayoutConstants.DEFAULT_PARENT_LAYOUT_ID) {
1392 return;
1393 }
1394
1395
1396
1397 Layout parentLayout = layoutPersistence.findByG_P_L(
1398 groupId, privateLayout, parentLayoutId);
1399
1400 if (!PortalUtil.isLayoutParentable(parentLayout)) {
1401 throw new LayoutParentLayoutIdException(
1402 LayoutParentLayoutIdException.NOT_PARENTABLE);
1403 }
1404
1405
1406
1407 if (isDescendant(layout, parentLayoutId)) {
1408 throw new LayoutParentLayoutIdException(
1409 LayoutParentLayoutIdException.SELF_DESCENDANT);
1410 }
1411
1412
1413
1414 if (layout.getParentLayoutId() ==
1415 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID) {
1416
1417 List<Layout> layouts = layoutPersistence.findByG_P_P(
1418 groupId, privateLayout,
1419 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, 0, 2);
1420
1421
1422
1423
1424 long firstLayoutId = layouts.get(0).getLayoutId();
1425
1426 if (firstLayoutId == layoutId) {
1427 Layout secondLayout = layouts.get(1);
1428
1429 try {
1430 validateFirstLayout(
1431 secondLayout.getType(), secondLayout.getHidden());
1432 }
1433 catch (LayoutHiddenException lhe) {
1434 throw new LayoutParentLayoutIdException(
1435 LayoutParentLayoutIdException.FIRST_LAYOUT_HIDDEN);
1436 }
1437 catch (LayoutTypeException lte) {
1438 throw new LayoutParentLayoutIdException(
1439 LayoutParentLayoutIdException.FIRST_LAYOUT_TYPE);
1440 }
1441 }
1442 }
1443 }
1444 }
1445
1446 private static Log _log = LogFactoryUtil.getLog(
1447 LayoutLocalServiceImpl.class);
1448
1449 }