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