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 (true) {
100                            if (!group.isRoot()) {
101                                    group = group.getParentGroup();
102    
103                                    groups.add(group);
104                            }
105                            else {
106                                    break;
107                            }
108                    }
109    
110                    return groups;
111            }
112    
113            public List<Group> getChildren(boolean site) throws SystemException {
114                    return GroupLocalServiceUtil.getGroups(
115                            getCompanyId(), getGroupId(), site);
116            }
117    
118            public List<Group> getChildrenWithLayouts(boolean site, int start, int end)
119                    throws SystemException {
120    
121                    return GroupLocalServiceUtil.getLayoutsGroups(
122                            getCompanyId(), getGroupId(), site, start, end);
123            }
124    
125            public int getChildrenWithLayoutsCount(boolean site)
126                    throws SystemException {
127    
128                    return GroupLocalServiceUtil.getLayoutsGroupsCount(
129                            getCompanyId(), getGroupId(), site);
130            }
131    
132            public long getDefaultPrivatePlid() {
133                    return getDefaultPlid(true);
134            }
135    
136            public long getDefaultPublicPlid() {
137                    return getDefaultPlid(false);
138            }
139    
140            public String getDescriptiveName() throws PortalException, SystemException {
141                    return getDescriptiveName(LocaleUtil.getDefault());
142            }
143    
144            public String getDescriptiveName(Locale locale)
145                    throws PortalException, SystemException {
146    
147                    return GroupLocalServiceUtil.getGroupDescriptiveName(this, locale);
148            }
149    
150            public Group getLiveGroup() {
151                    if (!isStagingGroup()) {
152                            return null;
153                    }
154    
155                    try {
156                            if (_liveGroup == null) {
157                                    _liveGroup = GroupLocalServiceUtil.getGroup(getLiveGroupId());
158                            }
159    
160                            return _liveGroup;
161                    }
162                    catch (Exception e) {
163                            _log.error("Error getting live group for " + getLiveGroupId(), e);
164    
165                            return null;
166                    }
167            }
168    
169            public long getOrganizationId() {
170                    if (isOrganization()) {
171                            if (isStagingGroup()) {
172                                    Group liveGroup = getLiveGroup();
173    
174                                    return liveGroup.getClassPK();
175                            }
176                            else {
177                                    return getClassPK();
178                            }
179                    }
180    
181                    return 0;
182            }
183    
184            public Group getParentGroup() throws PortalException, SystemException {
185                    long parentGroupId = getParentGroupId();
186    
187                    if (parentGroupId <= 0) {
188                            return null;
189                    }
190    
191                    return GroupLocalServiceUtil.getGroup(parentGroupId);
192            }
193    
194            public String getPathFriendlyURL(
195                    boolean privateLayout, ThemeDisplay themeDisplay) {
196    
197                    if (privateLayout) {
198                            if (isUser()) {
199                                    return themeDisplay.getPathFriendlyURLPrivateUser();
200                            }
201                            else {
202                                    return themeDisplay.getPathFriendlyURLPrivateGroup();
203                            }
204                    }
205                    else {
206                            return themeDisplay.getPathFriendlyURLPublic();
207                    }
208            }
209    
210            public LayoutSet getPrivateLayoutSet() {
211                    LayoutSet layoutSet = null;
212    
213                    try {
214                            layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
215                                    getGroupId(), true);
216                    }
217                    catch (Exception e) {
218                            _log.error(e, e);
219                    }
220    
221                    return layoutSet;
222            }
223    
224            public int getPrivateLayoutsPageCount() {
225                    try {
226                            return LayoutLocalServiceUtil.getLayoutsCount(this, true);
227                    }
228                    catch (Exception e) {
229                            _log.error(e, e);
230                    }
231    
232                    return 0;
233            }
234    
235            public LayoutSet getPublicLayoutSet() {
236                    LayoutSet layoutSet = null;
237    
238                    try {
239                            layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
240                                    getGroupId(), false);
241                    }
242                    catch (Exception e) {
243                            _log.error(e, e);
244                    }
245    
246                    return layoutSet;
247            }
248    
249            public int getPublicLayoutsPageCount() {
250                    try {
251                            return LayoutLocalServiceUtil.getLayoutsCount(this, false);
252                    }
253                    catch (Exception e) {
254                            _log.error(e, e);
255                    }
256    
257                    return 0;
258            }
259    
260            public Group getStagingGroup() {
261                    if (isStagingGroup()) {
262                            return null;
263                    }
264    
265                    try {
266                            if (_stagingGroup == null) {
267                                    _stagingGroup = GroupLocalServiceUtil.getStagingGroup(
268                                            getGroupId());
269                            }
270    
271                            return _stagingGroup;
272                    }
273                    catch (Exception e) {
274                            _log.error("Error getting staging group for " + getGroupId(), e);
275    
276                            return null;
277                    }
278            }
279    
280            public String getTypeLabel() {
281                    return GroupConstants.getTypeLabel(getType());
282            }
283    
284            @Override
285            public String getTypeSettings() {
286                    if (_typeSettingsProperties == null) {
287                            return super.getTypeSettings();
288                    }
289                    else {
290                            return _typeSettingsProperties.toString();
291                    }
292            }
293    
294            public UnicodeProperties getTypeSettingsProperties() {
295                    if (_typeSettingsProperties == null) {
296                            _typeSettingsProperties = new UnicodeProperties(true);
297    
298                            try {
299                                    _typeSettingsProperties.load(super.getTypeSettings());
300                            }
301                            catch (IOException ioe) {
302                                    _log.error(ioe, ioe);
303                            }
304                    }
305    
306                    return _typeSettingsProperties;
307            }
308    
309            public String getTypeSettingsProperty(String key) {
310                    UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
311    
312                    return typeSettingsProperties.getProperty(key);
313            }
314    
315            public boolean hasPrivateLayouts() {
316                    if (getPrivateLayoutsPageCount() > 0) {
317                            return true;
318                    }
319                    else {
320                            return false;
321                    }
322            }
323    
324            public boolean hasPublicLayouts() {
325                    if (getPublicLayoutsPageCount() > 0) {
326                            return true;
327                    }
328                    else {
329                            return false;
330                    }
331            }
332    
333            public boolean hasStagingGroup() {
334                    if (isStagingGroup()) {
335                            return false;
336                    }
337    
338                    if (_stagingGroup != null) {
339                            return true;
340                    }
341    
342                    try {
343                            return GroupLocalServiceUtil.hasStagingGroup(getGroupId());
344                    }
345                    catch (Exception e) {
346                            return false;
347                    }
348            }
349    
350            /**
351             * @deprecated As of 6.1, renamed to {@link #isRegularSite}
352             */
353            public boolean isCommunity() {
354                    return isRegularSite();
355            }
356    
357            public boolean isCompany() {
358                    return hasClassName(Company.class);
359            }
360    
361            public boolean isControlPanel() {
362                    String name = getName();
363    
364                    if (name.equals(GroupConstants.CONTROL_PANEL)) {
365                            return true;
366                    }
367                    else {
368                            return false;
369                    }
370            }
371    
372            public boolean isGuest() {
373                    String name = getName();
374    
375                    if (name.equals(GroupConstants.GUEST)) {
376                            return true;
377                    }
378                    else {
379                            return false;
380                    }
381            }
382    
383            public boolean isLayout() {
384                    return hasClassName(Layout.class);
385            }
386    
387            public boolean isLayoutPrototype() {
388                    return hasClassName(LayoutPrototype.class);
389            }
390    
391            public boolean isLayoutSetPrototype() {
392                    return hasClassName(LayoutSetPrototype.class);
393            }
394    
395            public boolean isOrganization() {
396                    return hasClassName(Organization.class);
397            }
398    
399            public boolean isRegularSite() {
400                    return hasClassName(Group.class);
401            }
402    
403            public boolean isRoot() {
404                    if (getParentGroupId() ==
405                                    GroupConstants.DEFAULT_PARENT_GROUP_ID) {
406    
407                            return true;
408                    }
409                    else {
410                            return false;
411                    }
412            }
413    
414            public boolean isShowSite(
415                            PermissionChecker permissionChecker, boolean privateSite)
416                    throws PortalException, SystemException {
417    
418                    if (!isControlPanel() && !isSite() && !isUser()) {
419                            return false;
420                    }
421    
422                    boolean showSite = true;
423    
424                    Layout defaultLayout = null;
425    
426                    int siteLayoutsCount = LayoutLocalServiceUtil.getLayoutsCount(
427                            this, true);
428    
429                    if (siteLayoutsCount == 0) {
430                            boolean hasPowerUserRole = RoleLocalServiceUtil.hasUserRole(
431                                    permissionChecker.getUserId(), permissionChecker.getCompanyId(),
432                                    RoleConstants.POWER_USER, true);
433    
434                            if (isSite()) {
435                                    if (privateSite) {
436                                            showSite =
437                                                    PropsValues.MY_SITES_SHOW_PRIVATE_SITES_WITH_NO_LAYOUTS;
438                                    }
439                                    else {
440                                            showSite =
441                                                    PropsValues.MY_SITES_SHOW_PUBLIC_SITES_WITH_NO_LAYOUTS;
442                                    }
443                            }
444                            else if (isOrganization()) {
445                                    showSite = false;
446                            }
447                            else if (isUser()) {
448                                    if (privateSite) {
449                                            showSite =
450                                                    PropsValues.
451                                                            MY_SITES_SHOW_USER_PRIVATE_SITES_WITH_NO_LAYOUTS;
452    
453                                            if (PropsValues.
454                                                            LAYOUT_USER_PRIVATE_LAYOUTS_POWER_USER_REQUIRED &&
455                                                    !hasPowerUserRole) {
456    
457                                                    showSite = false;
458                                            }
459                                    }
460                                    else {
461                                            showSite =
462                                                    PropsValues.
463                                                            MY_SITES_SHOW_USER_PUBLIC_SITES_WITH_NO_LAYOUTS;
464    
465                                            if (PropsValues.
466                                                            LAYOUT_USER_PUBLIC_LAYOUTS_POWER_USER_REQUIRED &&
467                                                    !hasPowerUserRole) {
468    
469                                                    showSite = false;
470                                            }
471                                    }
472                            }
473                    }
474                    else {
475                            defaultLayout = LayoutLocalServiceUtil.fetchFirstLayout(
476                                    getGroupId(), privateSite,
477                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
478    
479                            if ((defaultLayout != null ) &&
480                                    !LayoutPermissionUtil.contains(
481                                            permissionChecker, defaultLayout, true, ActionKeys.VIEW)) {
482    
483                                    showSite = false;
484                            }
485                            else if (isOrganization() && !isSite()) {
486                                    _log.error(
487                                            "Group " + getGroupId() +
488                                                    " is an organization site that does not have pages");
489                            }
490                    }
491    
492                    return showSite;
493            }
494    
495            public boolean isStaged() {
496                    return GetterUtil.getBoolean(getTypeSettingsProperty("staged"));
497            }
498    
499            public boolean isStagedPortlet(String portletId) {
500                    try {
501                            if (isLayout()) {
502                                    Group parentGroup = GroupLocalServiceUtil.getGroup(
503                                            getParentGroupId());
504    
505                                    return parentGroup.isStagedPortlet(portletId);
506                            }
507                    }
508                    catch (Exception e) {
509                    }
510    
511                    portletId = PortletConstants.getRootPortletId(portletId);
512    
513                    String typeSettingsProperty = getTypeSettingsProperty(
514                            StagingConstants.STAGED_PORTLET.concat(portletId));
515    
516                    if (Validator.isNotNull(typeSettingsProperty)) {
517                            return GetterUtil.getBoolean(typeSettingsProperty);
518                    }
519    
520                    try {
521                            Portlet portlet = PortletLocalServiceUtil.getPortletById(portletId);
522    
523                            String portletDataHandlerClass =
524                                    portlet.getPortletDataHandlerClass();
525    
526                            if (Validator.isNull(portletDataHandlerClass)) {
527                                    return true;
528                            }
529    
530                            UnicodeProperties typeSettingsProperties =
531                                    getTypeSettingsProperties();
532    
533                            for (Map.Entry<String, String> entry :
534                                            typeSettingsProperties.entrySet()) {
535    
536                                    String key = entry.getKey();
537    
538                                    if (!key.contains(StagingConstants.STAGED_PORTLET)) {
539                                            continue;
540                                    }
541    
542                                    String stagedPortletId = StringUtil.replace(
543                                            key, StagingConstants.STAGED_PORTLET, StringPool.BLANK);
544    
545                                    Portlet stagedPortlet = PortletLocalServiceUtil.getPortletById(
546                                            stagedPortletId);
547    
548                                    if (portletDataHandlerClass.equals(
549                                                    stagedPortlet.getPortletDataHandlerClass())) {
550    
551                                            return GetterUtil.getBoolean(entry.getValue());
552                                    }
553                            }
554                    }
555                    catch (Exception e) {
556                    }
557    
558                    return true;
559            }
560    
561            public boolean isStagedRemotely() {
562                    return GetterUtil.getBoolean(getTypeSettingsProperty("stagedRemotely"));
563            }
564    
565            public boolean isStagingGroup() {
566                    if (getLiveGroupId() == GroupConstants.DEFAULT_LIVE_GROUP_ID) {
567                            return false;
568                    }
569                    else {
570                            return true;
571                    }
572            }
573    
574            public boolean isUser() {
575                    return hasClassName(User.class);
576            }
577    
578            public boolean isUserGroup() {
579                    return hasClassName(UserGroup.class);
580            }
581    
582            public boolean isUserPersonalSite() {
583                    return hasClassName(UserPersonalSite.class);
584            }
585    
586            @Override
587            public void setTypeSettings(String typeSettings) {
588                    _typeSettingsProperties = null;
589    
590                    super.setTypeSettings(typeSettings);
591            }
592    
593            public void setTypeSettingsProperties(
594                    UnicodeProperties typeSettingsProperties) {
595    
596                    _typeSettingsProperties = typeSettingsProperties;
597    
598                    super.setTypeSettings(_typeSettingsProperties.toString());
599            }
600    
601            protected long getDefaultPlid(boolean privateLayout) {
602                    try {
603                            Layout firstLayout = LayoutLocalServiceUtil.fetchFirstLayout(
604                                    getGroupId(), privateLayout,
605                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
606    
607                            if (firstLayout != null) {
608                                    return firstLayout.getPlid();
609                            }
610                    }
611                    catch (Exception e) {
612                            if (_log.isWarnEnabled()) {
613                                    _log.warn(e.getMessage());
614                            }
615                    }
616    
617                    return LayoutConstants.DEFAULT_PLID;
618            }
619    
620            protected boolean hasClassName(Class<?> clazz) {
621                    long classNameId = getClassNameId();
622    
623                    if (classNameId == PortalUtil.getClassNameId(clazz)) {
624                            return true;
625                    }
626                    else {
627                            return false;
628                    }
629            }
630    
631            private static Log _log = LogFactoryUtil.getLog(GroupImpl.class);
632    
633            private Group _liveGroup;
634            private Group _stagingGroup;
635            private UnicodeProperties _typeSettingsProperties;
636    
637    }