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