001
014
015 package com.liferay.portal.model.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.language.LanguageUtil;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.staging.StagingConstants;
023 import com.liferay.portal.kernel.staging.StagingUtil;
024 import com.liferay.portal.kernel.util.GetterUtil;
025 import com.liferay.portal.kernel.util.HtmlUtil;
026 import com.liferay.portal.kernel.util.LocaleUtil;
027 import com.liferay.portal.kernel.util.StringBundler;
028 import com.liferay.portal.kernel.util.StringPool;
029 import com.liferay.portal.kernel.util.StringUtil;
030 import com.liferay.portal.kernel.util.UnicodeProperties;
031 import com.liferay.portal.kernel.util.Validator;
032 import com.liferay.portal.model.Company;
033 import com.liferay.portal.model.Group;
034 import com.liferay.portal.model.GroupConstants;
035 import com.liferay.portal.model.GroupWrapper;
036 import com.liferay.portal.model.Layout;
037 import com.liferay.portal.model.LayoutConstants;
038 import com.liferay.portal.model.LayoutPrototype;
039 import com.liferay.portal.model.LayoutSet;
040 import com.liferay.portal.model.LayoutSetPrototype;
041 import com.liferay.portal.model.Organization;
042 import com.liferay.portal.model.Portlet;
043 import com.liferay.portal.model.PortletConstants;
044 import com.liferay.portal.model.RoleConstants;
045 import com.liferay.portal.model.User;
046 import com.liferay.portal.model.UserGroup;
047 import com.liferay.portal.model.UserPersonalSite;
048 import com.liferay.portal.security.permission.ActionKeys;
049 import com.liferay.portal.security.permission.PermissionChecker;
050 import com.liferay.portal.service.GroupLocalServiceUtil;
051 import com.liferay.portal.service.LayoutLocalServiceUtil;
052 import com.liferay.portal.service.LayoutSetLocalServiceUtil;
053 import com.liferay.portal.service.PortletLocalServiceUtil;
054 import com.liferay.portal.service.RoleLocalServiceUtil;
055 import com.liferay.portal.service.permission.LayoutPermissionUtil;
056 import com.liferay.portal.theme.ThemeDisplay;
057 import com.liferay.portal.util.PortalUtil;
058 import com.liferay.portal.util.PropsValues;
059
060 import java.io.IOException;
061
062 import java.util.ArrayList;
063 import java.util.List;
064 import java.util.Locale;
065 import java.util.Map;
066
067
094 public class GroupImpl extends GroupBaseImpl {
095
096 public GroupImpl() {
097 }
098
099 @Override
100 public void clearStagingGroup() {
101 _stagingGroup = null;
102 }
103
104 @Override
105 public List<Group> getAncestors() throws PortalException, SystemException {
106 Group group = null;
107
108 if (isStagingGroup()) {
109 group = getLiveGroup();
110 }
111 else {
112 group = this;
113 }
114
115 List<Group> groups = new ArrayList<Group>();
116
117 while (!group.isRoot()) {
118 group = group.getParentGroup();
119
120 groups.add(group);
121 }
122
123 return groups;
124 }
125
126 @Override
127 public List<Group> getChildren(boolean site) throws SystemException {
128 return GroupLocalServiceUtil.getGroups(
129 getCompanyId(), getGroupId(), site);
130 }
131
132 @Override
133 public List<Group> getChildrenWithLayouts(boolean site, int start, int end)
134 throws SystemException {
135
136 return GroupLocalServiceUtil.getLayoutsGroups(
137 getCompanyId(), getGroupId(), site, start, end);
138 }
139
140 @Override
141 public int getChildrenWithLayoutsCount(boolean site)
142 throws SystemException {
143
144 return GroupLocalServiceUtil.getLayoutsGroupsCount(
145 getCompanyId(), getGroupId(), site);
146 }
147
148 @Override
149 public long getDefaultPrivatePlid() {
150 return getDefaultPlid(true);
151 }
152
153 @Override
154 public long getDefaultPublicPlid() {
155 return getDefaultPlid(false);
156 }
157
158 @Override
159 public String getDescriptiveName() throws PortalException, SystemException {
160 return getDescriptiveName(LocaleUtil.getDefault());
161 }
162
163 @Override
164 public String getDescriptiveName(Locale locale)
165 throws PortalException, SystemException {
166
167 return GroupLocalServiceUtil.getGroupDescriptiveName(this, locale);
168 }
169
170 @Override
171 public String getIconURL(ThemeDisplay themeDisplay) {
172 String iconURL = themeDisplay.getPathThemeImages() + "/common/";
173
174 if (isCompany()) {
175 iconURL = iconURL.concat("global.png");
176 }
177 else if (isLayout()) {
178 iconURL = iconURL.concat("page.png");
179 }
180 else if (isOrganization()) {
181 iconURL = iconURL.concat("organization_icon.png");
182 }
183 else if (isUser()) {
184 iconURL = iconURL.concat("user_icon.png");
185 }
186 else {
187 iconURL = iconURL.concat("site_icon.png");
188 }
189
190 return iconURL;
191 }
192
193 @Override
194 public String getLayoutRootNodeName(boolean privateLayout, Locale locale) {
195 String pagesName = null;
196
197 if (isLayoutPrototype() || isLayoutSetPrototype() || isUserGroup()) {
198 pagesName = "pages";
199 }
200 else if (privateLayout) {
201 if (isUser()) {
202 pagesName = "my-dashboard";
203 }
204 else {
205 pagesName = "private-pages";
206 }
207 }
208 else {
209 if (isUser()) {
210 pagesName = "my-profile";
211 }
212 else {
213 pagesName = "public-pages";
214 }
215 }
216
217 return LanguageUtil.get(locale, pagesName);
218 }
219
220 @Override
221 public Group getLiveGroup() {
222 if (!isStagingGroup()) {
223 return null;
224 }
225
226 try {
227 if (_liveGroup == null) {
228 _liveGroup = GroupLocalServiceUtil.getGroup(getLiveGroupId());
229
230 if (_liveGroup instanceof GroupImpl) {
231 GroupImpl groupImpl = (GroupImpl)_liveGroup;
232
233 groupImpl._stagingGroup = this;
234 }
235 else {
236 _liveGroup = new GroupWrapper(_liveGroup) {
237
238 @Override
239 public Group getStagingGroup() {
240 return GroupImpl.this;
241 }
242
243 };
244 }
245 }
246
247 return _liveGroup;
248 }
249 catch (Exception e) {
250 _log.error("Error getting live group for " + getLiveGroupId(), e);
251
252 return null;
253 }
254 }
255
256 @Override
257 public String getLiveParentTypeSettingsProperty(String key) {
258 UnicodeProperties typeSettingsProperties =
259 getParentLiveGroupTypeSettingsProperties();
260
261 return typeSettingsProperties.getProperty(key);
262 }
263
264 @Override
265 public long getOrganizationId() {
266 if (isOrganization()) {
267 if (isStagingGroup()) {
268 Group liveGroup = getLiveGroup();
269
270 return liveGroup.getClassPK();
271 }
272 else {
273 return getClassPK();
274 }
275 }
276
277 return 0;
278 }
279
280 @Override
281 public Group getParentGroup() throws PortalException, SystemException {
282 long parentGroupId = getParentGroupId();
283
284 if (parentGroupId <= 0) {
285 return null;
286 }
287
288 return GroupLocalServiceUtil.getGroup(parentGroupId);
289 }
290
291 @Override
292 public UnicodeProperties getParentLiveGroupTypeSettingsProperties() {
293 try {
294 if (isLayout()) {
295 Group parentGroup = GroupLocalServiceUtil.getGroup(
296 getParentGroupId());
297
298 return parentGroup.getParentLiveGroupTypeSettingsProperties();
299 }
300
301 if (isStagingGroup()) {
302 Group liveGroup = getLiveGroup();
303
304 return liveGroup.getTypeSettingsProperties();
305 }
306 }
307 catch (Exception e) {
308 }
309
310 return getTypeSettingsProperties();
311 }
312
313 @Override
314 public String getPathFriendlyURL(
315 boolean privateLayout, ThemeDisplay themeDisplay) {
316
317 if (privateLayout) {
318 if (isUser()) {
319 return themeDisplay.getPathFriendlyURLPrivateUser();
320 }
321 else {
322 return themeDisplay.getPathFriendlyURLPrivateGroup();
323 }
324 }
325 else {
326 return themeDisplay.getPathFriendlyURLPublic();
327 }
328 }
329
330 @Override
331 public LayoutSet getPrivateLayoutSet() {
332 LayoutSet layoutSet = null;
333
334 try {
335 layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
336 getGroupId(), true);
337 }
338 catch (Exception e) {
339 _log.error(e, e);
340 }
341
342 return layoutSet;
343 }
344
345 @Override
346 public int getPrivateLayoutsPageCount() {
347 try {
348 return LayoutLocalServiceUtil.getLayoutsCount(this, true);
349 }
350 catch (Exception e) {
351 _log.error(e, e);
352 }
353
354 return 0;
355 }
356
357 @Override
358 public LayoutSet getPublicLayoutSet() {
359 LayoutSet layoutSet = null;
360
361 try {
362 layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
363 getGroupId(), false);
364 }
365 catch (Exception e) {
366 _log.error(e, e);
367 }
368
369 return layoutSet;
370 }
371
372 @Override
373 public int getPublicLayoutsPageCount() {
374 try {
375 return LayoutLocalServiceUtil.getLayoutsCount(this, false);
376 }
377 catch (Exception e) {
378 _log.error(e, e);
379 }
380
381 return 0;
382 }
383
384 @Override
385 public String getScopeDescriptiveName(ThemeDisplay themeDisplay)
386 throws PortalException, SystemException {
387
388 if (getGroupId() == themeDisplay.getScopeGroupId()) {
389 StringBundler sb = new StringBundler(5);
390
391 sb.append(themeDisplay.translate("current-site"));
392 sb.append(StringPool.SPACE);
393 sb.append(StringPool.OPEN_PARENTHESIS);
394 sb.append(
395 HtmlUtil.escape(getDescriptiveName(themeDisplay.getLocale())));
396 sb.append(StringPool.CLOSE_PARENTHESIS);
397
398 return sb.toString();
399 }
400 else if (isLayout() && (getClassPK() == themeDisplay.getPlid())) {
401 StringBundler sb = new StringBundler(5);
402
403 sb.append(themeDisplay.translate("current-page"));
404 sb.append(StringPool.SPACE);
405 sb.append(StringPool.OPEN_PARENTHESIS);
406 sb.append(
407 HtmlUtil.escape(getDescriptiveName(themeDisplay.getLocale())));
408 sb.append(StringPool.CLOSE_PARENTHESIS);
409
410 return sb.toString();
411 }
412 else if (isLayoutPrototype()) {
413 return themeDisplay.translate("default");
414 }
415 else {
416 return HtmlUtil.escape(
417 getDescriptiveName(themeDisplay.getLocale()));
418 }
419 }
420
421 @Override
422 public String getScopeLabel(ThemeDisplay themeDisplay) {
423 String label = "site";
424
425 if (getGroupId() == themeDisplay.getScopeGroupId()) {
426 label = "current-site";
427 }
428 else if (getGroupId() == themeDisplay.getCompanyGroupId()) {
429 label = "global";
430 }
431 else if (isLayout()) {
432 label = "page";
433 }
434 else {
435 Group scopeGroup = themeDisplay.getScopeGroup();
436
437 if (scopeGroup.hasAncestor(getGroupId())) {
438 label = "parent-site";
439 }
440 else if (hasAncestor(scopeGroup.getGroupId())) {
441 label = "child-site";
442 }
443 }
444
445 return label;
446 }
447
448 @Override
449 public Group getStagingGroup() {
450 if (isStagingGroup()) {
451 return null;
452 }
453
454 try {
455 if (_stagingGroup == null) {
456 _stagingGroup = GroupLocalServiceUtil.getStagingGroup(
457 getGroupId());
458
459 if (_stagingGroup instanceof GroupImpl) {
460 GroupImpl groupImpl = (GroupImpl)_stagingGroup;
461
462 groupImpl._liveGroup = this;
463 }
464 else {
465 _stagingGroup = new GroupWrapper(_stagingGroup) {
466
467 @Override
468 public Group getLiveGroup() {
469 return GroupImpl.this;
470 }
471
472 };
473 }
474 }
475
476 return _stagingGroup;
477 }
478 catch (Exception e) {
479 _log.error("Error getting staging group for " + getGroupId(), e);
480
481 return null;
482 }
483 }
484
485 @Override
486 public String getTypeLabel() {
487 return GroupConstants.getTypeLabel(getType());
488 }
489
490 @Override
491 public String getTypeSettings() {
492 if (_typeSettingsProperties == null) {
493 return super.getTypeSettings();
494 }
495 else {
496 return _typeSettingsProperties.toString();
497 }
498 }
499
500 @Override
501 public UnicodeProperties getTypeSettingsProperties() {
502 if (_typeSettingsProperties == null) {
503 _typeSettingsProperties = new UnicodeProperties(true);
504
505 try {
506 _typeSettingsProperties.load(super.getTypeSettings());
507 }
508 catch (IOException ioe) {
509 _log.error(ioe, ioe);
510 }
511 }
512
513 return _typeSettingsProperties;
514 }
515
516 @Override
517 public String getTypeSettingsProperty(String key) {
518 UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
519
520 return typeSettingsProperties.getProperty(key);
521 }
522
523 @Override
524 public boolean hasAncestor(long groupId) {
525 Group group = null;
526
527 if (isStagingGroup()) {
528 group = getLiveGroup();
529 }
530 else {
531 group = this;
532 }
533
534 String treePath = group.getTreePath();
535
536 if ((groupId != group.getGroupId()) &&
537 treePath.contains(StringPool.SLASH + groupId + StringPool.SLASH)) {
538
539 return true;
540 }
541
542 return false;
543 }
544
545 @Override
546 public boolean hasLocalOrRemoteStagingGroup() {
547 if (hasStagingGroup() || (getRemoteStagingGroupCount() > 0)) {
548 return true;
549 }
550
551 return false;
552 }
553
554 @Override
555 public boolean hasPrivateLayouts() {
556 if (getPrivateLayoutsPageCount() > 0) {
557 return true;
558 }
559 else {
560 return false;
561 }
562 }
563
564 @Override
565 public boolean hasPublicLayouts() {
566 if (getPublicLayoutsPageCount() > 0) {
567 return true;
568 }
569 else {
570 return false;
571 }
572 }
573
574 @Override
575 public boolean hasStagingGroup() {
576 if (isStagingGroup()) {
577 return false;
578 }
579
580 if (_stagingGroup != null) {
581 return true;
582 }
583
584 try {
585 return GroupLocalServiceUtil.hasStagingGroup(getGroupId());
586 }
587 catch (Exception e) {
588 return false;
589 }
590 }
591
592
595 @Override
596 public boolean isChild(long groupId) {
597 return hasAncestor(groupId);
598 }
599
600
603 @Override
604 public boolean isCommunity() {
605 return isRegularSite();
606 }
607
608 @Override
609 public boolean isCompany() {
610 return hasClassName(Company.class) || isCompanyStagingGroup();
611 }
612
613 @Override
614 public boolean isCompanyStagingGroup() {
615 Group liveGroup = getLiveGroup();
616
617 if (liveGroup == null) {
618 return false;
619 }
620
621 return liveGroup.isCompany();
622 }
623
624 @Override
625 public boolean isControlPanel() {
626 String name = getName();
627
628 if (name.equals(GroupConstants.CONTROL_PANEL)) {
629 return true;
630 }
631 else {
632 return false;
633 }
634 }
635
636 @Override
637 public boolean isGuest() {
638 String name = getName();
639
640 if (name.equals(GroupConstants.GUEST)) {
641 return true;
642 }
643 else {
644 return false;
645 }
646 }
647
648 @Override
649 public boolean isInStagingPortlet(String portletId) {
650 Group liveGroup = getLiveGroup();
651
652 if (liveGroup == null) {
653 return false;
654 }
655
656 return liveGroup.isStagedPortlet(portletId);
657 }
658
659 @Override
660 public boolean isLayout() {
661 return hasClassName(Layout.class);
662 }
663
664 @Override
665 public boolean isLayoutPrototype() {
666 return hasClassName(LayoutPrototype.class);
667 }
668
669 @Override
670 public boolean isLayoutSetPrototype() {
671 return hasClassName(LayoutSetPrototype.class);
672 }
673
674 @Override
675 public boolean isLimitedToParentSiteMembers() {
676 if ((getParentGroupId() != GroupConstants.DEFAULT_PARENT_GROUP_ID) &&
677 (getMembershipRestriction() ==
678 GroupConstants.MEMBERSHIP_RESTRICTION_TO_PARENT_SITE_MEMBERS)) {
679
680 return true;
681 }
682
683 return false;
684 }
685
686 @Override
687 public boolean isOrganization() {
688 return hasClassName(Organization.class);
689 }
690
691 @Override
692 public boolean isRegularSite() {
693 return hasClassName(Group.class);
694 }
695
696 @Override
697 public boolean isRoot() {
698 if (getParentGroupId() ==
699 GroupConstants.DEFAULT_PARENT_GROUP_ID) {
700
701 return true;
702 }
703
704 return false;
705 }
706
707 @Override
708 public boolean isShowSite(
709 PermissionChecker permissionChecker, boolean privateSite)
710 throws PortalException, SystemException {
711
712 if (!isControlPanel() && !isSite() && !isUser()) {
713 return false;
714 }
715
716 boolean showSite = true;
717
718 Layout defaultLayout = null;
719
720 int siteLayoutsCount = LayoutLocalServiceUtil.getLayoutsCount(
721 this, privateSite);
722
723 if (siteLayoutsCount == 0) {
724 boolean hasPowerUserRole = RoleLocalServiceUtil.hasUserRole(
725 permissionChecker.getUserId(), permissionChecker.getCompanyId(),
726 RoleConstants.POWER_USER, true);
727
728 if (isSite()) {
729 if (privateSite) {
730 showSite =
731 PropsValues.MY_SITES_SHOW_PRIVATE_SITES_WITH_NO_LAYOUTS;
732 }
733 else {
734 showSite =
735 PropsValues.MY_SITES_SHOW_PUBLIC_SITES_WITH_NO_LAYOUTS;
736 }
737 }
738 else if (isOrganization()) {
739 showSite = false;
740 }
741 else if (isUser()) {
742 if (privateSite) {
743 showSite =
744 PropsValues.
745 MY_SITES_SHOW_USER_PRIVATE_SITES_WITH_NO_LAYOUTS;
746
747 if (PropsValues.
748 LAYOUT_USER_PRIVATE_LAYOUTS_POWER_USER_REQUIRED &&
749 !hasPowerUserRole) {
750
751 showSite = false;
752 }
753 }
754 else {
755 showSite =
756 PropsValues.
757 MY_SITES_SHOW_USER_PUBLIC_SITES_WITH_NO_LAYOUTS;
758
759 if (PropsValues.
760 LAYOUT_USER_PUBLIC_LAYOUTS_POWER_USER_REQUIRED &&
761 !hasPowerUserRole) {
762
763 showSite = false;
764 }
765 }
766 }
767 }
768 else {
769 defaultLayout = LayoutLocalServiceUtil.fetchFirstLayout(
770 getGroupId(), privateSite,
771 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
772
773 if ((defaultLayout != null ) &&
774 !LayoutPermissionUtil.contains(
775 permissionChecker, defaultLayout, true, ActionKeys.VIEW)) {
776
777 showSite = false;
778 }
779 else if (isOrganization() && !isSite()) {
780 _log.error(
781 "Group " + getGroupId() +
782 " is an organization site that does not have pages");
783 }
784 }
785
786 return showSite;
787 }
788
789 @Override
790 public boolean isStaged() {
791 return GetterUtil.getBoolean(
792 getLiveParentTypeSettingsProperty("staged"));
793 }
794
795 @Override
796 public boolean isStagedPortlet(String portletId) {
797 UnicodeProperties typeSettingsProperties =
798 getParentLiveGroupTypeSettingsProperties();
799
800 portletId = PortletConstants.getRootPortletId(portletId);
801
802 String typeSettingsProperty = typeSettingsProperties.getProperty(
803 StagingUtil.getStagedPortletId(portletId));
804
805 if (Validator.isNotNull(typeSettingsProperty)) {
806 return GetterUtil.getBoolean(typeSettingsProperty);
807 }
808
809 try {
810 Portlet portlet = PortletLocalServiceUtil.getPortletById(portletId);
811
812 String portletDataHandlerClass =
813 portlet.getPortletDataHandlerClass();
814
815 for (Map.Entry<String, String> entry :
816 typeSettingsProperties.entrySet()) {
817
818 String key = entry.getKey();
819
820 if (!key.contains(StagingConstants.STAGED_PORTLET)) {
821 continue;
822 }
823
824 String stagedPortletId = StringUtil.replace(
825 key, StagingConstants.STAGED_PORTLET, StringPool.BLANK);
826
827 Portlet stagedPortlet = PortletLocalServiceUtil.getPortletById(
828 stagedPortletId);
829
830 if (portletDataHandlerClass.equals(
831 stagedPortlet.getPortletDataHandlerClass())) {
832
833 return GetterUtil.getBoolean(entry.getValue());
834 }
835 }
836 }
837 catch (Exception e) {
838 }
839
840 return true;
841 }
842
843 @Override
844 public boolean isStagedRemotely() {
845 return GetterUtil.getBoolean(
846 getLiveParentTypeSettingsProperty("stagedRemotely"));
847 }
848
849 @Override
850 public boolean isStagingGroup() {
851 if (getLiveGroupId() == GroupConstants.DEFAULT_LIVE_GROUP_ID) {
852 return false;
853 }
854 else {
855 return true;
856 }
857 }
858
859 @Override
860 public boolean isUser() {
861 return hasClassName(User.class);
862 }
863
864 @Override
865 public boolean isUserGroup() {
866 return hasClassName(UserGroup.class);
867 }
868
869 @Override
870 public boolean isUserPersonalSite() {
871 return hasClassName(UserPersonalSite.class);
872 }
873
874 @Override
875 public void setTypeSettings(String typeSettings) {
876 _typeSettingsProperties = null;
877
878 super.setTypeSettings(typeSettings);
879 }
880
881 @Override
882 public void setTypeSettingsProperties(
883 UnicodeProperties typeSettingsProperties) {
884
885 _typeSettingsProperties = typeSettingsProperties;
886
887 super.setTypeSettings(_typeSettingsProperties.toString());
888 }
889
890 protected long getDefaultPlid(boolean privateLayout) {
891 try {
892 Layout firstLayout = LayoutLocalServiceUtil.fetchFirstLayout(
893 getGroupId(), privateLayout,
894 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
895
896 if (firstLayout != null) {
897 return firstLayout.getPlid();
898 }
899 }
900 catch (Exception e) {
901 if (_log.isWarnEnabled()) {
902 _log.warn(e.getMessage());
903 }
904 }
905
906 return LayoutConstants.DEFAULT_PLID;
907 }
908
909 protected boolean hasClassName(Class<?> clazz) {
910 long classNameId = getClassNameId();
911
912 if (classNameId == PortalUtil.getClassNameId(clazz)) {
913 return true;
914 }
915 else {
916 return false;
917 }
918 }
919
920 private static Log _log = LogFactoryUtil.getLog(GroupImpl.class);
921
922 private Group _liveGroup;
923 private Group _stagingGroup;
924 private UnicodeProperties _typeSettingsProperties;
925
926 }