001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.User;
040    import com.liferay.portal.model.UserGroup;
041    import com.liferay.portal.model.UserPersonalSite;
042    import com.liferay.portal.service.GroupLocalServiceUtil;
043    import com.liferay.portal.service.LayoutLocalServiceUtil;
044    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
045    import com.liferay.portal.service.PortletLocalServiceUtil;
046    import com.liferay.portal.theme.ThemeDisplay;
047    import com.liferay.portal.util.PortalUtil;
048    
049    import java.io.IOException;
050    
051    import java.util.Locale;
052    import java.util.Map;
053    
054    /**
055     * Represents either a site or a generic resource container.
056     *
057     * <p>
058     * Groups are most used in Liferay as a resource container for permissioning and
059     * content scoping purposes. For instance, an site is group, meaning that it can
060     * contain layouts, web content, wiki entries, etc. However, a single layout can
061     * also be a group containing its own unique set of resources. An example of
062     * this would be a site that has several distinct wikis on different layouts.
063     * Each of these layouts would have its own group, and all of the nodes in the
064     * wiki for a certain layout would be associated with that layout's group. This
065     * allows users to be given different permissions on each of the wikis, even
066     * though they are all within the same site. In addition to sites and layouts,
067     * users and organizations are also groups.
068     * </p>
069     *
070     * <p>
071     * Groups also have a second, partially conflicting purpose in Liferay. For
072     * legacy reasons, groups are also the model used to represent sites (known as
073     * communities before Liferay v6.1). Confusion may arise from the fact that a
074     * site group is both the resource container and the site itself, whereas a
075     * layout or organization would have both a primary model and an associated
076     * group.
077     * </p>
078     *
079     * @author Brian Wing Shun Chan
080     */
081    public class GroupImpl extends GroupBaseImpl {
082    
083            public GroupImpl() {
084            }
085    
086            public long getDefaultPrivatePlid() {
087                    return getDefaultPlid(true);
088            }
089    
090            public long getDefaultPublicPlid() {
091                    return getDefaultPlid(false);
092            }
093    
094            public String getDescriptiveName() throws PortalException, SystemException {
095                    return getDescriptiveName(LocaleUtil.getDefault());
096            }
097    
098            public String getDescriptiveName(Locale locale)
099                    throws PortalException, SystemException {
100    
101                    return GroupLocalServiceUtil.getGroupDescriptiveName(this, locale);
102            }
103    
104            public Group getLiveGroup() {
105                    if (!isStagingGroup()) {
106                            return null;
107                    }
108    
109                    try {
110                            if (_liveGroup == null) {
111                                    _liveGroup = GroupLocalServiceUtil.getGroup(getLiveGroupId());
112                            }
113    
114                            return _liveGroup;
115                    }
116                    catch (Exception e) {
117                            _log.error("Error getting live group for " + getLiveGroupId(), e);
118    
119                            return null;
120                    }
121            }
122    
123            public long getOrganizationId() {
124                    if (isOrganization()) {
125                            if (isStagingGroup()) {
126                                    Group liveGroup = getLiveGroup();
127    
128                                    return liveGroup.getClassPK();
129                            }
130                            else {
131                                    return getClassPK();
132                            }
133                    }
134    
135                    return 0;
136            }
137    
138            public Group getParentGroup() throws PortalException, SystemException {
139                    long parentGroupId = getParentGroupId();
140    
141                    if (parentGroupId <= 0) {
142                            return null;
143                    }
144    
145                    return GroupLocalServiceUtil.getGroup(parentGroupId);
146            }
147    
148            public String getPathFriendlyURL(
149                    boolean privateLayout, ThemeDisplay themeDisplay) {
150    
151                    if (privateLayout) {
152                            if (isUser()) {
153                                    return themeDisplay.getPathFriendlyURLPrivateUser();
154                            }
155                            else {
156                                    return themeDisplay.getPathFriendlyURLPrivateGroup();
157                            }
158                    }
159                    else {
160                            return themeDisplay.getPathFriendlyURLPublic();
161                    }
162            }
163    
164            public LayoutSet getPrivateLayoutSet() {
165                    LayoutSet layoutSet = null;
166    
167                    try {
168                            layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
169                                    getGroupId(), true);
170                    }
171                    catch (Exception e) {
172                            _log.error(e, e);
173                    }
174    
175                    return layoutSet;
176            }
177    
178            public int getPrivateLayoutsPageCount() {
179                    try {
180                            return LayoutLocalServiceUtil.getLayoutsCount(this, true);
181                    }
182                    catch (Exception e) {
183                            _log.error(e, e);
184                    }
185    
186                    return 0;
187            }
188    
189            public LayoutSet getPublicLayoutSet() {
190                    LayoutSet layoutSet = null;
191    
192                    try {
193                            layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
194                                    getGroupId(), false);
195                    }
196                    catch (Exception e) {
197                            _log.error(e, e);
198                    }
199    
200                    return layoutSet;
201            }
202    
203            public int getPublicLayoutsPageCount() {
204                    try {
205                            return LayoutLocalServiceUtil.getLayoutsCount(this, false);
206                    }
207                    catch (Exception e) {
208                            _log.error(e, e);
209                    }
210    
211                    return 0;
212            }
213    
214            public Group getStagingGroup() {
215                    if (isStagingGroup()) {
216                            return null;
217                    }
218    
219                    try {
220                            if (_stagingGroup == null) {
221                                    _stagingGroup = GroupLocalServiceUtil.getStagingGroup(
222                                            getGroupId());
223                            }
224    
225                            return _stagingGroup;
226                    }
227                    catch (Exception e) {
228                            _log.error("Error getting staging group for " + getGroupId(), e);
229    
230                            return null;
231                    }
232            }
233    
234            public String getTypeLabel() {
235                    return GroupConstants.getTypeLabel(getType());
236            }
237    
238            @Override
239            public String getTypeSettings() {
240                    if (_typeSettingsProperties == null) {
241                            return super.getTypeSettings();
242                    }
243                    else {
244                            return _typeSettingsProperties.toString();
245                    }
246            }
247    
248            public UnicodeProperties getTypeSettingsProperties() {
249                    if (_typeSettingsProperties == null) {
250                            _typeSettingsProperties = new UnicodeProperties(true);
251    
252                            try {
253                                    _typeSettingsProperties.load(super.getTypeSettings());
254                            }
255                            catch (IOException ioe) {
256                                    _log.error(ioe, ioe);
257                            }
258                    }
259    
260                    return _typeSettingsProperties;
261            }
262    
263            public String getTypeSettingsProperty(String key) {
264                    UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
265    
266                    return typeSettingsProperties.getProperty(key);
267            }
268    
269            public boolean hasPrivateLayouts() {
270                    if (getPrivateLayoutsPageCount() > 0) {
271                            return true;
272                    }
273                    else {
274                            return false;
275                    }
276            }
277    
278            public boolean hasPublicLayouts() {
279                    if (getPublicLayoutsPageCount() > 0) {
280                            return true;
281                    }
282                    else {
283                            return false;
284                    }
285            }
286    
287            public boolean hasStagingGroup() {
288                    if (isStagingGroup()) {
289                            return false;
290                    }
291    
292                    if (_stagingGroup != null) {
293                            return true;
294                    }
295    
296                    try {
297                            return GroupLocalServiceUtil.hasStagingGroup(getGroupId());
298                    }
299                    catch (Exception e) {
300                            return false;
301                    }
302            }
303    
304            /**
305             * @deprecated As of 6.1, renamed to {@link #isRegularSite}
306             */
307            public boolean isCommunity() {
308                    return isRegularSite();
309            }
310    
311            public boolean isCompany() {
312                    return hasClassName(Company.class);
313            }
314    
315            public boolean isControlPanel() {
316                    String name = getName();
317    
318                    if (name.equals(GroupConstants.CONTROL_PANEL)) {
319                            return true;
320                    }
321                    else {
322                            return false;
323                    }
324            }
325    
326            public boolean isGuest() {
327                    String name = getName();
328    
329                    if (name.equals(GroupConstants.GUEST)) {
330                            return true;
331                    }
332                    else {
333                            return false;
334                    }
335            }
336    
337            public boolean isLayout() {
338                    return hasClassName(Layout.class);
339            }
340    
341            public boolean isLayoutPrototype() {
342                    return hasClassName(LayoutPrototype.class);
343            }
344    
345            public boolean isLayoutSetPrototype() {
346                    return hasClassName(LayoutSetPrototype.class);
347            }
348    
349            public boolean isOrganization() {
350                    return hasClassName(Organization.class);
351            }
352    
353            public boolean isRegularSite() {
354                    return hasClassName(Group.class);
355            }
356    
357            public boolean isStaged() {
358                    return GetterUtil.getBoolean(getTypeSettingsProperty("staged"));
359            }
360    
361            public boolean isStagedPortlet(String portletId) {
362                    try {
363                            if (isLayout()) {
364                                    Group parentGroup = GroupLocalServiceUtil.getGroup(
365                                            getParentGroupId());
366    
367                                    return parentGroup.isStagedPortlet(portletId);
368                            }
369                    }
370                    catch (Exception e) {
371                    }
372    
373                    portletId = PortletConstants.getRootPortletId(portletId);
374    
375                    String typeSettingsProperty = getTypeSettingsProperty(
376                            StagingConstants.STAGED_PORTLET.concat(portletId));
377    
378                    if (Validator.isNotNull(typeSettingsProperty)) {
379                            return GetterUtil.getBoolean(typeSettingsProperty);
380                    }
381    
382                    try {
383                            Portlet portlet = PortletLocalServiceUtil.getPortletById(portletId);
384    
385                            String portletDataHandlerClass =
386                                    portlet.getPortletDataHandlerClass();
387    
388                            if (Validator.isNull(portletDataHandlerClass)) {
389                                    return true;
390                            }
391    
392                            UnicodeProperties typeSettingsProperties =
393                                    getTypeSettingsProperties();
394    
395                            for (Map.Entry<String, String> entry :
396                                            typeSettingsProperties.entrySet()) {
397    
398                                    String key = entry.getKey();
399    
400                                    if (!key.contains(StagingConstants.STAGED_PORTLET)) {
401                                            continue;
402                                    }
403    
404                                    String stagedPortletId = StringUtil.replace(
405                                            key, StagingConstants.STAGED_PORTLET, StringPool.BLANK);
406    
407                                    Portlet stagedPortlet = PortletLocalServiceUtil.getPortletById(
408                                            stagedPortletId);
409    
410                                    if (portletDataHandlerClass.equals(
411                                                    stagedPortlet.getPortletDataHandlerClass())) {
412    
413                                            return GetterUtil.getBoolean(entry.getValue());
414                                    }
415                            }
416                    }
417                    catch (Exception e) {
418                    }
419    
420                    return true;
421            }
422    
423            public boolean isStagedRemotely() {
424                    return GetterUtil.getBoolean(getTypeSettingsProperty("stagedRemotely"));
425            }
426    
427            public boolean isStagingGroup() {
428                    if (getLiveGroupId() == GroupConstants.DEFAULT_LIVE_GROUP_ID) {
429                            return false;
430                    }
431                    else {
432                            return true;
433                    }
434            }
435    
436            public boolean isUser() {
437                    return hasClassName(User.class);
438            }
439    
440            public boolean isUserGroup() {
441                    return hasClassName(UserGroup.class);
442            }
443    
444            public boolean isUserPersonalSite() {
445                    return hasClassName(UserPersonalSite.class);
446            }
447    
448            @Override
449            public void setTypeSettings(String typeSettings) {
450                    _typeSettingsProperties = null;
451    
452                    super.setTypeSettings(typeSettings);
453            }
454    
455            public void setTypeSettingsProperties(
456                    UnicodeProperties typeSettingsProperties) {
457    
458                    _typeSettingsProperties = typeSettingsProperties;
459    
460                    super.setTypeSettings(_typeSettingsProperties.toString());
461            }
462    
463            protected long getDefaultPlid(boolean privateLayout) {
464                    try {
465                            Layout firstLayout = LayoutLocalServiceUtil.fetchFirstLayout(
466                                    getGroupId(), privateLayout,
467                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
468    
469                            if (firstLayout != null) {
470                                    return firstLayout.getPlid();
471                            }
472                    }
473                    catch (Exception e) {
474                            if (_log.isWarnEnabled()) {
475                                    _log.warn(e.getMessage());
476                            }
477                    }
478    
479                    return LayoutConstants.DEFAULT_PLID;
480            }
481    
482            protected boolean hasClassName(Class<?> clazz) {
483                    long classNameId = getClassNameId();
484    
485                    if (classNameId == PortalUtil.getClassNameId(clazz)) {
486                            return true;
487                    }
488                    else {
489                            return false;
490                    }
491            }
492    
493            private static Log _log = LogFactoryUtil.getLog(GroupImpl.class);
494    
495            private Group _liveGroup;
496            private Group _stagingGroup;
497            private UnicodeProperties _typeSettingsProperties;
498    
499    }