1 40 package org.dspace.app.webui.servlet.admin; 41 42 import java.io.BufferedInputStream ; 43 import java.io.File ; 44 import java.io.FileInputStream ; 45 import java.io.IOException ; 46 import java.io.InputStream ; 47 import java.sql.SQLException ; 48 import java.util.Collections ; 49 import java.util.Enumeration ; 50 import java.util.HashMap ; 51 import java.util.Iterator ; 52 import java.util.LinkedList ; 53 import java.util.List ; 54 import java.util.StringTokenizer ; 55 56 import javax.servlet.ServletException ; 57 import javax.servlet.http.HttpServletRequest ; 58 import javax.servlet.http.HttpServletResponse ; 59 60 import org.apache.log4j.Logger; 61 import org.dspace.app.webui.servlet.DSpaceServlet; 62 import org.dspace.app.webui.util.FileUploadRequest; 63 import org.dspace.app.webui.util.JSPManager; 64 import org.dspace.app.webui.util.UIUtil; 65 import org.dspace.authorize.AuthorizeException; 66 import org.dspace.authorize.AuthorizeManager; 67 import org.dspace.content.Bitstream; 68 import org.dspace.content.BitstreamFormat; 69 import org.dspace.content.Bundle; 70 import org.dspace.content.Collection; 71 import org.dspace.content.DSpaceObject; 72 import org.dspace.content.FormatIdentifier; 73 import org.dspace.content.Item; 74 import org.dspace.content.MetadataField; 75 import org.dspace.content.MetadataSchema; 76 import org.dspace.core.Constants; 77 import org.dspace.core.Context; 78 import org.dspace.core.LogManager; 79 import org.dspace.handle.HandleManager; 80 import org.dspace.search.DSIndexer; 81 import org.dspace.license.CreativeCommons; 82 83 89 public class EditItemServlet extends DSpaceServlet 90 { 91 92 public static final int START_DELETE = 1; 93 94 95 public static final int CONFIRM_DELETE = 2; 96 97 98 public static final int UPDATE_ITEM = 3; 99 100 101 public static final int START_WITHDRAW = 4; 102 103 104 public static final int CONFIRM_WITHDRAW = 5; 105 106 107 public static final int REINSTATE = 6; 108 109 110 private static Logger log = Logger.getLogger(EditCommunitiesServlet.class); 111 112 protected void doDSGet(Context context, HttpServletRequest request, 113 HttpServletResponse response) throws ServletException , IOException , 114 SQLException , AuthorizeException 115 { 116 123 int internalID = UIUtil.getIntParameter(request, "item_id"); 124 String handle = request.getParameter("handle"); 125 boolean showError = false; 126 127 Item itemToEdit = null; 129 130 if (internalID > 0) 131 { 132 itemToEdit = Item.find(context, internalID); 133 134 showError = (itemToEdit == null); 135 } 136 else if ((handle != null) && !handle.equals("")) 137 { 138 DSpaceObject dso = HandleManager.resolveToObject(context, handle); 140 141 if ((dso != null) && (dso.getType() == Constants.ITEM)) 143 { 144 itemToEdit = (Item) dso; 145 showError = false; 146 } 147 else 148 { 149 showError = true; 150 } 151 } 152 153 if (itemToEdit != null) 155 { 156 checkEditAuthorization(context, itemToEdit); 158 showEditForm(context, request, response, itemToEdit); 159 } 160 else 161 { 162 if (showError) 163 { 164 request.setAttribute("invalid.id", new Boolean (true)); 165 } 166 167 JSPManager.showJSP(request, response, "/tools/get-item-id.jsp"); 168 } 169 } 170 171 protected void doDSPost(Context context, HttpServletRequest request, 172 HttpServletResponse response) throws ServletException , IOException , 173 SQLException , AuthorizeException 174 { 175 String contentType = request.getContentType(); 177 178 if ((contentType != null) 179 && (contentType.indexOf("multipart/form-data") != -1)) 180 { 181 processUploadBitstream(context, request, response); 183 184 return; 185 } 186 187 191 if (request.getParameter("submit_cancel") != null) 192 { 193 JSPManager.showJSP(request, response, "/tools/get-item-id.jsp"); 194 195 return; 196 } 197 198 202 int action = UIUtil.getIntParameter(request, "action"); 203 204 Item item = Item.find(context, UIUtil.getIntParameter(request, 205 "item_id")); 206 207 String handle = HandleManager.findHandle(context, item); 208 209 checkEditAuthorization(context, item); 211 212 request.setAttribute("item", item); 213 request.setAttribute("handle", handle); 214 215 switch (action) 216 { 217 case START_DELETE: 218 219 JSPManager.showJSP(request, response, 221 "/tools/confirm-delete-item.jsp"); 222 223 break; 224 225 case CONFIRM_DELETE: 226 227 Collection[] collections = item.getCollections(); 231 232 for (int i = 0; i < collections.length; i++) 234 { 235 collections[i].removeItem(item); 236 } 237 238 JSPManager.showJSP(request, response, "/tools/get-item-id.jsp"); 239 context.complete(); 240 241 break; 242 243 case UPDATE_ITEM: 244 processUpdateItem(context, request, response, item); 245 246 break; 247 248 case START_WITHDRAW: 249 250 JSPManager.showJSP(request, response, 252 "/tools/confirm-withdraw-item.jsp"); 253 254 break; 255 256 case CONFIRM_WITHDRAW: 257 258 item.withdraw(); 260 JSPManager.showJSP(request, response, "/tools/get-item-id.jsp"); 261 context.complete(); 262 263 break; 264 265 case REINSTATE: 266 item.reinstate(); 267 JSPManager.showJSP(request, response, "/tools/get-item-id.jsp"); 268 context.complete(); 269 270 break; 271 272 default: 273 274 log.warn(LogManager.getHeader(context, "integrity_error", UIUtil 276 .getRequestLogInfo(request))); 277 JSPManager.showIntegrityError(request, response); 278 } 279 } 280 281 287 private void checkEditAuthorization(Context c, Item item) 288 throws AuthorizeException, java.sql.SQLException 289 { 290 if (!item.canEdit()) 291 { 292 int userID = 0; 293 294 if (c.getCurrentUser() != null) 296 { 297 userID = c.getCurrentUser().getID(); 298 } 299 300 throw new AuthorizeException("EditItemServlet: User " + userID 302 + " not authorized to edit item " + item.getID()); 303 } 304 } 305 306 318 private void showEditForm(Context context, HttpServletRequest request, 319 HttpServletResponse response, Item item) throws ServletException , 320 IOException , SQLException , AuthorizeException 321 { 322 if ( request.getParameter("cc_license_url") != null ) 323 { 324 CreativeCommons.setLicense( context, item, 326 request.getParameter("cc_license_url") ); 327 context.commit(); 328 } 329 330 String handle = HandleManager.findHandle(context, item); 332 333 Collection[] collections = item.getCollections(); 335 336 MetadataField[] types = MetadataField.findAll(context); 338 339 HashMap metadataFields = new HashMap (); 341 342 MetadataSchema[] schemas = MetadataSchema.findAll(context); 344 for (int i = 0; i < schemas.length; i++) 345 { 346 String schemaName = schemas[i].getName(); 347 MetadataField[] fields = MetadataField.findAllInSchema(context, schemas[i].getSchemaID()); 349 for (int j = 0; j < fields.length; j++) 350 { 351 Integer fieldID = new Integer (fields[j].getFieldID()); 352 String displayName = ""; 353 displayName = schemaName + "." + fields[j].getElement() + (fields[j].getQualifier() == null ? "" : "." + fields[j].getQualifier()); 354 metadataFields.put(fieldID, displayName); 355 } 356 } 357 358 request.setAttribute("item", item); 359 request.setAttribute("handle", handle); 360 request.setAttribute("collections", collections); 361 request.setAttribute("dc.types", types); 362 request.setAttribute("metadataFields", metadataFields); 363 364 JSPManager.showJSP(request, response, "/tools/edit-item-form.jsp"); 365 } 366 367 379 private void processUpdateItem(Context context, HttpServletRequest request, 380 HttpServletResponse response, Item item) throws ServletException , 381 IOException , SQLException , AuthorizeException 382 { 383 String button = UIUtil.getSubmitButton(request, "submit"); 384 388 item.clearMetadata(Item.ANY, Item.ANY, Item.ANY, Item.ANY); 389 390 Enumeration unsortedParamNames = request.getParameterNames(); 394 395 List sortedParamNames = new LinkedList (); 397 398 while (unsortedParamNames.hasMoreElements()) 399 { 400 sortedParamNames.add(unsortedParamNames.nextElement()); 401 } 402 403 Collections.sort(sortedParamNames); 405 406 Iterator iterator = sortedParamNames.iterator(); 407 408 while (iterator.hasNext()) 409 { 410 String p = (String ) iterator.next(); 411 412 if (p.startsWith("value")) 413 { 414 420 StringTokenizer st = new StringTokenizer (p, "_"); 421 422 st.nextToken(); 424 String schema = st.nextToken(); 425 426 String element = st.nextToken(); 427 428 String qualifier = null; 429 430 if (st.countTokens() == 2) 431 { 432 qualifier = st.nextToken(); 433 } 434 435 String sequenceNumber = st.nextToken(); 436 437 String key = MetadataField.formKey(schema,element,qualifier); 440 441 String language = request.getParameter("language_" + key + "_" 443 + sequenceNumber); 444 445 if ((language != null) && language.equals("")) 447 { 448 language = null; 449 } 450 451 String value = request.getParameter(p).trim(); 453 454 if (!(value.equals("") || button.equals("submit_remove_" + key 457 + "_" + sequenceNumber))) 458 { 459 item.addMetadata(schema, element, qualifier, language, value); 461 } 462 } 463 else if (p.startsWith("bitstream_name") 465 && AuthorizeManager.isAdmin(context)) 466 { 467 StringTokenizer st = new StringTokenizer (p, "_"); 471 472 st.nextToken(); 474 st.nextToken(); 475 476 int bundleID = Integer.parseInt(st.nextToken()); 478 int bitstreamID = Integer.parseInt(st.nextToken()); 479 480 Bundle bundle = Bundle.find(context, bundleID); 481 Bitstream bitstream = Bitstream.find(context, bitstreamID); 482 483 String key = String.valueOf(bundleID) + "_" + bitstreamID; 486 487 if (button.equals("submit_delete_bitstream_" + key)) 489 { 490 bundle.removeBitstream(bitstream); 492 493 if (bundle.getBitstreams().length == 0) 495 { 496 item.removeBundle(bundle); 497 } 498 } 499 else 500 { 501 String name = request.getParameter(p); 503 String source = request.getParameter("bitstream_source_" 504 + key); 505 String desc = request.getParameter("bitstream_description_" 506 + key); 507 int formatID = UIUtil.getIntParameter(request, 508 "bitstream_format_id_" + key); 509 String userFormatDesc = request 510 .getParameter("bitstream_user_format_description_" 511 + key); 512 int primaryBitstreamID = UIUtil.getIntParameter(request, 513 bundleID + "_primary_bitstream_id"); 514 515 if (source.equals("")) 517 { 518 source = null; 519 } 520 521 if (desc.equals("")) 522 { 523 desc = null; 524 } 525 526 if (userFormatDesc.equals("")) 527 { 528 userFormatDesc = null; 529 } 530 531 bitstream.setName(name); 532 bitstream.setSource(source); 533 bitstream.setDescription(desc); 534 bitstream 535 .setFormat(BitstreamFormat.find(context, formatID)); 536 537 if (primaryBitstreamID > 0) 538 { 539 bundle.setPrimaryBitstreamID(primaryBitstreamID); 540 } 541 542 if (userFormatDesc != null) 543 { 544 bitstream.setUserFormatDescription(userFormatDesc); 545 } 546 547 bitstream.update(); 548 bundle.update(); 549 } 550 } 551 } 552 553 item.update(); 554 555 559 if (button.equals("submit_addfield")) 560 { 561 int dcTypeID = UIUtil.getIntParameter(request, "addfield_dctype"); 563 String value = request.getParameter("addfield_value").trim(); 564 String lang = request.getParameter("addfield_language"); 565 566 if (lang.equals("")) 568 { 569 lang = null; 570 } 571 572 MetadataField field = MetadataField.find(context, dcTypeID); 573 MetadataSchema schema = MetadataSchema.find(context, field 574 .getSchemaID()); 575 item.addMetadata(schema.getName(), field.getElement(), field 576 .getQualifier(), lang, value); 577 item.update(); 578 } 579 580 if (button.equals("submit_addcc")) 581 { 582 request.setAttribute("item", item); 584 JSPManager 585 .showJSP(request, response, "/tools/creative-commons-edit.jsp"); 586 } 587 588 if (button.equals("submit_addbitstream")) 589 { 590 request.setAttribute("item", item); 592 JSPManager 593 .showJSP(request, response, "/tools/upload-bitstream.jsp"); 594 } 595 else 596 { 597 showEditForm(context, request, response, item); 599 } 600 601 DSIndexer.reIndexContent(context, item); 603 604 context.complete(); 606 } 607 608 618 private void processUploadBitstream(Context context, 619 HttpServletRequest request, HttpServletResponse response) 620 throws ServletException , IOException , SQLException , 621 AuthorizeException 622 { 623 FileUploadRequest wrapper = new FileUploadRequest(request); 625 Bitstream b = null; 626 627 Item item = Item.find(context, UIUtil.getIntParameter(wrapper, 628 "item_id")); 629 630 File temp = wrapper.getFile("file"); 631 632 InputStream is = new BufferedInputStream (new FileInputStream (temp)); 634 635 checkEditAuthorization(context, item); 637 638 Bundle[] bundles = item.getBundles("ORIGINAL"); 640 641 if (bundles.length < 1) 642 { 643 b = item.createSingleBitstream(is, "ORIGINAL"); 645 } 646 else 647 { 648 b = bundles[0].createBitstream(is); 650 } 651 652 String noPath = wrapper.getFilesystemName("file"); 655 656 while (noPath.indexOf('/') > -1) 657 { 658 noPath = noPath.substring(noPath.indexOf('/') + 1); 659 } 660 661 while (noPath.indexOf('\\') > -1) 662 { 663 noPath = noPath.substring(noPath.indexOf('\\') + 1); 664 } 665 666 b.setName(noPath); 667 b.setSource(wrapper.getFilesystemName("file")); 668 669 BitstreamFormat bf = FormatIdentifier.guessFormat(context, b); 671 b.setFormat(bf); 672 b.update(); 673 674 item.update(); 675 676 showEditForm(context, request, response, item); 678 679 temp.delete(); 681 682 context.complete(); 684 } 685 } | Popular Tags |