1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.model.impl;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.LocaleUtil;
21  import com.liferay.portal.kernel.util.UnicodeProperties;
22  import com.liferay.portal.model.Company;
23  import com.liferay.portal.model.Group;
24  import com.liferay.portal.model.GroupConstants;
25  import com.liferay.portal.model.Layout;
26  import com.liferay.portal.model.LayoutConstants;
27  import com.liferay.portal.model.LayoutPrototype;
28  import com.liferay.portal.model.LayoutSet;
29  import com.liferay.portal.model.LayoutSetPrototype;
30  import com.liferay.portal.model.Organization;
31  import com.liferay.portal.model.User;
32  import com.liferay.portal.model.UserGroup;
33  import com.liferay.portal.service.CompanyLocalServiceUtil;
34  import com.liferay.portal.service.GroupLocalServiceUtil;
35  import com.liferay.portal.service.LayoutLocalServiceUtil;
36  import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
37  import com.liferay.portal.service.LayoutSetLocalServiceUtil;
38  import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
39  import com.liferay.portal.service.OrganizationLocalServiceUtil;
40  import com.liferay.portal.service.UserGroupLocalServiceUtil;
41  import com.liferay.portal.service.UserLocalServiceUtil;
42  import com.liferay.portal.theme.ThemeDisplay;
43  import com.liferay.portal.util.PortalUtil;
44  import com.liferay.portal.util.PropsValues;
45  
46  import java.io.IOException;
47  
48  import java.util.List;
49  
50  /**
51   * <a href="GroupImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   */
55  public class GroupImpl extends GroupModelImpl implements Group {
56  
57      public GroupImpl() {
58      }
59  
60      public boolean isCommunity() {
61          return hasClassName(Group.class);
62      }
63  
64      public boolean isCompany() {
65          return hasClassName(Company.class);
66      }
67  
68      public boolean isLayout() {
69          return hasClassName(Layout.class);
70      }
71  
72      public boolean isLayoutPrototype() {
73          return hasClassName(LayoutPrototype.class);
74      }
75  
76      public boolean isLayoutSetPrototype() {
77          return hasClassName(LayoutSetPrototype.class);
78      }
79  
80      public boolean isOrganization() {
81          return hasClassName(Organization.class);
82      }
83  
84      public boolean isUser() {
85          return hasClassName(User.class);
86      }
87  
88      public boolean isUserGroup() {
89          return hasClassName(UserGroup.class);
90      }
91  
92      public Group getLiveGroup() {
93          if (!isStagingGroup()) {
94              return null;
95          }
96  
97          try {
98              if (_liveGroup == null) {
99                  _liveGroup = GroupLocalServiceUtil.getGroup(
100                     getLiveGroupId());
101             }
102 
103             return _liveGroup;
104         }
105         catch (Exception e) {
106             _log.error("Error getting live group for " + getLiveGroupId(), e);
107 
108             return null;
109         }
110     }
111 
112     public Group getStagingGroup() {
113         if (isStagingGroup()) {
114             return null;
115         }
116 
117         try {
118             if (_stagingGroup == null) {
119                 _stagingGroup =
120                     GroupLocalServiceUtil.getStagingGroup(getGroupId());
121             }
122 
123             return _stagingGroup;
124         }
125         catch (Exception e) {
126             _log.error("Error getting staging group for " + getGroupId(), e);
127 
128             return null;
129         }
130     }
131 
132     public boolean hasStagingGroup() {
133         if (isStagingGroup()) {
134             return false;
135         }
136 
137         if (_stagingGroup != null) {
138             return true;
139         }
140 
141         try {
142             return GroupLocalServiceUtil.hasStagingGroup(getGroupId());
143         }
144         catch (Exception e) {
145             return false;
146         }
147     }
148 
149     public boolean isStagingGroup() {
150         if (getLiveGroupId() == GroupConstants.DEFAULT_LIVE_GROUP_ID) {
151             return false;
152         }
153         else {
154             return true;
155         }
156     }
157 
158     public String getDescriptiveName() {
159         String name = getName();
160 
161         try {
162             if (isCompany()) {
163                 name = "global";
164             }
165             else if (isLayout()) {
166                 Layout layout = LayoutLocalServiceUtil.getLayout(
167                     getClassPK());
168 
169                 name = layout.getName(LocaleUtil.getDefault());
170             }
171             else if (isLayoutPrototype()) {
172                 LayoutPrototype layoutPrototype =
173                     LayoutPrototypeLocalServiceUtil.getLayoutPrototype(
174                         getClassPK());
175 
176                 name = layoutPrototype.getName(LocaleUtil.getDefault());
177             }
178             else if (isLayoutSetPrototype()) {
179                 LayoutSetPrototype layoutSetPrototype =
180                     LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototype(
181                         getClassPK());
182 
183                 name = layoutSetPrototype.getName(LocaleUtil.getDefault());
184             }
185             else if (isOrganization()) {
186                 long organizationId = getClassPK();
187 
188                 Organization organization =
189                     OrganizationLocalServiceUtil.getOrganization(
190                         organizationId);
191 
192                 name = organization.getName();
193             }
194             else if (isUser()) {
195                 long userId = getClassPK();
196 
197                 User user = UserLocalServiceUtil.getUserById(userId);
198 
199                 name = user.getFullName();
200             }
201             else if (isUserGroup()) {
202                 long userGroupId = getClassPK();
203 
204                 UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
205                     userGroupId);
206 
207                 name = userGroup.getName();
208             }
209             else if (name.equals(GroupConstants.GUEST)) {
210                 Company company = CompanyLocalServiceUtil.getCompany(
211                     getCompanyId());
212 
213                 company.getAccount().getName();
214             }
215         }
216         catch (Exception e) {
217             _log.error("Error getting descriptive name for " + getGroupId(), e);
218         }
219 
220         return name;
221     }
222 
223     public String getTypeLabel() {
224         return GroupConstants.getTypeLabel(getType());
225     }
226 
227     public String getTypeSettings() {
228         if (_typeSettingsProperties == null) {
229             return super.getTypeSettings();
230         }
231         else {
232             return _typeSettingsProperties.toString();
233         }
234     }
235 
236     public void setTypeSettings(String typeSettings) {
237         _typeSettingsProperties = null;
238 
239         super.setTypeSettings(typeSettings);
240     }
241 
242     public UnicodeProperties getTypeSettingsProperties() {
243         if (_typeSettingsProperties == null) {
244             _typeSettingsProperties = new UnicodeProperties(true);
245 
246             try {
247                 _typeSettingsProperties.load(super.getTypeSettings());
248             }
249             catch (IOException ioe) {
250                 _log.error(ioe, ioe);
251             }
252         }
253 
254         return _typeSettingsProperties;
255     }
256 
257     public void setTypeSettingsProperties(
258         UnicodeProperties typeSettingsProperties) {
259 
260         _typeSettingsProperties = typeSettingsProperties;
261 
262         super.setTypeSettings(_typeSettingsProperties.toString());
263     }
264 
265     public String getTypeSettingsProperty(String key) {
266         UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
267 
268         return typeSettingsProperties.getProperty(key);
269     }
270 
271     public String getPathFriendlyURL(
272         boolean privateLayout, ThemeDisplay themeDisplay) {
273 
274         if (privateLayout) {
275             if (isUser()) {
276                 return themeDisplay.getPathFriendlyURLPrivateUser();
277             }
278             else {
279                 return themeDisplay.getPathFriendlyURLPrivateGroup();
280             }
281         }
282         else {
283             return themeDisplay.getPathFriendlyURLPublic();
284         }
285     }
286 
287     public long getDefaultPrivatePlid() {
288         return getDefaultPlid(true);
289     }
290 
291     public LayoutSet getPrivateLayoutSet() {
292         LayoutSet layoutSet = null;
293 
294         try {
295             layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
296                 getGroupId(), true);
297         }
298         catch (Exception e) {
299             _log.error(e);
300         }
301 
302         return layoutSet;
303     }
304 
305     public int getPrivateLayoutsPageCount() {
306         try {
307             LayoutSet layoutSet = getPrivateLayoutSet();
308 
309             return layoutSet.getPageCount();
310         }
311         catch (Exception e) {
312             _log.error(e);
313         }
314 
315         return 0;
316     }
317 
318     public boolean hasPrivateLayouts() {
319         if (getPrivateLayoutsPageCount() > 0) {
320             return true;
321         }
322         else {
323             return false;
324         }
325     }
326 
327     public long getDefaultPublicPlid() {
328         return getDefaultPlid(false);
329     }
330 
331     public LayoutSet getPublicLayoutSet() {
332         LayoutSet layoutSet = null;
333 
334         try {
335             layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
336                 getGroupId(), false);
337         }
338         catch (Exception e) {
339             _log.error(e);
340         }
341 
342         return layoutSet;
343     }
344 
345     public int getPublicLayoutsPageCount() {
346         try {
347             LayoutSet layoutSet = getPublicLayoutSet();
348 
349             return layoutSet.getPageCount();
350         }
351         catch (Exception e) {
352             _log.error(e);
353         }
354 
355         return 0;
356     }
357 
358     public boolean hasPublicLayouts() {
359         if (getPublicLayoutsPageCount() > 0) {
360             return true;
361         }
362         else {
363             return false;
364         }
365     }
366 
367     public boolean isWorkflowEnabled() {
368         return GetterUtil.getBoolean(
369             getTypeSettingsProperty("workflowEnabled"));
370     }
371 
372     public int getWorkflowStages() {
373         return GetterUtil.getInteger(
374             getTypeSettingsProperty("workflowStages"),
375             PropsValues.TASKS_DEFAULT_STAGES);
376     }
377 
378     public String getWorkflowRoleNames() {
379         return GetterUtil.getString(
380             getTypeSettingsProperty("workflowRoleNames"),
381             PropsValues.TASKS_DEFAULT_ROLE_NAMES);
382     }
383 
384     protected long getDefaultPlid(boolean privateLayout) {
385         try {
386             List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
387                 getGroupId(), privateLayout,
388                 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, 0, 1);
389 
390             if (layouts.size() > 0) {
391                 Layout layout = layouts.get(0);
392 
393                 return layout.getPlid();
394             }
395         }
396         catch (Exception e) {
397             if (_log.isWarnEnabled()) {
398                 _log.warn(e.getMessage());
399             }
400         }
401 
402         return LayoutConstants.DEFAULT_PLID;
403     }
404 
405     protected boolean hasClassName(Class<?> classObj) {
406         long classNameId = getClassNameId();
407 
408         if (classNameId == PortalUtil.getClassNameId(classObj)) {
409             return true;
410         }
411         else {
412             return false;
413         }
414     }
415 
416     private static Log _log = LogFactoryUtil.getLog(GroupImpl.class);
417 
418     private Group _stagingGroup;
419     private Group _liveGroup;
420     private UnicodeProperties _typeSettingsProperties = null;
421 
422 }