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