001    /**
002     * Copyright (c) 2000-2011 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.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(
168                                            getLiveGroupId());
169                            }
170    
171                            return _liveGroup;
172                    }
173                    catch (Exception e) {
174                            _log.error("Error getting live group for " + getLiveGroupId(), e);
175    
176                            return null;
177                    }
178            }
179    
180            public long getOrganizationId() {
181                    if (isOrganization()) {
182                            if (isStagingGroup()) {
183                                    Group liveGroup = getLiveGroup();
184    
185                                    return liveGroup.getClassPK();
186                            }
187                            else {
188                                    return getClassPK();
189                            }
190                    }
191    
192                    return 0;
193            }
194    
195            public String getPathFriendlyURL(
196                    boolean privateLayout, ThemeDisplay themeDisplay) {
197    
198                    if (privateLayout) {
199                            if (isUser()) {
200                                    return themeDisplay.getPathFriendlyURLPrivateUser();
201                            }
202                            else {
203                                    return themeDisplay.getPathFriendlyURLPrivateGroup();
204                            }
205                    }
206                    else {
207                            return themeDisplay.getPathFriendlyURLPublic();
208                    }
209            }
210    
211            public LayoutSet getPrivateLayoutSet() {
212                    LayoutSet layoutSet = null;
213    
214                    try {
215                            layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
216                                    getGroupId(), true);
217                    }
218                    catch (Exception e) {
219                            _log.error(e);
220                    }
221    
222                    return layoutSet;
223            }
224    
225            public int getPrivateLayoutsPageCount() {
226                    try {
227                            LayoutSet layoutSet = getPrivateLayoutSet();
228    
229                            return layoutSet.getPageCount();
230                    }
231                    catch (Exception e) {
232                            _log.error(e);
233                    }
234    
235                    return 0;
236            }
237    
238            public LayoutSet getPublicLayoutSet() {
239                    LayoutSet layoutSet = null;
240    
241                    try {
242                            layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
243                                    getGroupId(), false);
244                    }
245                    catch (Exception e) {
246                            _log.error(e);
247                    }
248    
249                    return layoutSet;
250            }
251    
252            public int getPublicLayoutsPageCount() {
253                    try {
254                            LayoutSet layoutSet = getPublicLayoutSet();
255    
256                            return layoutSet.getPageCount();
257                    }
258                    catch (Exception e) {
259                            _log.error(e);
260                    }
261    
262                    return 0;
263            }
264    
265            public Group getStagingGroup() {
266                    if (isStagingGroup()) {
267                            return null;
268                    }
269    
270                    try {
271                            if (_stagingGroup == null) {
272                                    _stagingGroup =
273                                            GroupLocalServiceUtil.getStagingGroup(getGroupId());
274                            }
275    
276                            return _stagingGroup;
277                    }
278                    catch (Exception e) {
279                            _log.error("Error getting staging group for " + getGroupId(), e);
280    
281                            return null;
282                    }
283            }
284    
285            public String getTypeLabel() {
286                    return GroupConstants.getTypeLabel(getType());
287            }
288    
289            @Override
290            public String getTypeSettings() {
291                    if (_typeSettingsProperties == null) {
292                            return super.getTypeSettings();
293                    }
294                    else {
295                            return _typeSettingsProperties.toString();
296                    }
297            }
298    
299            public UnicodeProperties getTypeSettingsProperties() {
300                    if (_typeSettingsProperties == null) {
301                            _typeSettingsProperties = new UnicodeProperties(true);
302    
303                            try {
304                                    _typeSettingsProperties.load(super.getTypeSettings());
305                            }
306                            catch (IOException ioe) {
307                                    _log.error(ioe, ioe);
308                            }
309                    }
310    
311                    return _typeSettingsProperties;
312            }
313    
314            public String getTypeSettingsProperty(String key) {
315                    UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
316    
317                    return typeSettingsProperties.getProperty(key);
318            }
319    
320            public boolean hasPrivateLayouts() {
321                    if (getPrivateLayoutsPageCount() > 0) {
322                            return true;
323                    }
324                    else {
325                            return false;
326                    }
327            }
328    
329            public boolean hasPublicLayouts() {
330                    if (getPublicLayoutsPageCount() > 0) {
331                            return true;
332                    }
333                    else {
334                            return false;
335                    }
336            }
337    
338            public boolean hasStagingGroup() {
339                    if (isStagingGroup()) {
340                            return false;
341                    }
342    
343                    if (_stagingGroup != null) {
344                            return true;
345                    }
346    
347                    try {
348                            return GroupLocalServiceUtil.hasStagingGroup(getGroupId());
349                    }
350                    catch (Exception e) {
351                            return false;
352                    }
353            }
354    
355            /**
356             * @deprecated As of 6.1, renamed to {@link #isRegularSite}
357             */
358            public boolean isCommunity() {
359                    return isRegularSite();
360            }
361    
362            public boolean isCompany() {
363                    return hasClassName(Company.class);
364            }
365    
366            public boolean isControlPanel() {
367                    String name = getName();
368    
369                    if (name.equals(GroupConstants.CONTROL_PANEL)) {
370                            return true;
371                    }
372                    else {
373                            return false;
374                    }
375            }
376    
377            public boolean isGuest() {
378                    String name = getName();
379    
380                    if (name.equals(GroupConstants.GUEST)) {
381                            return true;
382                    }
383                    else {
384                            return false;
385                    }
386            }
387    
388            public boolean isLayout() {
389                    return hasClassName(Layout.class);
390            }
391    
392            public boolean isLayoutPrototype() {
393                    return hasClassName(LayoutPrototype.class);
394            }
395    
396            public boolean isLayoutSetPrototype() {
397                    return hasClassName(LayoutSetPrototype.class);
398            }
399    
400            public boolean isOrganization() {
401                    return hasClassName(Organization.class);
402            }
403    
404            public boolean isRegularSite() {
405                    return hasClassName(Group.class);
406            }
407    
408            public boolean isStaged() {
409                    return GetterUtil.getBoolean(getTypeSettingsProperty("staged"));
410            }
411    
412            public boolean isStagedPortlet(String portletId) {
413                    portletId = PortletConstants.getRootPortletId(portletId);
414    
415                    String typeSettingsProperty = getTypeSettingsProperty(
416                            StagingConstants.STAGED_PORTLET.concat(portletId));
417    
418                    if (Validator.isNotNull(typeSettingsProperty)) {
419                            return GetterUtil.getBoolean(typeSettingsProperty);
420                    }
421    
422                    try {
423                            Portlet portlet = PortletLocalServiceUtil.getPortletById(portletId);
424    
425                            String portletDataHandlerClass =
426                                    portlet.getPortletDataHandlerClass();
427    
428                            if (Validator.isNull(portletDataHandlerClass)) {
429                                    return true;
430                            }
431    
432                            UnicodeProperties typeSettingsProperties =
433                                    getTypeSettingsProperties();
434    
435                            for (Map.Entry<String, String> entry :
436                                            typeSettingsProperties.entrySet()) {
437    
438                                    String key = entry.getKey();
439    
440                                    if (!key.contains(StagingConstants.STAGED_PORTLET)) {
441                                            continue;
442                                    }
443    
444                                    String stagedPortletId = StringUtil.replace(
445                                            key, StagingConstants.STAGED_PORTLET, StringPool.BLANK);
446    
447                                    Portlet stagedPortlet = PortletLocalServiceUtil.getPortletById(
448                                            stagedPortletId);
449    
450                                    if (portletDataHandlerClass.equals(
451                                                    stagedPortlet.getPortletDataHandlerClass())) {
452    
453                                            return GetterUtil.getBoolean(entry.getValue());
454                                    }
455                            }
456                    }
457                    catch (Exception e) {
458                    }
459    
460                    return true;
461            }
462    
463            public boolean isStagedRemotely() {
464                    return GetterUtil.getBoolean(getTypeSettingsProperty("stagedRemotely"));
465            }
466    
467            public boolean isStagingGroup() {
468                    if (getLiveGroupId() == GroupConstants.DEFAULT_LIVE_GROUP_ID) {
469                            return false;
470                    }
471                    else {
472                            return true;
473                    }
474            }
475    
476            public boolean isUser() {
477                    return hasClassName(User.class);
478            }
479    
480            public boolean isUserGroup() {
481                    return hasClassName(UserGroup.class);
482            }
483    
484            @Override
485            public void setTypeSettings(String typeSettings) {
486                    _typeSettingsProperties = null;
487    
488                    super.setTypeSettings(typeSettings);
489            }
490    
491            public void setTypeSettingsProperties(
492                    UnicodeProperties typeSettingsProperties) {
493    
494                    _typeSettingsProperties = typeSettingsProperties;
495    
496                    super.setTypeSettings(_typeSettingsProperties.toString());
497            }
498    
499            protected long getDefaultPlid(boolean privateLayout) {
500                    try {
501                            List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
502                                    getGroupId(), privateLayout,
503                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, true, 0, 1);
504    
505                            if (layouts.size() > 0) {
506                                    Layout layout = layouts.get(0);
507    
508                                    return layout.getPlid();
509                            }
510                    }
511                    catch (Exception e) {
512                            if (_log.isWarnEnabled()) {
513                                    _log.warn(e.getMessage());
514                            }
515                    }
516    
517                    return LayoutConstants.DEFAULT_PLID;
518            }
519    
520            protected boolean hasClassName(Class<?> clazz) {
521                    long classNameId = getClassNameId();
522    
523                    if (classNameId == PortalUtil.getClassNameId(clazz)) {
524                            return true;
525                    }
526                    else {
527                            return false;
528                    }
529            }
530    
531            private static Log _log = LogFactoryUtil.getLog(GroupImpl.class);
532    
533            private Group _liveGroup;
534            private Group _stagingGroup;
535            private UnicodeProperties _typeSettingsProperties;
536    
537    }