1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.model.impl;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.UnicodeProperties;
27  import com.liferay.portal.model.Group;
28  import com.liferay.portal.model.Layout;
29  import com.liferay.portal.model.LayoutConstants;
30  import com.liferay.portal.model.LayoutSet;
31  import com.liferay.portal.model.Organization;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.model.UserGroup;
34  import com.liferay.portal.service.GroupLocalServiceUtil;
35  import com.liferay.portal.service.LayoutLocalServiceUtil;
36  import com.liferay.portal.service.LayoutSetLocalServiceUtil;
37  import com.liferay.portal.service.OrganizationLocalServiceUtil;
38  import com.liferay.portal.service.UserGroupLocalServiceUtil;
39  import com.liferay.portal.service.UserLocalServiceUtil;
40  import com.liferay.portal.theme.ThemeDisplay;
41  import com.liferay.portal.util.GroupNames;
42  import com.liferay.portal.util.PortalUtil;
43  import com.liferay.portal.util.PropsValues;
44  
45  import java.io.IOException;
46  
47  import java.util.List;
48  
49  import org.apache.commons.logging.Log;
50  import org.apache.commons.logging.LogFactory;
51  
52  /**
53   * <a href="GroupImpl.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   *
57   */
58  public class GroupImpl extends GroupModelImpl implements Group {
59  
60      public static final long DEFAULT_PARENT_GROUP_ID = 0;
61  
62      public static final long DEFAULT_LIVE_GROUP_ID = 0;
63  
64      public static final String GUEST = GroupNames.GUEST;
65  
66      public static final String[] SYSTEM_GROUPS = GroupNames.SYSTEM_GROUPS;
67  
68      public static final int TYPE_COMMUNITY_OPEN = 1;
69  
70      public static final String TYPE_COMMUNITY_OPEN_LABEL = "open";
71  
72      public static final int TYPE_COMMUNITY_PRIVATE = 3;
73  
74      public static final String TYPE_COMMUNITY_PRIVATE_LABEL = "private";
75  
76      public static final int TYPE_COMMUNITY_RESTRICTED = 2;
77  
78      public static final String TYPE_COMMUNITY_RESTRICTED_LABEL = "restricted";
79  
80      public static String getTypeLabel(int type) {
81          if (type == TYPE_COMMUNITY_OPEN) {
82              return TYPE_COMMUNITY_OPEN_LABEL;
83          }
84          else if (type == TYPE_COMMUNITY_PRIVATE) {
85              return TYPE_COMMUNITY_PRIVATE_LABEL;
86          }
87          else {
88              return TYPE_COMMUNITY_RESTRICTED_LABEL;
89          }
90      }
91  
92      public GroupImpl() {
93      }
94  
95      public boolean isCommunity() {
96          long classNameId = getClassNameId();
97          long classPK = getClassPK();
98  
99          if ((classNameId <= 0) && (classPK <= 0)) {
100             return true;
101         }
102         else {
103             return false;
104         }
105     }
106 
107     public boolean isOrganization() {
108         long classNameId = getClassNameId();
109         long classPK = getClassPK();
110 
111         if ((classNameId > 0) && (classPK > 0)) {
112             long organizationClassNameId = PortalUtil.getClassNameId(
113                 Organization.class);
114 
115             if (classNameId == organizationClassNameId) {
116                 return true;
117             }
118         }
119 
120         return false;
121     }
122 
123     public boolean isUser() {
124         long classNameId = getClassNameId();
125         long classPK = getClassPK();
126 
127         if ((classNameId > 0) && (classPK > 0)) {
128             long userClassNameId = PortalUtil.getClassNameId(User.class);
129 
130             if (classNameId == userClassNameId) {
131                 return true;
132             }
133         }
134 
135         return false;
136     }
137 
138     public boolean isUserGroup() {
139         long classNameId = getClassNameId();
140         long classPK = getClassPK();
141 
142         if ((classNameId > 0) && (classPK > 0)) {
143             long userGroupClassNameId = PortalUtil.getClassNameId(
144                 UserGroup.class);
145 
146             if (classNameId == userGroupClassNameId) {
147                 return true;
148             }
149         }
150 
151         return false;
152     }
153 
154     public Group getLiveGroup() {
155         if (!isStagingGroup()) {
156             return null;
157         }
158 
159         try {
160             if (_liveGroup == null) {
161                 _liveGroup = GroupLocalServiceUtil.getGroup(
162                     getLiveGroupId());
163             }
164 
165             return _liveGroup;
166         }
167         catch (Exception e) {
168             _log.error("Error getting live group for " + getLiveGroupId(), e);
169 
170             return null;
171         }
172     }
173 
174     public Group getStagingGroup() {
175         if (isStagingGroup()) {
176             return null;
177         }
178 
179         try {
180             if (_stagingGroup == null) {
181                 _stagingGroup =
182                     GroupLocalServiceUtil.getStagingGroup(getGroupId());
183             }
184 
185             return _stagingGroup;
186         }
187         catch (Exception e) {
188             _log.error("Error getting staging group for " + getGroupId(), e);
189 
190             return null;
191         }
192     }
193 
194     public boolean hasStagingGroup() {
195         if (isStagingGroup()) {
196             return false;
197         }
198 
199         if (_stagingGroup != null) {
200             return true;
201         }
202         else {
203             try {
204                 _stagingGroup =
205                     GroupLocalServiceUtil.getStagingGroup(getGroupId());
206 
207                 return true;
208             }
209             catch (Exception e) {
210                 return false;
211             }
212         }
213     }
214 
215     public boolean isStagingGroup() {
216         if (getLiveGroupId() == DEFAULT_LIVE_GROUP_ID) {
217             return false;
218         }
219         else {
220             return true;
221         }
222     }
223 
224     public String getDescriptiveName() {
225         String name = getName();
226 
227         try {
228             if (isOrganization()) {
229                 long organizationId = getClassPK();
230 
231                 Organization organization =
232                     OrganizationLocalServiceUtil.getOrganization(
233                         organizationId);
234 
235                 name = organization.getName();
236             }
237             else if (isUser()) {
238                 long userId = getClassPK();
239 
240                 User user = UserLocalServiceUtil.getUserById(userId);
241 
242                 name = user.getFullName();
243             }
244             else if (isUserGroup()) {
245                 long userGroupId = getClassPK();
246 
247                 UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
248                     userGroupId);
249 
250                 name = userGroup.getName();
251             }
252         }
253         catch (Exception e) {
254             _log.error("Error getting descriptive name for " + getGroupId(), e);
255         }
256 
257         return name;
258     }
259 
260     public String getTypeLabel() {
261         return getTypeLabel(getType());
262     }
263 
264     public String getTypeSettings() {
265         if (_typeSettingsProperties == null) {
266             return super.getTypeSettings();
267         }
268         else {
269             return _typeSettingsProperties.toString();
270         }
271     }
272 
273     public void setTypeSettings(String typeSettings) {
274         _typeSettingsProperties = null;
275 
276         super.setTypeSettings(typeSettings);
277     }
278 
279     public UnicodeProperties getTypeSettingsProperties() {
280         if (_typeSettingsProperties == null) {
281             _typeSettingsProperties = new UnicodeProperties(true);
282 
283             try {
284                 _typeSettingsProperties.load(super.getTypeSettings());
285             }
286             catch (IOException ioe) {
287                 _log.error(ioe);
288             }
289         }
290 
291         return _typeSettingsProperties;
292     }
293 
294     public void setTypeSettingsProperties(
295         UnicodeProperties typeSettingsProperties) {
296 
297         _typeSettingsProperties = typeSettingsProperties;
298 
299         super.setTypeSettings(_typeSettingsProperties.toString());
300     }
301 
302     public String getTypeSettingsProperty(String key) {
303         UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
304 
305         return typeSettingsProperties.getProperty(key);
306     }
307 
308     public String getPathFriendlyURL(
309         boolean privateLayout, ThemeDisplay themeDisplay) {
310 
311         if (privateLayout) {
312             if (isUser()) {
313                 return themeDisplay.getPathFriendlyURLPrivateUser();
314             }
315             else {
316                 return themeDisplay.getPathFriendlyURLPrivateGroup();
317             }
318         }
319         else {
320             return themeDisplay.getPathFriendlyURLPublic();
321         }
322     }
323 
324     public long getDefaultPrivatePlid() {
325         return getDefaultPlid(true);
326     }
327 
328     public int getPrivateLayoutsPageCount() {
329         try {
330             LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
331                 getGroupId(), true);
332 
333             return layoutSet.getPageCount();
334         }
335         catch (Exception e) {
336             _log.error(e);
337         }
338 
339         return 0;
340     }
341 
342     public boolean hasPrivateLayouts() {
343         if (getPrivateLayoutsPageCount() > 0) {
344             return true;
345         }
346         else {
347             return false;
348         }
349     }
350 
351     public long getDefaultPublicPlid() {
352         return getDefaultPlid(false);
353     }
354 
355     public int getPublicLayoutsPageCount() {
356         try {
357             LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
358                 getGroupId(), false);
359 
360             return layoutSet.getPageCount();
361         }
362         catch (Exception e) {
363             _log.error(e);
364         }
365 
366         return 0;
367     }
368 
369     public boolean hasPublicLayouts() {
370         if (getPublicLayoutsPageCount() > 0) {
371             return true;
372         }
373         else {
374             return false;
375         }
376     }
377 
378     public boolean isWorkflowEnabled() {
379         return GetterUtil.getBoolean(
380             getTypeSettingsProperty("workflowEnabled"));
381     }
382 
383     public int getWorkflowStages() {
384         return GetterUtil.getInteger(
385             getTypeSettingsProperty("workflowStages"),
386             PropsValues.TASKS_DEFAULT_STAGES);
387     }
388 
389     public String getWorkflowRoleNames() {
390         return GetterUtil.getString(
391             getTypeSettingsProperty("workflowRoleNames"),
392             PropsValues.TASKS_DEFAULT_ROLE_NAMES);
393     }
394 
395     protected long getDefaultPlid(boolean privateLayout) {
396         try {
397             List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
398                 getGroupId(), privateLayout,
399                 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, 0, 1);
400 
401             if (layouts.size() > 0) {
402                 Layout layout = layouts.get(0);
403 
404                 return layout.getPlid();
405             }
406         }
407         catch (Exception e) {
408             if (_log.isWarnEnabled()) {
409                 _log.warn(e.getMessage());
410             }
411         }
412 
413         return LayoutConstants.DEFAULT_PLID;
414     }
415 
416     private static Log _log = LogFactory.getLog(GroupImpl.class);
417 
418     private Group _stagingGroup;
419     private Group _liveGroup;
420     private UnicodeProperties _typeSettingsProperties = null;
421 
422 }