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