001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.model.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.staging.StagingConstants;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.LocaleUtil;
024    import com.liferay.portal.kernel.util.UnicodeProperties;
025    import com.liferay.portal.model.Company;
026    import com.liferay.portal.model.Group;
027    import com.liferay.portal.model.GroupConstants;
028    import com.liferay.portal.model.Layout;
029    import com.liferay.portal.model.LayoutConstants;
030    import com.liferay.portal.model.LayoutPrototype;
031    import com.liferay.portal.model.LayoutSet;
032    import com.liferay.portal.model.LayoutSetPrototype;
033    import com.liferay.portal.model.Organization;
034    import com.liferay.portal.model.User;
035    import com.liferay.portal.model.UserGroup;
036    import com.liferay.portal.service.CompanyLocalServiceUtil;
037    import com.liferay.portal.service.GroupLocalServiceUtil;
038    import com.liferay.portal.service.LayoutLocalServiceUtil;
039    import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
040    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
041    import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
042    import com.liferay.portal.service.OrganizationLocalServiceUtil;
043    import com.liferay.portal.service.UserGroupLocalServiceUtil;
044    import com.liferay.portal.service.UserLocalServiceUtil;
045    import com.liferay.portal.theme.ThemeDisplay;
046    import com.liferay.portal.util.PortalUtil;
047    import com.liferay.portal.util.PropsValues;
048    
049    import java.io.IOException;
050    
051    import java.util.List;
052    
053    /**
054     * @author Brian Wing Shun Chan
055     */
056    public class GroupImpl extends GroupModelImpl implements Group {
057    
058            public GroupImpl() {
059            }
060    
061            public long getDefaultPrivatePlid() {
062                    return getDefaultPlid(true);
063            }
064    
065            public long getDefaultPublicPlid() {
066                    return getDefaultPlid(false);
067            }
068    
069            public String getDescriptiveName() throws PortalException, SystemException {
070                    String name = getName();
071    
072                    if (isCompany()) {
073                            name = "global";
074                    }
075                    else if (isLayout()) {
076                            Layout layout = LayoutLocalServiceUtil.getLayout(getClassPK());
077    
078                            name = layout.getName(LocaleUtil.getDefault());
079                    }
080                    else if (isLayoutPrototype()) {
081                            LayoutPrototype layoutPrototype =
082                                    LayoutPrototypeLocalServiceUtil.getLayoutPrototype(
083                                            getClassPK());
084    
085                            name = layoutPrototype.getName(LocaleUtil.getDefault());
086                    }
087                    else if (isLayoutSetPrototype()) {
088                            LayoutSetPrototype layoutSetPrototype =
089                                    LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototype(
090                                            getClassPK());
091    
092                            name = layoutSetPrototype.getName(LocaleUtil.getDefault());
093                    }
094                    else if (isOrganization()) {
095                            long organizationId = getClassPK();
096    
097                            Organization organization =
098                                    OrganizationLocalServiceUtil.getOrganization(organizationId);
099    
100                            name = organization.getName();
101                    }
102                    else if (isUser()) {
103                            long userId = getClassPK();
104    
105                            User user = UserLocalServiceUtil.getUserById(userId);
106    
107                            name = user.getFullName();
108                    }
109                    else if (isUserGroup()) {
110                            long userGroupId = getClassPK();
111    
112                            UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
113                                    userGroupId);
114    
115                            name = userGroup.getName();
116                    }
117                    else if (name.equals(GroupConstants.GUEST)) {
118                            Company company = CompanyLocalServiceUtil.getCompany(
119                                    getCompanyId());
120    
121                            name = company.getAccount().getName();
122                    }
123    
124                    return name;
125            }
126    
127            public Group getLiveGroup() {
128                    if (!isStagingGroup()) {
129                            return null;
130                    }
131    
132                    try {
133                            if (_liveGroup == null) {
134                                    _liveGroup = GroupLocalServiceUtil.getGroup(
135                                            getLiveGroupId());
136                            }
137    
138                            return _liveGroup;
139                    }
140                    catch (Exception e) {
141                            _log.error("Error getting live group for " + getLiveGroupId(), e);
142    
143                            return null;
144                    }
145            }
146    
147            public String getPathFriendlyURL(
148                    boolean privateLayout, ThemeDisplay themeDisplay) {
149    
150                    if (privateLayout) {
151                            if (isUser()) {
152                                    return themeDisplay.getPathFriendlyURLPrivateUser();
153                            }
154                            else {
155                                    return themeDisplay.getPathFriendlyURLPrivateGroup();
156                            }
157                    }
158                    else {
159                            return themeDisplay.getPathFriendlyURLPublic();
160                    }
161            }
162    
163            public LayoutSet getPrivateLayoutSet() {
164                    LayoutSet layoutSet = null;
165    
166                    try {
167                            layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
168                                    getGroupId(), true);
169                    }
170                    catch (Exception e) {
171                            _log.error(e);
172                    }
173    
174                    return layoutSet;
175            }
176    
177            public int getPrivateLayoutsPageCount() {
178                    try {
179                            LayoutSet layoutSet = getPrivateLayoutSet();
180    
181                            return layoutSet.getPageCount();
182                    }
183                    catch (Exception e) {
184                            _log.error(e);
185                    }
186    
187                    return 0;
188            }
189    
190            public LayoutSet getPublicLayoutSet() {
191                    LayoutSet layoutSet = null;
192    
193                    try {
194                            layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
195                                    getGroupId(), false);
196                    }
197                    catch (Exception e) {
198                            _log.error(e);
199                    }
200    
201                    return layoutSet;
202            }
203    
204            public int getPublicLayoutsPageCount() {
205                    try {
206                            LayoutSet layoutSet = getPublicLayoutSet();
207    
208                            return layoutSet.getPageCount();
209                    }
210                    catch (Exception e) {
211                            _log.error(e);
212                    }
213    
214                    return 0;
215            }
216    
217            public Group getStagingGroup() {
218                    if (isStagingGroup()) {
219                            return null;
220                    }
221    
222                    try {
223                            if (_stagingGroup == null) {
224                                    _stagingGroup =
225                                            GroupLocalServiceUtil.getStagingGroup(getGroupId());
226                            }
227    
228                            return _stagingGroup;
229                    }
230                    catch (Exception e) {
231                            _log.error("Error getting staging group for " + getGroupId(), e);
232    
233                            return null;
234                    }
235            }
236    
237            public String getTypeLabel() {
238                    return GroupConstants.getTypeLabel(getType());
239            }
240    
241            public String getTypeSettings() {
242                    if (_typeSettingsProperties == null) {
243                            return super.getTypeSettings();
244                    }
245                    else {
246                            return _typeSettingsProperties.toString();
247                    }
248            }
249    
250            public UnicodeProperties getTypeSettingsProperties() {
251                    if (_typeSettingsProperties == null) {
252                            _typeSettingsProperties = new UnicodeProperties(true);
253    
254                            try {
255                                    _typeSettingsProperties.load(super.getTypeSettings());
256                            }
257                            catch (IOException ioe) {
258                                    _log.error(ioe, ioe);
259                            }
260                    }
261    
262                    return _typeSettingsProperties;
263            }
264    
265            public String getTypeSettingsProperty(String key) {
266                    UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
267    
268                    return typeSettingsProperties.getProperty(key);
269            }
270    
271            public String getWorkflowRoleNames() {
272                    return GetterUtil.getString(
273                            getTypeSettingsProperty("workflowRoleNames"),
274                            PropsValues.TASKS_DEFAULT_ROLE_NAMES);
275            }
276    
277            public int getWorkflowStages() {
278                    return GetterUtil.getInteger(
279                            getTypeSettingsProperty("workflowStages"),
280                            PropsValues.TASKS_DEFAULT_STAGES);
281            }
282    
283            public boolean hasPrivateLayouts() {
284                    if (getPrivateLayoutsPageCount() > 0) {
285                            return true;
286                    }
287                    else {
288                            return false;
289                    }
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 hasStagingGroup() {
302                    if (isStagingGroup()) {
303                            return false;
304                    }
305    
306                    if (_stagingGroup != null) {
307                            return true;
308                    }
309    
310                    try {
311                            return GroupLocalServiceUtil.hasStagingGroup(getGroupId());
312                    }
313                    catch (Exception e) {
314                            return false;
315                    }
316            }
317    
318            public boolean isCommunity() {
319                    return hasClassName(Group.class);
320            }
321    
322            public boolean isCompany() {
323                    return hasClassName(Company.class);
324            }
325    
326            public boolean isControlPanel() {
327                    if (getName().equals(GroupConstants.CONTROL_PANEL)) {
328                            return true;
329                    }
330                    else {
331                            return false;
332                    }
333            }
334    
335            public boolean isLayout() {
336                    return hasClassName(Layout.class);
337            }
338    
339            public boolean isLayoutPrototype() {
340                    return hasClassName(LayoutPrototype.class);
341            }
342    
343            public boolean isLayoutSetPrototype() {
344                    return hasClassName(LayoutSetPrototype.class);
345            }
346    
347            public boolean isOrganization() {
348                    return hasClassName(Organization.class);
349            }
350    
351            public boolean isStaged() {
352                    return GetterUtil.getBoolean(getTypeSettingsProperty("staged"));
353            }
354    
355            public boolean isStagedPortlet(String portletId) {
356                    return GetterUtil.getBoolean(
357                            getTypeSettingsProperty(
358                                    StagingConstants.STAGED_PORTLET.concat(portletId)),
359                            true);
360            }
361    
362            public boolean isStagedRemotely() {
363                    return GetterUtil.getBoolean(getTypeSettingsProperty("stagedRemotely"));
364            }
365    
366            public boolean isStagingGroup() {
367                    if (getLiveGroupId() == GroupConstants.DEFAULT_LIVE_GROUP_ID) {
368                            return false;
369                    }
370                    else {
371                            return true;
372                    }
373            }
374    
375            public boolean isUser() {
376                    return hasClassName(User.class);
377            }
378    
379            public boolean isUserGroup() {
380                    return hasClassName(UserGroup.class);
381            }
382    
383            public boolean isWorkflowEnabled() {
384                    return GetterUtil.getBoolean(
385                            getTypeSettingsProperty("workflowEnabled"));
386            }
387    
388            public void setTypeSettings(String typeSettings) {
389                    _typeSettingsProperties = null;
390    
391                    super.setTypeSettings(typeSettings);
392            }
393    
394            public void setTypeSettingsProperties(
395                    UnicodeProperties typeSettingsProperties) {
396    
397                    _typeSettingsProperties = typeSettingsProperties;
398    
399                    super.setTypeSettings(_typeSettingsProperties.toString());
400            }
401    
402            protected long getDefaultPlid(boolean privateLayout) {
403                    try {
404                            List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
405                                    getGroupId(), privateLayout,
406                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, 0, 1);
407    
408                            if (layouts.size() > 0) {
409                                    Layout layout = layouts.get(0);
410    
411                                    return layout.getPlid();
412                            }
413                    }
414                    catch (Exception e) {
415                            if (_log.isWarnEnabled()) {
416                                    _log.warn(e.getMessage());
417                            }
418                    }
419    
420                    return LayoutConstants.DEFAULT_PLID;
421            }
422    
423            protected boolean hasClassName(Class<?> classObj) {
424                    long classNameId = getClassNameId();
425    
426                    if (classNameId == PortalUtil.getClassNameId(classObj)) {
427                            return true;
428                    }
429                    else {
430                            return false;
431                    }
432            }
433    
434            private static Log _log = LogFactoryUtil.getLog(GroupImpl.class);
435    
436            private Group _liveGroup;
437            private Group _stagingGroup;
438            private UnicodeProperties _typeSettingsProperties;
439    
440    }