001    /**
002     * Copyright (c) 2000-present 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.exportimport.kernel.lar.PortletDataHandler;
018    import com.liferay.exportimport.kernel.staging.StagingConstants;
019    import com.liferay.exportimport.kernel.staging.StagingUtil;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.language.LanguageUtil;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.model.Account;
025    import com.liferay.portal.kernel.model.Company;
026    import com.liferay.portal.kernel.model.Group;
027    import com.liferay.portal.kernel.model.GroupConstants;
028    import com.liferay.portal.kernel.model.GroupWrapper;
029    import com.liferay.portal.kernel.model.Layout;
030    import com.liferay.portal.kernel.model.LayoutConstants;
031    import com.liferay.portal.kernel.model.LayoutPrototype;
032    import com.liferay.portal.kernel.model.LayoutSet;
033    import com.liferay.portal.kernel.model.LayoutSetPrototype;
034    import com.liferay.portal.kernel.model.Organization;
035    import com.liferay.portal.kernel.model.Portlet;
036    import com.liferay.portal.kernel.model.PortletConstants;
037    import com.liferay.portal.kernel.model.RoleConstants;
038    import com.liferay.portal.kernel.model.User;
039    import com.liferay.portal.kernel.model.UserGroup;
040    import com.liferay.portal.kernel.model.UserPersonalSite;
041    import com.liferay.portal.kernel.security.permission.ActionKeys;
042    import com.liferay.portal.kernel.security.permission.PermissionChecker;
043    import com.liferay.portal.kernel.service.CompanyLocalServiceUtil;
044    import com.liferay.portal.kernel.service.GroupLocalServiceUtil;
045    import com.liferay.portal.kernel.service.LayoutLocalServiceUtil;
046    import com.liferay.portal.kernel.service.LayoutPrototypeLocalServiceUtil;
047    import com.liferay.portal.kernel.service.LayoutSetLocalServiceUtil;
048    import com.liferay.portal.kernel.service.LayoutSetPrototypeLocalServiceUtil;
049    import com.liferay.portal.kernel.service.OrganizationLocalServiceUtil;
050    import com.liferay.portal.kernel.service.PortletLocalServiceUtil;
051    import com.liferay.portal.kernel.service.RoleLocalServiceUtil;
052    import com.liferay.portal.kernel.service.UserGroupLocalServiceUtil;
053    import com.liferay.portal.kernel.service.UserLocalServiceUtil;
054    import com.liferay.portal.kernel.service.permission.LayoutPermissionUtil;
055    import com.liferay.portal.kernel.theme.ThemeDisplay;
056    import com.liferay.portal.kernel.util.GetterUtil;
057    import com.liferay.portal.kernel.util.HtmlUtil;
058    import com.liferay.portal.kernel.util.LocaleUtil;
059    import com.liferay.portal.kernel.util.OrderByComparator;
060    import com.liferay.portal.kernel.util.PortalUtil;
061    import com.liferay.portal.kernel.util.StringBundler;
062    import com.liferay.portal.kernel.util.StringPool;
063    import com.liferay.portal.kernel.util.StringUtil;
064    import com.liferay.portal.kernel.util.UnicodeProperties;
065    import com.liferay.portal.kernel.util.Validator;
066    import com.liferay.portal.kernel.webserver.WebServerServletTokenUtil;
067    import com.liferay.portal.util.PropsValues;
068    
069    import java.io.IOException;
070    
071    import java.util.ArrayList;
072    import java.util.LinkedHashSet;
073    import java.util.List;
074    import java.util.Locale;
075    import java.util.Map;
076    import java.util.Set;
077    
078    /**
079     * Represents either a site or a generic resource container.
080     *
081     * <p>
082     * Groups are most used in Liferay as a resource container for permissioning and
083     * content scoping purposes. For instance, an site is group, meaning that it can
084     * contain layouts, web content, wiki entries, etc. However, a single layout can
085     * also be a group containing its own unique set of resources. An example of
086     * this would be a site that has several distinct wikis on different layouts.
087     * Each of these layouts would have its own group, and all of the nodes in the
088     * wiki for a certain layout would be associated with that layout's group. This
089     * allows users to be given different permissions on each of the wikis, even
090     * though they are all within the same site. In addition to sites and layouts,
091     * users and organizations are also groups.
092     * </p>
093     *
094     * <p>
095     * Groups also have a second, partially conflicting purpose in Liferay. For
096     * legacy reasons, groups are also the model used to represent sites (known as
097     * communities before Liferay v6.1). Confusion may arise from the fact that a
098     * site group is both the resource container and the site itself, whereas a
099     * layout or organization would have both a primary model and an associated
100     * group.
101     * </p>
102     *
103     * @author Brian Wing Shun Chan
104     */
105    public class GroupImpl extends GroupBaseImpl {
106    
107            @Override
108            public void clearStagingGroup() {
109                    _stagingGroup = null;
110            }
111    
112            @Override
113            public List<Group> getAncestors() throws PortalException {
114                    Group group = null;
115    
116                    if (isStagingGroup()) {
117                            group = getLiveGroup();
118                    }
119                    else {
120                            group = this;
121                    }
122    
123                    List<Group> groups = new ArrayList<>();
124    
125                    while (!group.isRoot()) {
126                            group = group.getParentGroup();
127    
128                            groups.add(group);
129                    }
130    
131                    return groups;
132            }
133    
134            @Override
135            public List<Group> getChildren(boolean site) {
136                    return GroupLocalServiceUtil.getGroups(
137                            getCompanyId(), getGroupId(), site);
138            }
139    
140            /**
141             * @deprecated As of 7.0.0, replaced by {@link
142             *             #getChildrenWithLayouts(boolean, int, int,
143             *             OrderByComparator)}
144             */
145            @Deprecated
146            @Override
147            public List<Group> getChildrenWithLayouts(
148                    boolean site, int start, int end) {
149    
150                    return getChildrenWithLayouts(site, start, end, null);
151            }
152    
153            @Override
154            public List<Group> getChildrenWithLayouts(
155                    boolean site, int start, int end, OrderByComparator<Group> obc) {
156    
157                    return GroupLocalServiceUtil.getLayoutsGroups(
158                            getCompanyId(), getGroupId(), site, start, end, obc);
159            }
160    
161            @Override
162            public int getChildrenWithLayoutsCount(boolean site) {
163                    return GroupLocalServiceUtil.getLayoutsGroupsCount(
164                            getCompanyId(), getGroupId(), site);
165            }
166    
167            @Override
168            public long getDefaultPrivatePlid() {
169                    return getDefaultPlid(true);
170            }
171    
172            @Override
173            public long getDefaultPublicPlid() {
174                    return getDefaultPlid(false);
175            }
176    
177            @Override
178            public List<Group> getDescendants(boolean site) {
179                    Set<Group> descendants = new LinkedHashSet<>();
180    
181                    for (Group group : getChildren(site)) {
182                            descendants.add(group);
183                            descendants.addAll(group.getDescendants(site));
184                    }
185    
186                    return new ArrayList<>(descendants);
187            }
188    
189            @Override
190            public String getDescriptiveName() throws PortalException {
191                    return getDescriptiveName(LocaleUtil.getMostRelevantLocale());
192            }
193    
194            @Override
195            public String getDescriptiveName(Locale locale) throws PortalException {
196                    Group curGroup = this;
197    
198                    String name = getName(locale);
199    
200                    if (Validator.isNull(name)) {
201                            Locale siteDefaultLocale = PortalUtil.getSiteDefaultLocale(
202                                    getGroupId());
203    
204                            name = getName(siteDefaultLocale);
205                    }
206    
207                    if (isCompany() && !isCompanyStagingGroup()) {
208                            name = LanguageUtil.get(locale, "global");
209                    }
210                    else if (isControlPanel()) {
211                            name = LanguageUtil.get(locale, "control-panel");
212                    }
213                    else if (isGuest()) {
214                            Company company = CompanyLocalServiceUtil.getCompany(
215                                    getCompanyId());
216    
217                            Account account = company.getAccount();
218    
219                            name = account.getName();
220                    }
221                    else if (isLayout()) {
222                            Layout layout = LayoutLocalServiceUtil.getLayout(getClassPK());
223    
224                            name = layout.getName(locale);
225                    }
226                    else if (isLayoutPrototype()) {
227                            LayoutPrototype layoutPrototype =
228                                    LayoutPrototypeLocalServiceUtil.getLayoutPrototype(
229                                            getClassPK());
230    
231                            name = layoutPrototype.getName(locale);
232                    }
233                    else if (isLayoutSetPrototype()) {
234                            LayoutSetPrototype layoutSetPrototype =
235                                    LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototype(
236                                            getClassPK());
237    
238                            name = layoutSetPrototype.getName(locale);
239                    }
240                    else if (isOrganization()) {
241                            long organizationId = getOrganizationId();
242    
243                            Organization organization =
244                                    OrganizationLocalServiceUtil.getOrganization(organizationId);
245    
246                            name = organization.getName();
247    
248                            curGroup = organization.getGroup();
249                    }
250                    else if (isUser()) {
251                            long userId = getClassPK();
252    
253                            User user = UserLocalServiceUtil.getUser(userId);
254    
255                            name = user.getFullName();
256                    }
257                    else if (isUserGroup()) {
258                            long userGroupId = getClassPK();
259    
260                            UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
261                                    userGroupId);
262    
263                            name = userGroup.getName();
264                    }
265                    else if (isUserPersonalSite()) {
266                            name = LanguageUtil.get(locale, "user-personal-site");
267                    }
268    
269                    if (curGroup.isStaged() && !curGroup.isStagedRemotely() &&
270                            curGroup.isStagingGroup()) {
271    
272                            Group liveGroup = getLiveGroup();
273    
274                            name = liveGroup.getDescriptiveName(locale);
275                    }
276    
277                    return name;
278            }
279    
280            @Override
281            public String getDisplayURL(ThemeDisplay themeDisplay) {
282                    return getDisplayURL(themeDisplay, false);
283            }
284    
285            @Override
286            public String getDisplayURL(
287                    ThemeDisplay themeDisplay, boolean privateLayout) {
288    
289                    if (!privateLayout && (getPublicLayoutsPageCount() > 0)) {
290                            try {
291                                    String groupFriendlyURL = PortalUtil.getGroupFriendlyURL(
292                                            getPublicLayoutSet(), themeDisplay);
293    
294                                    return PortalUtil.addPreservedParameters(
295                                            themeDisplay, groupFriendlyURL);
296                            }
297                            catch (PortalException pe) {
298                                    _log.error(pe, pe);
299                            }
300                    }
301                    else if (privateLayout && (getPrivateLayoutsPageCount() > 0)) {
302                            try {
303                                    String groupFriendlyURL = PortalUtil.getGroupFriendlyURL(
304                                            getPrivateLayoutSet(), themeDisplay);
305    
306                                    return PortalUtil.addPreservedParameters(
307                                            themeDisplay, groupFriendlyURL);
308                            }
309                            catch (PortalException pe) {
310                                    _log.error(pe);
311                            }
312                    }
313    
314                    return StringPool.BLANK;
315            }
316    
317            @Override
318            public String getIconCssClass() {
319                    String iconCss = "sites";
320    
321                    if (isCompany()) {
322                            iconCss = "sites";
323                    }
324                    else if (isLayout()) {
325                            iconCss = "edit-layout";
326                    }
327                    else if (isOrganization()) {
328                            iconCss = "sites";
329                    }
330                    else if (isUser()) {
331                            iconCss = "user";
332                    }
333    
334                    return iconCss;
335            }
336    
337            @Override
338            public String getIconURL(ThemeDisplay themeDisplay) {
339                    String iconURL = StringPool.BLANK;
340    
341                    if (isCompany()) {
342                            iconURL = "../aui/globe";
343                    }
344                    else if (isLayout()) {
345                            iconURL = "../aui/file";
346                    }
347                    else if (isOrganization()) {
348                            iconURL = "../aui/globe";
349                    }
350                    else if (isUser()) {
351                            iconURL = "../aui/user";
352                    }
353                    else {
354                            iconURL = "../aui/globe";
355                    }
356    
357                    return iconURL;
358            }
359    
360            @Override
361            public String getLayoutRootNodeName(boolean privateLayout, Locale locale) {
362                    String pagesName = null;
363    
364                    if (isLayoutPrototype() || isLayoutSetPrototype()) {
365                            pagesName = "pages";
366                    }
367                    else if (privateLayout) {
368                            if (isUser() || isUserGroup()) {
369                                    pagesName = "my-dashboard";
370                            }
371                            else {
372                                    pagesName = "private-pages";
373                            }
374                    }
375                    else {
376                            if (isUser() || isUserGroup()) {
377                                    pagesName = "my-profile";
378                            }
379                            else {
380                                    pagesName = "public-pages";
381                            }
382                    }
383    
384                    return LanguageUtil.get(locale, pagesName);
385            }
386    
387            @Override
388            public Group getLiveGroup() {
389                    if (!isStagingGroup()) {
390                            return null;
391                    }
392    
393                    try {
394                            if (_liveGroup == null) {
395                                    _liveGroup = GroupLocalServiceUtil.getGroup(getLiveGroupId());
396    
397                                    if (_liveGroup instanceof GroupImpl) {
398                                            GroupImpl groupImpl = (GroupImpl)_liveGroup;
399    
400                                            groupImpl._stagingGroup = this;
401                                    }
402                                    else {
403                                            _liveGroup = new GroupWrapper(_liveGroup) {
404    
405                                                    @Override
406                                                    public Group getStagingGroup() {
407                                                            return GroupImpl.this;
408                                                    }
409    
410                                            };
411                                    }
412                            }
413    
414                            return _liveGroup;
415                    }
416                    catch (Exception e) {
417                            _log.error("Error getting live group for " + getLiveGroupId(), e);
418    
419                            return null;
420                    }
421            }
422    
423            @Override
424            public String getLiveParentTypeSettingsProperty(String key) {
425                    UnicodeProperties typeSettingsProperties =
426                            getParentLiveGroupTypeSettingsProperties();
427    
428                    return typeSettingsProperties.getProperty(key);
429            }
430    
431            @Override
432            public String getLogoURL(ThemeDisplay themeDisplay, boolean useDefault) {
433                    long logoId = 0;
434    
435                    LayoutSet publicLayoutSet = getPublicLayoutSet();
436    
437                    if (publicLayoutSet.getLogoId() > 0) {
438                            logoId = publicLayoutSet.getLogoId();
439                    }
440                    else {
441                            LayoutSet privateLayoutSet = getPrivateLayoutSet();
442    
443                            if (privateLayoutSet.getLogoId() > 0) {
444                                    logoId = privateLayoutSet.getLogoId();
445                            }
446                    }
447    
448                    if ((logoId == 0) && !useDefault) {
449                            return null;
450                    }
451    
452                    StringBundler sb = new StringBundler(5);
453    
454                    sb.append(themeDisplay.getPathImage());
455                    sb.append("/layout_set_logo?img_id=");
456                    sb.append(logoId);
457                    sb.append("&t=");
458                    sb.append(WebServerServletTokenUtil.getToken(logoId));
459    
460                    return sb.toString();
461            }
462    
463            @Override
464            public long getOrganizationId() {
465                    if (isOrganization()) {
466                            if (isStagingGroup()) {
467                                    Group liveGroup = getLiveGroup();
468    
469                                    return liveGroup.getClassPK();
470                            }
471                            else {
472                                    return getClassPK();
473                            }
474                    }
475    
476                    return 0;
477            }
478    
479            @Override
480            public Group getParentGroup() throws PortalException {
481                    long parentGroupId = getParentGroupId();
482    
483                    if (parentGroupId <= 0) {
484                            return null;
485                    }
486    
487                    return GroupLocalServiceUtil.getGroup(parentGroupId);
488            }
489    
490            @Override
491            public UnicodeProperties getParentLiveGroupTypeSettingsProperties() {
492                    try {
493                            if (isLayout()) {
494                                    Group parentGroup = GroupLocalServiceUtil.getGroup(
495                                            getParentGroupId());
496    
497                                    return parentGroup.getParentLiveGroupTypeSettingsProperties();
498                            }
499    
500                            if (isStagingGroup()) {
501                                    Group liveGroup = getLiveGroup();
502    
503                                    return liveGroup.getTypeSettingsProperties();
504                            }
505                    }
506                    catch (Exception e) {
507                    }
508    
509                    return getTypeSettingsProperties();
510            }
511    
512            @Override
513            public String getPathFriendlyURL(
514                    boolean privateLayout, ThemeDisplay themeDisplay) {
515    
516                    if (privateLayout) {
517                            if (isUser()) {
518                                    return themeDisplay.getPathFriendlyURLPrivateUser();
519                            }
520                            else {
521                                    return themeDisplay.getPathFriendlyURLPrivateGroup();
522                            }
523                    }
524                    else {
525                            return themeDisplay.getPathFriendlyURLPublic();
526                    }
527            }
528    
529            @Override
530            public LayoutSet getPrivateLayoutSet() {
531                    LayoutSet layoutSet = null;
532    
533                    try {
534                            layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
535                                    getGroupId(), true);
536                    }
537                    catch (Exception e) {
538                            _log.error(e, e);
539                    }
540    
541                    return layoutSet;
542            }
543    
544            @Override
545            public int getPrivateLayoutsPageCount() {
546                    try {
547                            return LayoutLocalServiceUtil.getLayoutsCount(this, true);
548                    }
549                    catch (Exception e) {
550                            _log.error(e, e);
551                    }
552    
553                    return 0;
554            }
555    
556            @Override
557            public LayoutSet getPublicLayoutSet() {
558                    LayoutSet layoutSet = null;
559    
560                    try {
561                            layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
562                                    getGroupId(), false);
563                    }
564                    catch (Exception e) {
565                            _log.error(e, e);
566                    }
567    
568                    return layoutSet;
569            }
570    
571            @Override
572            public int getPublicLayoutsPageCount() {
573                    try {
574                            return LayoutLocalServiceUtil.getLayoutsCount(this, false);
575                    }
576                    catch (Exception e) {
577                            _log.error(e, e);
578                    }
579    
580                    return 0;
581            }
582    
583            @Override
584            public long getRemoteLiveGroupId() {
585                    if (!isStagedRemotely()) {
586                            return GroupConstants.DEFAULT_LIVE_GROUP_ID;
587                    }
588    
589                    return GetterUtil.getLong(getTypeSettingsProperty("remoteGroupId"));
590            }
591    
592            @Override
593            public String getScopeDescriptiveName(ThemeDisplay themeDisplay)
594                    throws PortalException {
595    
596                    if (getGroupId() == themeDisplay.getScopeGroupId()) {
597                            return StringUtil.appendParentheticalSuffix(
598                                    themeDisplay.translate("current-site"),
599                                    HtmlUtil.escape(getDescriptiveName(themeDisplay.getLocale())));
600                    }
601                    else if (isLayout() && (getClassPK() == themeDisplay.getPlid())) {
602                            return StringUtil.appendParentheticalSuffix(
603                                    themeDisplay.translate("current-page"),
604                                    HtmlUtil.escape(getDescriptiveName(themeDisplay.getLocale())));
605                    }
606                    else if (isLayoutPrototype()) {
607                            return themeDisplay.translate("default");
608                    }
609                    else {
610                            return HtmlUtil.escape(
611                                    getDescriptiveName(themeDisplay.getLocale()));
612                    }
613            }
614    
615            @Override
616            public String getScopeLabel(ThemeDisplay themeDisplay) {
617                    String label = "site";
618    
619                    if (getGroupId() == themeDisplay.getScopeGroupId()) {
620                            label = "current-site";
621                    }
622                    else if (getGroupId() == themeDisplay.getCompanyGroupId()) {
623                            label = "global";
624                    }
625                    else if (isLayout()) {
626                            label = "page";
627                    }
628                    else {
629                            Group scopeGroup = themeDisplay.getScopeGroup();
630    
631                            if (scopeGroup.hasAncestor(getGroupId())) {
632                                    label = "parent-site";
633                            }
634                            else if (hasAncestor(scopeGroup.getGroupId())) {
635                                    label = "child-site";
636                            }
637                    }
638    
639                    return label;
640            }
641    
642            @Override
643            public Group getStagingGroup() {
644                    if (isStagingGroup()) {
645                            return null;
646                    }
647    
648                    try {
649                            if (_stagingGroup == null) {
650                                    _stagingGroup = GroupLocalServiceUtil.getStagingGroup(
651                                            getGroupId());
652    
653                                    if (_stagingGroup instanceof GroupImpl) {
654                                            GroupImpl groupImpl = (GroupImpl)_stagingGroup;
655    
656                                            groupImpl._liveGroup = this;
657                                    }
658                                    else {
659                                            _stagingGroup = new GroupWrapper(_stagingGroup) {
660    
661                                                    @Override
662                                                    public Group getLiveGroup() {
663                                                            return GroupImpl.this;
664                                                    }
665    
666                                            };
667                                    }
668                            }
669    
670                            return _stagingGroup;
671                    }
672                    catch (Exception e) {
673                            if (_log.isDebugEnabled()) {
674                                    _log.debug(
675                                            "Unable to get staging group for group " + getGroupId(), e);
676                            }
677    
678                            return null;
679                    }
680            }
681    
682            @Override
683            public String getTypeLabel() {
684                    return GroupConstants.getTypeLabel(getType());
685            }
686    
687            @Override
688            public String getTypeSettings() {
689                    if (_typeSettingsProperties == null) {
690                            return super.getTypeSettings();
691                    }
692                    else {
693                            return _typeSettingsProperties.toString();
694                    }
695            }
696    
697            @Override
698            public UnicodeProperties getTypeSettingsProperties() {
699                    if (_typeSettingsProperties == null) {
700                            _typeSettingsProperties = new UnicodeProperties(true);
701    
702                            try {
703                                    _typeSettingsProperties.load(super.getTypeSettings());
704                            }
705                            catch (IOException ioe) {
706                                    _log.error(ioe, ioe);
707                            }
708                    }
709    
710                    return _typeSettingsProperties;
711            }
712    
713            @Override
714            public String getTypeSettingsProperty(String key) {
715                    UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
716    
717                    return typeSettingsProperties.getProperty(key);
718            }
719    
720            @Override
721            public String getUnambiguousName(String name, Locale locale) {
722                    try {
723                            return StringUtil.appendParentheticalSuffix(
724                                    name, getDescriptiveName(locale));
725                    }
726                    catch (Exception e) {
727                            return name;
728                    }
729            }
730    
731            @Override
732            public boolean hasAncestor(long groupId) {
733                    Group group = null;
734    
735                    if (isStagingGroup()) {
736                            group = getLiveGroup();
737                    }
738                    else {
739                            group = this;
740                    }
741    
742                    String treePath = group.getTreePath();
743    
744                    if ((groupId != group.getGroupId()) &&
745                            treePath.contains(StringPool.SLASH + groupId + StringPool.SLASH)) {
746    
747                            return true;
748                    }
749    
750                    return false;
751            }
752    
753            @Override
754            public boolean hasLocalOrRemoteStagingGroup() {
755                    if (hasRemoteStagingGroup() || hasStagingGroup()) {
756                            return true;
757                    }
758    
759                    return false;
760            }
761    
762            @Override
763            public boolean hasPrivateLayouts() {
764                    if (getPrivateLayoutsPageCount() > 0) {
765                            return true;
766                    }
767                    else {
768                            return false;
769                    }
770            }
771    
772            @Override
773            public boolean hasPublicLayouts() {
774                    if (getPublicLayoutsPageCount() > 0) {
775                            return true;
776                    }
777                    else {
778                            return false;
779                    }
780            }
781    
782            @Override
783            public boolean hasRemoteStagingGroup() {
784                    if (getRemoteStagingGroupCount() > 0) {
785                            return true;
786                    }
787    
788                    return false;
789            }
790    
791            @Override
792            public boolean hasStagingGroup() {
793                    if (isStagingGroup()) {
794                            return false;
795                    }
796    
797                    if (_stagingGroup != null) {
798                            return true;
799                    }
800    
801                    try {
802                            return GroupLocalServiceUtil.hasStagingGroup(getGroupId());
803                    }
804                    catch (Exception e) {
805                            return false;
806                    }
807            }
808    
809            /**
810             * @deprecated As of 7.0.0, replaced by {@link #hasAncestor}
811             */
812            @Deprecated
813            @Override
814            public boolean isChild(long groupId) {
815                    return hasAncestor(groupId);
816            }
817    
818            @Override
819            public boolean isCompany() {
820                    if ((getClassNameId() == ClassNameIds._COMPANY_CLASS_NAME_ID) ||
821                            isCompanyStagingGroup()) {
822    
823                            return true;
824                    }
825    
826                    return false;
827            }
828    
829            @Override
830            public boolean isCompanyStagingGroup() {
831                    Group liveGroup = getLiveGroup();
832    
833                    if (liveGroup == null) {
834                            return false;
835                    }
836    
837                    return liveGroup.isCompany();
838            }
839    
840            @Override
841            public boolean isControlPanel() {
842                    String groupKey = getGroupKey();
843    
844                    if (groupKey.equals(GroupConstants.CONTROL_PANEL)) {
845                            return true;
846                    }
847                    else {
848                            return false;
849                    }
850            }
851    
852            @Override
853            public boolean isGuest() {
854                    String groupKey = getGroupKey();
855    
856                    if (groupKey.equals(GroupConstants.GUEST)) {
857                            return true;
858                    }
859                    else {
860                            return false;
861                    }
862            }
863    
864            @Override
865            public boolean isInStagingPortlet(String portletId) {
866                    Group liveGroup = getLiveGroup();
867    
868                    if (liveGroup == null) {
869                            return false;
870                    }
871    
872                    return liveGroup.isStagedPortlet(portletId);
873            }
874    
875            @Override
876            public boolean isLayout() {
877                    if (getClassNameId() == ClassNameIds._LAYOUT_CLASS_NAME_ID) {
878                            return true;
879                    }
880    
881                    return false;
882            }
883    
884            @Override
885            public boolean isLayoutPrototype() {
886                    if (getClassNameId() == ClassNameIds._LAYOUT_PROTOTYPE_CLASS_NAME_ID) {
887                            return true;
888                    }
889    
890                    return false;
891            }
892    
893            @Override
894            public boolean isLayoutSetPrototype() {
895                    if (getClassNameId() ==
896                                    ClassNameIds._LAYOUT_SET_PROTOTYPE_CLASS_NAME_ID) {
897    
898                            return true;
899                    }
900    
901                    return false;
902            }
903    
904            @Override
905            public boolean isLimitedToParentSiteMembers() {
906                    if ((getParentGroupId() != GroupConstants.DEFAULT_PARENT_GROUP_ID) &&
907                            (getMembershipRestriction() ==
908                                    GroupConstants.MEMBERSHIP_RESTRICTION_TO_PARENT_SITE_MEMBERS)) {
909    
910                            return true;
911                    }
912    
913                    return false;
914            }
915    
916            @Override
917            public boolean isOrganization() {
918                    if (getClassNameId() == ClassNameIds._ORGANIZATION_CLASS_NAME_ID) {
919                            return true;
920                    }
921    
922                    return false;
923            }
924    
925            @Override
926            public boolean isRegularSite() {
927                    if (getClassNameId() == ClassNameIds._GROUP_CLASS_NAME_ID) {
928                            return true;
929                    }
930    
931                    return false;
932            }
933    
934            @Override
935            public boolean isRoot() {
936                    if (getParentGroupId() == GroupConstants.DEFAULT_PARENT_GROUP_ID) {
937                            return true;
938                    }
939    
940                    return false;
941            }
942    
943            @Override
944            public boolean isShowSite(
945                            PermissionChecker permissionChecker, boolean privateSite)
946                    throws PortalException {
947    
948                    if (!isControlPanel() && !isSite() && !isUser()) {
949                            return false;
950                    }
951    
952                    boolean showSite = true;
953    
954                    Layout defaultLayout = null;
955    
956                    int siteLayoutsCount = LayoutLocalServiceUtil.getLayoutsCount(
957                            this, privateSite);
958    
959                    if (siteLayoutsCount == 0) {
960                            boolean hasPowerUserRole = RoleLocalServiceUtil.hasUserRole(
961                                    permissionChecker.getUserId(), permissionChecker.getCompanyId(),
962                                    RoleConstants.POWER_USER, true);
963    
964                            if (isSite()) {
965                                    if (privateSite) {
966                                            showSite =
967                                                    PropsValues.MY_SITES_SHOW_PRIVATE_SITES_WITH_NO_LAYOUTS;
968                                    }
969                                    else {
970                                            showSite =
971                                                    PropsValues.MY_SITES_SHOW_PUBLIC_SITES_WITH_NO_LAYOUTS;
972                                    }
973                            }
974                            else if (isOrganization()) {
975                                    showSite = false;
976                            }
977                            else if (isUser()) {
978                                    if (privateSite) {
979                                            showSite =
980                                                    PropsValues.
981                                                            MY_SITES_SHOW_USER_PRIVATE_SITES_WITH_NO_LAYOUTS;
982    
983                                            if (PropsValues.
984                                                            LAYOUT_USER_PRIVATE_LAYOUTS_POWER_USER_REQUIRED &&
985                                                    !hasPowerUserRole) {
986    
987                                                    showSite = false;
988                                            }
989                                    }
990                                    else {
991                                            showSite =
992                                                    PropsValues.
993                                                            MY_SITES_SHOW_USER_PUBLIC_SITES_WITH_NO_LAYOUTS;
994    
995                                            if (PropsValues.
996                                                            LAYOUT_USER_PUBLIC_LAYOUTS_POWER_USER_REQUIRED &&
997                                                    !hasPowerUserRole) {
998    
999                                                    showSite = false;
1000                                            }
1001                                    }
1002                            }
1003                    }
1004                    else {
1005                            defaultLayout = LayoutLocalServiceUtil.fetchFirstLayout(
1006                                    getGroupId(), privateSite,
1007                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1008    
1009                            if ((defaultLayout != null) &&
1010                                    !LayoutPermissionUtil.contains(
1011                                            permissionChecker, defaultLayout, true, ActionKeys.VIEW)) {
1012    
1013                                    showSite = false;
1014                            }
1015                            else if (isOrganization() && !isSite()) {
1016                                    _log.error(
1017                                            "Group " + getGroupId() +
1018                                                    " is an organization site that does not have pages");
1019                            }
1020                    }
1021    
1022                    return showSite;
1023            }
1024    
1025            @Override
1026            public boolean isStaged() {
1027                    return GetterUtil.getBoolean(
1028                            getLiveParentTypeSettingsProperty("staged"));
1029            }
1030    
1031            @Override
1032            public boolean isStagedPortlet(String portletId) {
1033                    UnicodeProperties typeSettingsProperties =
1034                            getParentLiveGroupTypeSettingsProperties();
1035    
1036                    portletId = PortletConstants.getRootPortletId(portletId);
1037    
1038                    String typeSettingsProperty = typeSettingsProperties.getProperty(
1039                            StagingUtil.getStagedPortletId(portletId));
1040    
1041                    if (Validator.isNotNull(typeSettingsProperty)) {
1042                            return GetterUtil.getBoolean(typeSettingsProperty);
1043                    }
1044    
1045                    try {
1046                            Portlet portlet = PortletLocalServiceUtil.getPortletById(portletId);
1047    
1048                            PortletDataHandler portletDataHandler =
1049                                    portlet.getPortletDataHandlerInstance();
1050    
1051                            if (portletDataHandler == null) {
1052                                    return false;
1053                            }
1054    
1055                            for (Map.Entry<String, String> entry :
1056                                            typeSettingsProperties.entrySet()) {
1057    
1058                                    String key = entry.getKey();
1059    
1060                                    if (!key.contains(StagingConstants.STAGED_PORTLET)) {
1061                                            continue;
1062                                    }
1063    
1064                                    String stagedPortletId = StringUtil.replace(
1065                                            key, StagingConstants.STAGED_PORTLET, StringPool.BLANK);
1066    
1067                                    Portlet stagedPortlet = PortletLocalServiceUtil.getPortletById(
1068                                            stagedPortletId);
1069    
1070                                    if (portletDataHandler.equals(
1071                                                    stagedPortlet.getPortletDataHandlerInstance())) {
1072    
1073                                            return GetterUtil.getBoolean(entry.getValue());
1074                                    }
1075                            }
1076                    }
1077                    catch (Exception e) {
1078                    }
1079    
1080                    return true;
1081            }
1082    
1083            @Override
1084            public boolean isStagedRemotely() {
1085                    return GetterUtil.getBoolean(
1086                            getLiveParentTypeSettingsProperty("stagedRemotely"));
1087            }
1088    
1089            @Override
1090            public boolean isStagingGroup() {
1091                    if (getLiveGroupId() == GroupConstants.DEFAULT_LIVE_GROUP_ID) {
1092                            return false;
1093                    }
1094                    else {
1095                            return true;
1096                    }
1097            }
1098    
1099            @Override
1100            public boolean isUser() {
1101                    if (getClassNameId() == ClassNameIds._USER_CLASS_NAME_ID) {
1102                            return true;
1103                    }
1104    
1105                    return false;
1106            }
1107    
1108            @Override
1109            public boolean isUserGroup() {
1110                    if (getClassNameId() == ClassNameIds._USER_GROUP_CLASS_NAME_ID) {
1111                            return true;
1112                    }
1113    
1114                    return false;
1115            }
1116    
1117            @Override
1118            public boolean isUserPersonalSite() {
1119                    if (getClassNameId() ==
1120                                    ClassNameIds._USER_PERSONAL_SITE_CLASS_NAME_ID) {
1121    
1122                            return true;
1123                    }
1124    
1125                    return false;
1126            }
1127    
1128            @Override
1129            public void setTypeSettings(String typeSettings) {
1130                    _typeSettingsProperties = null;
1131    
1132                    super.setTypeSettings(typeSettings);
1133            }
1134    
1135            @Override
1136            public void setTypeSettingsProperties(
1137                    UnicodeProperties typeSettingsProperties) {
1138    
1139                    _typeSettingsProperties = typeSettingsProperties;
1140    
1141                    super.setTypeSettings(_typeSettingsProperties.toString());
1142            }
1143    
1144            protected long getDefaultPlid(boolean privateLayout) {
1145                    try {
1146                            Layout firstLayout = LayoutLocalServiceUtil.fetchFirstLayout(
1147                                    getGroupId(), privateLayout,
1148                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1149    
1150                            if (firstLayout != null) {
1151                                    return firstLayout.getPlid();
1152                            }
1153                    }
1154                    catch (Exception e) {
1155                            if (_log.isWarnEnabled()) {
1156                                    _log.warn(e.getMessage());
1157                            }
1158                    }
1159    
1160                    return LayoutConstants.DEFAULT_PLID;
1161            }
1162    
1163            private static final Log _log = LogFactoryUtil.getLog(GroupImpl.class);
1164    
1165            private Group _liveGroup;
1166            private Group _stagingGroup;
1167            private UnicodeProperties _typeSettingsProperties;
1168    
1169            private static class ClassNameIds {
1170    
1171                    private ClassNameIds() {
1172                    }
1173    
1174                    private static final long _COMPANY_CLASS_NAME_ID =
1175                            PortalUtil.getClassNameId(Company.class);
1176    
1177                    private static final long _GROUP_CLASS_NAME_ID =
1178                            PortalUtil.getClassNameId(Group.class);
1179    
1180                    private static final long _LAYOUT_CLASS_NAME_ID =
1181                            PortalUtil.getClassNameId(Layout.class);
1182    
1183                    private static final long _LAYOUT_PROTOTYPE_CLASS_NAME_ID =
1184                            PortalUtil.getClassNameId(LayoutPrototype.class);
1185    
1186                    private static final long _LAYOUT_SET_PROTOTYPE_CLASS_NAME_ID =
1187                            PortalUtil.getClassNameId(LayoutSetPrototype.class);
1188    
1189                    private static final long _ORGANIZATION_CLASS_NAME_ID =
1190                            PortalUtil.getClassNameId(Organization.class);
1191    
1192                    private static final long _USER_CLASS_NAME_ID =
1193                            PortalUtil.getClassNameId(User.class);
1194    
1195                    private static final long _USER_GROUP_CLASS_NAME_ID =
1196                            PortalUtil.getClassNameId(UserGroup.class);
1197    
1198                    private static final long _USER_PERSONAL_SITE_CLASS_NAME_ID =
1199                            PortalUtil.getClassNameId(UserPersonalSite.class);
1200    
1201            }
1202    
1203    }