KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > handle > HandleManager


1 /*
2  * HandleManager.java
3  *
4  * Version: $Revision: 1.19 $
5  *
6  * Date: $Date: 2006/05/26 14:18:41 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.handle;
41
42 import java.sql.SQLException JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.List JavaDoc;
45
46 import org.apache.log4j.Logger;
47 import org.dspace.content.Collection;
48 import org.dspace.content.Community;
49 import org.dspace.content.DSpaceObject;
50 import org.dspace.content.Item;
51 import org.dspace.core.ConfigurationManager;
52 import org.dspace.core.Constants;
53 import org.dspace.core.Context;
54 import org.dspace.storage.rdbms.DatabaseManager;
55 import org.dspace.storage.rdbms.TableRow;
56 import org.dspace.storage.rdbms.TableRowIterator;
57
58 /**
59  * Interface to the <a HREF="http://www.handle.net" target=_new>CNRI Handle
60  * System </a>.
61  *
62  * <p>
63  * Currently, this class simply maps handles to local facilities; handles which
64  * are owned by other sites (including other DSpaces) are treated as
65  * non-existent.
66  * </p>
67  *
68  * @author Peter Breton
69  * @version $Revision: 1.19 $
70  */

71 public class HandleManager
72 {
73     /** log4j category */
74     private static Logger log = Logger.getLogger(HandleManager.class);
75
76     /** Private Constructor */
77     private HandleManager()
78     {
79     }
80
81     /**
82      * Return the local URL for handle, or null if handle cannot be found.
83      *
84      * The returned URL is a (non-handle-based) location where a dissemination
85      * of the object referred to by handle can be obtained.
86      *
87      * @param context
88      * DSpace context
89      * @param handle
90      * The handle
91      * @return The local URL
92      * @exception SQLException
93      * If a database error occurs
94      */

95     public static String JavaDoc resolveToURL(Context context, String JavaDoc handle)
96             throws SQLException JavaDoc
97     {
98         TableRow dbhandle = findHandleInternal(context, handle);
99
100         if (dbhandle == null)
101         {
102             return null;
103         }
104
105         String JavaDoc url = ConfigurationManager.getProperty("dspace.url")
106                 + "/handle/" + handle;
107
108         if (log.isDebugEnabled())
109         {
110             log.debug("Resolved " + handle + " to " + url);
111         }
112
113         return url;
114     }
115
116     /**
117      * Transforms handle into the canonical form <em>hdl:handle</em>.
118      *
119      * No attempt is made to verify that handle is in fact valid.
120      *
121      * @param handle
122      * The handle
123      * @return The canonical form
124      */

125     public static String JavaDoc getCanonicalForm(String JavaDoc handle)
126     {
127         // return "hdl:" + handle;
128
return "http://hdl.handle.net/" + handle;
129     }
130
131     /**
132      * Returns displayable string of the handle's 'temporary' URL
133      * <em>http://hdl.handle.net/handle/em>.
134      *
135      * No attempt is made to verify that handle is in fact valid.
136      *
137      * @param handle The handle
138      * @return The canonical form
139      */

140
141     // public static String getURLForm(String handle)
142
// {
143
// return "http://hdl.handle.net/" + handle;
144
// }
145
/**
146      * Creates a new handle in the database.
147      *
148      * @param context
149      * DSpace context
150      * @param dso
151      * The DSpaceObject to create a handle for
152      * @return The newly created handle
153      * @exception SQLException
154      * If a database error occurs
155      */

156     public static String JavaDoc createHandle(Context context, DSpaceObject dso)
157             throws SQLException JavaDoc
158     {
159         TableRow handle = DatabaseManager.create(context, "Handle");
160         String JavaDoc handleId = createId(handle.getIntColumn("handle_id"));
161
162         handle.setColumn("handle", handleId);
163         handle.setColumn("resource_type_id", dso.getType());
164         handle.setColumn("resource_id", dso.getID());
165         DatabaseManager.update(context, handle);
166
167         if (log.isDebugEnabled())
168         {
169             log.debug("Created new handle for "
170                     + Constants.typeText[dso.getType()] + " " + handleId);
171         }
172
173         return handleId;
174     }
175
176     /**
177      * Creates a handle entry, but with a handle supplied by the caller (new
178      * Handle not generated)
179      *
180      * @param context
181      * DSpace context
182      * @param dso
183      * DSpaceObject
184      * @param suppliedHandle
185      * existing handle value
186      * @return the Handle
187      */

188     public static String JavaDoc createHandle(Context context, DSpaceObject dso,
189             String JavaDoc suppliedHandle) throws SQLException JavaDoc
190     {
191         TableRow handle = DatabaseManager.create(context, "Handle");
192         String JavaDoc handleId = suppliedHandle;
193
194         handle.setColumn("handle", handleId);
195         handle.setColumn("resource_type_id", dso.getType());
196         handle.setColumn("resource_id", dso.getID());
197         DatabaseManager.update(context, handle);
198
199         if (log.isDebugEnabled())
200         {
201             log.debug("Created new handle for "
202                     + Constants.typeText[dso.getType()] + " " + handleId);
203         }
204
205         return handleId;
206     }
207
208     /**
209      * Return the object which handle maps to, or null. This is the object
210      * itself, not a URL which points to it.
211      *
212      * @param context
213      * DSpace context
214      * @param handle
215      * The handle to resolve
216      * @return The object which handle maps to, or null if handle is not mapped
217      * to any object.
218      * @exception SQLException
219      * If a database error occurs
220      */

221     public static DSpaceObject resolveToObject(Context context, String JavaDoc handle)
222             throws SQLException JavaDoc
223     {
224         TableRow dbhandle = findHandleInternal(context, handle);
225
226         if (dbhandle == null)
227         {
228             return null;
229         }
230
231         if ((dbhandle.isColumnNull("resource_type_id"))
232                 || (dbhandle.isColumnNull("resource_id")))
233         {
234             throw new IllegalStateException JavaDoc("No associated resource type");
235         }
236
237         // What are we looking at here?
238
int handletypeid = dbhandle.getIntColumn("resource_type_id");
239         int resourceID = dbhandle.getIntColumn("resource_id");
240
241         if (handletypeid == Constants.ITEM)
242         {
243             Item item = Item.find(context, resourceID);
244
245             if (log.isDebugEnabled())
246             {
247                 log.debug("Resolved handle " + handle + " to item "
248                         + ((item == null) ? (-1) : item.getID()));
249             }
250
251             return item;
252         }
253         else if (handletypeid == Constants.COLLECTION)
254         {
255             Collection collection = Collection.find(context, resourceID);
256
257             if (log.isDebugEnabled())
258             {
259                 log.debug("Resolved handle " + handle + " to collection "
260                         + ((collection == null) ? (-1) : collection.getID()));
261             }
262
263             return collection;
264         }
265         else if (handletypeid == Constants.COMMUNITY)
266         {
267             Community community = Community.find(context, resourceID);
268
269             if (log.isDebugEnabled())
270             {
271                 log.debug("Resolved handle " + handle + " to community "
272                         + ((community == null) ? (-1) : community.getID()));
273             }
274
275             return community;
276         }
277
278         throw new IllegalStateException JavaDoc("Unsupported Handle Type "
279                 + Constants.typeText[handletypeid]);
280     }
281
282     /**
283      * Return the handle for an Object, or null if the Object has no handle.
284      *
285      * @param context
286      * DSpace context
287      * @param dso
288      * The object to obtain a handle for
289      * @return The handle for object, or null if the object has no handle.
290      * @exception SQLException
291      * If a database error occurs
292      */

293     public static String JavaDoc findHandle(Context context, DSpaceObject dso)
294             throws SQLException JavaDoc
295     {
296         // if (!(obj instanceof Item))
297
// return null;
298
// Item item = (Item) obj;
299
return getHandleInternal(context, dso.getType(), dso.getID());
300     }
301
302     /**
303      * Return all the handles which start with prefix.
304      *
305      * @param context
306      * DSpace context
307      * @param prefix
308      * The handle prefix
309      * @return A list of the handles starting with prefix. The list is
310      * guaranteed to be non-null. Each element of the list is a String.
311      * @exception SQLException
312      * If a database error occurs
313      */

314     static List JavaDoc getHandlesForPrefix(Context context, String JavaDoc prefix)
315             throws SQLException JavaDoc
316     {
317         String JavaDoc sql = "SELECT handle FROM handle WHERE handle LIKE ? ";
318         TableRowIterator iterator = DatabaseManager.queryTable(context, null, sql, prefix+"%");
319         List JavaDoc results = new ArrayList JavaDoc();
320
321         while (iterator.hasNext())
322         {
323             TableRow row = (TableRow) iterator.next();
324             results.add(row.getStringColumn("handle"));
325         }
326         
327         iterator.close();
328
329         return results;
330     }
331
332     ////////////////////////////////////////
333
// Internal methods
334
////////////////////////////////////////
335

336     /**
337      * Return the handle for an Object, or null if the Object has no handle.
338      *
339      * @param context
340      * DSpace context
341      * @param type
342      * The type of object
343      * @param id
344      * The id of object
345      * @return The handle for object, or null if the object has no handle.
346      * @exception SQLException
347      * If a database error occurs
348      */

349     private static String JavaDoc getHandleInternal(Context context, int type, int id)
350             throws SQLException JavaDoc
351     {
352         String JavaDoc sql = "SELECT handle FROM Handle WHERE resource_type_id = ? " +
353                      "AND resource_id = ?";
354         
355         TableRow row = DatabaseManager.querySingle(context, sql,type,id);
356
357         return (row == null) ? null : row.getStringColumn("handle");
358     }
359
360     /**
361      * Find the database row corresponding to handle.
362      *
363      * @param context
364      * DSpace context
365      * @param handle
366      * The handle to resolve
367      * @return The database row corresponding to the handle
368      * @exception SQLException
369      * If a database error occurs
370      */

371     private static TableRow findHandleInternal(Context context, String JavaDoc handle)
372             throws SQLException JavaDoc
373     {
374         if (handle == null)
375         {
376             throw new IllegalArgumentException JavaDoc("Handle is null");
377         }
378
379         return DatabaseManager
380                 .findByUnique(context, "Handle", "handle", handle);
381     }
382
383     /**
384      * Create a new handle id. The implementation uses the PK of the RDBMS
385      * Handle table.
386      *
387      * @return A new handle id
388      * @exception SQLException
389      * If a database error occurs
390      */

391     private static String JavaDoc createId(int id) throws SQLException JavaDoc
392     {
393         String JavaDoc handlePrefix = ConfigurationManager.getProperty("handle.prefix");
394
395         return new StringBuffer JavaDoc().append(handlePrefix).append(
396                 handlePrefix.endsWith("/") ? "" : "/").append(id).toString();
397     }
398 }
399
Popular Tags