001    /**
002     * Copyright (c) 2000-2010 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.UnicodeProperties;
025    import com.liferay.portal.model.Account;
026    import com.liferay.portal.model.Company;
027    import com.liferay.portal.model.Group;
028    import com.liferay.portal.model.GroupConstants;
029    import com.liferay.portal.model.Layout;
030    import com.liferay.portal.model.LayoutConstants;
031    import com.liferay.portal.model.LayoutPrototype;
032    import com.liferay.portal.model.LayoutSet;
033    import com.liferay.portal.model.LayoutSetPrototype;
034    import com.liferay.portal.model.Organization;
035    import com.liferay.portal.model.User;
036    import com.liferay.portal.model.UserGroup;
037    import com.liferay.portal.service.CompanyLocalServiceUtil;
038    import com.liferay.portal.service.GroupLocalServiceUtil;
039    import com.liferay.portal.service.LayoutLocalServiceUtil;
040    import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
041    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
042    import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
043    import com.liferay.portal.service.OrganizationLocalServiceUtil;
044    import com.liferay.portal.service.UserGroupLocalServiceUtil;
045    import com.liferay.portal.service.UserLocalServiceUtil;
046    import com.liferay.portal.theme.ThemeDisplay;
047    import com.liferay.portal.util.PortalUtil;
048    import com.liferay.portal.util.PropsValues;
049    
050    import java.io.IOException;
051    
052    import java.util.List;
053    
054    /**
055     * @author Brian Wing Shun Chan
056     */
057    public class GroupImpl extends GroupModelImpl implements Group {
058    
059            public GroupImpl() {
060            }
061    
062            public long getDefaultPrivatePlid() {
063                    return getDefaultPlid(true);
064            }
065    
066            public long getDefaultPublicPlid() {
067                    return getDefaultPlid(false);
068            }
069    
070            public String getDescriptiveName() throws PortalException, SystemException {
071                    String name = getName();
072    
073                    if (isCompany()) {
074                            name = "global";
075                    }
076                    else if (isLayout()) {
077                            Layout layout = LayoutLocalServiceUtil.getLayout(getClassPK());
078    
079                            name = layout.getName(LocaleUtil.getDefault());
080                    }
081                    else if (isLayoutPrototype()) {
082                            LayoutPrototype layoutPrototype =
083                                    LayoutPrototypeLocalServiceUtil.getLayoutPrototype(
084                                            getClassPK());
085    
086                            name = layoutPrototype.getName(LocaleUtil.getDefault());
087                    }
088                    else if (isLayoutSetPrototype()) {
089                            LayoutSetPrototype layoutSetPrototype =
090                                    LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototype(
091                                            getClassPK());
092    
093                            name = layoutSetPrototype.getName(LocaleUtil.getDefault());
094                    }
095                    else if (isOrganization()) {
096                            long organizationId = getOrganizationId();
097    
098                            Organization organization =
099                                    OrganizationLocalServiceUtil.getOrganization(organizationId);
100    
101                            name = organization.getName();
102                    }
103                    else if (isUser()) {
104                            long userId = getClassPK();
105    
106                            User user = UserLocalServiceUtil.getUserById(userId);
107    
108                            name = user.getFullName();
109                    }
110                    else if (isUserGroup()) {
111                            long userGroupId = getClassPK();
112    
113                            UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
114                                    userGroupId);
115    
116                            name = userGroup.getName();
117                    }
118                    else if (name.equals(GroupConstants.GUEST)) {
119                            Company company = CompanyLocalServiceUtil.getCompany(
120                                    getCompanyId());
121    
122                            Account account = company.getAccount();
123    
124                            name = account.getName();
125                    }
126    
127                    return name;
128            }
129    
130            public Group getLiveGroup() {
131                    if (!isStagingGroup()) {
132                            return null;
133                    }
134    
135                    try {
136                            if (_liveGroup == null) {
137                                    _liveGroup = GroupLocalServiceUtil.getGroup(
138                                            getLiveGroupId());
139                            }
140    
141                            return _liveGroup;
142                    }
143                    catch (Exception e) {
144                            _log.error("Error getting live group for " + getLiveGroupId(), e);
145    
146                            return null;
147                    }
148            }
149    
150            public long getOrganizationId() {
151                    if (isOrganization()) {
152                            if (isStagingGroup()) {
153                                    Group liveGroup = getLiveGroup();
154    
155                                    return liveGroup.getClassPK();
156                            }
157                            else {
158                                    return getClassPK();
159                            }
160                    }
161    
162                    return 0;
163            }
164    
165            public String getPathFriendlyURL(
166                    boolean privateLayout, ThemeDisplay themeDisplay) {
167    
168                    if (privateLayout) {
169                            if (isUser()) {
170                                    return themeDisplay.getPathFriendlyURLPrivateUser();
171                            }
172                            else {
173                                    return themeDisplay.getPathFriendlyURLPrivateGroup();
174                            }
175                    }
176                    else {
177                            return themeDisplay.getPathFriendlyURLPublic();
178                    }
179            }
180    
181            public LayoutSet getPrivateLayoutSet() {
182                    LayoutSet layoutSet = null;
183    
184                    try {
185                            layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
186                                    getGroupId(), true);
187                    }
188                    catch (Exception e) {
189                            _log.error(e);
190                    }
191    
192                    return layoutSet;
193            }
194    
195            public int getPrivateLayoutsPageCount() {
196                    try {
197                            LayoutSet layoutSet = getPrivateLayoutSet();
198    
199                            return layoutSet.getPageCount();
200                    }
201                    catch (Exception e) {
202                            _log.error(e);
203                    }
204    
205                    return 0;
206            }
207    
208            public LayoutSet getPublicLayoutSet() {
209                    LayoutSet layoutSet = null;
210    
211                    try {
212                            layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
213                                    getGroupId(), false);
214                    }
215                    catch (Exception e) {
216                            _log.error(e);
217                    }
218    
219                    return layoutSet;
220            }
221    
222            public int getPublicLayoutsPageCount() {
223                    try {
224                            LayoutSet layoutSet = getPublicLayoutSet();
225    
226                            return layoutSet.getPageCount();
227                    }
228                    catch (Exception e) {
229                            _log.error(e);
230                    }
231    
232                    return 0;
233            }
234    
235            public Group getStagingGroup() {
236                    if (isStagingGroup()) {
237                            return null;
238                    }
239    
240                    try {
241                            if (_stagingGroup == null) {
242                                    _stagingGroup =
243                                            GroupLocalServiceUtil.getStagingGroup(getGroupId());
244                            }
245    
246                            return _stagingGroup;
247                    }
248                    catch (Exception e) {
249                            _log.error("Error getting staging group for " + getGroupId(), e);
250    
251                            return null;
252                    }
253            }
254    
255            public String getTypeLabel() {
256                    return GroupConstants.getTypeLabel(getType());
257            }
258    
259            public String getTypeSettings() {
260                    if (_typeSettingsProperties == null) {
261                            return super.getTypeSettings();
262                    }
263                    else {
264                            return _typeSettingsProperties.toString();
265                    }
266            }
267    
268            public UnicodeProperties getTypeSettingsProperties() {
269                    if (_typeSettingsProperties == null) {
270                            _typeSettingsProperties = new UnicodeProperties(true);
271    
272                            try {
273                                    _typeSettingsProperties.load(super.getTypeSettings());
274                            }
275                            catch (IOException ioe) {
276                                    _log.error(ioe, ioe);
277                            }
278                    }
279    
280                    return _typeSettingsProperties;
281            }
282    
283            public String getTypeSettingsProperty(String key) {
284                    UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
285    
286                    return typeSettingsProperties.getProperty(key);
287            }
288    
289            public String getWorkflowRoleNames() {
290                    return GetterUtil.getString(
291                            getTypeSettingsProperty("workflowRoleNames"),
292                            PropsValues.TASKS_DEFAULT_ROLE_NAMES);
293            }
294    
295            public int getWorkflowStages() {
296                    return GetterUtil.getInteger(
297                            getTypeSettingsProperty("workflowStages"),
298                            PropsValues.TASKS_DEFAULT_STAGES);
299            }
300    
301            public boolean hasPrivateLayouts() {
302                    if (getPrivateLayoutsPageCount() > 0) {
303                            return true;
304                    }
305                    else {
306                            return false;
307                    }
308            }
309    
310            public boolean hasPublicLayouts() {
311                    if (getPublicLayoutsPageCount() > 0) {
312                            return true;
313                    }
314                    else {
315                            return false;
316                    }
317            }
318    
319            public boolean hasStagingGroup() {
320                    if (isStagingGroup()) {
321                            return false;
322                    }
323    
324                    if (_stagingGroup != null) {
325                            return true;
326                    }
327    
328                    try {
329                            return GroupLocalServiceUtil.hasStagingGroup(getGroupId());
330                    }
331                    catch (Exception e) {
332                            return false;
333                    }
334            }
335    
336            public boolean isCommunity() {
337                    return hasClassName(Group.class);
338            }
339    
340            public boolean isCompany() {
341                    return hasClassName(Company.class);
342            }
343    
344            public boolean isControlPanel() {
345                    if (getName().equals(GroupConstants.CONTROL_PANEL)) {
346                            return true;
347                    }
348                    else {
349                            return false;
350                    }
351            }
352    
353            public boolean isLayout() {
354                    return hasClassName(Layout.class);
355            }
356    
357            public boolean isLayoutPrototype() {
358                    return hasClassName(LayoutPrototype.class);
359            }
360    
361            public boolean isLayoutSetPrototype() {
362                    return hasClassName(LayoutSetPrototype.class);
363            }
364    
365            public boolean isOrganization() {
366                    return hasClassName(Organization.class);
367            }
368    
369            public boolean isStaged() {
370                    return GetterUtil.getBoolean(getTypeSettingsProperty("staged"));
371            }
372    
373            public boolean isStagedPortlet(String portletId) {
374                    return GetterUtil.getBoolean(
375                            getTypeSettingsProperty(
376                                    StagingConstants.STAGED_PORTLET.concat(portletId)),
377                            true);
378            }
379    
380            public boolean isStagedRemotely() {
381                    return GetterUtil.getBoolean(getTypeSettingsProperty("stagedRemotely"));
382            }
383    
384            public boolean isStagingGroup() {
385                    if (getLiveGroupId() == GroupConstants.DEFAULT_LIVE_GROUP_ID) {
386                            return false;
387                    }
388                    else {
389                            return true;
390                    }
391            }
392    
393            public boolean isUser() {
394                    return hasClassName(User.class);
395            }
396    
397            public boolean isUserGroup() {
398                    return hasClassName(UserGroup.class);
399            }
400    
401            public boolean isWorkflowEnabled() {
402                    return GetterUtil.getBoolean(
403                            getTypeSettingsProperty("workflowEnabled"));
404            }
405    
406            public void setTypeSettings(String typeSettings) {
407                    _typeSettingsProperties = null;
408    
409                    super.setTypeSettings(typeSettings);
410            }
411    
412            public void setTypeSettingsProperties(
413                    UnicodeProperties typeSettingsProperties) {
414    
415                    _typeSettingsProperties = typeSettingsProperties;
416    
417                    super.setTypeSettings(_typeSettingsProperties.toString());
418            }
419    
420            protected long getDefaultPlid(boolean privateLayout) {
421                    try {
422                            List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
423                                    getGroupId(), privateLayout,
424                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, 0, 1);
425    
426                            if (layouts.size() > 0) {
427                                    Layout layout = layouts.get(0);
428    
429                                    return layout.getPlid();
430                            }
431                    }
432                    catch (Exception e) {
433                            if (_log.isWarnEnabled()) {
434                                    _log.warn(e.getMessage());
435                            }
436                    }
437    
438                    return LayoutConstants.DEFAULT_PLID;
439            }
440    
441            protected boolean hasClassName(Class<?> classObj) {
442                    long classNameId = getClassNameId();
443    
444                    if (classNameId == PortalUtil.getClassNameId(classObj)) {
445                            return true;
446                    }
447                    else {
448                            return false;
449                    }
450            }
451    
452            private static Log _log = LogFactoryUtil.getLog(GroupImpl.class);
453    
454            private Group _liveGroup;
455            private Group _stagingGroup;
456            private UnicodeProperties _typeSettingsProperties;
457    
458    }