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