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          long classNameId = getClassNameId();
64          long classPK = getClassPK();
65  
66          if ((classNameId <= 0) && (classPK <= 0)) {
67              return true;
68          }
69          else {
70              return false;
71          }
72      }
73  
74      public boolean isLayout() {
75          long classNameId = getClassNameId();
76          long classPK = getClassPK();
77  
78          if ((classNameId > 0) && (classPK > 0)) {
79              long layoutClassNameId = PortalUtil.getClassNameId(
80                  Layout.class);
81  
82              if (classNameId == layoutClassNameId) {
83                  return true;
84              }
85          }
86  
87          return false;
88      }
89  
90      public boolean isOrganization() {
91          long classNameId = getClassNameId();
92          long classPK = getClassPK();
93  
94          if ((classNameId > 0) && (classPK > 0)) {
95              long organizationClassNameId = PortalUtil.getClassNameId(
96                  Organization.class);
97  
98              if (classNameId == organizationClassNameId) {
99                  return true;
100             }
101         }
102 
103         return false;
104     }
105 
106     public boolean isUser() {
107         long classNameId = getClassNameId();
108         long classPK = getClassPK();
109 
110         if ((classNameId > 0) && (classPK > 0)) {
111             long userClassNameId = PortalUtil.getClassNameId(User.class);
112 
113             if (classNameId == userClassNameId) {
114                 return true;
115             }
116         }
117 
118         return false;
119     }
120 
121     public boolean isUserGroup() {
122         long classNameId = getClassNameId();
123         long classPK = getClassPK();
124 
125         if ((classNameId > 0) && (classPK > 0)) {
126             long userGroupClassNameId = PortalUtil.getClassNameId(
127                 UserGroup.class);
128 
129             if (classNameId == userGroupClassNameId) {
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 
186         try {
187             return GroupLocalServiceUtil.hasStagingGroup(getGroupId());
188         }
189         catch (Exception e) {
190             return false;
191         }
192     }
193 
194     public boolean isStagingGroup() {
195         if (getLiveGroupId() == GroupConstants.DEFAULT_LIVE_GROUP_ID) {
196             return false;
197         }
198         else {
199             return true;
200         }
201     }
202 
203     public String getDescriptiveName() {
204         String name = getName();
205 
206         try {
207             if (isLayout()) {
208                 Layout layout = LayoutLocalServiceUtil.getLayout(
209                     getClassPK());
210 
211                 name = layout.getName();
212             }
213             else if (isOrganization()) {
214                 long organizationId = getClassPK();
215 
216                 Organization organization =
217                     OrganizationLocalServiceUtil.getOrganization(
218                         organizationId);
219 
220                 name = organization.getName();
221             }
222             else if (isUser()) {
223                 long userId = getClassPK();
224 
225                 User user = UserLocalServiceUtil.getUserById(userId);
226 
227                 name = user.getFullName();
228             }
229             else if (isUserGroup()) {
230                 long userGroupId = getClassPK();
231 
232                 UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
233                     userGroupId);
234 
235                 name = userGroup.getName();
236             }
237         }
238         catch (Exception e) {
239             _log.error("Error getting descriptive name for " + getGroupId(), e);
240         }
241 
242         return name;
243     }
244 
245     public String getTypeLabel() {
246         return GroupConstants.getTypeLabel(getType());
247     }
248 
249     public String getTypeSettings() {
250         if (_typeSettingsProperties == null) {
251             return super.getTypeSettings();
252         }
253         else {
254             return _typeSettingsProperties.toString();
255         }
256     }
257 
258     public void setTypeSettings(String typeSettings) {
259         _typeSettingsProperties = null;
260 
261         super.setTypeSettings(typeSettings);
262     }
263 
264     public UnicodeProperties getTypeSettingsProperties() {
265         if (_typeSettingsProperties == null) {
266             _typeSettingsProperties = new UnicodeProperties(true);
267 
268             try {
269                 _typeSettingsProperties.load(super.getTypeSettings());
270             }
271             catch (IOException ioe) {
272                 _log.error(ioe, ioe);
273             }
274         }
275 
276         return _typeSettingsProperties;
277     }
278 
279     public void setTypeSettingsProperties(
280         UnicodeProperties typeSettingsProperties) {
281 
282         _typeSettingsProperties = typeSettingsProperties;
283 
284         super.setTypeSettings(_typeSettingsProperties.toString());
285     }
286 
287     public String getTypeSettingsProperty(String key) {
288         UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
289 
290         return typeSettingsProperties.getProperty(key);
291     }
292 
293     public String getPathFriendlyURL(
294         boolean privateLayout, ThemeDisplay themeDisplay) {
295 
296         if (privateLayout) {
297             if (isUser()) {
298                 return themeDisplay.getPathFriendlyURLPrivateUser();
299             }
300             else {
301                 return themeDisplay.getPathFriendlyURLPrivateGroup();
302             }
303         }
304         else {
305             return themeDisplay.getPathFriendlyURLPublic();
306         }
307     }
308 
309     public long getDefaultPrivatePlid() {
310         return getDefaultPlid(true);
311     }
312 
313     public int getPrivateLayoutsPageCount() {
314         try {
315             LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
316                 getGroupId(), true);
317 
318             return layoutSet.getPageCount();
319         }
320         catch (Exception e) {
321             _log.error(e);
322         }
323 
324         return 0;
325     }
326 
327     public boolean hasPrivateLayouts() {
328         if (getPrivateLayoutsPageCount() > 0) {
329             return true;
330         }
331         else {
332             return false;
333         }
334     }
335 
336     public long getDefaultPublicPlid() {
337         return getDefaultPlid(false);
338     }
339 
340     public int getPublicLayoutsPageCount() {
341         try {
342             LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
343                 getGroupId(), false);
344 
345             return layoutSet.getPageCount();
346         }
347         catch (Exception e) {
348             _log.error(e);
349         }
350 
351         return 0;
352     }
353 
354     public boolean hasPublicLayouts() {
355         if (getPublicLayoutsPageCount() > 0) {
356             return true;
357         }
358         else {
359             return false;
360         }
361     }
362 
363     public boolean isWorkflowEnabled() {
364         return GetterUtil.getBoolean(
365             getTypeSettingsProperty("workflowEnabled"));
366     }
367 
368     public int getWorkflowStages() {
369         return GetterUtil.getInteger(
370             getTypeSettingsProperty("workflowStages"),
371             PropsValues.TASKS_DEFAULT_STAGES);
372     }
373 
374     public String getWorkflowRoleNames() {
375         return GetterUtil.getString(
376             getTypeSettingsProperty("workflowRoleNames"),
377             PropsValues.TASKS_DEFAULT_ROLE_NAMES);
378     }
379 
380     protected long getDefaultPlid(boolean privateLayout) {
381         try {
382             List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
383                 getGroupId(), privateLayout,
384                 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, 0, 1);
385 
386             if (layouts.size() > 0) {
387                 Layout layout = layouts.get(0);
388 
389                 return layout.getPlid();
390             }
391         }
392         catch (Exception e) {
393             if (_log.isWarnEnabled()) {
394                 _log.warn(e.getMessage());
395             }
396         }
397 
398         return LayoutConstants.DEFAULT_PLID;
399     }
400 
401     private static Log _log = LogFactoryUtil.getLog(GroupImpl.class);
402 
403     private Group _stagingGroup;
404     private Group _liveGroup;
405     private UnicodeProperties _typeSettingsProperties = null;
406 
407 }