001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.model.impl;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.NullSafeProperties;
026: import com.liferay.portal.kernel.util.PropertiesUtil;
027: import com.liferay.portal.kernel.util.StringPool;
028: import com.liferay.portal.model.Group;
029: import com.liferay.portal.model.Layout;
030: import com.liferay.portal.model.LayoutSet;
031: import com.liferay.portal.model.Organization;
032: import com.liferay.portal.model.User;
033: import com.liferay.portal.service.GroupLocalServiceUtil;
034: import com.liferay.portal.service.LayoutLocalServiceUtil;
035: import com.liferay.portal.service.LayoutSetLocalServiceUtil;
036: import com.liferay.portal.service.OrganizationLocalServiceUtil;
037: import com.liferay.portal.service.UserLocalServiceUtil;
038: import com.liferay.portal.theme.ThemeDisplay;
039: import com.liferay.portal.util.GroupNames;
040: import com.liferay.portal.util.PortalUtil;
041:
042: import java.io.IOException;
043:
044: import java.util.List;
045: import java.util.Properties;
046:
047: import org.apache.commons.logging.Log;
048: import org.apache.commons.logging.LogFactory;
049:
050: /**
051: * <a href="GroupImpl.java.html"><b><i>View Source</i></b></a>
052: *
053: * @author Brian Wing Shun Chan
054: *
055: */
056: public class GroupImpl extends GroupModelImpl implements Group {
057:
058: public static final long DEFAULT_PARENT_GROUP_ID = 0;
059:
060: public static final long DEFAULT_LIVE_GROUP_ID = 0;
061:
062: public static final String GUEST = GroupNames.GUEST;
063:
064: public static final String[] SYSTEM_GROUPS = GroupNames.SYSTEM_GROUPS;
065:
066: public static final int TYPE_COMMUNITY_OPEN = 1;
067:
068: public static final String TYPE_COMMUNITY_OPEN_LABEL = "open";
069:
070: public static final int TYPE_COMMUNITY_PRIVATE = 3;
071:
072: public static final String TYPE_COMMUNITY_PRIVATE_LABEL = "private";
073:
074: public static final int TYPE_COMMUNITY_RESTRICTED = 2;
075:
076: public static final String TYPE_COMMUNITY_RESTRICTED_LABEL = "restricted";
077:
078: public static String getTypeLabel(int type) {
079: if (type == TYPE_COMMUNITY_OPEN) {
080: return TYPE_COMMUNITY_OPEN_LABEL;
081: } else if (type == TYPE_COMMUNITY_PRIVATE) {
082: return TYPE_COMMUNITY_PRIVATE_LABEL;
083: } else {
084: return TYPE_COMMUNITY_RESTRICTED_LABEL;
085: }
086: }
087:
088: public GroupImpl() {
089: }
090:
091: public boolean isCommunity() {
092: long classNameId = getClassNameId();
093: long classPK = getClassPK();
094:
095: if ((classNameId <= 0) && (classPK <= 0)) {
096: return true;
097: } else {
098: return false;
099: }
100: }
101:
102: public boolean isOrganization() {
103: long classNameId = getClassNameId();
104: long classPK = getClassPK();
105:
106: if ((classNameId > 0) && (classPK > 0)) {
107: long organizationClassNameId = PortalUtil
108: .getClassNameId(Organization.class);
109:
110: if (classNameId == organizationClassNameId) {
111: return true;
112: }
113: }
114:
115: return false;
116: }
117:
118: public boolean isUser() {
119: long classNameId = getClassNameId();
120: long classPK = getClassPK();
121:
122: if ((classNameId > 0) && (classPK > 0)) {
123: long userClassNameId = PortalUtil
124: .getClassNameId(User.class);
125:
126: if (classNameId == userClassNameId) {
127: return true;
128: }
129: }
130:
131: return false;
132: }
133:
134: public Group getLiveGroup() {
135: if (!isStagingGroup()) {
136: return null;
137: }
138:
139: try {
140: if (_liveGroup == null) {
141: _liveGroup = GroupLocalServiceUtil
142: .getGroup(getLiveGroupId());
143: }
144:
145: return _liveGroup;
146: } catch (Exception e) {
147: _log.error("Error getting live group for "
148: + getLiveGroupId(), e);
149:
150: return null;
151: }
152: }
153:
154: public Group getStagingGroup() {
155: if (isStagingGroup()) {
156: return null;
157: }
158:
159: try {
160: if (_stagingGroup == null) {
161: _stagingGroup = GroupLocalServiceUtil
162: .getStagingGroup(getGroupId());
163: }
164:
165: return _stagingGroup;
166: } catch (Exception e) {
167: _log.error("Error getting staging group for "
168: + getGroupId(), e);
169:
170: return null;
171: }
172: }
173:
174: public boolean hasStagingGroup() {
175: if (isStagingGroup()) {
176: return false;
177: }
178:
179: if (_stagingGroup != null) {
180: return true;
181: } else {
182: try {
183: _stagingGroup = GroupLocalServiceUtil
184: .getStagingGroup(getGroupId());
185:
186: return true;
187: } catch (Exception e) {
188: return false;
189: }
190: }
191: }
192:
193: public boolean isStagingGroup() {
194: if (getLiveGroupId() == DEFAULT_LIVE_GROUP_ID) {
195: return false;
196: } else {
197: return true;
198: }
199: }
200:
201: public String getDescriptiveName() {
202: String name = getName();
203:
204: try {
205: if (isOrganization()) {
206: long organizationId = getClassPK();
207:
208: Organization organization = OrganizationLocalServiceUtil
209: .getOrganization(organizationId);
210:
211: name = organization.getName();
212: } else if (isUser()) {
213: long userId = getClassPK();
214:
215: User user = UserLocalServiceUtil.getUserById(userId);
216:
217: name = user.getFullName();
218: }
219: } catch (Exception e) {
220: _log.error("Error getting descriptive name for "
221: + getGroupId(), e);
222: }
223:
224: return name;
225: }
226:
227: public String getTypeLabel() {
228: return getTypeLabel(getType());
229: }
230:
231: public String getTypeSettings() {
232: if (_typeSettingsProperties == null) {
233: return super .getTypeSettings();
234: } else {
235: return PropertiesUtil.toString(_typeSettingsProperties);
236: }
237: }
238:
239: public void setTypeSettings(String typeSettings) {
240: _typeSettingsProperties = null;
241:
242: super .setTypeSettings(typeSettings);
243: }
244:
245: public Properties getTypeSettingsProperties() {
246: if (_typeSettingsProperties == null) {
247: _typeSettingsProperties = new NullSafeProperties();
248:
249: try {
250: PropertiesUtil.load(_typeSettingsProperties, super
251: .getTypeSettings());
252: } catch (IOException ioe) {
253: _log.error(ioe);
254: }
255: }
256:
257: return _typeSettingsProperties;
258: }
259:
260: public void setTypeSettingsProperties(
261: Properties typeSettingsProperties) {
262: _typeSettingsProperties = typeSettingsProperties;
263:
264: super .setTypeSettings(PropertiesUtil
265: .toString(_typeSettingsProperties));
266: }
267:
268: public String getPathFriendlyURL(boolean privateLayout,
269: ThemeDisplay themeDisplay) {
270:
271: if (privateLayout) {
272: if (isUser()) {
273: return themeDisplay.getPathFriendlyURLPrivateUser();
274: } else {
275: return themeDisplay.getPathFriendlyURLPrivateGroup();
276: }
277: } else {
278: return themeDisplay.getPathFriendlyURLPublic();
279: }
280: }
281:
282: public String getDefaultFriendlyURL(boolean privateLayout)
283: throws PortalException, SystemException {
284:
285: if (isUser()) {
286: long userId = getClassPK();
287:
288: User user = UserLocalServiceUtil.getUserById(userId);
289:
290: return StringPool.SLASH + user.getScreenName();
291: } else {
292: return StringPool.SLASH + String.valueOf(getGroupId());
293: }
294: }
295:
296: public long getDefaultPrivatePlid() {
297: return getDefaultPlid(true);
298: }
299:
300: public int getPrivateLayoutsPageCount() {
301: try {
302: LayoutSet layoutSet = LayoutSetLocalServiceUtil
303: .getLayoutSet(getGroupId(), true);
304:
305: return layoutSet.getPageCount();
306: } catch (Exception e) {
307: _log.error(e);
308: }
309:
310: return 0;
311: }
312:
313: public boolean hasPrivateLayouts() {
314: if (getPrivateLayoutsPageCount() > 0) {
315: return true;
316: } else {
317: return false;
318: }
319: }
320:
321: public long getDefaultPublicPlid() {
322: return getDefaultPlid(false);
323: }
324:
325: public int getPublicLayoutsPageCount() {
326: try {
327: LayoutSet layoutSet = LayoutSetLocalServiceUtil
328: .getLayoutSet(getGroupId(), false);
329:
330: return layoutSet.getPageCount();
331: } catch (Exception e) {
332: _log.error(e);
333: }
334:
335: return 0;
336: }
337:
338: public boolean hasPublicLayouts() {
339: if (getPublicLayoutsPageCount() > 0) {
340: return true;
341: } else {
342: return false;
343: }
344: }
345:
346: protected long getDefaultPlid(boolean privateLayout) {
347: try {
348: List layouts = LayoutLocalServiceUtil.getLayouts(
349: getGroupId(), privateLayout,
350: LayoutImpl.DEFAULT_PARENT_LAYOUT_ID, 0, 1);
351:
352: if (layouts.size() > 0) {
353: Layout layout = (Layout) layouts.get(0);
354:
355: return layout.getPlid();
356: }
357: } catch (Exception e) {
358: if (_log.isWarnEnabled()) {
359: _log.warn(e.getMessage());
360: }
361: }
362:
363: return LayoutImpl.DEFAULT_PLID;
364: }
365:
366: private static Log _log = LogFactory.getLog(GroupImpl.class);
367:
368: private Group _stagingGroup;
369: private Group _liveGroup;
370: private Properties _typeSettingsProperties = null;
371:
372: }
|