1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.model.impl;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.GetterUtil;
25  import com.liferay.portal.kernel.util.UnicodeProperties;
26  import com.liferay.portal.model.Group;
27  import com.liferay.portal.model.GroupConstants;
28  import com.liferay.portal.model.Layout;
29  import com.liferay.portal.model.LayoutConstants;
30  import com.liferay.portal.model.LayoutSet;
31  import com.liferay.portal.model.Organization;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.model.UserGroup;
34  import com.liferay.portal.service.GroupLocalServiceUtil;
35  import com.liferay.portal.service.LayoutLocalServiceUtil;
36  import com.liferay.portal.service.LayoutSetLocalServiceUtil;
37  import com.liferay.portal.service.OrganizationLocalServiceUtil;
38  import com.liferay.portal.service.UserGroupLocalServiceUtil;
39  import com.liferay.portal.service.UserLocalServiceUtil;
40  import com.liferay.portal.theme.ThemeDisplay;
41  import com.liferay.portal.util.PortalUtil;
42  import com.liferay.portal.util.PropsValues;
43  
44  import java.io.IOException;
45  
46  import java.util.List;
47  
48  /**
49   * <a href="GroupImpl.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   *
53   */
54  public class GroupImpl extends GroupModelImpl implements Group {
55  
56      public GroupImpl() {
57      }
58  
59      public boolean isCommunity() {
60          long classNameId = getClassNameId();
61          long classPK = getClassPK();
62  
63          if ((classNameId <= 0) && (classPK <= 0)) {
64              return true;
65          }
66          else {
67              return false;
68          }
69      }
70  
71      public boolean isOrganization() {
72          long classNameId = getClassNameId();
73          long classPK = getClassPK();
74  
75          if ((classNameId > 0) && (classPK > 0)) {
76              long organizationClassNameId = PortalUtil.getClassNameId(
77                  Organization.class);
78  
79              if (classNameId == organizationClassNameId) {
80                  return true;
81              }
82          }
83  
84          return false;
85      }
86  
87      public boolean isUser() {
88          long classNameId = getClassNameId();
89          long classPK = getClassPK();
90  
91          if ((classNameId > 0) && (classPK > 0)) {
92              long userClassNameId = PortalUtil.getClassNameId(User.class);
93  
94              if (classNameId == userClassNameId) {
95                  return true;
96              }
97          }
98  
99          return false;
100     }
101 
102     public boolean isUserGroup() {
103         long classNameId = getClassNameId();
104         long classPK = getClassPK();
105 
106         if ((classNameId > 0) && (classPK > 0)) {
107             long userGroupClassNameId = PortalUtil.getClassNameId(
108                 UserGroup.class);
109 
110             if (classNameId == userGroupClassNameId) {
111                 return true;
112             }
113         }
114 
115         return false;
116     }
117 
118     public Group getLiveGroup() {
119         if (!isStagingGroup()) {
120             return null;
121         }
122 
123         try {
124             if (_liveGroup == null) {
125                 _liveGroup = GroupLocalServiceUtil.getGroup(
126                     getLiveGroupId());
127             }
128 
129             return _liveGroup;
130         }
131         catch (Exception e) {
132             _log.error("Error getting live group for " + getLiveGroupId(), e);
133 
134             return null;
135         }
136     }
137 
138     public Group getStagingGroup() {
139         if (isStagingGroup()) {
140             return null;
141         }
142 
143         try {
144             if (_stagingGroup == null) {
145                 _stagingGroup =
146                     GroupLocalServiceUtil.getStagingGroup(getGroupId());
147             }
148 
149             return _stagingGroup;
150         }
151         catch (Exception e) {
152             _log.error("Error getting staging group for " + getGroupId(), e);
153 
154             return null;
155         }
156     }
157 
158     public boolean hasStagingGroup() {
159         if (isStagingGroup()) {
160             return false;
161         }
162 
163         if (_stagingGroup != null) {
164             return true;
165         }
166 
167         try {
168             return GroupLocalServiceUtil.hasStagingGroup(getGroupId());
169         }
170         catch (Exception e) {
171             return false;
172         }
173     }
174 
175     public boolean isStagingGroup() {
176         if (getLiveGroupId() == GroupConstants.DEFAULT_LIVE_GROUP_ID) {
177             return false;
178         }
179         else {
180             return true;
181         }
182     }
183 
184     public String getDescriptiveName() {
185         String name = getName();
186 
187         try {
188             if (isOrganization()) {
189                 long organizationId = getClassPK();
190 
191                 Organization organization =
192                     OrganizationLocalServiceUtil.getOrganization(
193                         organizationId);
194 
195                 name = organization.getName();
196             }
197             else if (isUser()) {
198                 long userId = getClassPK();
199 
200                 User user = UserLocalServiceUtil.getUserById(userId);
201 
202                 name = user.getFullName();
203             }
204             else if (isUserGroup()) {
205                 long userGroupId = getClassPK();
206 
207                 UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
208                     userGroupId);
209 
210                 name = userGroup.getName();
211             }
212         }
213         catch (Exception e) {
214             _log.error("Error getting descriptive name for " + getGroupId(), e);
215         }
216 
217         return name;
218     }
219 
220     public String getTypeLabel() {
221         return GroupConstants.getTypeLabel(getType());
222     }
223 
224     public String getTypeSettings() {
225         if (_typeSettingsProperties == null) {
226             return super.getTypeSettings();
227         }
228         else {
229             return _typeSettingsProperties.toString();
230         }
231     }
232 
233     public void setTypeSettings(String typeSettings) {
234         _typeSettingsProperties = null;
235 
236         super.setTypeSettings(typeSettings);
237     }
238 
239     public UnicodeProperties getTypeSettingsProperties() {
240         if (_typeSettingsProperties == null) {
241             _typeSettingsProperties = new UnicodeProperties(true);
242 
243             try {
244                 _typeSettingsProperties.load(super.getTypeSettings());
245             }
246             catch (IOException ioe) {
247                 _log.error(ioe);
248             }
249         }
250 
251         return _typeSettingsProperties;
252     }
253 
254     public void setTypeSettingsProperties(
255         UnicodeProperties typeSettingsProperties) {
256 
257         _typeSettingsProperties = typeSettingsProperties;
258 
259         super.setTypeSettings(_typeSettingsProperties.toString());
260     }
261 
262     public String getTypeSettingsProperty(String key) {
263         UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
264 
265         return typeSettingsProperties.getProperty(key);
266     }
267 
268     public String getPathFriendlyURL(
269         boolean privateLayout, ThemeDisplay themeDisplay) {
270 
271         if (privateLayout) {
272             if (isUser()) {
273                 return themeDisplay.getPathFriendlyURLPrivateUser();
274             }
275             else {
276                 return themeDisplay.getPathFriendlyURLPrivateGroup();
277             }
278         }
279         else {
280             return themeDisplay.getPathFriendlyURLPublic();
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     public boolean isWorkflowEnabled() {
339         return GetterUtil.getBoolean(
340             getTypeSettingsProperty("workflowEnabled"));
341     }
342 
343     public int getWorkflowStages() {
344         return GetterUtil.getInteger(
345             getTypeSettingsProperty("workflowStages"),
346             PropsValues.TASKS_DEFAULT_STAGES);
347     }
348 
349     public String getWorkflowRoleNames() {
350         return GetterUtil.getString(
351             getTypeSettingsProperty("workflowRoleNames"),
352             PropsValues.TASKS_DEFAULT_ROLE_NAMES);
353     }
354 
355     protected long getDefaultPlid(boolean privateLayout) {
356         try {
357             List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
358                 getGroupId(), privateLayout,
359                 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, 0, 1);
360 
361             if (layouts.size() > 0) {
362                 Layout layout = layouts.get(0);
363 
364                 return layout.getPlid();
365             }
366         }
367         catch (Exception e) {
368             if (_log.isWarnEnabled()) {
369                 _log.warn(e.getMessage());
370             }
371         }
372 
373         return LayoutConstants.DEFAULT_PLID;
374     }
375 
376     private static Log _log = LogFactoryUtil.getLog(GroupImpl.class);
377 
378     private Group _stagingGroup;
379     private Group _liveGroup;
380     private UnicodeProperties _typeSettingsProperties = null;
381 
382 }