001    /**
002     * Copyright (c) 2000-2011 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.Account;
029    import com.liferay.portal.model.Company;
030    import com.liferay.portal.model.Group;
031    import com.liferay.portal.model.GroupConstants;
032    import com.liferay.portal.model.Layout;
033    import com.liferay.portal.model.LayoutConstants;
034    import com.liferay.portal.model.LayoutPrototype;
035    import com.liferay.portal.model.LayoutSet;
036    import com.liferay.portal.model.LayoutSetPrototype;
037    import com.liferay.portal.model.Organization;
038    import com.liferay.portal.model.Portlet;
039    import com.liferay.portal.model.PortletConstants;
040    import com.liferay.portal.model.User;
041    import com.liferay.portal.model.UserGroup;
042    import com.liferay.portal.service.CompanyLocalServiceUtil;
043    import com.liferay.portal.service.GroupLocalServiceUtil;
044    import com.liferay.portal.service.LayoutLocalServiceUtil;
045    import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
046    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
047    import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
048    import com.liferay.portal.service.OrganizationLocalServiceUtil;
049    import com.liferay.portal.service.PortletLocalServiceUtil;
050    import com.liferay.portal.service.UserGroupLocalServiceUtil;
051    import com.liferay.portal.service.UserLocalServiceUtil;
052    import com.liferay.portal.theme.ThemeDisplay;
053    import com.liferay.portal.util.PortalUtil;
054    
055    import java.io.IOException;
056    
057    import java.util.List;
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            public long getDefaultPrivatePlid() {
093                    return getDefaultPlid(true);
094            }
095    
096            public long getDefaultPublicPlid() {
097                    return getDefaultPlid(false);
098            }
099    
100            public String getDescriptiveName() throws PortalException, SystemException {
101                    String name = getName();
102    
103                    if (isCompany()) {
104                            name = "global";
105                    }
106                    else if (isLayout()) {
107                            Layout layout = LayoutLocalServiceUtil.getLayout(getClassPK());
108    
109                            name = layout.getName(LocaleUtil.getDefault());
110                    }
111                    else if (isLayoutPrototype()) {
112                            LayoutPrototype layoutPrototype =
113                                    LayoutPrototypeLocalServiceUtil.getLayoutPrototype(
114                                            getClassPK());
115    
116                            name = layoutPrototype.getName(LocaleUtil.getDefault());
117                    }
118                    else if (isLayoutSetPrototype()) {
119                            LayoutSetPrototype layoutSetPrototype =
120                                    LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototype(
121                                            getClassPK());
122    
123                            name = layoutSetPrototype.getName(LocaleUtil.getDefault());
124                    }
125                    else if (isOrganization()) {
126                            long organizationId = getOrganizationId();
127    
128                            Organization organization =
129                                    OrganizationLocalServiceUtil.getOrganization(organizationId);
130    
131                            name = organization.getName();
132                    }
133                    else if (isUser()) {
134                            long userId = getClassPK();
135    
136                            User user = UserLocalServiceUtil.getUserById(userId);
137    
138                            name = user.getFullName();
139                    }
140                    else if (isUserGroup()) {
141                            long userGroupId = getClassPK();
142    
143                            UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
144                                    userGroupId);
145    
146                            name = userGroup.getName();
147                    }
148                    else if (name.equals(GroupConstants.GUEST)) {
149                            Company company = CompanyLocalServiceUtil.getCompany(
150                                    getCompanyId());
151    
152                            Account account = company.getAccount();
153    
154                            name = account.getName();
155                    }
156    
157                    return name;
158            }
159    
160            public Group getLiveGroup() {
161                    if (!isStagingGroup()) {
162                            return null;
163                    }
164    
165                    try {
166                            if (_liveGroup == null) {
167                                    _liveGroup = GroupLocalServiceUtil.getGroup(getLiveGroupId());
168                            }
169    
170                            return _liveGroup;
171                    }
172                    catch (Exception e) {
173                            _log.error("Error getting live group for " + getLiveGroupId(), e);
174    
175                            return null;
176                    }
177            }
178    
179            public long getOrganizationId() {
180                    if (isOrganization()) {
181                            if (isStagingGroup()) {
182                                    Group liveGroup = getLiveGroup();
183    
184                                    return liveGroup.getClassPK();
185                            }
186                            else {
187                                    return getClassPK();
188                            }
189                    }
190    
191                    return 0;
192            }
193    
194            public Group getParentGroup() throws SystemException, PortalException {
195                    long parentGroupId = getParentGroupId();
196    
197                    if (parentGroupId <= 0) {
198                            return null;
199                    }
200    
201                    return GroupLocalServiceUtil.getGroup(parentGroupId);
202            }
203    
204            public String getPathFriendlyURL(
205                    boolean privateLayout, ThemeDisplay themeDisplay) {
206    
207                    if (privateLayout) {
208                            if (isUser()) {
209                                    return themeDisplay.getPathFriendlyURLPrivateUser();
210                            }
211                            else {
212                                    return themeDisplay.getPathFriendlyURLPrivateGroup();
213                            }
214                    }
215                    else {
216                            return themeDisplay.getPathFriendlyURLPublic();
217                    }
218            }
219    
220            public LayoutSet getPrivateLayoutSet() {
221                    LayoutSet layoutSet = null;
222    
223                    try {
224                            layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
225                                    getGroupId(), true);
226                    }
227                    catch (Exception e) {
228                            _log.error(e, e);
229                    }
230    
231                    return layoutSet;
232            }
233    
234            public int getPrivateLayoutsPageCount() {
235                    try {
236                            LayoutSet layoutSet = getPrivateLayoutSet();
237    
238                            int pageCount = layoutSet.getPageCount();
239    
240                            if (isUser()) {
241                                    List<UserGroup> userGroups =
242                                            UserGroupLocalServiceUtil.getUserUserGroups(getClassPK());
243    
244                                    for (UserGroup userGroup : userGroups) {
245                                            pageCount += userGroup.getPrivateLayoutsPageCount();
246                                    }
247                            }
248    
249                            return pageCount;
250                    }
251                    catch (Exception e) {
252                            _log.error(e, e);
253                    }
254    
255                    return 0;
256            }
257    
258            public LayoutSet getPublicLayoutSet() {
259                    LayoutSet layoutSet = null;
260    
261                    try {
262                            layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
263                                    getGroupId(), false);
264                    }
265                    catch (Exception e) {
266                            _log.error(e, e);
267                    }
268    
269                    return layoutSet;
270            }
271    
272            public int getPublicLayoutsPageCount() {
273                    try {
274                            LayoutSet layoutSet = getPublicLayoutSet();
275    
276                            int pageCount = layoutSet.getPageCount();
277    
278                            if (isUser()) {
279                                    List<UserGroup> userGroups =
280                                            UserGroupLocalServiceUtil.getUserUserGroups(getClassPK());
281    
282                                    for (UserGroup userGroup : userGroups) {
283                                            pageCount += userGroup.getPublicLayoutsPageCount();
284                                    }
285                            }
286    
287                            return pageCount;
288                    }
289                    catch (Exception e) {
290                            _log.error(e, e);
291                    }
292    
293                    return 0;
294            }
295    
296            public Group getStagingGroup() {
297                    if (isStagingGroup()) {
298                            return null;
299                    }
300    
301                    try {
302                            if (_stagingGroup == null) {
303                                    _stagingGroup =
304                                            GroupLocalServiceUtil.getStagingGroup(getGroupId());
305                            }
306    
307                            return _stagingGroup;
308                    }
309                    catch (Exception e) {
310                            _log.error("Error getting staging group for " + getGroupId(), e);
311    
312                            return null;
313                    }
314            }
315    
316            public String getTypeLabel() {
317                    return GroupConstants.getTypeLabel(getType());
318            }
319    
320            @Override
321            public String getTypeSettings() {
322                    if (_typeSettingsProperties == null) {
323                            return super.getTypeSettings();
324                    }
325                    else {
326                            return _typeSettingsProperties.toString();
327                    }
328            }
329    
330            public UnicodeProperties getTypeSettingsProperties() {
331                    if (_typeSettingsProperties == null) {
332                            _typeSettingsProperties = new UnicodeProperties(true);
333    
334                            try {
335                                    _typeSettingsProperties.load(super.getTypeSettings());
336                            }
337                            catch (IOException ioe) {
338                                    _log.error(ioe, ioe);
339                            }
340                    }
341    
342                    return _typeSettingsProperties;
343            }
344    
345            public String getTypeSettingsProperty(String key) {
346                    UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
347    
348                    return typeSettingsProperties.getProperty(key);
349            }
350    
351            public boolean hasPrivateLayouts() {
352                    if (getPrivateLayoutsPageCount() > 0) {
353                            return true;
354                    }
355                    else {
356                            return false;
357                    }
358            }
359    
360            public boolean hasPublicLayouts() {
361                    if (getPublicLayoutsPageCount() > 0) {
362                            return true;
363                    }
364                    else {
365                            return false;
366                    }
367            }
368    
369            public boolean hasStagingGroup() {
370                    if (isStagingGroup()) {
371                            return false;
372                    }
373    
374                    if (_stagingGroup != null) {
375                            return true;
376                    }
377    
378                    try {
379                            return GroupLocalServiceUtil.hasStagingGroup(getGroupId());
380                    }
381                    catch (Exception e) {
382                            return false;
383                    }
384            }
385    
386            /**
387             * @deprecated As of 6.1, renamed to {@link #isRegularSite}
388             */
389            public boolean isCommunity() {
390                    return isRegularSite();
391            }
392    
393            public boolean isCompany() {
394                    return hasClassName(Company.class);
395            }
396    
397            public boolean isControlPanel() {
398                    String name = getName();
399    
400                    if (name.equals(GroupConstants.CONTROL_PANEL)) {
401                            return true;
402                    }
403                    else {
404                            return false;
405                    }
406            }
407    
408            public boolean isGuest() {
409                    String name = getName();
410    
411                    if (name.equals(GroupConstants.GUEST)) {
412                            return true;
413                    }
414                    else {
415                            return false;
416                    }
417            }
418    
419            public boolean isLayout() {
420                    return hasClassName(Layout.class);
421            }
422    
423            public boolean isLayoutPrototype() {
424                    return hasClassName(LayoutPrototype.class);
425            }
426    
427            public boolean isLayoutSetPrototype() {
428                    return hasClassName(LayoutSetPrototype.class);
429            }
430    
431            public boolean isOrganization() {
432                    return hasClassName(Organization.class);
433            }
434    
435            public boolean isRegularSite() {
436                    return hasClassName(Group.class);
437            }
438    
439            public boolean isStaged() {
440                    return GetterUtil.getBoolean(getTypeSettingsProperty("staged"));
441            }
442    
443            public boolean isStagedPortlet(String portletId) {
444                    try {
445                            if (isLayout()) {
446                                    Group parentGroup = GroupLocalServiceUtil.getGroup(
447                                            getParentGroupId());
448    
449                                    return parentGroup.isStagedPortlet(portletId);
450                            }
451                    }
452                    catch (Exception e) {
453                    }
454    
455                    portletId = PortletConstants.getRootPortletId(portletId);
456    
457                    String typeSettingsProperty = getTypeSettingsProperty(
458                            StagingConstants.STAGED_PORTLET.concat(portletId));
459    
460                    if (Validator.isNotNull(typeSettingsProperty)) {
461                            return GetterUtil.getBoolean(typeSettingsProperty);
462                    }
463    
464                    try {
465                            Portlet portlet = PortletLocalServiceUtil.getPortletById(portletId);
466    
467                            String portletDataHandlerClass =
468                                    portlet.getPortletDataHandlerClass();
469    
470                            if (Validator.isNull(portletDataHandlerClass)) {
471                                    return true;
472                            }
473    
474                            UnicodeProperties typeSettingsProperties =
475                                    getTypeSettingsProperties();
476    
477                            for (Map.Entry<String, String> entry :
478                                            typeSettingsProperties.entrySet()) {
479    
480                                    String key = entry.getKey();
481    
482                                    if (!key.contains(StagingConstants.STAGED_PORTLET)) {
483                                            continue;
484                                    }
485    
486                                    String stagedPortletId = StringUtil.replace(
487                                            key, StagingConstants.STAGED_PORTLET, StringPool.BLANK);
488    
489                                    Portlet stagedPortlet = PortletLocalServiceUtil.getPortletById(
490                                            stagedPortletId);
491    
492                                    if (portletDataHandlerClass.equals(
493                                                    stagedPortlet.getPortletDataHandlerClass())) {
494    
495                                            return GetterUtil.getBoolean(entry.getValue());
496                                    }
497                            }
498                    }
499                    catch (Exception e) {
500                    }
501    
502                    return true;
503            }
504    
505            public boolean isStagedRemotely() {
506                    return GetterUtil.getBoolean(getTypeSettingsProperty("stagedRemotely"));
507            }
508    
509            public boolean isStagingGroup() {
510                    if (getLiveGroupId() == GroupConstants.DEFAULT_LIVE_GROUP_ID) {
511                            return false;
512                    }
513                    else {
514                            return true;
515                    }
516            }
517    
518            public boolean isUser() {
519                    return hasClassName(User.class);
520            }
521    
522            public boolean isUserGroup() {
523                    return hasClassName(UserGroup.class);
524            }
525    
526            @Override
527            public void setTypeSettings(String typeSettings) {
528                    _typeSettingsProperties = null;
529    
530                    super.setTypeSettings(typeSettings);
531            }
532    
533            public void setTypeSettingsProperties(
534                    UnicodeProperties typeSettingsProperties) {
535    
536                    _typeSettingsProperties = typeSettingsProperties;
537    
538                    super.setTypeSettings(_typeSettingsProperties.toString());
539            }
540    
541            protected long getDefaultPlid(boolean privateLayout) {
542                    try {
543                            List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
544                                    getGroupId(), privateLayout,
545                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, true, 0, 1);
546    
547                            if (layouts.size() > 0) {
548                                    Layout layout = layouts.get(0);
549    
550                                    return layout.getPlid();
551                            }
552                    }
553                    catch (Exception e) {
554                            if (_log.isWarnEnabled()) {
555                                    _log.warn(e.getMessage());
556                            }
557                    }
558    
559                    return LayoutConstants.DEFAULT_PLID;
560            }
561    
562            protected boolean hasClassName(Class<?> clazz) {
563                    long classNameId = getClassNameId();
564    
565                    if (classNameId == PortalUtil.getClassNameId(clazz)) {
566                            return true;
567                    }
568                    else {
569                            return false;
570                    }
571            }
572    
573            private static Log _log = LogFactoryUtil.getLog(GroupImpl.class);
574    
575            private Group _liveGroup;
576            private Group _stagingGroup;
577            private UnicodeProperties _typeSettingsProperties;
578    
579    }