001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/security/CmsOrganizationalUnit.java,v $
003: * Date : $Date: 2008-02-27 12:05:29 $
004: * Version: $Revision: 1.9 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.security;
033:
034: import org.opencms.main.CmsIllegalArgumentException;
035: import org.opencms.util.CmsMacroResolver;
036: import org.opencms.util.CmsStringUtil;
037: import org.opencms.util.CmsUUID;
038:
039: import java.util.Locale;
040:
041: /**
042: * An organizational unit in OpenCms.<p>
043: *
044: * Be sure the flags does not conflict with the flags defined in {@link org.opencms.file.CmsResource}.<p>
045: *
046: * @author Michael Moossen
047: *
048: * @version $Revision: 1.9 $
049: *
050: * @since 6.5.6
051: */
052: public class CmsOrganizationalUnit {
053:
054: /** The flag constant to hide the organizational units from the login form. */
055: public static final int FLAG_HIDE_LOGIN = 1;
056:
057: /** The flag constant to mark the organizational units as containing only webusers. */
058: public static final int FLAG_WEBUSERS = 8;
059:
060: /** The character used to separate each level in a fully qualified name. */
061: public static final String SEPARATOR = "/";
062:
063: /** The description of this organizational unit. */
064: private String m_description;
065:
066: /** The flags of this organizational unit. */
067: private int m_flags;
068:
069: /** The unique id of this organizational unit. */
070: private final CmsUUID m_id;
071:
072: /** The fully qualified name of this organizational unit. */
073: private final String m_name;
074:
075: /** The id of the related default project. */
076: private final CmsUUID m_projectId;
077:
078: /**
079: * Creates a new OpenCms organizational unit principal.
080: *
081: * @param id the unique id of the organizational unit
082: * @param fqn the fully qualified name of the this organizational unit (should end with slash)
083: * @param description the description of the organizational unit
084: * @param flags the flags of the organizational unit
085: * @param projectId the id of the related default project
086: */
087: public CmsOrganizationalUnit(CmsUUID id, String fqn,
088: String description, int flags, CmsUUID projectId) {
089:
090: m_id = id;
091: m_name = fqn;
092: m_description = description;
093: m_flags = flags;
094: m_projectId = projectId;
095: }
096:
097: /**
098: * Returns the parent fully qualified name.<p>
099: *
100: * This is <code>null</code> for the root ou, and
101: * the empty string for first level ous.<p>
102: *
103: * @param fqn the fully qualified name to get the parent from
104: *
105: * @return the parent fully qualified name
106: */
107: public static final String getParentFqn(String fqn) {
108:
109: if (CmsStringUtil.isEmptyOrWhitespaceOnly(fqn)) {
110: // in case of the root ou
111: return null;
112: }
113: int pos;
114: if (fqn.endsWith(CmsOrganizationalUnit.SEPARATOR)) {
115: pos = fqn.substring(0, fqn.length() - 1).lastIndexOf(
116: CmsOrganizationalUnit.SEPARATOR);
117: } else {
118: pos = fqn.lastIndexOf(CmsOrganizationalUnit.SEPARATOR);
119: }
120: if (pos <= 0) {
121: // in case of simple names assume root ou
122: return "";
123: }
124: return fqn.substring(0, pos + 1);
125: }
126:
127: /**
128: * Returns the last name of the given fully qualified name.<p>
129: *
130: * @param fqn the fully qualified name to get the last name from
131: *
132: * @return the last name of the given fully qualified name
133: */
134: public static final String getSimpleName(String fqn) {
135:
136: String parentFqn = getParentFqn(fqn);
137: if (parentFqn != null) {
138: fqn = fqn.substring(parentFqn.length());
139: }
140: if ((fqn != null)
141: && fqn.startsWith(CmsOrganizationalUnit.SEPARATOR)) {
142: fqn = fqn.substring(CmsOrganizationalUnit.SEPARATOR
143: .length());
144: }
145: return fqn;
146: }
147:
148: /**
149: * Returns the given fully qualified name without leading separator.<p>
150: *
151: * @param fqn the fully qualified name to fix
152: *
153: * @return the given fully qualified name without leading separator
154: */
155: public static String removeLeadingSeparator(String fqn) {
156:
157: if ((fqn != null)
158: && fqn.startsWith(CmsOrganizationalUnit.SEPARATOR)) {
159: return fqn.substring(1);
160: }
161: return fqn;
162: }
163:
164: /**
165: * Adds the given flag to the flags for this organizational unit.<p>
166: *
167: * @param flag the flag to add
168: */
169: public void addFlag(int flag) {
170:
171: m_flags = (m_flags ^ flag);
172: }
173:
174: /**
175: * @see java.lang.Object#clone()
176: */
177: public Object clone() {
178:
179: return new CmsOrganizationalUnit(m_id, m_name, m_description,
180: m_flags, m_projectId);
181: }
182:
183: /**
184: * @see java.lang.Object#equals(java.lang.Object)
185: */
186: public boolean equals(Object obj) {
187:
188: if (obj == this ) {
189: return true;
190: }
191: if (obj instanceof CmsOrganizationalUnit) {
192: if (m_id != null) {
193: return m_id.equals(((CmsOrganizationalUnit) obj)
194: .getId());
195: }
196: }
197: return false;
198: }
199:
200: /**
201: * Returns the description of this organizational unit.<p>
202: *
203: * This could return also just a macro, so please use the
204: * {@link #getDescription(Locale)} method.<p>
205: *
206: * @return the description of this organizational unit
207: */
208: public String getDescription() {
209:
210: return m_description;
211: }
212:
213: /**
214: * Returns the description of this organizational unit.<p>
215: *
216: * @param locale the locale
217: *
218: * @return the description of this organizational unit
219: */
220: public String getDescription(Locale locale) {
221:
222: CmsMacroResolver macroResolver = new CmsMacroResolver();
223: macroResolver.setMessages(org.opencms.db.generic.Messages.get()
224: .getBundle(locale));
225: return macroResolver.resolveMacros(m_description);
226: }
227:
228: /**
229: * Returns the display name for this organizational unit.<p>
230: *
231: * @param locale the locale
232: *
233: * @return the display name for this organizational unit
234: */
235: public String getDisplayName(Locale locale) {
236:
237: if (getParentFqn() == null) {
238: // for the root ou
239: return getDescription(locale);
240: }
241: return Messages.get().getBundle(locale).key(
242: Messages.GUI_ORGUNIT_DISPLAY_NAME_2,
243: getDescription(locale),
244: CmsOrganizationalUnit.SEPARATOR + getName());
245: }
246:
247: /**
248: * Returns the flags of this organizational unit.<p>
249: *
250: * The organizational unit flags are used to store special information about the
251: * organizational unit state encoded bitwise. Usually the flags int value should not
252: * be directly accessed. <p>
253: *
254: * @return the flags of this organizational unit
255: */
256: public int getFlags() {
257:
258: return m_flags;
259: }
260:
261: /**
262: * Returns the id of this organizational unit.
263: *
264: * @return the id of this organizational unit.
265: */
266: public CmsUUID getId() {
267:
268: return m_id;
269: }
270:
271: /**
272: * Returns the fully qualified name of this organizational unit.<p>
273: *
274: * @return the fully qualified name of this organizational unit
275: */
276: public String getName() {
277:
278: return m_name;
279: }
280:
281: /**
282: * Returns the full qualified name of the parent organizational unit of this organizational unit.<p>
283: *
284: * This is <code>null</code> for the root ou, and the empty string for first level ous.<p>
285: *
286: * @return the full qualified name of the parent organizational unit of this organizational unit
287: */
288: public String getParentFqn() {
289:
290: return getParentFqn(m_name);
291: }
292:
293: /**
294: * Returns the id of the related default project.<p>
295: *
296: * @return the id of the related default project
297: */
298: public CmsUUID getProjectId() {
299:
300: return m_projectId;
301: }
302:
303: /**
304: * Returns the simple name of this organizational unit.
305: *
306: * @return the simple name of this organizational unit.
307: */
308: public String getSimpleName() {
309:
310: return getSimpleName(m_name);
311: }
312:
313: /**
314: * Checks if this organizational unit has the given flag set.<p>
315: *
316: * @param flag the flag to check
317: *
318: * @return <code>true</code> if this organizational unit has the given flag set
319: */
320: public boolean hasFlag(int flag) {
321:
322: return (m_flags & flag) == flag;
323: }
324:
325: /**
326: * Checks if this organizational unit has the "hide from login form" flag set.<p>
327: *
328: * @return <code>true</code> if this organizational unit has the "hide from login form" flag set
329: */
330: public boolean hasFlagHideLogin() {
331:
332: return hasFlag(FLAG_HIDE_LOGIN);
333: }
334:
335: /**
336: * Checks if this organizational unit has the "webusers" flag set.<p>
337: *
338: * @return <code>true</code> if this organizational unit has the "webusers" flag set
339: */
340: public boolean hasFlagWebuser() {
341:
342: return hasFlag(FLAG_WEBUSERS);
343: }
344:
345: /**
346: * @see java.lang.Object#hashCode()
347: */
348: public int hashCode() {
349:
350: if (m_id != null) {
351: return m_id.hashCode();
352: }
353: return CmsUUID.getNullUUID().hashCode();
354: }
355:
356: /**
357: * Sets the description of this organizational unit.<p>
358: *
359: * @param description the principal organizational unit to set
360: */
361: public void setDescription(String description) {
362:
363: if (CmsStringUtil.isEmptyOrWhitespaceOnly(description)) {
364: throw new CmsIllegalArgumentException(
365: org.opencms.db.Messages
366: .get()
367: .container(
368: org.opencms.db.Messages.ERR_BAD_OU_DESCRIPTION_EMPTY_0));
369: }
370:
371: m_description = description;
372: }
373:
374: /**
375: * Sets the "hide from login form" flag.<p>
376: */
377: public void setFlagHideLogin() {
378:
379: addFlag(FLAG_HIDE_LOGIN);
380: }
381:
382: /**
383: * Sets this organizational unit flags to the specified value.<p>
384: *
385: * The organizational unit flags are used to store special information about the
386: * organizational units state encoded bitwise. Usually the flags int value should not
387: * be directly accessed. <p>
388: *
389: * @param value the value to set this organizational units flags to
390: */
391: public void setFlags(int value) {
392:
393: m_flags = value;
394: }
395:
396: /**
397: * Sets the "webusers" flag.<p>
398: */
399: public void setFlagWebusers() {
400:
401: addFlag(FLAG_WEBUSERS);
402: }
403:
404: /**
405: * @see java.lang.Object#toString()
406: */
407: public String toString() {
408:
409: StringBuffer result = new StringBuffer();
410: result.append("[Organizational Unit]");
411: result.append(" fqn:");
412: result.append(getName());
413: result.append(" id:");
414: result.append(m_id);
415: result.append(" description:");
416: result.append(m_description);
417: return result.toString();
418: }
419: }
|