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