1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.model.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.GetterUtil;
22  import com.liferay.portal.kernel.util.LocaleUtil;
23  import com.liferay.portal.kernel.util.UnicodeProperties;
24  import com.liferay.portal.model.Company;
25  import com.liferay.portal.model.Group;
26  import com.liferay.portal.model.GroupConstants;
27  import com.liferay.portal.model.Layout;
28  import com.liferay.portal.model.LayoutConstants;
29  import com.liferay.portal.model.LayoutPrototype;
30  import com.liferay.portal.model.LayoutSet;
31  import com.liferay.portal.model.LayoutSetPrototype;
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.CompanyLocalServiceUtil;
36  import com.liferay.portal.service.GroupLocalServiceUtil;
37  import com.liferay.portal.service.LayoutLocalServiceUtil;
38  import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
39  import com.liferay.portal.service.LayoutSetLocalServiceUtil;
40  import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
41  import com.liferay.portal.service.OrganizationLocalServiceUtil;
42  import com.liferay.portal.service.UserGroupLocalServiceUtil;
43  import com.liferay.portal.service.UserLocalServiceUtil;
44  import com.liferay.portal.theme.ThemeDisplay;
45  import com.liferay.portal.util.PortalUtil;
46  import com.liferay.portal.util.PropsValues;
47  
48  import java.io.IOException;
49  
50  import java.util.List;
51  
52  /**
53   * <a href="GroupImpl.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   */
57  public class GroupImpl extends GroupModelImpl implements Group {
58  
59      public GroupImpl() {
60      }
61  
62      public long getDefaultPrivatePlid() {
63          return getDefaultPlid(true);
64      }
65  
66      public long getDefaultPublicPlid() {
67          return getDefaultPlid(false);
68      }
69  
70      public String getDescriptiveName() throws PortalException, SystemException {
71          String name = getName();
72  
73          if (isCompany()) {
74              name = "global";
75          }
76          else if (isLayout()) {
77              Layout layout = LayoutLocalServiceUtil.getLayout(getClassPK());
78  
79              name = layout.getName(LocaleUtil.getDefault());
80          }
81          else if (isLayoutPrototype()) {
82              LayoutPrototype layoutPrototype =
83                  LayoutPrototypeLocalServiceUtil.getLayoutPrototype(
84                      getClassPK());
85  
86              name = layoutPrototype.getName(LocaleUtil.getDefault());
87          }
88          else if (isLayoutSetPrototype()) {
89              LayoutSetPrototype layoutSetPrototype =
90                  LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototype(
91                      getClassPK());
92  
93              name = layoutSetPrototype.getName(LocaleUtil.getDefault());
94          }
95          else if (isOrganization()) {
96              long organizationId = getClassPK();
97  
98              Organization organization =
99                  OrganizationLocalServiceUtil.getOrganization(organizationId);
100 
101             name = organization.getName();
102         }
103         else if (isUser()) {
104             long userId = getClassPK();
105 
106             User user = UserLocalServiceUtil.getUserById(userId);
107 
108             name = user.getFullName();
109         }
110         else if (isUserGroup()) {
111             long userGroupId = getClassPK();
112 
113             UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
114                 userGroupId);
115 
116             name = userGroup.getName();
117         }
118         else if (name.equals(GroupConstants.GUEST)) {
119             Company company = CompanyLocalServiceUtil.getCompany(
120                 getCompanyId());
121 
122             name = company.getAccount().getName();
123         }
124 
125         return name;
126     }
127 
128     public Group getLiveGroup() {
129         if (!isStagingGroup()) {
130             return null;
131         }
132 
133         try {
134             if (_liveGroup == null) {
135                 _liveGroup = GroupLocalServiceUtil.getGroup(
136                     getLiveGroupId());
137             }
138 
139             return _liveGroup;
140         }
141         catch (Exception e) {
142             _log.error("Error getting live group for " + getLiveGroupId(), e);
143 
144             return null;
145         }
146     }
147 
148     public String getPathFriendlyURL(
149         boolean privateLayout, ThemeDisplay themeDisplay) {
150 
151         if (privateLayout) {
152             if (isUser()) {
153                 return themeDisplay.getPathFriendlyURLPrivateUser();
154             }
155             else {
156                 return themeDisplay.getPathFriendlyURLPrivateGroup();
157             }
158         }
159         else {
160             return themeDisplay.getPathFriendlyURLPublic();
161         }
162     }
163 
164     public LayoutSet getPrivateLayoutSet() {
165         LayoutSet layoutSet = null;
166 
167         try {
168             layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
169                 getGroupId(), true);
170         }
171         catch (Exception e) {
172             _log.error(e);
173         }
174 
175         return layoutSet;
176     }
177 
178     public int getPrivateLayoutsPageCount() {
179         try {
180             LayoutSet layoutSet = getPrivateLayoutSet();
181 
182             return layoutSet.getPageCount();
183         }
184         catch (Exception e) {
185             _log.error(e);
186         }
187 
188         return 0;
189     }
190 
191     public LayoutSet getPublicLayoutSet() {
192         LayoutSet layoutSet = null;
193 
194         try {
195             layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
196                 getGroupId(), false);
197         }
198         catch (Exception e) {
199             _log.error(e);
200         }
201 
202         return layoutSet;
203     }
204 
205     public int getPublicLayoutsPageCount() {
206         try {
207             LayoutSet layoutSet = getPublicLayoutSet();
208 
209             return layoutSet.getPageCount();
210         }
211         catch (Exception e) {
212             _log.error(e);
213         }
214 
215         return 0;
216     }
217 
218     public Group getStagingGroup() {
219         if (isStagingGroup()) {
220             return null;
221         }
222 
223         try {
224             if (_stagingGroup == null) {
225                 _stagingGroup =
226                     GroupLocalServiceUtil.getStagingGroup(getGroupId());
227             }
228 
229             return _stagingGroup;
230         }
231         catch (Exception e) {
232             _log.error("Error getting staging group for " + getGroupId(), e);
233 
234             return null;
235         }
236     }
237 
238     public String getTypeLabel() {
239         return GroupConstants.getTypeLabel(getType());
240     }
241 
242     public String getTypeSettings() {
243         if (_typeSettingsProperties == null) {
244             return super.getTypeSettings();
245         }
246         else {
247             return _typeSettingsProperties.toString();
248         }
249     }
250 
251     public UnicodeProperties getTypeSettingsProperties() {
252         if (_typeSettingsProperties == null) {
253             _typeSettingsProperties = new UnicodeProperties(true);
254 
255             try {
256                 _typeSettingsProperties.load(super.getTypeSettings());
257             }
258             catch (IOException ioe) {
259                 _log.error(ioe, ioe);
260             }
261         }
262 
263         return _typeSettingsProperties;
264     }
265 
266     public String getTypeSettingsProperty(String key) {
267         UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
268 
269         return typeSettingsProperties.getProperty(key);
270     }
271 
272     public String getWorkflowRoleNames() {
273         return GetterUtil.getString(
274             getTypeSettingsProperty("workflowRoleNames"),
275             PropsValues.TASKS_DEFAULT_ROLE_NAMES);
276     }
277 
278     public int getWorkflowStages() {
279         return GetterUtil.getInteger(
280             getTypeSettingsProperty("workflowStages"),
281             PropsValues.TASKS_DEFAULT_STAGES);
282     }
283 
284     public boolean hasPrivateLayouts() {
285         if (getPrivateLayoutsPageCount() > 0) {
286             return true;
287         }
288         else {
289             return false;
290         }
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 hasStagingGroup() {
303         if (isStagingGroup()) {
304             return false;
305         }
306 
307         if (_stagingGroup != null) {
308             return true;
309         }
310 
311         try {
312             return GroupLocalServiceUtil.hasStagingGroup(getGroupId());
313         }
314         catch (Exception e) {
315             return false;
316         }
317     }
318 
319     public boolean isCommunity() {
320         if (isStagingGroup() && getLiveGroup().isOrganization()) {
321             return false;
322         }
323         else {
324             return hasClassName(Group.class);
325         }
326     }
327 
328     public boolean isCompany() {
329         return hasClassName(Company.class);
330     }
331 
332     public boolean isControlPanel() {
333         if (getName().equals(GroupConstants.CONTROL_PANEL)) {
334             return true;
335         }
336         else {
337             return false;
338         }
339     }
340 
341     public boolean isLayout() {
342         return hasClassName(Layout.class);
343     }
344 
345     public boolean isLayoutPrototype() {
346         return hasClassName(LayoutPrototype.class);
347     }
348 
349     public boolean isLayoutSetPrototype() {
350         return hasClassName(LayoutSetPrototype.class);
351     }
352 
353     public boolean isOrganization() {
354         return isOrganization(false);
355     }
356 
357     public boolean isOrganization(boolean includeStaging) {
358         if (includeStaging) {
359             if (isOrganization() ||
360                 (isStagingGroup() && getLiveGroup().isOrganization())) {
361 
362                 return true;
363             }
364             else {
365                 return false;
366             }
367         }
368         else {
369             return hasClassName(Organization.class);
370         }
371     }
372 
373     public boolean isStagingGroup() {
374         if (getLiveGroupId() == GroupConstants.DEFAULT_LIVE_GROUP_ID) {
375             return false;
376         }
377         else {
378             return true;
379         }
380     }
381 
382     public boolean isUser() {
383         return hasClassName(User.class);
384     }
385 
386     public boolean isUserGroup() {
387         return hasClassName(UserGroup.class);
388     }
389 
390     public boolean isWorkflowEnabled() {
391         return GetterUtil.getBoolean(
392             getTypeSettingsProperty("workflowEnabled"));
393     }
394 
395     public void setTypeSettings(String typeSettings) {
396         _typeSettingsProperties = null;
397 
398         super.setTypeSettings(typeSettings);
399     }
400 
401     public void setTypeSettingsProperties(
402         UnicodeProperties typeSettingsProperties) {
403 
404         _typeSettingsProperties = typeSettingsProperties;
405 
406         super.setTypeSettings(_typeSettingsProperties.toString());
407     }
408 
409     protected long getDefaultPlid(boolean privateLayout) {
410         try {
411             List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
412                 getGroupId(), privateLayout,
413                 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, 0, 1);
414 
415             if (layouts.size() > 0) {
416                 Layout layout = layouts.get(0);
417 
418                 return layout.getPlid();
419             }
420         }
421         catch (Exception e) {
422             if (_log.isWarnEnabled()) {
423                 _log.warn(e.getMessage());
424             }
425         }
426 
427         return LayoutConstants.DEFAULT_PLID;
428     }
429 
430     protected boolean hasClassName(Class<?> classObj) {
431         long classNameId = getClassNameId();
432 
433         if (classNameId == PortalUtil.getClassNameId(classObj)) {
434             return true;
435         }
436         else {
437             return false;
438         }
439     }
440 
441     private static Log _log = LogFactoryUtil.getLog(GroupImpl.class);
442 
443     private Group _liveGroup;
444     private Group _stagingGroup;
445     private UnicodeProperties _typeSettingsProperties;
446 
447 }