1 40 package org.dspace.handle; 41 42 import java.sql.SQLException ; 43 import java.util.ArrayList ; 44 import java.util.List ; 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 71 public class HandleManager 72 { 73 74 private static Logger log = Logger.getLogger(HandleManager.class); 75 76 77 private HandleManager() 78 { 79 } 80 81 95 public static String resolveToURL(Context context, String handle) 96 throws SQLException 97 { 98 TableRow dbhandle = findHandleInternal(context, handle); 99 100 if (dbhandle == null) 101 { 102 return null; 103 } 104 105 String 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 125 public static String getCanonicalForm(String handle) 126 { 127 return "http://hdl.handle.net/" + handle; 129 } 130 131 140 141 156 public static String createHandle(Context context, DSpaceObject dso) 157 throws SQLException 158 { 159 TableRow handle = DatabaseManager.create(context, "Handle"); 160 String 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 188 public static String createHandle(Context context, DSpaceObject dso, 189 String suppliedHandle) throws SQLException 190 { 191 TableRow handle = DatabaseManager.create(context, "Handle"); 192 String 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 221 public static DSpaceObject resolveToObject(Context context, String handle) 222 throws SQLException 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 ("No associated resource type"); 235 } 236 237 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 ("Unsupported Handle Type " 279 + Constants.typeText[handletypeid]); 280 } 281 282 293 public static String findHandle(Context context, DSpaceObject dso) 294 throws SQLException 295 { 296 return getHandleInternal(context, dso.getType(), dso.getID()); 300 } 301 302 314 static List getHandlesForPrefix(Context context, String prefix) 315 throws SQLException 316 { 317 String sql = "SELECT handle FROM handle WHERE handle LIKE ? "; 318 TableRowIterator iterator = DatabaseManager.queryTable(context, null, sql, prefix+"%"); 319 List results = new ArrayList (); 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 336 349 private static String getHandleInternal(Context context, int type, int id) 350 throws SQLException 351 { 352 String 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 371 private static TableRow findHandleInternal(Context context, String handle) 372 throws SQLException 373 { 374 if (handle == null) 375 { 376 throw new IllegalArgumentException ("Handle is null"); 377 } 378 379 return DatabaseManager 380 .findByUnique(context, "Handle", "handle", handle); 381 } 382 383 391 private static String createId(int id) throws SQLException 392 { 393 String handlePrefix = ConfigurationManager.getProperty("handle.prefix"); 394 395 return new StringBuffer ().append(handlePrefix).append( 396 handlePrefix.endsWith("/") ? "" : "/").append(id).toString(); 397 } 398 } 399 | Popular Tags |