1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.UnicodeProperties;
29  import com.liferay.portal.model.Group;
30  import com.liferay.portal.model.GroupConstants;
31  import com.liferay.portal.model.Layout;
32  import com.liferay.portal.model.LayoutConstants;
33  import com.liferay.portal.model.LayoutSet;
34  import com.liferay.portal.model.Organization;
35  import com.liferay.portal.model.User;
36  import com.liferay.portal.model.UserGroup;
37  import com.liferay.portal.service.GroupLocalServiceUtil;
38  import com.liferay.portal.service.LayoutLocalServiceUtil;
39  import com.liferay.portal.service.LayoutSetLocalServiceUtil;
40  import com.liferay.portal.service.OrganizationLocalServiceUtil;
41  import com.liferay.portal.service.UserGroupLocalServiceUtil;
42  import com.liferay.portal.service.UserLocalServiceUtil;
43  import com.liferay.portal.theme.ThemeDisplay;
44  import com.liferay.portal.util.PortalUtil;
45  import com.liferay.portal.util.PropsValues;
46  
47  import java.io.IOException;
48  
49  import java.util.List;
50  
51  /**
52   * <a href="GroupImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   */
56  public class GroupImpl extends GroupModelImpl implements Group {
57  
58      public GroupImpl() {
59      }
60  
61      public boolean isCommunity() {
62          return hasClassName(Group.class);
63      }
64  
65      public boolean isLayout() {
66          return hasClassName(Layout.class);
67      }
68  
69      public boolean isOrganization() {
70          return hasClassName(Organization.class);
71      }
72  
73      public boolean isUser() {
74          return hasClassName(User.class);
75      }
76  
77      public boolean isUserGroup() {
78          return hasClassName(UserGroup.class);
79      }
80  
81      public Group getLiveGroup() {
82          if (!isStagingGroup()) {
83              return null;
84          }
85  
86          try {
87              if (_liveGroup == null) {
88                  _liveGroup = GroupLocalServiceUtil.getGroup(
89                      getLiveGroupId());
90              }
91  
92              return _liveGroup;
93          }
94          catch (Exception e) {
95              _log.error("Error getting live group for " + getLiveGroupId(), e);
96  
97              return null;
98          }
99      }
100 
101     public Group getStagingGroup() {
102         if (isStagingGroup()) {
103             return null;
104         }
105 
106         try {
107             if (_stagingGroup == null) {
108                 _stagingGroup =
109                     GroupLocalServiceUtil.getStagingGroup(getGroupId());
110             }
111 
112             return _stagingGroup;
113         }
114         catch (Exception e) {
115             _log.error("Error getting staging group for " + getGroupId(), e);
116 
117             return null;
118         }
119     }
120 
121     public boolean hasStagingGroup() {
122         if (isStagingGroup()) {
123             return false;
124         }
125 
126         if (_stagingGroup != null) {
127             return true;
128         }
129 
130         try {
131             return GroupLocalServiceUtil.hasStagingGroup(getGroupId());
132         }
133         catch (Exception e) {
134             return false;
135         }
136     }
137 
138     public boolean isStagingGroup() {
139         if (getLiveGroupId() == GroupConstants.DEFAULT_LIVE_GROUP_ID) {
140             return false;
141         }
142         else {
143             return true;
144         }
145     }
146 
147     public String getDescriptiveName() {
148         String name = getName();
149 
150         try {
151             if (isOrganization()) {
152                 long organizationId = getClassPK();
153 
154                 Organization organization =
155                     OrganizationLocalServiceUtil.getOrganization(
156                         organizationId);
157 
158                 name = organization.getName();
159             }
160             else if (isUser()) {
161                 long userId = getClassPK();
162 
163                 User user = UserLocalServiceUtil.getUserById(userId);
164 
165                 name = user.getFullName();
166             }
167             else if (isUserGroup()) {
168                 long userGroupId = getClassPK();
169 
170                 UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
171                     userGroupId);
172 
173                 name = userGroup.getName();
174             }
175         }
176         catch (Exception e) {
177             _log.error("Error getting descriptive name for " + getGroupId(), e);
178         }
179 
180         return name;
181     }
182 
183     public String getTypeLabel() {
184         return GroupConstants.getTypeLabel(getType());
185     }
186 
187     public String getTypeSettings() {
188         if (_typeSettingsProperties == null) {
189             return super.getTypeSettings();
190         }
191         else {
192             return _typeSettingsProperties.toString();
193         }
194     }
195 
196     public void setTypeSettings(String typeSettings) {
197         _typeSettingsProperties = null;
198 
199         super.setTypeSettings(typeSettings);
200     }
201 
202     public UnicodeProperties getTypeSettingsProperties() {
203         if (_typeSettingsProperties == null) {
204             _typeSettingsProperties = new UnicodeProperties(true);
205 
206             try {
207                 _typeSettingsProperties.load(super.getTypeSettings());
208             }
209             catch (IOException ioe) {
210                 _log.error(ioe, ioe);
211             }
212         }
213 
214         return _typeSettingsProperties;
215     }
216 
217     public void setTypeSettingsProperties(
218         UnicodeProperties typeSettingsProperties) {
219 
220         _typeSettingsProperties = typeSettingsProperties;
221 
222         super.setTypeSettings(_typeSettingsProperties.toString());
223     }
224 
225     public String getTypeSettingsProperty(String key) {
226         UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
227 
228         return typeSettingsProperties.getProperty(key);
229     }
230 
231     public String getPathFriendlyURL(
232         boolean privateLayout, ThemeDisplay themeDisplay) {
233 
234         if (privateLayout) {
235             if (isUser()) {
236                 return themeDisplay.getPathFriendlyURLPrivateUser();
237             }
238             else {
239                 return themeDisplay.getPathFriendlyURLPrivateGroup();
240             }
241         }
242         else {
243             return themeDisplay.getPathFriendlyURLPublic();
244         }
245     }
246 
247     public long getDefaultPrivatePlid() {
248         return getDefaultPlid(true);
249     }
250 
251     public int getPrivateLayoutsPageCount() {
252         try {
253             LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
254                 getGroupId(), true);
255 
256             return layoutSet.getPageCount();
257         }
258         catch (Exception e) {
259             _log.error(e);
260         }
261 
262         return 0;
263     }
264 
265     public boolean hasPrivateLayouts() {
266         if (getPrivateLayoutsPageCount() > 0) {
267             return true;
268         }
269         else {
270             return false;
271         }
272     }
273 
274     public long getDefaultPublicPlid() {
275         return getDefaultPlid(false);
276     }
277 
278     public int getPublicLayoutsPageCount() {
279         try {
280             LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
281                 getGroupId(), false);
282 
283             return layoutSet.getPageCount();
284         }
285         catch (Exception e) {
286             _log.error(e);
287         }
288 
289         return 0;
290     }
291 
292     public boolean hasPublicLayouts() {
293         if (getPublicLayoutsPageCount() > 0) {
294             return true;
295         }
296         else {
297             return false;
298         }
299     }
300 
301     public boolean isWorkflowEnabled() {
302         return GetterUtil.getBoolean(
303             getTypeSettingsProperty("workflowEnabled"));
304     }
305 
306     public int getWorkflowStages() {
307         return GetterUtil.getInteger(
308             getTypeSettingsProperty("workflowStages"),
309             PropsValues.TASKS_DEFAULT_STAGES);
310     }
311 
312     public String getWorkflowRoleNames() {
313         return GetterUtil.getString(
314             getTypeSettingsProperty("workflowRoleNames"),
315             PropsValues.TASKS_DEFAULT_ROLE_NAMES);
316     }
317 
318     protected long getDefaultPlid(boolean privateLayout) {
319         try {
320             List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
321                 getGroupId(), privateLayout,
322                 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, 0, 1);
323 
324             if (layouts.size() > 0) {
325                 Layout layout = layouts.get(0);
326 
327                 return layout.getPlid();
328             }
329         }
330         catch (Exception e) {
331             if (_log.isWarnEnabled()) {
332                 _log.warn(e.getMessage());
333             }
334         }
335 
336         return LayoutConstants.DEFAULT_PLID;
337     }
338 
339     protected boolean hasClassName(Class<?> classObj) {
340         long classNameId = getClassNameId();
341 
342         if (classNameId == PortalUtil.getClassNameId(classObj)) {
343             return true;
344         }
345         else {
346             return false;
347         }
348     }
349 
350     private static Log _log = LogFactoryUtil.getLog(GroupImpl.class);
351 
352     private Group _stagingGroup;
353     private Group _liveGroup;
354     private UnicodeProperties _typeSettingsProperties = null;
355 
356 }