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             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         return hasClassName(Group.class);
321     }
322 
323     public boolean isCompany() {
324         return hasClassName(Company.class);
325     }
326 
327     public boolean isControlPanel() {
328         if (getName().equals(GroupConstants.CONTROL_PANEL)) {
329             return true;
330         }
331         else {
332             return false;
333         }
334     }
335 
336     public boolean isLayout() {
337         return hasClassName(Layout.class);
338     }
339 
340     public boolean isLayoutPrototype() {
341         return hasClassName(LayoutPrototype.class);
342     }
343 
344     public boolean isLayoutSetPrototype() {
345         return hasClassName(LayoutSetPrototype.class);
346     }
347 
348     public boolean isOrganization() {
349         return hasClassName(Organization.class);
350     }
351 
352     public boolean isStagingGroup() {
353         if (getLiveGroupId() == GroupConstants.DEFAULT_LIVE_GROUP_ID) {
354             return false;
355         }
356         else {
357             return true;
358         }
359     }
360 
361     public boolean isUser() {
362         return hasClassName(User.class);
363     }
364 
365     public boolean isUserGroup() {
366         return hasClassName(UserGroup.class);
367     }
368 
369     public boolean isWorkflowEnabled() {
370         return GetterUtil.getBoolean(
371             getTypeSettingsProperty("workflowEnabled"));
372     }
373 
374     public void setTypeSettings(String typeSettings) {
375         _typeSettingsProperties = null;
376 
377         super.setTypeSettings(typeSettings);
378     }
379 
380     public void setTypeSettingsProperties(
381         UnicodeProperties typeSettingsProperties) {
382 
383         _typeSettingsProperties = typeSettingsProperties;
384 
385         super.setTypeSettings(_typeSettingsProperties.toString());
386     }
387 
388     protected long getDefaultPlid(boolean privateLayout) {
389         try {
390             List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
391                 getGroupId(), privateLayout,
392                 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, 0, 1);
393 
394             if (layouts.size() > 0) {
395                 Layout layout = layouts.get(0);
396 
397                 return layout.getPlid();
398             }
399         }
400         catch (Exception e) {
401             if (_log.isWarnEnabled()) {
402                 _log.warn(e.getMessage());
403             }
404         }
405 
406         return LayoutConstants.DEFAULT_PLID;
407     }
408 
409     protected boolean hasClassName(Class<?> classObj) {
410         long classNameId = getClassNameId();
411 
412         if (classNameId == PortalUtil.getClassNameId(classObj)) {
413             return true;
414         }
415         else {
416             return false;
417         }
418     }
419 
420     private static Log _log = LogFactoryUtil.getLog(GroupImpl.class);
421 
422     private Group _liveGroup;
423     private Group _stagingGroup;
424     private UnicodeProperties _typeSettingsProperties;
425 
426 }