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.List ; 49 50 import javax.servlet.ServletException ; 51 import javax.servlet.http.HttpServletRequest ; 52 import javax.servlet.http.HttpServletResponse ; 53 54 import org.apache.log4j.Logger; 55 import org.dspace.app.webui.servlet.DSpaceServlet; 56 import org.dspace.app.webui.util.FileUploadRequest; 57 import org.dspace.app.webui.util.JSPManager; 58 import org.dspace.app.webui.util.UIUtil; 59 import org.dspace.authorize.AuthorizeException; 60 import org.dspace.authorize.AuthorizeManager; 61 import org.dspace.content.Bitstream; 62 import org.dspace.content.BitstreamFormat; 63 import org.dspace.content.Collection; 64 import org.dspace.content.Community; 65 import org.dspace.content.FormatIdentifier; 66 import org.dspace.content.Item; 67 import org.dspace.content.MetadataField; 68 import org.dspace.content.MetadataSchema; 69 import org.dspace.core.Constants; 70 import org.dspace.core.Context; 71 import org.dspace.core.LogManager; 72 import org.dspace.eperson.EPerson; 73 import org.dspace.eperson.Group; 74 75 81 public class CollectionWizardServlet extends DSpaceServlet 82 { 83 84 public final static int INITIAL_QUESTIONS = 1; 85 86 87 public final static int BASIC_INFO = 2; 88 89 90 public final static int PERMISSIONS = 3; 91 92 93 public final static int DEFAULT_ITEM = 4; 94 95 96 public final static int SUMMARY = 5; 97 98 99 public final static int PERM_READ = 10; 100 101 102 public final static int PERM_SUBMIT = 11; 103 104 105 public final static int PERM_WF1 = 12; 106 107 108 public final static int PERM_WF2 = 13; 109 110 111 public final static int PERM_WF3 = 14; 112 113 114 public final static int PERM_ADMIN = 15; 115 116 117 private static Logger log = Logger.getLogger(CollectionWizardServlet.class); 118 119 protected void doDSGet(Context context, HttpServletRequest request, 120 HttpServletResponse response) throws ServletException , IOException , 121 SQLException , AuthorizeException 122 { 123 127 doDSPost(context, request, response); 128 } 129 130 protected void doDSPost(Context context, HttpServletRequest request, 131 HttpServletResponse response) throws ServletException , IOException , 132 SQLException , AuthorizeException 133 { 134 144 145 String contentType = request.getContentType(); 148 149 if ((contentType != null) 150 && (contentType.indexOf("multipart/form-data") != -1)) 151 { 152 processBasicInfo(context, request, response); 154 155 return; 156 } 157 158 int communityID = UIUtil.getIntParameter(request, "community_id"); 159 160 if (communityID > -1) 161 { 162 Community c = Community.find(context, communityID); 164 165 if (c == null) 166 { 167 log.warn(LogManager.getHeader(context, "integrity_error", 168 UIUtil.getRequestLogInfo(request))); 169 JSPManager.showIntegrityError(request, response); 170 171 return; 172 } 173 174 Collection newCollection = c.createCollection(); 176 request.setAttribute("collection", newCollection); 177 if (AuthorizeManager.isAdmin(context)) 178 { 179 request.setAttribute("admin_button", new Boolean (true)); 181 } 182 JSPManager.showJSP(request, response, 183 "/dspace-admin/wizard-questions.jsp"); 184 context.complete(); 185 } 186 else 187 { 188 int collectionID = UIUtil.getIntParameter(request, "collection_id"); 190 int stage = UIUtil.getIntParameter(request, "stage"); 191 192 Collection collection = Collection.find(context, collectionID); 194 195 request.setAttribute("collection", collection); 197 198 if (collection == null) 199 { 200 log.warn(LogManager.getHeader(context, "integrity_error", 201 UIUtil.getRequestLogInfo(request))); 202 JSPManager.showIntegrityError(request, response); 203 204 return; 205 } 206 207 request.setAttribute("collection.id", String.valueOf(collection 209 .getID())); 210 211 switch (stage) 212 { 213 case INITIAL_QUESTIONS: 214 processInitialQuestions(context, request, response, collection); 215 216 break; 217 218 case PERMISSIONS: 219 processPermissions(context, request, response, collection); 220 221 break; 222 223 case DEFAULT_ITEM: 224 processDefaultItem(context, request, response, collection); 225 226 break; 227 228 default: 229 log.warn(LogManager.getHeader(context, "integrity_error", 230 UIUtil.getRequestLogInfo(request))); 231 JSPManager.showIntegrityError(request, response); 232 } 233 } 234 } 235 236 248 private void processInitialQuestions(Context context, 249 HttpServletRequest request, HttpServletResponse response, 250 Collection collection) throws SQLException , ServletException , 251 IOException , AuthorizeException 252 { 253 Group anonymousGroup = Group.find(context, 0); 254 255 if (!UIUtil.getBoolParameter(request, "public_read")) 258 { 259 AuthorizeManager.removePoliciesActionFilter(context, collection, 261 Constants.DEFAULT_ITEM_READ); 262 AuthorizeManager.removePoliciesActionFilter(context, collection, 263 Constants.DEFAULT_BITSTREAM_READ); 264 } 265 266 if (UIUtil.getBoolParameter(request, "submitters")) 268 { 269 Group g = collection.createSubmitters(); 271 } 272 273 for (int i = 1; i <= 3; i++) 275 { 276 if (UIUtil.getBoolParameter(request, "workflow" + i)) 277 { 278 Group g = collection.createWorkflowGroup(i); 280 } 281 } 282 283 if (UIUtil.getBoolParameter(request, "admins")) 285 { 286 Group g = collection.createAdministrators(); 288 } 289 290 if (UIUtil.getBoolParameter(request, "default.item")) 292 { 293 collection.createTemplateItem(); 294 } 295 296 collection.setMetadata("name", ""); 298 collection.update(); 299 300 JSPManager.showJSP(request, response, 302 "/dspace-admin/wizard-basicinfo.jsp"); 303 context.complete(); 304 } 305 306 318 private void processPermissions(Context context, 319 HttpServletRequest request, HttpServletResponse response, 320 Collection collection) throws SQLException , ServletException , 321 IOException , AuthorizeException 322 { 323 int permission = UIUtil.getIntParameter(request, "permission"); 325 326 if (UIUtil.getBoolParameter(request, "mitgroup")) 328 { 329 Group mitGroup = Group.findByName(context, "MIT Users"); 330 int action; 331 332 if (permission == PERM_READ) 333 { 334 AuthorizeManager.addPolicy(context, collection, 336 Constants.DEFAULT_ITEM_READ, mitGroup); 337 AuthorizeManager.addPolicy(context, collection, 338 Constants.DEFAULT_BITSTREAM_READ, mitGroup); 339 } 340 else 341 { 342 AuthorizeManager.addPolicy(context, collection, Constants.ADD, 344 mitGroup); 345 } 346 } 347 348 Group g = null; 351 352 switch (permission) 353 { 354 case PERM_READ: 355 356 g = Group.create(context); 358 359 g 361 .setName("COLLECTION_" + collection.getID() 362 + "_DEFAULT_ITEM_READ"); 363 364 AuthorizeManager.addPolicy(context, collection, 366 Constants.DEFAULT_ITEM_READ, g); 367 AuthorizeManager.addPolicy(context, collection, 368 Constants.DEFAULT_BITSTREAM_READ, g); 369 370 break; 371 372 case PERM_SUBMIT: 373 g = collection.getSubmitters(); 374 375 break; 376 377 case PERM_WF1: 378 g = collection.getWorkflowGroup(1); 379 380 break; 381 382 case PERM_WF2: 383 g = collection.getWorkflowGroup(2); 384 385 break; 386 387 case PERM_WF3: 388 g = collection.getWorkflowGroup(3); 389 390 break; 391 392 case PERM_ADMIN: 393 g = collection.getAdministrators(); 394 395 break; 396 } 397 398 int[] eperson_ids = UIUtil.getIntParameters(request, "eperson_id"); 400 int[] group_ids = UIUtil.getIntParameters(request, "group_ids"); 401 402 if (eperson_ids != null) 403 { 404 for (int i = 0; i < eperson_ids.length; i++) 405 { 406 EPerson eperson = EPerson.find(context, eperson_ids[i]); 407 408 if (eperson != null) 409 { 410 g.addMember(eperson); 411 } 412 } 413 } 414 415 if (group_ids != null) 416 { 417 for (int i = 0; i < group_ids.length; i++) 418 { 419 Group group = Group.find(context, group_ids[i]); 420 421 if (group != null) 422 { 423 g.addMember(group); 424 } 425 } 426 } 427 428 429 g.update(); 431 432 showNextPage(context, request, response, collection, permission); 433 434 context.complete(); 435 } 436 437 449 private void processBasicInfo(Context context, HttpServletRequest request, 450 HttpServletResponse response) throws SQLException , 451 ServletException , IOException , AuthorizeException 452 { 453 FileUploadRequest wrapper = new FileUploadRequest(request); 455 456 Collection collection = Collection.find(context, UIUtil 457 .getIntParameter(wrapper, "collection_id")); 458 459 if (collection == null) 460 { 461 log.warn(LogManager.getHeader(context, "integrity_error", UIUtil 462 .getRequestLogInfo(wrapper))); 463 JSPManager.showIntegrityError(request, response); 464 465 return; 466 } 467 468 collection.setMetadata("name", wrapper.getParameter("name")); 470 collection.setMetadata("short_description", wrapper 471 .getParameter("short_description")); 472 collection.setMetadata("introductory_text", wrapper 473 .getParameter("introductory_text")); 474 collection.setMetadata("copyright_text", wrapper 475 .getParameter("copyright_text")); 476 collection.setMetadata("side_bar_text", wrapper 477 .getParameter("side_bar_text")); 478 collection.setMetadata("provenance_description", wrapper 479 .getParameter("provenance_description")); 480 481 String license = wrapper.getParameter("license"); 484 485 if ((license != null) || "".equals(license)) 486 { 487 collection.setLicense(license); 488 } 489 490 File temp = wrapper.getFile("file"); 491 492 if (temp != null) 493 { 494 InputStream is = new BufferedInputStream (new FileInputStream (temp)); 496 Bitstream logoBS = collection.setLogo(is); 497 498 String noPath = wrapper.getFilesystemName("file"); 501 502 while (noPath.indexOf('/') > -1) 503 { 504 noPath = noPath.substring(noPath.indexOf('/') + 1); 505 } 506 507 while (noPath.indexOf('\\') > -1) 508 { 509 noPath = noPath.substring(noPath.indexOf('\\') + 1); 510 } 511 512 logoBS.setName(noPath); 513 logoBS.setSource(wrapper.getFilesystemName("file")); 514 515 BitstreamFormat bf = FormatIdentifier.guessFormat(context, logoBS); 517 logoBS.setFormat(bf); 518 logoBS.update(); 519 520 temp.delete(); 522 } 523 524 collection.update(); 525 526 showNextPage(context, request, response, collection, BASIC_INFO); 528 529 context.complete(); 530 } 531 532 544 private void processDefaultItem(Context context, 545 HttpServletRequest request, HttpServletResponse response, 546 Collection collection) throws SQLException , ServletException , 547 IOException , AuthorizeException 548 { 549 Item item = collection.getTemplateItem(); 550 551 for (int i = 0; i < 10; i++) 552 { 553 int dcTypeID = UIUtil.getIntParameter(request, "dctype_" + i); 554 String value = request.getParameter("value_" + i); 555 String lang = request.getParameter("lang_" + i); 556 557 if ((dcTypeID != -1) && (value != null) && !value.equals("")) 558 { 559 MetadataField field = MetadataField.find(context,dcTypeID); 560 MetadataSchema schema = MetadataSchema.find(context,field.getSchemaID()); 561 item.addMetadata(schema.getName(),field.getElement(), field.getQualifier(), lang, value); 562 } 563 } 564 565 item.update(); 566 567 showNextPage(context, request, response, collection, DEFAULT_ITEM); 569 570 context.complete(); 571 } 572 573 588 private void showNextPage(Context context, HttpServletRequest request, 589 HttpServletResponse response, Collection collection, int stage) 590 throws SQLException , ServletException , IOException , 591 AuthorizeException 592 { 593 request.setAttribute("collection", collection); 595 596 if (Group.findByName(context, "MIT Users") != null) 598 { 599 request.setAttribute("mitgroup", new Boolean (true)); 600 } 601 602 log.debug(LogManager.getHeader(context, "nextpage", "stage=" + stage)); 603 604 switch (stage) 605 { 606 case BASIC_INFO: 607 608 List anonReadPols = AuthorizeManager.getPoliciesActionFilter( 611 context, collection, Constants.DEFAULT_ITEM_READ); 612 613 if (anonReadPols.size() == 0) 616 { 617 request.setAttribute("permission", new Integer (PERM_READ)); 618 JSPManager.showJSP(request, response, 619 "/dspace-admin/wizard-permissions.jsp"); 620 621 break; 622 } 623 624 case PERM_READ: 625 626 if (collection.getSubmitters() != null) 629 { 630 request.setAttribute("permission", new Integer (PERM_SUBMIT)); 631 JSPManager.showJSP(request, response, 632 "/dspace-admin/wizard-permissions.jsp"); 633 634 break; 635 } 636 637 case PERM_SUBMIT: 638 639 if (collection.getWorkflowGroup(1) != null) 642 { 643 request.setAttribute("permission", new Integer (PERM_WF1)); 644 JSPManager.showJSP(request, response, 645 "/dspace-admin/wizard-permissions.jsp"); 646 647 break; 648 } 649 650 case PERM_WF1: 651 652 if (collection.getWorkflowGroup(2) != null) 655 { 656 request.setAttribute("permission", new Integer (PERM_WF2)); 657 JSPManager.showJSP(request, response, 658 "/dspace-admin/wizard-permissions.jsp"); 659 660 break; 661 } 662 663 case PERM_WF2: 664 665 if (collection.getWorkflowGroup(3) != null) 668 { 669 request.setAttribute("permission", new Integer (PERM_WF3)); 670 JSPManager.showJSP(request, response, 671 "/dspace-admin/wizard-permissions.jsp"); 672 673 break; 674 } 675 676 case PERM_WF3: 677 678 if (collection.getAdministrators() != null) 681 { 682 request.setAttribute("permission", new Integer (PERM_ADMIN)); 683 JSPManager.showJSP(request, response, 684 "/dspace-admin/wizard-permissions.jsp"); 685 686 break; 687 } 688 689 case PERM_ADMIN: 690 691 if (collection.getTemplateItem() != null) 693 { 694 MetadataField[] types = MetadataField.findAll(context); 695 request.setAttribute("dctypes", types); 696 JSPManager.showJSP(request, response, 697 "/dspace-admin/wizard-default-item.jsp"); 698 699 break; 700 } 701 702 case DEFAULT_ITEM: 703 704 if (collection != null) 709 { 710 Community[] communities = collection.getCommunities(); 711 request.setAttribute("community", communities[0]); 712 713 if (AuthorizeManager.isAdmin(context)) 714 { 715 request.setAttribute("admin_button", new Boolean (true)); 717 } 718 } 719 720 JSPManager.showJSP(request, response, "/tools/edit-collection.jsp"); 721 722 break; 723 } 724 } 725 } 726 | Popular Tags |