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