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