001    /**
002     * Copyright (c) 2000-2013 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 isLimitedToParentSiteMembers() {
488                    if ((getParentGroupId() != GroupConstants.DEFAULT_PARENT_GROUP_ID) &&
489                            (getType() ==
490                                    GroupConstants.TYPE_SITE_LIMITED_TO_PARENT_SITE_MEMBERS)) {
491    
492                            return true;
493                    }
494    
495                    return false;
496            }
497    
498            public boolean isOrganization() {
499                    return hasClassName(Organization.class);
500            }
501    
502            public boolean isRegularSite() {
503                    return hasClassName(Group.class);
504            }
505    
506            public boolean isRoot() {
507                    if (getParentGroupId() ==
508                                    GroupConstants.DEFAULT_PARENT_GROUP_ID) {
509    
510                            return true;
511                    }
512    
513                    return false;
514            }
515    
516            public boolean isShowSite(
517                            PermissionChecker permissionChecker, boolean privateSite)
518                    throws PortalException, SystemException {
519    
520                    if (!isControlPanel() && !isSite() && !isUser()) {
521                            return false;
522                    }
523    
524                    boolean showSite = true;
525    
526                    Layout defaultLayout = null;
527    
528                    int siteLayoutsCount = LayoutLocalServiceUtil.getLayoutsCount(
529                            this, true);
530    
531                    if (siteLayoutsCount == 0) {
532                            boolean hasPowerUserRole = RoleLocalServiceUtil.hasUserRole(
533                                    permissionChecker.getUserId(), permissionChecker.getCompanyId(),
534                                    RoleConstants.POWER_USER, true);
535    
536                            if (isSite()) {
537                                    if (privateSite) {
538                                            showSite =
539                                                    PropsValues.MY_SITES_SHOW_PRIVATE_SITES_WITH_NO_LAYOUTS;
540                                    }
541                                    else {
542                                            showSite =
543                                                    PropsValues.MY_SITES_SHOW_PUBLIC_SITES_WITH_NO_LAYOUTS;
544                                    }
545                            }
546                            else if (isOrganization()) {
547                                    showSite = false;
548                            }
549                            else if (isUser()) {
550                                    if (privateSite) {
551                                            showSite =
552                                                    PropsValues.
553                                                            MY_SITES_SHOW_USER_PRIVATE_SITES_WITH_NO_LAYOUTS;
554    
555                                            if (PropsValues.
556                                                            LAYOUT_USER_PRIVATE_LAYOUTS_POWER_USER_REQUIRED &&
557                                                    !hasPowerUserRole) {
558    
559                                                    showSite = false;
560                                            }
561                                    }
562                                    else {
563                                            showSite =
564                                                    PropsValues.
565                                                            MY_SITES_SHOW_USER_PUBLIC_SITES_WITH_NO_LAYOUTS;
566    
567                                            if (PropsValues.
568                                                            LAYOUT_USER_PUBLIC_LAYOUTS_POWER_USER_REQUIRED &&
569                                                    !hasPowerUserRole) {
570    
571                                                    showSite = false;
572                                            }
573                                    }
574                            }
575                    }
576                    else {
577                            defaultLayout = LayoutLocalServiceUtil.fetchFirstLayout(
578                                    getGroupId(), privateSite,
579                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
580    
581                            if ((defaultLayout != null ) &&
582                                    !LayoutPermissionUtil.contains(
583                                            permissionChecker, defaultLayout, true, ActionKeys.VIEW)) {
584    
585                                    showSite = false;
586                            }
587                            else if (isOrganization() && !isSite()) {
588                                    _log.error(
589                                            "Group " + getGroupId() +
590                                                    " is an organization site that does not have pages");
591                            }
592                    }
593    
594                    return showSite;
595            }
596    
597            public boolean isStaged() {
598                    return GetterUtil.getBoolean(
599                            getLiveParentTypeSettingsProperty("staged"));
600            }
601    
602            public boolean isStagedPortlet(String portletId) {
603                    UnicodeProperties typeSettingsProperties =
604                            getParentLiveGroupTypeSettingsProperties();
605    
606                    portletId = PortletConstants.getRootPortletId(portletId);
607    
608                    String typeSettingsProperty = typeSettingsProperties.getProperty(
609                            StagingConstants.STAGED_PORTLET.concat(portletId));
610    
611                    if (Validator.isNotNull(typeSettingsProperty)) {
612                            return GetterUtil.getBoolean(typeSettingsProperty);
613                    }
614    
615                    try {
616                            Portlet portlet = PortletLocalServiceUtil.getPortletById(portletId);
617    
618                            String portletDataHandlerClass =
619                                    portlet.getPortletDataHandlerClass();
620    
621                            if (Validator.isNull(portletDataHandlerClass)) {
622                                    return true;
623                            }
624    
625                            for (Map.Entry<String, String> entry :
626                                            typeSettingsProperties.entrySet()) {
627    
628                                    String key = entry.getKey();
629    
630                                    if (!key.contains(StagingConstants.STAGED_PORTLET)) {
631                                            continue;
632                                    }
633    
634                                    String stagedPortletId = StringUtil.replace(
635                                            key, StagingConstants.STAGED_PORTLET, StringPool.BLANK);
636    
637                                    Portlet stagedPortlet = PortletLocalServiceUtil.getPortletById(
638                                            stagedPortletId);
639    
640                                    if (portletDataHandlerClass.equals(
641                                                    stagedPortlet.getPortletDataHandlerClass())) {
642    
643                                            return GetterUtil.getBoolean(entry.getValue());
644                                    }
645                            }
646                    }
647                    catch (Exception e) {
648                    }
649    
650                    return true;
651            }
652    
653            public boolean isStagedRemotely() {
654                    return GetterUtil.getBoolean(
655                            getLiveParentTypeSettingsProperty("stagedRemotely"));
656            }
657    
658            public boolean isStagingGroup() {
659                    if (getLiveGroupId() == GroupConstants.DEFAULT_LIVE_GROUP_ID) {
660                            return false;
661                    }
662                    else {
663                            return true;
664                    }
665            }
666    
667            public boolean isUser() {
668                    return hasClassName(User.class);
669            }
670    
671            public boolean isUserGroup() {
672                    return hasClassName(UserGroup.class);
673            }
674    
675            public boolean isUserPersonalSite() {
676                    return hasClassName(UserPersonalSite.class);
677            }
678    
679            @Override
680            public void setTypeSettings(String typeSettings) {
681                    _typeSettingsProperties = null;
682    
683                    super.setTypeSettings(typeSettings);
684            }
685    
686            public void setTypeSettingsProperties(
687                    UnicodeProperties typeSettingsProperties) {
688    
689                    _typeSettingsProperties = typeSettingsProperties;
690    
691                    super.setTypeSettings(_typeSettingsProperties.toString());
692            }
693    
694            protected void buildTreePath(StringBundler sb, Group group)
695                    throws PortalException, SystemException {
696    
697                    if (group == null) {
698                            sb.append(StringPool.SLASH);
699                    }
700                    else {
701                            buildTreePath(sb, group.getParentGroup());
702    
703                            sb.append(group.getGroupId());
704                            sb.append(StringPool.SLASH);
705                    }
706            }
707    
708            protected long getDefaultPlid(boolean privateLayout) {
709                    try {
710                            Layout firstLayout = LayoutLocalServiceUtil.fetchFirstLayout(
711                                    getGroupId(), privateLayout,
712                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
713    
714                            if (firstLayout != null) {
715                                    return firstLayout.getPlid();
716                            }
717                    }
718                    catch (Exception e) {
719                            if (_log.isWarnEnabled()) {
720                                    _log.warn(e.getMessage());
721                            }
722                    }
723    
724                    return LayoutConstants.DEFAULT_PLID;
725            }
726    
727            protected boolean hasClassName(Class<?> clazz) {
728                    long classNameId = getClassNameId();
729    
730                    if (classNameId == PortalUtil.getClassNameId(clazz)) {
731                            return true;
732                    }
733                    else {
734                            return false;
735                    }
736            }
737    
738            private static Log _log = LogFactoryUtil.getLog(GroupImpl.class);
739    
740            private Group _liveGroup;
741            private Group _stagingGroup;
742            private UnicodeProperties _typeSettingsProperties;
743    
744    }