001: /*
002: * HandleUtil.java
003: *
004: * Version: $Revision: 1.6 $
005: *
006: * Date: $Date: 2006/08/08 21:00:27 $
007: *
008: * Copyright (c) 2002, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */
040: package org.dspace.app.xmlui.utils;
041:
042: import java.sql.SQLException;
043: import java.util.Map;
044: import java.util.Stack;
045:
046: import org.apache.cocoon.environment.ObjectModelHelper;
047: import org.apache.cocoon.environment.Request;
048: import org.dspace.app.xmlui.wing.Message;
049: import org.dspace.app.xmlui.wing.WingException;
050: import org.dspace.app.xmlui.wing.element.PageMeta;
051: import org.dspace.content.Bitstream;
052: import org.dspace.content.Bundle;
053: import org.dspace.content.Collection;
054: import org.dspace.content.Community;
055: import org.dspace.content.DSpaceObject;
056: import org.dspace.content.Item;
057: import org.dspace.core.Constants;
058: import org.dspace.core.Context;
059: import org.dspace.handle.HandleManager;
060:
061: /**
062: * Simple utility class for extracting handles.
063: *
064: * @author Scott Phillips
065: */
066:
067: public class HandleUtil {
068:
069: /** The URL prefix of all handle */
070: protected static final String HANDLE_PREFIX = "handle/";
071:
072: protected static final String DSPACE_OBJECT = "dspace.object";
073:
074: /**
075: * Obtain the current DSpace handle for the specified request.
076: *
077: * @param objectModel
078: * The cocoon model.
079: * @return A DSpace handle, or null if none found.
080: */
081: public static DSpaceObject obtainHandle(Map objectModel)
082: throws SQLException {
083: Request request = ObjectModelHelper.getRequest(objectModel);
084:
085: DSpaceObject dso = (DSpaceObject) request
086: .getAttribute(DSPACE_OBJECT);
087:
088: if (dso == null) {
089: String uri = request.getSitemapURI();
090:
091: if (!uri.startsWith(HANDLE_PREFIX))
092: // Dosn't start with the prefix then no match
093: return null;
094:
095: String handle = uri.substring(HANDLE_PREFIX.length());
096:
097: int firstSlash = handle.indexOf('/');
098: if (firstSlash < 0)
099: // If there is no first slash then no match
100: return null;
101:
102: int secondSlash = handle.indexOf('/', firstSlash + 1);
103: if (secondSlash < 0)
104: // A trailing slash is not nesssary if there is nothing after
105: // the handle.
106: secondSlash = handle.length();
107:
108: handle = handle.substring(0, secondSlash);
109:
110: Context context = ContextUtil.obtainContext(objectModel);
111: dso = HandleManager.resolveToObject(context, handle);
112:
113: request.setAttribute(DSPACE_OBJECT, dso);
114: }
115:
116: return dso;
117: }
118:
119: /**
120: * Determine if the given DSO is an ancestor of the the parent handle.
121: *
122: * @param dso
123: * The child DSO object.
124: * @param parent
125: * The Handle to test against.
126: * @return The matched DSO object or null if none found.
127: */
128: public static boolean inheritsFrom(DSpaceObject dso, String parent)
129: throws SQLException {
130:
131: DSpaceObject current = dso;
132:
133: while (current != null) {
134:
135: // Check if the current object has the handle we are looking for.
136: if (current.getHandle().equals(parent))
137: return true;
138:
139: if (current.getType() == Constants.ITEM) {
140: current = ((Item) current).getOwningCollection();
141: } else if (current.getType() == Constants.COLLECTION) {
142: current = ((Collection) current).getCommunities()[0];
143: } else if (current.getType() == Constants.COMMUNITY) {
144: current = ((Community) current).getParentCommunity();
145: }
146: }
147:
148: // If the loop finished then we searched the entire parant-child chain
149: // and did not find this handle, so the object was not found.
150:
151: return false;
152: }
153:
154: /**
155: * Build a list of trail metadata starting with the owning collection and
156: * ending with the root level parent. If the Object is an item the item is
157: * not included but all it's parents are. However if the item is a community
158: * or collection then it is included along with all parents.
159: *
160: * @param dso
161: * @param pageMeta
162: */
163: public static void buildHandleTrail(DSpaceObject dso,
164: PageMeta pageMeta, String contextPath) throws SQLException,
165: WingException {
166: // Add the trail back to the repository root.
167: Stack<DSpaceObject> stack = new Stack<DSpaceObject>();
168:
169: if (dso instanceof Bitstream) {
170: Bitstream bitstream = (Bitstream) dso;
171: Bundle[] bundles = bitstream.getBundles();
172:
173: dso = bundles[0];
174: }
175:
176: if (dso instanceof Bundle) {
177: Bundle bundle = (Bundle) dso;
178: Item[] items = bundle.getItems();
179:
180: dso = items[0];
181: }
182:
183: if (dso instanceof Item) {
184: Item item = (Item) dso;
185: Collection collection = item.getOwningCollection();
186: dso = collection;
187: }
188:
189: if (dso instanceof Collection) {
190: Collection collection = (Collection) dso;
191: stack.push(collection);
192: Community[] communities = collection.getCommunities();
193:
194: dso = communities[0];
195: }
196:
197: if (dso instanceof Community) {
198: Community community = (Community) dso;
199: stack.push(community);
200:
201: for (Community parent : community.getAllParents()) {
202: stack.push(parent);
203: }
204: }
205:
206: while (!stack.empty()) {
207: DSpaceObject pop = stack.pop();
208:
209: if (pop instanceof Collection) {
210: Collection collection = (Collection) pop;
211: String name = collection.getMetadata("name");
212: if (name == null || name.length() == 0)
213: pageMeta.addTrailLink(contextPath + "/handle/"
214: + pop.getHandle(), new Message("default",
215: "xmlui.general.untitled"));
216: else
217: pageMeta.addTrailLink(contextPath + "/handle/"
218: + pop.getHandle(), name);
219: } else if (pop instanceof Community) {
220: Community community = (Community) pop;
221: String name = community.getMetadata("name");
222: if (name == null || name.length() == 0)
223: pageMeta.addTrailLink(contextPath + "/handle/"
224: + pop.getHandle(), new Message("default",
225: "xmlui.general.untitled"));
226: else
227: pageMeta.addTrailLink(contextPath + "/handle/"
228: + pop.getHandle(), name);
229:
230: pageMeta.addTrailLink(contextPath + "/handle/"
231: + pop.getHandle(), ((Community) pop)
232: .getMetadata("name"));
233: }
234:
235: }
236: }
237:
238: }
|