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