001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.staging.StagingConstants;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.LocaleUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.UnicodeProperties;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.model.Company;
030    import com.liferay.portal.model.Group;
031    import com.liferay.portal.model.GroupConstants;
032    import com.liferay.portal.model.Layout;
033    import com.liferay.portal.model.LayoutConstants;
034    import com.liferay.portal.model.LayoutPrototype;
035    import com.liferay.portal.model.LayoutSet;
036    import com.liferay.portal.model.LayoutSetPrototype;
037    import com.liferay.portal.model.Organization;
038    import com.liferay.portal.model.Portlet;
039    import com.liferay.portal.model.PortletConstants;
040    import com.liferay.portal.model.RoleConstants;
041    import com.liferay.portal.model.User;
042    import com.liferay.portal.model.UserGroup;
043    import com.liferay.portal.model.UserPersonalSite;
044    import com.liferay.portal.security.permission.ActionKeys;
045    import com.liferay.portal.security.permission.PermissionChecker;
046    import com.liferay.portal.service.GroupLocalServiceUtil;
047    import com.liferay.portal.service.LayoutLocalServiceUtil;
048    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
049    import com.liferay.portal.service.PortletLocalServiceUtil;
050    import com.liferay.portal.service.RoleLocalServiceUtil;
051    import com.liferay.portal.service.permission.LayoutPermissionUtil;
052    import com.liferay.portal.theme.ThemeDisplay;
053    import com.liferay.portal.util.PortalUtil;
054    import com.liferay.portal.util.PropsValues;
055    
056    import java.io.IOException;
057    
058    import java.util.ArrayList;
059    import java.util.List;
060    import java.util.Locale;
061    import java.util.Map;
062    
063    /**
064     * Represents either a site or a generic resource container.
065     *
066     * <p>
067     * Groups are most used in Liferay as a resource container for permissioning and
068     * content scoping purposes. For instance, an site is group, meaning that it can
069     * contain layouts, web content, wiki entries, etc. However, a single layout can
070     * also be a group containing its own unique set of resources. An example of
071     * this would be a site that has several distinct wikis on different layouts.
072     * Each of these layouts would have its own group, and all of the nodes in the
073     * wiki for a certain layout would be associated with that layout's group. This
074     * allows users to be given different permissions on each of the wikis, even
075     * though they are all within the same site. In addition to sites and layouts,
076     * users and organizations are also groups.
077     * </p>
078     *
079     * <p>
080     * Groups also have a second, partially conflicting purpose in Liferay. For
081     * legacy reasons, groups are also the model used to represent sites (known as
082     * communities before Liferay v6.1). Confusion may arise from the fact that a
083     * site group is both the resource container and the site itself, whereas a
084     * layout or organization would have both a primary model and an associated
085     * group.
086     * </p>
087     *
088     * @author Brian Wing Shun Chan
089     */
090    public class GroupImpl extends GroupBaseImpl {
091    
092            public GroupImpl() {
093            }
094    
095            public String buildTreePath() throws PortalException, SystemException {
096                    StringBundler sb = new StringBundler();
097    
098                    buildTreePath(sb, this);
099    
100                    return sb.toString();
101            }
102    
103            public List<Group> getAncestors() throws PortalException, SystemException {
104                    Group group = null;
105    
106                    if (isStagingGroup()) {
107                            group = getLiveGroup();
108                    }
109                    else {
110                            group = this;
111                    }
112    
113                    List<Group> groups = new ArrayList<Group>();
114    
115                    while (!group.isRoot()) {
116                            group = group.getParentGroup();
117    
118                            groups.add(group);
119                    }
120    
121                    return groups;
122            }
123    
124            public List<Group> getChildren(boolean site) throws SystemException {
125                    return GroupLocalServiceUtil.getGroups(
126                            getCompanyId(), getGroupId(), site);
127            }
128    
129            public List<Group> getChildrenWithLayouts(boolean site, int start, int end)
130                    throws SystemException {
131    
132                    return GroupLocalServiceUtil.getLayoutsGroups(
133                            getCompanyId(), getGroupId(), site, start, end);
134            }
135    
136            public int getChildrenWithLayoutsCount(boolean site)
137                    throws SystemException {
138    
139                    return GroupLocalServiceUtil.getLayoutsGroupsCount(
140                            getCompanyId(), getGroupId(), site);
141            }
142    
143            public long getDefaultPrivatePlid() {
144                    return getDefaultPlid(true);
145            }
146    
147            public long getDefaultPublicPlid() {
148                    return getDefaultPlid(false);
149            }
150    
151            public String getDescriptiveName() throws PortalException, SystemException {
152                    return getDescriptiveName(LocaleUtil.getDefault());
153            }
154    
155            public String getDescriptiveName(Locale locale)
156                    throws PortalException, SystemException {
157    
158                    return GroupLocalServiceUtil.getGroupDescriptiveName(this, locale);
159            }
160    
161            public String getIconURL(ThemeDisplay themeDisplay) {
162                    String iconURL = themeDisplay.getPathThemeImages() + "/common/";
163    
164                    if (isCompany()) {
165                            iconURL = iconURL.concat("global.png");
166                    }
167                    else if (isLayout()) {
168                            iconURL = iconURL.concat("page.png");
169                    }
170                    else if (isOrganization()) {
171                            iconURL = iconURL.concat("organization_icon.png");
172                    }
173                    else if (isUser()) {
174                            iconURL = iconURL.concat("user_icon.png");
175                    }
176                    else {
177                            iconURL = iconURL.concat("site_icon.png");
178                    }
179    
180                    return iconURL;
181            }
182    
183            public Group getLiveGroup() {
184                    if (!isStagingGroup()) {
185                            return null;
186                    }
187    
188                    try {
189                            if (_liveGroup == null) {
190                                    _liveGroup = GroupLocalServiceUtil.getGroup(getLiveGroupId());
191                            }
192    
193                            return _liveGroup;
194                    }
195                    catch (Exception e) {
196                            _log.error("Error getting live group for " + getLiveGroupId(), e);
197    
198                            return null;
199                    }
200            }
201    
202            public String getLiveParentTypeSettingsProperty(String key) {
203                    UnicodeProperties typeSettingsProperties =
204                            getParentLiveGroupTypeSettingsProperties();
205    
206                    return typeSettingsProperties.getProperty(key);
207            }
208    
209            public long getOrganizationId() {
210                    if (isOrganization()) {
211                            if (isStagingGroup()) {
212                                    Group liveGroup = getLiveGroup();
213    
214                                    return liveGroup.getClassPK();
215                            }
216                            else {
217                                    return getClassPK();
218                            }
219                    }
220    
221                    return 0;
222            }
223    
224            public Group getParentGroup() throws PortalException, SystemException {
225                    long parentGroupId = getParentGroupId();
226    
227                    if (parentGroupId <= 0) {
228                            return null;
229                    }
230    
231                    return GroupLocalServiceUtil.getGroup(parentGroupId);
232            }
233    
234            public UnicodeProperties getParentLiveGroupTypeSettingsProperties() {
235                    try {
236                            if (isLayout()) {
237                                    Group parentGroup = GroupLocalServiceUtil.getGroup(
238                                            getParentGroupId());
239    
240                                    return parentGroup.getParentLiveGroupTypeSettingsProperties();
241                            }
242    
243                            if (isStagingGroup()) {
244                                    Group liveGroup = getLiveGroup();
245    
246                                    return liveGroup.getTypeSettingsProperties();
247                            }
248                    }
249                    catch (Exception e) {
250                    }
251    
252                    return getTypeSettingsProperties();
253            }
254    
255            public String getPathFriendlyURL(
256                    boolean privateLayout, ThemeDisplay themeDisplay) {
257    
258                    if (privateLayout) {
259                            if (isUser()) {
260                                    return themeDisplay.getPathFriendlyURLPrivateUser();
261                            }
262                            else {
263                                    return themeDisplay.getPathFriendlyURLPrivateGroup();
264                            }
265                    }
266                    else {
267                            return themeDisplay.getPathFriendlyURLPublic();
268                    }
269            }
270    
271            public LayoutSet getPrivateLayoutSet() {
272                    LayoutSet layoutSet = null;
273    
274                    try {
275                            layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
276                                    getGroupId(), true);
277                    }
278                    catch (Exception e) {
279                            _log.error(e, e);
280                    }
281    
282                    return layoutSet;
283            }
284    
285            public int getPrivateLayoutsPageCount() {
286                    try {
287                            return LayoutLocalServiceUtil.getLayoutsCount(this, true);
288                    }
289                    catch (Exception e) {
290                            _log.error(e, e);
291                    }
292    
293                    return 0;
294            }
295    
296            public LayoutSet getPublicLayoutSet() {
297                    LayoutSet layoutSet = null;
298    
299                    try {
300                            layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
301                                    getGroupId(), false);
302                    }
303                    catch (Exception e) {
304                            _log.error(e, e);
305                    }
306    
307                    return layoutSet;
308            }
309    
310            public int getPublicLayoutsPageCount() {
311                    try {
312                            return LayoutLocalServiceUtil.getLayoutsCount(this, false);
313                    }
314                    catch (Exception e) {
315                            _log.error(e, e);
316                    }
317    
318                    return 0;
319            }
320    
321            public Group getStagingGroup() {
322                    if (isStagingGroup()) {
323                            return null;
324                    }
325    
326                    try {
327                            if (_stagingGroup == null) {
328                                    _stagingGroup = GroupLocalServiceUtil.getStagingGroup(
329                                            getGroupId());
330                            }
331    
332                            return _stagingGroup;
333                    }
334                    catch (Exception e) {
335                            _log.error("Error getting staging group for " + getGroupId(), e);
336    
337                            return null;
338                    }
339            }
340    
341            public String getTypeLabel() {
342                    return GroupConstants.getTypeLabel(getType());
343            }
344    
345            @Override
346            public String getTypeSettings() {
347                    if (_typeSettingsProperties == null) {
348                            return super.getTypeSettings();
349                    }
350                    else {
351                            return _typeSettingsProperties.toString();
352                    }
353            }
354    
355            public UnicodeProperties getTypeSettingsProperties() {
356                    if (_typeSettingsProperties == null) {
357                            _typeSettingsProperties = new UnicodeProperties(true);
358    
359                            try {
360                                    _typeSettingsProperties.load(super.getTypeSettings());
361                            }
362                            catch (IOException ioe) {
363                                    _log.error(ioe, ioe);
364                            }
365                    }
366    
367                    return _typeSettingsProperties;
368            }
369    
370            public String getTypeSettingsProperty(String key) {
371                    UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
372    
373                    return typeSettingsProperties.getProperty(key);
374            }
375    
376            public boolean hasAncestor(long groupId) {
377                    Group group = null;
378    
379                    if (isStagingGroup()) {
380                            group = getLiveGroup();
381                    }
382                    else {
383                            group = this;
384                    }
385    
386                    String treePath = group.getTreePath();
387    
388                    if ((groupId != group.getGroupId()) &&
389                            treePath.contains(StringPool.SLASH + groupId + StringPool.SLASH)) {
390    
391                            return true;
392                    }
393    
394                    return false;
395            }
396    
397            public boolean hasPrivateLayouts() {
398                    if (getPrivateLayoutsPageCount() > 0) {
399                            return true;
400                    }
401                    else {
402                            return false;
403                    }
404            }
405    
406            public boolean hasPublicLayouts() {
407                    if (getPublicLayoutsPageCount() > 0) {
408                            return true;
409                    }
410                    else {
411                            return false;
412                    }
413            }
414    
415            public boolean hasStagingGroup() {
416                    if (isStagingGroup()) {
417                            return false;
418                    }
419    
420                    if (_stagingGroup != null) {
421                            return true;
422                    }
423    
424                    try {
425                            return GroupLocalServiceUtil.hasStagingGroup(getGroupId());
426                    }
427                    catch (Exception e) {
428                            return false;
429                    }
430            }
431    
432            /**
433             * @deprecated As of 6.1, renamed to {@link #isRegularSite}
434             */
435            public boolean isCommunity() {
436                    return isRegularSite();
437            }
438    
439            public boolean isCompany() {
440                    return hasClassName(Company.class);
441            }
442    
443            public boolean isControlPanel() {
444                    String name = getName();
445    
446                    if (name.equals(GroupConstants.CONTROL_PANEL)) {
447                            return true;
448                    }
449                    else {
450                            return false;
451                    }
452            }
453    
454            public boolean isGuest() {
455                    String name = getName();
456    
457                    if (name.equals(GroupConstants.GUEST)) {
458                            return true;
459                    }
460                    else {
461                            return false;
462                    }
463            }
464    
465            public boolean isInStagingPortlet(String portletId) {
466                    Group liveGroup = getLiveGroup();
467    
468                    if (liveGroup == null) {
469                            return false;
470                    }
471    
472                    return liveGroup.isStagedPortlet(portletId);
473            }
474    
475            public boolean isLayout() {
476                    return hasClassName(Layout.class);
477            }
478    
479            public boolean isLayoutPrototype() {
480                    return hasClassName(LayoutPrototype.class);
481            }
482    
483            public boolean isLayoutSetPrototype() {
484                    return hasClassName(LayoutSetPrototype.class);
485            }
486    
487            public boolean isOrganization() {
488                    return hasClassName(Organization.class);
489            }
490    
491            public boolean isRegularSite() {
492                    return hasClassName(Group.class);
493            }
494    
495            public boolean isRoot() {
496                    if (getParentGroupId() ==
497                                    GroupConstants.DEFAULT_PARENT_GROUP_ID) {
498    
499                            return true;
500                    }
501    
502                    return false;
503            }
504    
505            public boolean isShowSite(
506                            PermissionChecker permissionChecker, boolean privateSite)
507                    throws PortalException, SystemException {
508    
509                    if (!isControlPanel() && !isSite() && !isUser()) {
510                            return false;
511                    }
512    
513                    boolean showSite = true;
514    
515                    Layout defaultLayout = null;
516    
517                    int siteLayoutsCount = LayoutLocalServiceUtil.getLayoutsCount(
518                            this, true);
519    
520                    if (siteLayoutsCount == 0) {
521                            boolean hasPowerUserRole = RoleLocalServiceUtil.hasUserRole(
522                                    permissionChecker.getUserId(), permissionChecker.getCompanyId(),
523                                    RoleConstants.POWER_USER, true);
524    
525                            if (isSite()) {
526                                    if (privateSite) {
527                                            showSite =
528                                                    PropsValues.MY_SITES_SHOW_PRIVATE_SITES_WITH_NO_LAYOUTS;
529                                    }
530                                    else {
531                                            showSite =
532                                                    PropsValues.MY_SITES_SHOW_PUBLIC_SITES_WITH_NO_LAYOUTS;
533                                    }
534                            }
535                            else if (isOrganization()) {
536                                    showSite = false;
537                            }
538                            else if (isUser()) {
539                                    if (privateSite) {
540                                            showSite =
541                                                    PropsValues.
542                                                            MY_SITES_SHOW_USER_PRIVATE_SITES_WITH_NO_LAYOUTS;
543    
544                                            if (PropsValues.
545                                                            LAYOUT_USER_PRIVATE_LAYOUTS_POWER_USER_REQUIRED &&
546                                                    !hasPowerUserRole) {
547    
548                                                    showSite = false;
549                                            }
550                                    }
551                                    else {
552                                            showSite =
553                                                    PropsValues.
554                                                            MY_SITES_SHOW_USER_PUBLIC_SITES_WITH_NO_LAYOUTS;
555    
556                                            if (PropsValues.
557                                                            LAYOUT_USER_PUBLIC_LAYOUTS_POWER_USER_REQUIRED &&
558                                                    !hasPowerUserRole) {
559    
560                                                    showSite = false;
561                                            }
562                                    }
563                            }
564                    }
565                    else {
566                            defaultLayout = LayoutLocalServiceUtil.fetchFirstLayout(
567                                    getGroupId(), privateSite,
568                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
569    
570                            if ((defaultLayout != null ) &&
571                                    !LayoutPermissionUtil.contains(
572                                            permissionChecker, defaultLayout, true, ActionKeys.VIEW)) {
573    
574                                    showSite = false;
575                            }
576                            else if (isOrganization() && !isSite()) {
577                                    _log.error(
578                                            "Group " + getGroupId() +
579                                                    " is an organization site that does not have pages");
580                            }
581                    }
582    
583                    return showSite;
584            }
585    
586            public boolean isStaged() {
587                    return GetterUtil.getBoolean(
588                            getLiveParentTypeSettingsProperty("staged"));
589            }
590    
591            public boolean isStagedPortlet(String portletId) {
592                    UnicodeProperties typeSettingsProperties =
593                            getParentLiveGroupTypeSettingsProperties();
594    
595                    portletId = PortletConstants.getRootPortletId(portletId);
596    
597                    String typeSettingsProperty = typeSettingsProperties.getProperty(
598                            StagingConstants.STAGED_PORTLET.concat(portletId));
599    
600                    if (Validator.isNotNull(typeSettingsProperty)) {
601                            return GetterUtil.getBoolean(typeSettingsProperty);
602                    }
603    
604                    try {
605                            Portlet portlet = PortletLocalServiceUtil.getPortletById(portletId);
606    
607                            String portletDataHandlerClass =
608                                    portlet.getPortletDataHandlerClass();
609    
610                            if (Validator.isNull(portletDataHandlerClass)) {
611                                    return true;
612                            }
613    
614                            for (Map.Entry<String, String> entry :
615                                            typeSettingsProperties.entrySet()) {
616    
617                                    String key = entry.getKey();
618    
619                                    if (!key.contains(StagingConstants.STAGED_PORTLET)) {
620                                            continue;
621                                    }
622    
623                                    String stagedPortletId = StringUtil.replace(
624                                            key, StagingConstants.STAGED_PORTLET, StringPool.BLANK);
625    
626                                    Portlet stagedPortlet = PortletLocalServiceUtil.getPortletById(
627                                            stagedPortletId);
628    
629                                    if (portletDataHandlerClass.equals(
630                                                    stagedPortlet.getPortletDataHandlerClass())) {
631    
632                                            return GetterUtil.getBoolean(entry.getValue());
633                                    }
634                            }
635                    }
636                    catch (Exception e) {
637                    }
638    
639                    return true;
640            }
641    
642            public boolean isStagedRemotely() {
643                    return GetterUtil.getBoolean(
644                            getLiveParentTypeSettingsProperty("stagedRemotely"));
645            }
646    
647            public boolean isStagingGroup() {
648                    if (getLiveGroupId() == GroupConstants.DEFAULT_LIVE_GROUP_ID) {
649                            return false;
650                    }
651                    else {
652                            return true;
653                    }
654            }
655    
656            public boolean isUser() {
657                    return hasClassName(User.class);
658            }
659    
660            public boolean isUserGroup() {
661                    return hasClassName(UserGroup.class);
662            }
663    
664            public boolean isUserPersonalSite() {
665                    return hasClassName(UserPersonalSite.class);
666            }
667    
668            @Override
669            public void setTypeSettings(String typeSettings) {
670                    _typeSettingsProperties = null;
671    
672                    super.setTypeSettings(typeSettings);
673            }
674    
675            public void setTypeSettingsProperties(
676                    UnicodeProperties typeSettingsProperties) {
677    
678                    _typeSettingsProperties = typeSettingsProperties;
679    
680                    super.setTypeSettings(_typeSettingsProperties.toString());
681            }
682    
683            protected void buildTreePath(StringBundler sb, Group group)
684                    throws PortalException, SystemException {
685    
686                    if (group == null) {
687                            sb.append(StringPool.SLASH);
688                    }
689                    else {
690                            buildTreePath(sb, group.getParentGroup());
691    
692                            sb.append(group.getGroupId());
693                            sb.append(StringPool.SLASH);
694                    }
695            }
696    
697            protected long getDefaultPlid(boolean privateLayout) {
698                    try {
699                            Layout firstLayout = LayoutLocalServiceUtil.fetchFirstLayout(
700                                    getGroupId(), privateLayout,
701                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
702    
703                            if (firstLayout != null) {
704                                    return firstLayout.getPlid();
705                            }
706                    }
707                    catch (Exception e) {
708                            if (_log.isWarnEnabled()) {
709                                    _log.warn(e.getMessage());
710                            }
711                    }
712    
713                    return LayoutConstants.DEFAULT_PLID;
714            }
715    
716            protected boolean hasClassName(Class<?> clazz) {
717                    long classNameId = getClassNameId();
718    
719                    if (classNameId == PortalUtil.getClassNameId(clazz)) {
720                            return true;
721                    }
722                    else {
723                            return false;
724                    }
725            }
726    
727            private static Log _log = LogFactoryUtil.getLog(GroupImpl.class);
728    
729            private Group _liveGroup;
730            private Group _stagingGroup;
731            private UnicodeProperties _typeSettingsProperties;
732    
733    }