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