1 40 package org.dspace.content; 41 42 import java.io.IOException ; 43 import java.sql.SQLException ; 44 import java.util.ArrayList ; 45 import java.util.List ; 46 47 import org.apache.log4j.Logger; 48 import org.dspace.authorize.AuthorizeException; 49 import org.dspace.authorize.AuthorizeManager; 50 import org.dspace.core.Constants; 51 import org.dspace.core.Context; 52 import org.dspace.core.LogManager; 53 import org.dspace.eperson.EPerson; 54 import org.dspace.eperson.Group; 55 import org.dspace.history.HistoryManager; 56 import org.dspace.storage.rdbms.DatabaseManager; 57 import org.dspace.storage.rdbms.TableRow; 58 import org.dspace.storage.rdbms.TableRowIterator; 59 60 66 public class WorkspaceItem implements InProgressSubmission 67 { 68 69 private static Logger log = Logger.getLogger(WorkspaceItem.class); 70 71 72 private Item item; 73 74 75 private Context ourContext; 76 77 78 private TableRow wiRow; 79 80 81 private Collection collection; 82 83 91 WorkspaceItem(Context context, TableRow row) throws SQLException 92 { 93 ourContext = context; 94 wiRow = row; 95 96 item = Item.find(context, wiRow.getIntColumn("item_id")); 97 collection = Collection.find(context, wiRow 98 .getIntColumn("collection_id")); 99 100 context.cache(this, row.getIntColumn("workspace_item_id")); 102 } 103 104 115 public static WorkspaceItem find(Context context, int id) 116 throws SQLException 117 { 118 WorkspaceItem fromCache = (WorkspaceItem) context.fromCache( 120 WorkspaceItem.class, id); 121 122 if (fromCache != null) 123 { 124 return fromCache; 125 } 126 127 TableRow row = DatabaseManager.find(context, "workspaceitem", id); 128 129 if (row == null) 130 { 131 if (log.isDebugEnabled()) 132 { 133 log.debug(LogManager.getHeader(context, "find_workspace_item", 134 "not_found,workspace_item_id=" + id)); 135 } 136 137 return null; 138 } 139 else 140 { 141 if (log.isDebugEnabled()) 142 { 143 log.debug(LogManager.getHeader(context, "find_workspace_item", 144 "workspace_item_id=" + id)); 145 } 146 147 return new WorkspaceItem(context, row); 148 } 149 } 150 151 165 public static WorkspaceItem create(Context c, Collection coll, 166 boolean template) throws AuthorizeException, SQLException , 167 IOException 168 { 169 AuthorizeManager.authorizeAction(c, coll, Constants.ADD); 171 172 Item i = Item.create(c); 174 i.setSubmitter(c.getCurrentUser()); 175 176 Group step1group = coll.getWorkflowGroup(1); 181 Group step2group = coll.getWorkflowGroup(2); 182 Group step3group = coll.getWorkflowGroup(3); 183 184 EPerson e = c.getCurrentUser(); 185 186 AuthorizeManager.addPolicy(c, i, Constants.READ, e); 188 189 if (step1group != null) 190 { 191 AuthorizeManager.addPolicy(c, i, Constants.READ, step1group); 192 } 193 194 if (step2group != null) 195 { 196 AuthorizeManager.addPolicy(c, i, Constants.READ, step2group); 197 } 198 199 if (step3group != null) 200 { 201 AuthorizeManager.addPolicy(c, i, Constants.READ, step3group); 202 } 203 204 AuthorizeManager.addPolicy(c, i, Constants.WRITE, e); 206 207 if (step1group != null) 208 { 209 AuthorizeManager.addPolicy(c, i, Constants.WRITE, step1group); 210 } 211 212 if (step2group != null) 213 { 214 AuthorizeManager.addPolicy(c, i, Constants.WRITE, step2group); 215 } 216 217 if (step3group != null) 218 { 219 AuthorizeManager.addPolicy(c, i, Constants.WRITE, step3group); 220 } 221 222 AuthorizeManager.addPolicy(c, i, Constants.ADD, e); 224 225 if (step1group != null) 226 { 227 AuthorizeManager.addPolicy(c, i, Constants.ADD, step1group); 228 } 229 230 if (step2group != null) 231 { 232 AuthorizeManager.addPolicy(c, i, Constants.ADD, step2group); 233 } 234 235 if (step3group != null) 236 { 237 AuthorizeManager.addPolicy(c, i, Constants.ADD, step3group); 238 } 239 240 AuthorizeManager.addPolicy(c, i, Constants.REMOVE, e); 242 243 if (step1group != null) 244 { 245 AuthorizeManager.addPolicy(c, i, Constants.REMOVE, step1group); 246 } 247 248 if (step2group != null) 249 { 250 AuthorizeManager.addPolicy(c, i, Constants.REMOVE, step2group); 251 } 252 253 if (step3group != null) 254 { 255 AuthorizeManager.addPolicy(c, i, Constants.REMOVE, step3group); 256 } 257 258 Item templateItem = coll.getTemplateItem(); 260 261 if (template && (templateItem != null)) 262 { 263 DCValue[] md = templateItem.getMetadata(Item.ANY, Item.ANY, Item.ANY, Item.ANY); 264 265 for (int n = 0; n < md.length; n++) 266 { 267 i.addMetadata(md[n].schema, md[n].element, md[n].qualifier, md[n].language, 268 md[n].value); 269 } 270 } 271 272 i.update(); 273 274 TableRow row = DatabaseManager.create(c, "workspaceitem"); 276 277 row.setColumn("item_id", i.getID()); 278 row.setColumn("collection_id", coll.getID()); 279 280 log.info(LogManager.getHeader(c, "create_workspace_item", 281 "workspace_item_id=" + row.getIntColumn("workspace_item_id") 282 + "item_id=" + i.getID() + "collection_id=" 283 + coll.getID())); 284 285 DatabaseManager.update(c, row); 286 287 WorkspaceItem wi = new WorkspaceItem(c, row); 288 289 HistoryManager.saveHistory(c, wi, HistoryManager.CREATE, c 290 .getCurrentUser(), c.getExtraLogInfo()); 291 292 return wi; 293 } 294 295 307 public static WorkspaceItem[] findByEPerson(Context context, EPerson ep) 308 throws SQLException 309 { 310 List wsItems = new ArrayList (); 311 312 TableRowIterator tri = DatabaseManager.queryTable(context, "workspaceitem", 313 "SELECT workspaceitem.* FROM workspaceitem, item WHERE " + 314 "workspaceitem.item_id=item.item_id AND " + 315 "item.submitter_id= ? " + 316 "ORDER BY workspaceitem.workspace_item_id", 317 ep.getID()); 318 319 while (tri.hasNext()) 320 { 321 TableRow row = tri.next(); 322 323 WorkspaceItem wi = (WorkspaceItem) context.fromCache( 325 WorkspaceItem.class, row.getIntColumn("workspace_item_id")); 326 327 if (wi == null) 328 { 329 wi = new WorkspaceItem(context, row); 330 } 331 332 wsItems.add(wi); 333 } 334 tri.close(); 336 337 WorkspaceItem[] wsArray = new WorkspaceItem[wsItems.size()]; 338 wsArray = (WorkspaceItem[]) wsItems.toArray(wsArray); 339 340 return wsArray; 341 } 342 343 353 public static WorkspaceItem[] findByCollection(Context context, Collection c) 354 throws SQLException 355 { 356 List wsItems = new ArrayList (); 357 358 TableRowIterator tri = DatabaseManager.queryTable(context, "workspaceitem", 359 "SELECT workspaceitem.* FROM workspaceitem WHERE " + 360 "workspaceitem.collection_id= ? ", 361 c.getID()); 362 363 while (tri.hasNext()) 364 { 365 TableRow row = tri.next(); 366 367 WorkspaceItem wi = (WorkspaceItem) context.fromCache( 369 WorkspaceItem.class, row.getIntColumn("workspace_item_id")); 370 371 if (wi == null) 373 { 374 wi = new WorkspaceItem(context, row); 375 } 376 377 wsItems.add(wi); 378 } 379 tri.close(); 381 382 WorkspaceItem[] wsArray = new WorkspaceItem[wsItems.size()]; 383 wsArray = (WorkspaceItem[]) wsItems.toArray(wsArray); 384 385 return wsArray; 386 } 387 388 395 public static WorkspaceItem[] findAll(Context context) 396 throws SQLException 397 { 398 List wsItems = new ArrayList (); 399 String query = "SELECT * FROM workspaceitem ORDER BY item_id"; 400 TableRowIterator tri = DatabaseManager.queryTable(context, 401 "workspaceitem", 402 query); 403 404 while (tri.hasNext()) 405 { 406 TableRow row = tri.next(); 407 408 WorkspaceItem wi = (WorkspaceItem) context.fromCache( 410 WorkspaceItem.class, row.getIntColumn("workspace_item_id")); 411 412 if (wi == null) 414 { 415 wi = new WorkspaceItem(context, row); 416 } 417 418 wsItems.add(wi); 419 } 420 421 tri.close(); 422 423 WorkspaceItem[] wsArray = new WorkspaceItem[wsItems.size()]; 424 wsArray = (WorkspaceItem[]) wsItems.toArray(wsArray); 425 426 return wsArray; 427 } 428 429 434 public int getID() 435 { 436 return wiRow.getIntColumn("workspace_item_id"); 437 } 438 439 444 public int getStageReached() 445 { 446 return wiRow.getIntColumn("stage_reached"); 447 } 448 449 455 public void setStageReached(int v) 456 { 457 wiRow.setColumn("stage_reached", v); 458 } 459 460 463 public void update() throws SQLException , AuthorizeException, IOException 464 { 465 HistoryManager.saveHistory(ourContext, this, HistoryManager.MODIFY, 467 ourContext.getCurrentUser(), ourContext.getExtraLogInfo()); 468 469 log.info(LogManager.getHeader(ourContext, "update_workspace_item", 470 "workspace_item_id=" + getID())); 471 472 item.update(); 474 475 DatabaseManager.update(ourContext, wiRow); 477 } 478 479 484 public void deleteAll() throws SQLException , AuthorizeException, 485 IOException 486 { 487 492 if (!AuthorizeManager.isAdmin(ourContext) 493 && ((ourContext.getCurrentUser() == null) || (ourContext 494 .getCurrentUser().getID() != item.getSubmitter() 495 .getID()))) 496 { 497 throw new AuthorizeException("Must be an administrator or the " 499 + "original submitter to delete a workspace item"); 500 } 501 502 HistoryManager.saveHistory(ourContext, this, HistoryManager.REMOVE, 503 ourContext.getCurrentUser(), ourContext.getExtraLogInfo()); 504 505 log.info(LogManager.getHeader(ourContext, "delete_workspace_item", 506 "workspace_item_id=" + getID() + "item_id=" + item.getID() 507 + "collection_id=" + collection.getID())); 508 509 ourContext.removeCached(this, getID()); 512 513 deleteEpersonGroup2WorkspaceItem(); 516 517 DatabaseManager.delete(ourContext, wiRow); 520 521 item.delete(); 523 } 524 525 private void deleteEpersonGroup2WorkspaceItem() throws SQLException 526 { 527 528 String removeSQL="DELETE FROM epersongroup2workspaceitem WHERE workspace_item_id = ?"; 529 DatabaseManager.updateQuery(ourContext, removeSQL,getID()); 530 531 } 532 533 public void deleteWrapper() throws SQLException , AuthorizeException, 534 IOException 535 { 536 AuthorizeManager.authorizeAction(ourContext, item, Constants.WRITE); 538 539 HistoryManager.saveHistory(ourContext, this, HistoryManager.REMOVE, 540 ourContext.getCurrentUser(), ourContext.getExtraLogInfo()); 541 542 log.info(LogManager.getHeader(ourContext, "delete_workspace_item", 543 "workspace_item_id=" + getID() + "item_id=" + item.getID() 544 + "collection_id=" + collection.getID())); 545 546 ourContext.removeCached(this, getID()); 549 550 DatabaseManager.delete(ourContext, wiRow); 553 } 554 555 public Item getItem() 557 { 558 return item; 559 } 560 561 public Collection getCollection() 562 { 563 return collection; 564 } 565 566 public EPerson getSubmitter() throws SQLException 567 { 568 return item.getSubmitter(); 569 } 570 571 public boolean hasMultipleFiles() 572 { 573 return wiRow.getBooleanColumn("multiple_files"); 574 } 575 576 public void setMultipleFiles(boolean b) 577 { 578 wiRow.setColumn("multiple_files", b); 579 } 580 581 public boolean hasMultipleTitles() 582 { 583 return wiRow.getBooleanColumn("multiple_titles"); 584 } 585 586 public void setMultipleTitles(boolean b) 587 { 588 wiRow.setColumn("multiple_titles", b); 589 } 590 591 public boolean isPublishedBefore() 592 { 593 return wiRow.getBooleanColumn("published_before"); 594 } 595 596 public void setPublishedBefore(boolean b) 597 { 598 wiRow.setColumn("published_before", b); 599 } 600 } 601 | Popular Tags |