/** * Look up the id of a group authorized for one of the given roles. If no group is currently * authorized to perform this role then a new group will be created and assigned the role. * * @param context The current DSpace context. * @param collectionID The collection id. * @param roleName ADMIN, WF_STEP1, WF_STEP2, WF_STEP3, SUBMIT, DEFAULT_READ. * @return The id of the group associated with that particular role, or -1 if the role was not * found. */ public static int getCollectionRole(Context context, int collectionID, String roleName) throws SQLException, AuthorizeException, IOException, TransformerException, SAXException, WorkflowConfigurationException, ParserConfigurationException { Collection collection = Collection.find(context, collectionID); // Determine the group based upon wich role we are looking for. Group roleGroup = null; if (ROLE_ADMIN.equals(roleName)) { roleGroup = collection.getAdministrators(); if (roleGroup == null) { roleGroup = collection.createAdministrators(); } } else if (ROLE_SUBMIT.equals(roleName)) { roleGroup = collection.getSubmitters(); if (roleGroup == null) roleGroup = collection.createSubmitters(); } else { if (ConfigurationManager.getProperty("workflow", "workflow.framework") .equals("xmlworkflow")) { // Resolve our id to a role roleGroup = getXMLWorkflowRole(context, collectionID, roleName, collection, roleGroup); } else { roleGroup = getOriginalWorkflowRole(roleName, collection, roleGroup); } } // In case we needed to create a group, save our changes collection.update(); context.commit(); // If the role name was valid then role should be non null, if (roleGroup != null) return roleGroup.getID(); return -1; }
/** * Delete one of collection's roles * * @param context The current DSpace context. * @param collectionID The collection id. * @param roleName ADMIN, WF_STEP1, WF_STEP2, WF_STEP3, SUBMIT, DEFAULT_READ. * @param groupID The id of the group associated with this role. * @return A process result's object. */ public static FlowResult processDeleteCollectionRole( Context context, int collectionID, String roleName, int groupID) throws SQLException, UIException, IOException, AuthorizeException, WorkflowConfigurationException { FlowResult result = new FlowResult(); Collection collection = Collection.find(context, collectionID); Group role = Group.find(context, groupID); // First, Unregister the role if (ROLE_ADMIN.equals(roleName)) { collection.removeAdministrators(); } else if (ROLE_SUBMIT.equals(roleName)) { collection.removeSubmitters(); } else { WorkflowUtils.deleteRoleGroup(context, collection, roleName); } // else if (ROLE_WF_STEP1.equals(roleName)) // { // collection.setWorkflowGroup(1, null); // } // else if (ROLE_WF_STEP2.equals(roleName)) // { // collection.setWorkflowGroup(2, null); // } // else if (ROLE_WF_STEP3.equals(roleName)) // { // collection.setWorkflowGroup(3, null); // // } // Second, remove all authorizations for this role by searching for all policies that this // group has on the collection and remove them otherwise the delete will fail because // there are dependencies. @SuppressWarnings("unchecked") // the cast is correct List<ResourcePolicy> policies = AuthorizeManager.getPolicies(context, collection); for (ResourcePolicy policy : policies) { if (policy.getGroupID() == groupID) { policy.delete(); } } // Finally, Delete the role's actual group. collection.update(); role.delete(); context.commit(); result.setContinue(true); result.setOutcome(true); result.setMessage(new Message("default", "The role was successfully deleted.")); return result; }
/** * Look up the id of the template item for a given collection. * * @param context The current DSpace context. * @param collectionID The collection id. * @return The id of the template item. * @throws IOException */ public static int getTemplateItemID(Context context, int collectionID) throws SQLException, AuthorizeException, IOException { Collection collection = Collection.find(context, collectionID); Item template = collection.getTemplateItem(); if (template == null) { collection.createTemplateItem(); template = collection.getTemplateItem(); collection.update(); template.update(); context.commit(); } return template.getID(); }
/** * Purge the collection of all items, then run a fresh harvest cycle. * * @param context The current DSpace context. * @param collectionID The collection id. * @param request the Cocoon request object * @return A process result's object. * @throws TransformerException * @throws SAXException * @throws ParserConfigurationException * @throws CrosswalkException * @throws BrowseException */ public static FlowResult processReimportCollection( Context context, int collectionID, Request request) throws SQLException, IOException, AuthorizeException, CrosswalkException, ParserConfigurationException, SAXException, TransformerException, BrowseException { Collection collection = Collection.find(context, collectionID); HarvestedCollection hc = HarvestedCollection.find(context, collectionID); ItemIterator it = collection.getAllItems(); // IndexBrowse ib = new IndexBrowse(context); while (it.hasNext()) { Item item = it.next(); // System.out.println("Deleting: " + item.getHandle()); // ib.itemRemoved(item); collection.removeItem(item); } hc.setHarvestResult(null, ""); hc.update(); collection.update(); context.commit(); return processRunCollectionHarvest(context, collectionID, request); }
/** * Take a node list of collections and create the structure from them * * @param context the context of the request * @param collections the node list of collections to be created * @param parent the parent community to whom the collections belong * @return an Element array containing additional information about the created collections (e.g. * the handle) */ private static Element[] handleCollections( Context context, NodeList collections, Community parent) throws Exception { Element[] elements = new Element[collections.getLength()]; for (int i = 0; i < collections.getLength(); i++) { Element element = new Element("collection"); Collection collection = parent.createCollection(); // default the short description to the empty string collection.setMetadata("short_description", " "); // import the rest of the metadata Node tn = collections.item(i); for (Map.Entry<String, String> entry : collectionMap.entrySet()) { NodeList nl = XPathAPI.selectNodeList(tn, entry.getKey()); if (nl.getLength() == 1) { collection.setMetadata(entry.getValue(), getStringValue(nl.item(0))); } } collection.update(); element.setAttribute("identifier", collection.getHandle()); Element nameElement = new Element("name"); nameElement.setText(collection.getMetadata("name")); element.addContent(nameElement); if (collection.getMetadata("short_description") != null) { Element descriptionElement = new Element("description"); descriptionElement.setText(collection.getMetadata("short_description")); element.addContent(descriptionElement); } if (collection.getMetadata("introductory_text") != null) { Element introElement = new Element("intro"); introElement.setText(collection.getMetadata("introductory_text")); element.addContent(introElement); } if (collection.getMetadata("copyright_text") != null) { Element copyrightElement = new Element("copyright"); copyrightElement.setText(collection.getMetadata("copyright_text")); element.addContent(copyrightElement); } if (collection.getMetadata("side_bar_text") != null) { Element sidebarElement = new Element("sidebar"); sidebarElement.setText(collection.getMetadata("side_bar_text")); element.addContent(sidebarElement); } if (collection.getMetadata("license") != null) { Element sidebarElement = new Element("license"); sidebarElement.setText(collection.getMetadata("license")); element.addContent(sidebarElement); } if (collection.getMetadata("provenance_description") != null) { Element sidebarElement = new Element("provenance"); sidebarElement.setText(collection.getMetadata("provenance_description")); element.addContent(sidebarElement); } elements[i] = element; } return elements; }
/** * Update collection. It replace all properties. * * @param collectionId Id of collection in DSpace. * @param collection Collection which will replace properties of actual collection. * @param headers If you want to access to collection under logged user into context. In headers * must be set header "rest-dspace-token" with passed token from login method. * @return Return response 200 if was everything all right. Otherwise 400 when id of community was * incorrect or 401 if was problem with permission to write into collection. * @throws WebApplicationException It is thrown when was problem with database reading or writing. * Or problem with authorization to collection. Or problem with creating context. */ @PUT @Path("/{collection_id}") @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) public Response updateCollection( @PathParam("collection_id") Integer collectionId, org.dspace.rest.common.Collection collection, @QueryParam("userIP") String user_ip, @QueryParam("userAgent") String user_agent, @QueryParam("xforwarderfor") String xforwarderfor, @Context HttpHeaders headers, @Context HttpServletRequest request) throws WebApplicationException { log.info("Updating collection(id=" + collectionId + ")."); org.dspace.core.Context context = null; try { context = createContext(getUser(headers)); org.dspace.content.Collection dspaceCollection = findCollection(context, collectionId, org.dspace.core.Constants.WRITE); writeStats( dspaceCollection, UsageEvent.Action.UPDATE, user_ip, user_agent, xforwarderfor, headers, request, context); dspaceCollection.setMetadata("name", collection.getName()); dspaceCollection.setLicense(collection.getLicense()); // dspaceCollection.setLogo(collection.getLogo()); // TODO Add this option. dspaceCollection.setMetadata( org.dspace.content.Collection.COPYRIGHT_TEXT, collection.getCopyrightText()); dspaceCollection.setMetadata( org.dspace.content.Collection.INTRODUCTORY_TEXT, collection.getIntroductoryText()); dspaceCollection.setMetadata( org.dspace.content.Collection.SHORT_DESCRIPTION, collection.getShortDescription()); dspaceCollection.setMetadata( org.dspace.content.Collection.SIDEBAR_TEXT, collection.getSidebarText()); dspaceCollection.update(); context.complete(); } catch (ContextException e) { processException( "Could not update collection(id=" + collectionId + "), ContextException. Message: " + e.getMessage(), context); } catch (SQLException e) { processException( "Could not update collection(id=" + collectionId + "), SQLException. Message: " + e, context); } catch (AuthorizeException e) { processException( "Could not update collection(id=" + collectionId + "), AuthorizeException. Message: " + e, context); } finally { processFinally(context); } log.info("Collection(id=" + collectionId + ") successfully updated."); return Response.ok().build(); }
/** * Process the collection metadata edit form. * * @param context The current DSpace context. * @param collectionID The collection id. * @param deleteLogo Determines if the logo should be deleted along with the metadata editing * action. * @param request the Cocoon request object * @return A process result's object. */ public static FlowResult processEditCollection( Context context, int collectionID, boolean deleteLogo, Request request) throws SQLException, IOException, AuthorizeException { FlowResult result = new FlowResult(); Collection collection = Collection.find(context, collectionID); // Get the metadata String name = request.getParameter("name"); String shortDescription = request.getParameter("short_description"); String introductoryText = request.getParameter("introductory_text"); String copyrightText = request.getParameter("copyright_text"); String sideBarText = request.getParameter("side_bar_text"); String license = request.getParameter("license"); String provenanceDescription = request.getParameter("provenance_description"); // If they don't have a name then make it untitled. if (name == null || name.length() == 0) { name = "Untitled"; } // If empty, make it null. if (shortDescription != null && shortDescription.length() == 0) { shortDescription = null; } if (introductoryText != null && introductoryText.length() == 0) { introductoryText = null; } if (copyrightText != null && copyrightText.length() == 0) { copyrightText = null; } if (sideBarText != null && sideBarText.length() == 0) { sideBarText = null; } if (license != null && license.length() == 0) { license = null; } if (provenanceDescription != null && provenanceDescription.length() == 0) { provenanceDescription = null; } // Save the metadata collection.setMetadata("name", name); collection.setMetadata("short_description", shortDescription); collection.setMetadata("introductory_text", introductoryText); collection.setMetadata("copyright_text", copyrightText); collection.setMetadata("side_bar_text", sideBarText); collection.setMetadata("license", license); collection.setMetadata("provenance_description", provenanceDescription); // Change or delete the logo if (deleteLogo) { // Remove the logo collection.setLogo(null); } else { // Update the logo Object object = request.get("logo"); Part filePart = null; if (object instanceof Part) { filePart = (Part) object; } if (filePart != null && filePart.getSize() > 0) { InputStream is = filePart.getInputStream(); collection.setLogo(is); } } // Save everything collection.update(); context.commit(); // No notice... result.setContinue(true); return result; }
/** * Create a new collection * * @param context The current DSpace context. * @param communityID The id of the parent community. * @return A process result's object. */ public static FlowResult processCreateCollection( Context context, int communityID, Request request) throws SQLException, AuthorizeException, IOException { FlowResult result = new FlowResult(); Community parent = Community.find(context, communityID); Collection newCollection = parent.createCollection(); // Get the metadata String name = request.getParameter("name"); String shortDescription = request.getParameter("short_description"); String introductoryText = request.getParameter("introductory_text"); String copyrightText = request.getParameter("copyright_text"); String sideBarText = request.getParameter("side_bar_text"); String license = request.getParameter("license"); String provenanceDescription = request.getParameter("provenance_description"); // If they don't have a name then make it untitled. if (name == null || name.length() == 0) { name = "Untitled"; } // If empty, make it null. if (shortDescription != null && shortDescription.length() == 0) { shortDescription = null; } if (introductoryText != null && introductoryText.length() == 0) { introductoryText = null; } if (copyrightText != null && copyrightText.length() == 0) { copyrightText = null; } if (sideBarText != null && sideBarText.length() == 0) { sideBarText = null; } if (license != null && license.length() == 0) { license = null; } if (provenanceDescription != null && provenanceDescription.length() == 0) { provenanceDescription = null; } // Save the metadata newCollection.setMetadata("name", name); newCollection.setMetadata("short_description", shortDescription); newCollection.setMetadata("introductory_text", introductoryText); newCollection.setMetadata("copyright_text", copyrightText); newCollection.setMetadata("side_bar_text", sideBarText); newCollection.setMetadata("license", license); newCollection.setMetadata("provenance_description", provenanceDescription); // Set the logo Object object = request.get("logo"); Part filePart = null; if (object instanceof Part) { filePart = (Part) object; } if (filePart != null && filePart.getSize() > 0) { InputStream is = filePart.getInputStream(); newCollection.setLogo(is); } // Save everything newCollection.update(); context.commit(); // success result.setContinue(true); result.setOutcome(true); result.setMessage(new Message("default", "The collection was successfully created.")); result.setParameter("collectionID", newCollection.getID()); return result; }