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 49 import javax.servlet.ServletException ; 50 import javax.servlet.http.HttpServletRequest ; 51 import javax.servlet.http.HttpServletResponse ; 52 53 import org.apache.log4j.Logger; 54 import org.dspace.app.webui.servlet.DSpaceServlet; 55 import org.dspace.app.webui.util.FileUploadRequest; 56 import org.dspace.app.webui.util.JSPManager; 57 import org.dspace.app.webui.util.UIUtil; 58 import org.dspace.authorize.AuthorizeException; 59 import org.dspace.authorize.AuthorizeManager; 60 import org.dspace.content.Bitstream; 61 import org.dspace.content.BitstreamFormat; 62 import org.dspace.content.Collection; 63 import org.dspace.content.Community; 64 import org.dspace.content.FormatIdentifier; 65 import org.dspace.content.Item; 66 import org.dspace.core.Constants; 67 import org.dspace.core.Context; 68 import org.dspace.core.LogManager; 69 import org.dspace.eperson.Group; 70 71 78 public class EditCommunitiesServlet extends DSpaceServlet 79 { 80 81 public static final int START_EDIT_COMMUNITY = 1; 82 83 84 public static final int START_DELETE_COMMUNITY = 2; 85 86 87 public static final int START_CREATE_COMMUNITY = 3; 88 89 90 public static final int START_EDIT_COLLECTION = 4; 91 92 93 public static final int START_DELETE_COLLECTION = 5; 94 95 96 public static final int START_CREATE_COLLECTION = 6; 97 98 99 public static final int CONFIRM_EDIT_COMMUNITY = 7; 100 101 102 public static final int CONFIRM_DELETE_COMMUNITY = 8; 103 104 105 public static final int CONFIRM_EDIT_COLLECTION = 9; 106 107 108 public static final int CONFIRM_DELETE_COLLECTION = 10; 109 110 111 private static Logger log = Logger.getLogger(EditCommunitiesServlet.class); 112 113 protected void doDSGet(Context context, HttpServletRequest request, 114 HttpServletResponse response) throws ServletException , IOException , 115 SQLException , AuthorizeException 116 { 117 showControls(context, request, response); 119 } 120 121 protected void doDSPost(Context context, HttpServletRequest request, 122 HttpServletResponse response) throws ServletException , IOException , 123 SQLException , AuthorizeException 124 { 125 String contentType = request.getContentType(); 127 128 if ((contentType != null) 129 && (contentType.indexOf("multipart/form-data") != -1)) 130 { 131 processUploadLogo(context, request, response); 133 134 return; 135 } 136 137 141 int action = UIUtil.getIntParameter(request, "action"); 142 143 148 Community community = Community.find(context, UIUtil.getIntParameter( 149 request, "community_id")); 150 Community parentCommunity = Community.find(context, UIUtil 151 .getIntParameter(request, "parent_community_id")); 152 Collection collection = Collection.find(context, UIUtil 153 .getIntParameter(request, "collection_id")); 154 155 request.setAttribute("community", community); 157 request.setAttribute("parent", parentCommunity); 158 request.setAttribute("collection", collection); 159 160 164 if (request.getParameter("submit_cancel") != null) 165 { 166 showControls(context, request, response); 167 168 return; 169 } 170 171 if (AuthorizeManager.isAdmin(context)) 172 { 173 request.setAttribute("admin_button", new Boolean (true)); 175 } 176 177 switch (action) 179 { 180 case START_EDIT_COMMUNITY: 181 182 JSPManager.showJSP(request, response, "/tools/edit-community.jsp"); 184 185 break; 186 187 case START_DELETE_COMMUNITY: 188 189 JSPManager.showJSP(request, response, 191 "/tools/confirm-delete-community.jsp"); 192 193 break; 194 195 case START_CREATE_COMMUNITY: 196 197 JSPManager.showJSP(request, response, "/tools/edit-community.jsp"); 199 200 break; 201 202 case START_EDIT_COLLECTION: 203 204 JSPManager.showJSP(request, response, "/tools/edit-collection.jsp"); 206 207 break; 208 209 case START_DELETE_COLLECTION: 210 211 JSPManager.showJSP(request, response, 213 "/tools/confirm-delete-collection.jsp"); 214 215 break; 216 217 case START_CREATE_COLLECTION: 218 219 response.sendRedirect(response.encodeRedirectURL(request 221 .getContextPath() 222 + "/tools/collection-wizard?community_id=" 223 + community.getID())); 224 225 break; 226 227 case CONFIRM_EDIT_COMMUNITY: 228 229 processConfirmEditCommunity(context, request, response, community); 231 232 break; 233 234 case CONFIRM_DELETE_COMMUNITY: 235 236 Community parent = community.getParentCommunity(); 238 239 community.delete(); 241 242 if (parent == null) 244 { 245 response.sendRedirect(response.encodeRedirectURL(request 246 .getContextPath() 247 + "/community-list")); 248 } 249 else 250 { 252 response.sendRedirect(response.encodeRedirectURL(request 253 .getContextPath() 254 + "/handle/" + parent.getHandle())); 255 } 256 257 context.complete(); 261 262 break; 263 264 case CONFIRM_EDIT_COLLECTION: 265 266 processConfirmEditCollection(context, request, response, community, 268 collection); 269 270 break; 271 272 case CONFIRM_DELETE_COLLECTION: 273 274 community.removeCollection(collection); 276 277 showControls(context, request, response); 279 280 context.complete(); 282 283 break; 284 285 default: 286 287 log.warn(LogManager.getHeader(context, "integrity_error", UIUtil 289 .getRequestLogInfo(request))); 290 JSPManager.showIntegrityError(request, response); 291 } 292 } 293 294 304 private void showControls(Context context, HttpServletRequest request, 305 HttpServletResponse response) throws ServletException , IOException , 306 SQLException , AuthorizeException 307 { 308 Community community = (Community) request.getAttribute("community"); 312 313 if (community != null) 314 { 315 response.sendRedirect(response.encodeRedirectURL(request 316 .getContextPath() 317 + "/handle/" + community.getHandle())); 318 } 319 else 320 { 321 Community parent = (Community) request.getAttribute("parent"); 323 324 if (parent != null) 325 { 326 response.sendRedirect(response.encodeRedirectURL(request 327 .getContextPath() 328 + "/handle/" + parent.getHandle())); 329 } 330 else 331 { 332 response.sendRedirect(response.encodeRedirectURL(request 334 .getContextPath() 335 + "/community-list")); 336 } 337 } 338 } 339 340 352 private void processConfirmEditCommunity(Context context, 353 HttpServletRequest request, HttpServletResponse response, 354 Community community) throws ServletException , IOException , 355 SQLException , AuthorizeException 356 { 357 if (request.getParameter("create").equals("true")) 358 { 359 int parentCommunityID = UIUtil.getIntParameter(request, 362 "parent_community_id"); 363 364 if (parentCommunityID != -1) 365 { 366 Community parent = Community.find(context, parentCommunityID); 367 368 if (parent != null) 369 { 370 community = parent.createSubcommunity(); 371 } 372 } 373 else 374 { 375 community = Community.create(null, context); 376 } 377 378 request.setAttribute("community", community); 380 } 381 382 community.setMetadata("name", request.getParameter("name")); 383 community.setMetadata("short_description", request 384 .getParameter("short_description")); 385 386 String intro = request.getParameter("introductory_text"); 387 388 if (intro.equals("")) 389 { 390 intro = null; 391 } 392 393 String copy = request.getParameter("copyright_text"); 394 395 if (copy.equals("")) 396 { 397 copy = null; 398 } 399 400 String side = request.getParameter("side_bar_text"); 401 402 if (side.equals("")) 403 { 404 side = null; 405 } 406 407 community.setMetadata("introductory_text", intro); 408 community.setMetadata("copyright_text", copy); 409 community.setMetadata("side_bar_text", side); 410 community.update(); 411 412 String button = UIUtil.getSubmitButton(request, "submit"); 414 415 if (button.equals("submit_set_logo")) 416 { 417 community.setLogo(null); 419 community.update(); 420 421 JSPManager.showJSP(request, response, 424 "/dspace-admin/upload-logo.jsp"); 425 } 426 else if (button.equals("submit_delete_logo")) 427 { 428 community.setLogo(null); 430 community.update(); 431 432 JSPManager.showJSP(request, response, "/tools/edit-community.jsp"); 434 } 435 else if (button.equals("submit_authorization_edit")) 436 { 437 response.sendRedirect(response.encodeRedirectURL(request 439 .getContextPath() 440 + "/dspace-admin/authorize?community_id=" 441 + community.getID() + "&submit_community_select=1")); 442 } 443 else 444 { 445 showControls(context, request, response); 447 } 448 449 context.complete(); 451 } 452 453 467 private void processConfirmEditCollection(Context context, 468 HttpServletRequest request, HttpServletResponse response, 469 Community community, Collection collection) 470 throws ServletException , IOException , SQLException , 471 AuthorizeException 472 { 473 if (request.getParameter("create").equals("true")) 474 { 475 collection = community.createCollection(); 477 request.setAttribute("collection", collection); 478 } 479 480 collection.setMetadata("name", request.getParameter("name")); 482 collection.setMetadata("short_description", request 483 .getParameter("short_description")); 484 485 String intro = request.getParameter("introductory_text"); 486 487 if (intro.equals("")) 488 { 489 intro = null; 490 } 491 492 String copy = request.getParameter("copyright_text"); 493 494 if (copy.equals("")) 495 { 496 copy = null; 497 } 498 499 String side = request.getParameter("side_bar_text"); 500 501 if (side.equals("")) 502 { 503 side = null; 504 } 505 506 String license = request.getParameter("license"); 507 508 if (license.equals("")) 509 { 510 license = null; 511 } 512 513 String provenance = request.getParameter("provenance_description"); 514 515 if (provenance.equals("")) 516 { 517 provenance = null; 518 } 519 520 collection.setMetadata("introductory_text", intro); 521 collection.setMetadata("copyright_text", copy); 522 collection.setMetadata("side_bar_text", side); 523 collection.setMetadata("license", license); 524 collection.setMetadata("provenance_description", provenance); 525 526 String button = UIUtil.getSubmitButton(request, "submit"); 528 529 if (button.equals("submit_set_logo")) 530 { 531 collection.setLogo(null); 533 534 JSPManager.showJSP(request, response, 537 "/dspace-admin/upload-logo.jsp"); 538 } 539 else if (button.equals("submit_delete_logo")) 540 { 541 collection.setLogo(null); 543 544 JSPManager.showJSP(request, response, "/tools/edit-collection.jsp"); 546 } 547 else if (button.startsWith("submit_wf_create_")) 548 { 549 int step = Integer.parseInt(button.substring(17)); 550 551 Group newGroup = Group.create(context); 553 newGroup.setName("COLLECTION_" + collection.getID() + "_WFSTEP_" 554 + step); 555 newGroup.update(); 556 collection.setWorkflowGroup(step, newGroup); 557 collection.update(); 558 559 response.sendRedirect(response.encodeRedirectURL(request 561 .getContextPath() 562 + "/tools/group-edit?group_id=" + newGroup.getID())); 563 } 564 else if (button.equals("submit_admins_create")) 565 { 566 Group newGroup = collection.createAdministrators(); 568 569 response.sendRedirect(response.encodeRedirectURL(request 571 .getContextPath() 572 + "/tools/group-edit?group_id=" + newGroup.getID())); 573 } 574 else if (button.equals("submit_submitters_create")) 575 { 576 Group newGroup = collection.createSubmitters(); 578 579 response.sendRedirect(response.encodeRedirectURL(request 581 .getContextPath() 582 + "/tools/group-edit?group_id=" + newGroup.getID())); 583 } 584 else if (button.equals("submit_authorization_edit")) 585 { 586 response.sendRedirect(response.encodeRedirectURL(request 588 .getContextPath() 589 + "/dspace-admin/authorize?collection_id=" 590 + collection.getID() + "&submit_collection_select=1")); 591 } 592 else if (button.startsWith("submit_wf_edit_")) 593 { 594 int step = Integer.parseInt(button.substring(15)); 595 596 Group g = collection.getWorkflowGroup(step); 598 response.sendRedirect(response.encodeRedirectURL(request 599 .getContextPath() 600 + "/tools/group-edit?group_id=" + g.getID())); 601 } 602 else if (button.equals("submit_submitters_edit")) 603 { 604 Group g = collection.getSubmitters(); 606 response.sendRedirect(response.encodeRedirectURL(request 607 .getContextPath() 608 + "/tools/group-edit?group_id=" + g.getID())); 609 } 610 else if (button.equals("submit_admins_edit")) 611 { 612 Group g = collection.getAdministrators(); 614 response.sendRedirect(response.encodeRedirectURL(request 615 .getContextPath() 616 + "/tools/group-edit?group_id=" + g.getID())); 617 } 618 else if (button.startsWith("submit_wf_delete_")) 619 { 620 int step = Integer.parseInt(button.substring(17)); 622 623 Group g = collection.getWorkflowGroup(step); 624 collection.setWorkflowGroup(step, null); 625 626 collection.update(); 628 g.delete(); 629 630 JSPManager.showJSP(request, response, "/tools/edit-collection.jsp"); 632 } 633 else if (button.equals("submit_create_template")) 634 { 635 collection.createTemplateItem(); 637 638 Item i = collection.getTemplateItem(); 640 i.setOwningCollection(collection); 641 642 i.update(); 644 collection.update(); 645 context.complete(); 646 response.sendRedirect(response.encodeRedirectURL(request 647 .getContextPath() 648 + "/tools/edit-item?item_id=" + i.getID())); 649 650 return; 651 } 652 else if (button.equals("submit_edit_template")) 653 { 654 Item i = collection.getTemplateItem(); 656 response.sendRedirect(response.encodeRedirectURL(request 657 .getContextPath() 658 + "/tools/edit-item?item_id=" + i.getID())); 659 } 660 else if (button.equals("submit_delete_template")) 661 { 662 collection.removeTemplateItem(); 663 664 JSPManager.showJSP(request, response, "/tools/edit-collection.jsp"); 666 } 667 else 668 { 669 showControls(context, request, response); 671 } 672 673 collection.update(); 675 context.complete(); 676 } 677 678 688 private void processUploadLogo(Context context, HttpServletRequest request, 689 HttpServletResponse response) throws ServletException , IOException , 690 SQLException , AuthorizeException 691 { 692 FileUploadRequest wrapper = new FileUploadRequest(request); 694 695 Community community = Community.find(context, UIUtil.getIntParameter( 696 wrapper, "community_id")); 697 Collection collection = Collection.find(context, UIUtil 698 .getIntParameter(wrapper, "collection_id")); 699 700 File temp = wrapper.getFile("file"); 701 702 InputStream is = new BufferedInputStream (new FileInputStream (temp)); 704 Bitstream logoBS; 705 706 if (collection == null) 707 { 708 logoBS = community.setLogo(is); 709 } 710 else 711 { 712 logoBS = collection.setLogo(is); 713 } 714 715 String noPath = wrapper.getFilesystemName("file"); 718 719 while (noPath.indexOf('/') > -1) 720 { 721 noPath = noPath.substring(noPath.indexOf('/') + 1); 722 } 723 724 while (noPath.indexOf('\\') > -1) 725 { 726 noPath = noPath.substring(noPath.indexOf('\\') + 1); 727 } 728 729 logoBS.setName(noPath); 730 logoBS.setSource(wrapper.getFilesystemName("file")); 731 732 BitstreamFormat bf = FormatIdentifier.guessFormat(context, logoBS); 734 logoBS.setFormat(bf); 735 AuthorizeManager.addPolicy(context, logoBS, Constants.WRITE, context 736 .getCurrentUser()); 737 logoBS.update(); 738 739 if (AuthorizeManager.isAdmin(context)) 740 { 741 request.setAttribute("admin_button", new Boolean (true)); 743 } 744 745 if (collection == null) 746 { 747 community.update(); 748 749 request.setAttribute("community", community); 751 JSPManager.showJSP(request, response, "/tools/edit-community.jsp"); 752 } 753 else 754 { 755 collection.update(); 756 757 request.setAttribute("collection", collection); 759 request.setAttribute("community", community); 760 JSPManager.showJSP(request, response, "/tools/edit-collection.jsp"); 761 } 762 763 temp.delete(); 765 766 context.complete(); 768 } 769 } 770 | Popular Tags |