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.PropertiesUtil;
27  import com.liferay.portal.kernel.util.SafeProperties;
28  import com.liferay.portal.model.Group;
29  import com.liferay.portal.model.Layout;
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.service.GroupLocalServiceUtil;
34  import com.liferay.portal.service.LayoutLocalServiceUtil;
35  import com.liferay.portal.service.LayoutSetLocalServiceUtil;
36  import com.liferay.portal.service.OrganizationLocalServiceUtil;
37  import com.liferay.portal.service.UserLocalServiceUtil;
38  import com.liferay.portal.theme.ThemeDisplay;
39  import com.liferay.portal.util.GroupNames;
40  import com.liferay.portal.util.PortalUtil;
41  import com.liferay.portal.util.PropsValues;
42  
43  import java.io.IOException;
44  
45  import java.util.List;
46  import java.util.Properties;
47  
48  import org.apache.commons.logging.Log;
49  import org.apache.commons.logging.LogFactory;
50  
51  /**
52   * <a href="GroupImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class GroupImpl extends GroupModelImpl implements Group {
58  
59      public static final long DEFAULT_PARENT_GROUP_ID = 0;
60  
61      public static final long DEFAULT_LIVE_GROUP_ID = 0;
62  
63      public static final String GUEST = GroupNames.GUEST;
64  
65      public static final String[] SYSTEM_GROUPS = GroupNames.SYSTEM_GROUPS;
66  
67      public static final int TYPE_COMMUNITY_OPEN = 1;
68  
69      public static final String TYPE_COMMUNITY_OPEN_LABEL = "open";
70  
71      public static final int TYPE_COMMUNITY_PRIVATE = 3;
72  
73      public static final String TYPE_COMMUNITY_PRIVATE_LABEL = "private";
74  
75      public static final int TYPE_COMMUNITY_RESTRICTED = 2;
76  
77      public static final String TYPE_COMMUNITY_RESTRICTED_LABEL = "restricted";
78  
79      public static String getTypeLabel(int type) {
80          if (type == TYPE_COMMUNITY_OPEN) {
81              return TYPE_COMMUNITY_OPEN_LABEL;
82          }
83          else if (type == TYPE_COMMUNITY_PRIVATE) {
84              return TYPE_COMMUNITY_PRIVATE_LABEL;
85          }
86          else {
87              return TYPE_COMMUNITY_RESTRICTED_LABEL;
88          }
89      }
90  
91      public GroupImpl() {
92      }
93  
94      public boolean isCommunity() {
95          long classNameId = getClassNameId();
96          long classPK = getClassPK();
97  
98          if ((classNameId <= 0) && (classPK <= 0)) {
99              return true;
100         }
101         else {
102             return false;
103         }
104     }
105 
106     public boolean isOrganization() {
107         long classNameId = getClassNameId();
108         long classPK = getClassPK();
109 
110         if ((classNameId > 0) && (classPK > 0)) {
111             long organizationClassNameId = PortalUtil.getClassNameId(
112                 Organization.class);
113 
114             if (classNameId == organizationClassNameId) {
115                 return true;
116             }
117         }
118 
119         return false;
120     }
121 
122     public boolean isUser() {
123         long classNameId = getClassNameId();
124         long classPK = getClassPK();
125 
126         if ((classNameId > 0) && (classPK > 0)) {
127             long userClassNameId = PortalUtil.getClassNameId(User.class);
128 
129             if (classNameId == userClassNameId) {
130                 return true;
131             }
132         }
133 
134         return false;
135     }
136 
137     public Group getLiveGroup() {
138         if (!isStagingGroup()) {
139             return null;
140         }
141 
142         try {
143             if (_liveGroup == null) {
144                 _liveGroup = GroupLocalServiceUtil.getGroup(
145                     getLiveGroupId());
146             }
147 
148             return _liveGroup;
149         }
150         catch (Exception e) {
151             _log.error("Error getting live group for " + getLiveGroupId(), e);
152 
153             return null;
154         }
155     }
156 
157     public Group getStagingGroup() {
158         if (isStagingGroup()) {
159             return null;
160         }
161 
162         try {
163             if (_stagingGroup == null) {
164                 _stagingGroup =
165                     GroupLocalServiceUtil.getStagingGroup(getGroupId());
166             }
167 
168             return _stagingGroup;
169         }
170         catch (Exception e) {
171             _log.error("Error getting staging group for " + getGroupId(), e);
172 
173             return null;
174         }
175     }
176 
177     public boolean hasStagingGroup() {
178         if (isStagingGroup()) {
179             return false;
180         }
181 
182         if (_stagingGroup != null) {
183             return true;
184         }
185         else {
186             try {
187                 _stagingGroup =
188                     GroupLocalServiceUtil.getStagingGroup(getGroupId());
189 
190                 return true;
191             }
192             catch (Exception e) {
193                 return false;
194             }
195         }
196     }
197 
198     public boolean isStagingGroup() {
199         if (getLiveGroupId() == DEFAULT_LIVE_GROUP_ID) {
200             return false;
201         }
202         else {
203             return true;
204         }
205     }
206 
207     public String getDescriptiveName() {
208         String name = getName();
209 
210         try {
211             if (isOrganization()) {
212                 long organizationId = getClassPK();
213 
214                 Organization organization =
215                     OrganizationLocalServiceUtil.getOrganization(
216                         organizationId);
217 
218                 name = organization.getName();
219             }
220             else if (isUser()) {
221                 long userId = getClassPK();
222 
223                 User user = UserLocalServiceUtil.getUserById(userId);
224 
225                 name = user.getFullName();
226             }
227         }
228         catch (Exception e) {
229             _log.error("Error getting descriptive name for " + getGroupId(), e);
230         }
231 
232         return name;
233     }
234 
235     public String getTypeLabel() {
236         return getTypeLabel(getType());
237     }
238 
239     public String getTypeSettings() {
240         if (_typeSettingsProperties == null) {
241             return super.getTypeSettings();
242         }
243         else {
244             return PropertiesUtil.toString(_typeSettingsProperties);
245         }
246     }
247 
248     public void setTypeSettings(String typeSettings) {
249         _typeSettingsProperties = null;
250 
251         super.setTypeSettings(typeSettings);
252     }
253 
254     public Properties getTypeSettingsProperties() {
255         if (_typeSettingsProperties == null) {
256             _typeSettingsProperties = new SafeProperties();
257 
258             try {
259                 PropertiesUtil.load(
260                     _typeSettingsProperties, super.getTypeSettings());
261             }
262             catch (IOException ioe) {
263                 _log.error(ioe);
264             }
265         }
266 
267         return _typeSettingsProperties;
268     }
269 
270     public void setTypeSettingsProperties(Properties typeSettingsProperties) {
271         _typeSettingsProperties = typeSettingsProperties;
272 
273         super.setTypeSettings(PropertiesUtil.toString(_typeSettingsProperties));
274     }
275 
276     public String getTypeSettingsProperty(String key) {
277         Properties typeSettingsProperties = getTypeSettingsProperties();
278 
279         return typeSettingsProperties.getProperty(key);
280     }
281 
282     public String getPathFriendlyURL(
283         boolean privateLayout, ThemeDisplay themeDisplay) {
284 
285         if (privateLayout) {
286             if (isUser()) {
287                 return themeDisplay.getPathFriendlyURLPrivateUser();
288             }
289             else {
290                 return themeDisplay.getPathFriendlyURLPrivateGroup();
291             }
292         }
293         else {
294             return themeDisplay.getPathFriendlyURLPublic();
295         }
296     }
297 
298     public long getDefaultPrivatePlid() {
299         return getDefaultPlid(true);
300     }
301 
302     public int getPrivateLayoutsPageCount() {
303         try {
304             LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
305                 getGroupId(), true);
306 
307             return layoutSet.getPageCount();
308         }
309         catch (Exception e) {
310             _log.error(e);
311         }
312 
313         return 0;
314     }
315 
316     public boolean hasPrivateLayouts() {
317         if (getPrivateLayoutsPageCount() > 0) {
318             return true;
319         }
320         else {
321             return false;
322         }
323     }
324 
325     public long getDefaultPublicPlid() {
326         return getDefaultPlid(false);
327     }
328 
329     public int getPublicLayoutsPageCount() {
330         try {
331             LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
332                 getGroupId(), false);
333 
334             return layoutSet.getPageCount();
335         }
336         catch (Exception e) {
337             _log.error(e);
338         }
339 
340         return 0;
341     }
342 
343     public boolean hasPublicLayouts() {
344         if (getPublicLayoutsPageCount() > 0) {
345             return true;
346         }
347         else {
348             return false;
349         }
350     }
351 
352     public boolean isWorkflowEnabled() {
353         return GetterUtil.getBoolean(
354             getTypeSettingsProperty("workflowEnabled"));
355     }
356 
357     public int getWorkflowStages() {
358         return GetterUtil.getInteger(
359             getTypeSettingsProperty("workflowStages"),
360             PropsValues.TASKS_DEFAULT_STAGES);
361     }
362 
363     public String getWorkflowRoleNames() {
364         return GetterUtil.getString(
365             getTypeSettingsProperty("workflowRoleNames"),
366             PropsValues.TASKS_DEFAULT_ROLE_NAMES);
367     }
368 
369     protected long getDefaultPlid(boolean privateLayout) {
370         try {
371             List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
372                 getGroupId(), privateLayout,
373                 LayoutImpl.DEFAULT_PARENT_LAYOUT_ID, 0, 1);
374 
375             if (layouts.size() > 0) {
376                 Layout layout = layouts.get(0);
377 
378                 return layout.getPlid();
379             }
380         }
381         catch (Exception e) {
382             if (_log.isWarnEnabled()) {
383                 _log.warn(e.getMessage());
384             }
385         }
386 
387         return LayoutImpl.DEFAULT_PLID;
388     }
389 
390     private static Log _log = LogFactory.getLog(GroupImpl.class);
391 
392     private Group _stagingGroup;
393     private Group _liveGroup;
394     private Properties _typeSettingsProperties = null;
395 
396 }