001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/relations/CmsRelation.java,v $
003: * Date : $Date: 2008-02-27 12:05:42 $
004: * Version: $Revision: 1.4 $
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.relations;
033:
034: import org.opencms.file.CmsObject;
035: import org.opencms.file.CmsResource;
036: import org.opencms.file.CmsResourceFilter;
037: import org.opencms.file.CmsVfsResourceNotFoundException;
038: import org.opencms.main.CmsException;
039: import org.opencms.util.CmsUUID;
040:
041: import java.util.Comparator;
042:
043: /**
044: * A relation between two opencms resources.<p>
045: *
046: * @author Michael Moossen
047: *
048: * @version $Revision: 1.4 $
049: *
050: * @since 6.3.0
051: */
052: public class CmsRelation {
053:
054: /**
055: * A comparator for the source & target path plus the relation type of 2 relations.<p>
056: */
057: public static final Comparator COMPARATOR = new Comparator() {
058:
059: /**
060: * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
061: */
062: public int compare(Object o1, Object o2) {
063:
064: if ((o1 == o2) || !(o1 instanceof CmsRelation)
065: || !(o2 instanceof CmsRelation)) {
066: return 0;
067: }
068:
069: CmsRelation r1 = (CmsRelation) o1;
070: String p1 = r1.getSourcePath() + r1.getTargetPath()
071: + r1.getType().getId();
072: CmsRelation r2 = (CmsRelation) o2;
073: String p2 = r2.getSourcePath() + r2.getTargetPath()
074: + r2.getType().getId();
075:
076: return p1.compareTo(p2);
077: }
078: };
079:
080: /** Default value for undefined Strings. */
081: private static final String UNDEF = "";
082:
083: /** Precalculated hashcode. */
084: private int m_hashCode;
085:
086: /** The structure id of the source resource. */
087: private final CmsUUID m_sourceId;
088:
089: /** The path of the source resource. */
090: private final String m_sourcePath;
091:
092: /** The structure id of the target resource. */
093: private final CmsUUID m_targetId;
094:
095: /** The path of the target resource. */
096: private final String m_targetPath;
097:
098: /** The relation type. */
099: private final CmsRelationType m_type;
100:
101: /**
102: * Creates a new relation object of the given type between the given resources.<p>
103: *
104: * @param source the source resource
105: * @param target the target resource
106: * @param type the relation type
107: */
108: public CmsRelation(CmsResource source, CmsResource target,
109: CmsRelationType type) {
110:
111: this (source.getStructureId(), source.getRootPath(), target
112: .getStructureId(), target.getRootPath(), type);
113: }
114:
115: /**
116: * Base constructor.<p>
117: *
118: * @param sourceId the source structure id
119: * @param sourcePath the source path
120: * @param targetId the target structure id
121: * @param targetPath the target path
122: * @param type the relation type
123: */
124: public CmsRelation(CmsUUID sourceId, String sourcePath,
125: CmsUUID targetId, String targetPath, CmsRelationType type) {
126:
127: // make sure no value can ever be null
128: m_sourceId = ((sourceId != null) ? sourceId : CmsUUID
129: .getNullUUID());
130: m_sourcePath = ((sourcePath != null) ? sourcePath : UNDEF);
131: m_targetId = ((targetId != null) ? targetId : CmsUUID
132: .getNullUUID());
133: m_targetPath = ((targetPath != null) ? targetPath : UNDEF);
134: m_type = ((type != null) ? type : CmsRelationType.XML_WEAK);
135: }
136:
137: /**
138: * @see java.lang.Object#equals(java.lang.Object)
139: */
140: public boolean equals(Object obj) {
141:
142: if (this == obj) {
143: return true;
144: }
145: if (obj instanceof CmsRelation) {
146: CmsRelation other = (CmsRelation) obj;
147: return (m_type == other.m_type)
148: // && (m_dateBegin == other.m_dateBegin)
149: // && (m_dateEnd == other.m_dateEnd)
150: && (m_sourcePath.equals(other.m_sourcePath) || m_sourceId
151: .equals(other.m_sourceId))
152: && (m_targetPath.equals(other.m_targetPath) || m_targetId
153: .equals(other.m_targetId));
154: }
155: return false;
156: }
157:
158: /**
159: * Returns the source resource wenn possible to read with the given filter.<p>
160: *
161: * @param cms the current user context
162: * @param filter the filter to use
163: *
164: * @return the source resource
165: *
166: * @throws CmsException if something goes wrong
167: */
168: public CmsResource getSource(CmsObject cms, CmsResourceFilter filter)
169: throws CmsException {
170:
171: try {
172: // first look up by id
173: return cms.readResource(getSourceId(), filter);
174: } catch (CmsVfsResourceNotFoundException e) {
175: // then look up by name, but from the root site
176: String storedSiteRoot = cms.getRequestContext()
177: .getSiteRoot();
178: try {
179: cms.getRequestContext().setSiteRoot("");
180: return cms.readResource(getSourcePath(), filter);
181: } finally {
182: cms.getRequestContext().setSiteRoot(storedSiteRoot);
183: }
184: }
185: }
186:
187: /**
188: * Returns the structure id of the source resource.<p>
189: *
190: * @return the structure id of the source resource
191: */
192: public CmsUUID getSourceId() {
193:
194: return m_sourceId;
195: }
196:
197: /**
198: * Returns the path of the source resource.<p>
199: *
200: * @return the path of the source resource
201: */
202: public String getSourcePath() {
203:
204: return m_sourcePath;
205: }
206:
207: /**
208: * Returns the target resource wenn possible to read with the given filter.<p>
209: *
210: * @param cms the current user context
211: * @param filter the filter to use
212: *
213: * @return the target resource
214: *
215: * @throws CmsException if something goes wrong
216: */
217: public CmsResource getTarget(CmsObject cms, CmsResourceFilter filter)
218: throws CmsException {
219:
220: try {
221: // first look up by id
222: return cms.readResource(getTargetId(), filter);
223: } catch (CmsVfsResourceNotFoundException e) {
224: // then look up by name, but from the root site
225: String storedSiteRoot = cms.getRequestContext()
226: .getSiteRoot();
227: try {
228: cms.getRequestContext().setSiteRoot("");
229: return cms.readResource(getTargetPath(), filter);
230: } finally {
231: cms.getRequestContext().setSiteRoot(storedSiteRoot);
232: }
233: }
234: }
235:
236: /**
237: * Returns the tructure id of the target resource.<p>
238: *
239: * @return the tructure id of the target resource
240: */
241: public CmsUUID getTargetId() {
242:
243: return m_targetId;
244: }
245:
246: /**
247: * Returns the path of the target resource.<p>
248: *
249: * @return the path of the target resource
250: */
251: public String getTargetPath() {
252:
253: return m_targetPath;
254: }
255:
256: /**
257: * Returns the relation type.<p>
258: *
259: * @return the relation type
260: */
261: public CmsRelationType getType() {
262:
263: return m_type;
264: }
265:
266: /**
267: * @see java.lang.Object#hashCode()
268: */
269: public int hashCode() {
270:
271: if (m_hashCode == 0) {
272: // calculate hashcode only once
273: final int PRIME = 31;
274: int result = 1;
275: result = PRIME
276: * result
277: + ((m_sourceId == null) ? 0 : m_sourceId.hashCode());
278: result = PRIME
279: * result
280: + ((m_sourcePath == null) ? 0 : m_sourcePath
281: .hashCode());
282: result = PRIME
283: * result
284: + ((m_targetId == null) ? 0 : m_targetId.hashCode());
285: result = PRIME
286: * result
287: + ((m_targetPath == null) ? 0 : m_targetPath
288: .hashCode());
289: result = PRIME * result
290: + ((m_type == null) ? 0 : m_type.hashCode());
291: m_hashCode = result;
292: }
293: return m_hashCode;
294: }
295:
296: /**
297: * @see java.lang.Object#toString()
298: */
299: public String toString() {
300:
301: StringBuffer str = new StringBuffer();
302: str.append("CmsRelation [");
303: str.append("source id: ").append(m_sourceId).append(", ");
304: str.append("source path: ").append(m_sourcePath).append(", ");
305: str.append("target id: ").append(m_targetId).append(", ");
306: str.append("target path: ").append(m_targetPath).append(", ");
307: str.append("type: ").append(m_type);
308: str.append("]");
309: return str.toString();
310: }
311: }
|