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