1 40 package org.dspace.search; 41 42 import java.sql.Date ; 43 import java.sql.SQLException ; 44 import java.sql.Timestamp ; 45 import java.text.ParseException ; 46 import java.text.SimpleDateFormat ; 47 import java.util.ArrayList ; 48 import java.util.Calendar ; 49 import java.util.LinkedList ; 50 import java.util.List ; 51 import java.util.TimeZone ; 52 53 import org.apache.log4j.Logger; 54 import org.dspace.content.DSpaceObject; 55 import org.dspace.content.Item; 56 import org.dspace.core.ConfigurationManager; 57 import org.dspace.core.Constants; 58 import org.dspace.core.Context; 59 import org.dspace.core.LogManager; 60 import org.dspace.handle.HandleManager; 61 import org.dspace.storage.rdbms.DatabaseManager; 62 import org.dspace.storage.rdbms.TableRow; 63 import org.dspace.storage.rdbms.TableRowIterator; 64 65 73 public class Harvest 74 { 75 76 private static Logger log = Logger.getLogger(Harvest.class); 77 78 119 public static List harvest(Context context, DSpaceObject scope, 120 String startDate, String endDate, int offset, int limit, 121 boolean items, boolean collections, boolean withdrawn) 122 throws SQLException , ParseException 123 { 124 125 String query = "SELECT handle.handle, handle.resource_id, item.withdrawn, item.last_modified FROM handle, item"; 129 130 131 List parameters = new ArrayList (); 136 137 if (scope != null) 138 { 139 if (scope.getType() == Constants.COLLECTION) 140 { 141 query += ", collection2item"; 142 } 143 else if (scope.getType() == Constants.COMMUNITY) 144 { 145 query += ", community2item"; 146 } 147 } 148 149 150 query += " WHERE handle.resource_type_id=" + Constants.ITEM + " AND handle.resource_id=item.item_id "; 151 152 if (scope != null) 153 { 154 if (scope.getType() == Constants.COLLECTION) 155 { 156 query += " AND collection2item.collection_id= ? " + 157 " AND collection2item.item_id=handle.resource_id "; 158 parameters.add(new Integer (scope.getID())); 159 } 160 else if (scope.getType() == Constants.COMMUNITY) 161 { 162 query += " AND community2item.community_id= ? " + 163 " AND community2item.item_id=handle.resource_id"; 164 parameters.add(new Integer (scope.getID())); 165 } 166 } 167 168 if (startDate != null) 169 { 170 if ("oracle".equals(ConfigurationManager.getProperty("db.name"))) 171 { 172 173 startDate = oracleTimeStampFormat(startDate); 174 query += " AND item.last_modified >= " + 175 oracleTimeStampFunction(startDate); 176 parameters.add(startDate); 177 } 178 else { 180 query = query + " AND item.last_modified >= ? "; 181 parameters.add(toTimestamp(startDate, false)); 182 } 183 } 184 185 if (endDate != null) 186 { 187 205 boolean selfGenerated = false; 206 if (endDate.length() == 20) 207 { 208 endDate = endDate.substring(0, 19) + ".999Z"; 209 selfGenerated = true; 210 } 211 212 if ("oracle".equals(ConfigurationManager.getProperty("db.name"))) 213 { 214 endDate = oracleTimeStampFormat(endDate); 215 query += " AND item.last_modified <= " + 216 oracleTimeStampFunction(endDate); 217 parameters.add(endDate); 218 } 219 else { 221 query += " AND item.last_modified <= ? "; 222 parameters.add(toTimestamp(endDate, selfGenerated)); 223 } 224 } 225 226 if (withdrawn == false) 227 { 228 if ("oracle".equals(ConfigurationManager.getProperty("db.name"))) 230 { 231 query += " AND withdrawn=0 "; 232 } 233 else 234 { 235 query += " AND withdrawn=false "; 237 } 238 } 239 240 query += " ORDER BY handle.resource_id"; 244 245 log.debug(LogManager.getHeader(context, "harvest SQL", query)); 246 247 Object [] parametersArray = parameters.toArray(); 249 TableRowIterator tri = DatabaseManager.query(context, query, parametersArray); 250 List infoObjects = new LinkedList (); 251 int index = 0; 252 253 while (tri.hasNext()) 255 { 256 TableRow row = tri.next(); 257 258 262 if ((index >= offset) 263 && ((limit == 0) || (index < (offset + limit)))) 264 { 265 HarvestedItemInfo itemInfo = new HarvestedItemInfo(); 266 267 itemInfo.context = context; 268 itemInfo.handle = row.getStringColumn("handle"); 269 itemInfo.itemID = row.getIntColumn("resource_id"); 270 itemInfo.datestamp = row.getDateColumn("last_modified"); 271 itemInfo.withdrawn = row.getBooleanColumn("withdrawn"); 272 273 if (collections) 274 { 275 fillCollections(context, itemInfo); 276 } 277 278 if (items) 279 { 280 itemInfo.item = Item.find(context, itemInfo.itemID); 282 } 283 284 infoObjects.add(itemInfo); 285 } 286 287 index++; 288 } 289 tri.close(); 290 291 return infoObjects; 292 } 293 294 311 public static HarvestedItemInfo getSingle(Context context, String handle, 312 boolean collections) throws SQLException 313 { 314 Item i = (Item) HandleManager.resolveToObject(context, handle); 316 317 if (i == null) 318 { 319 return null; 320 } 321 322 HarvestedItemInfo itemInfo = new HarvestedItemInfo(); 324 325 itemInfo.context = context; 326 itemInfo.item = i; 327 itemInfo.handle = handle; 328 itemInfo.withdrawn = i.isWithdrawn(); 329 itemInfo.datestamp = i.getLastModified(); 330 itemInfo.itemID = i.getID(); 331 332 if (collections) 334 { 335 fillCollections(context, itemInfo); 336 } 337 338 return itemInfo; 339 } 340 341 350 private static void fillCollections(Context context, 351 HarvestedItemInfo itemInfo) throws SQLException 352 { 353 TableRowIterator colRows = DatabaseManager.query(context, 355 "SELECT handle.handle FROM handle, collection2item WHERE handle.resource_type_id= ? " + 356 "AND collection2item.collection_id=handle.resource_id AND collection2item.item_id = ? ", 357 Constants.COLLECTION, itemInfo.itemID); 358 359 itemInfo.collectionHandles = new LinkedList (); 361 362 while (colRows.hasNext()) 363 { 364 TableRow r = colRows.next(); 365 itemInfo.collectionHandles.add(r.getStringColumn("handle")); 366 } 367 } 368 369 370 378 private static Timestamp toTimestamp(String t, boolean selfGenerated) throws ParseException 379 { 380 SimpleDateFormat df; 381 382 if (t.length() == 10) 384 { 385 df = new SimpleDateFormat ("yyyy-MM-dd"); 386 } 387 else if (t.length() == 20) 388 { 389 df = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'"); 390 } 391 else if (selfGenerated) 392 { 393 df = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); 394 } 395 else { 396 throw new ParseException ("", 0); 398 } 399 400 df.setCalendar(Calendar.getInstance(TimeZone.getTimeZone("UTC"))); 402 return new Timestamp (df.parse(t).getTime()); 403 } 404 405 406 418 private static String oracleTimeStampFunction(String isoDateString) 419 { 420 if (isoDateString.length() == 19 ) 421 { 422 return "TO_TIMESTAMP( ? ,'YYYY-MM-DD\"T\"HH24:MI:SS')"; 423 } else if (isoDateString.length() > 19) 424 { 425 return "TO_TIMESTAMP( ? ,'YYYY-MM-DD\"T\"HH24:MI:SS.FF\"Z\"')"; 426 } else 427 { 428 throw new IllegalArgumentException ("argument does not seem to be in the expected ISO 8601 format"); 429 } 430 } 431 432 438 private static String oracleTimeStampFormat(String isoDateString) 439 { 440 if (isoDateString.length() == 10) 441 { 442 return isoDateString + "T00:00:00"; 443 } 444 else 445 { 446 return isoDateString; 447 } 448 } 449 450 } 451 | Popular Tags |