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