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