diff --git a/dspace-api/src/main/java/org/dspace/app/itemimport/ItemImport.java b/dspace-api/src/main/java/org/dspace/app/itemimport/ItemImport.java index 3c519dfa92..23895a467d 100644 --- a/dspace-api/src/main/java/org/dspace/app/itemimport/ItemImport.java +++ b/dspace-api/src/main/java/org/dspace/app/itemimport/ItemImport.java @@ -16,6 +16,7 @@ import gr.ekt.bteio.generators.DSpaceOutputGenerator; import gr.ekt.bteio.loaders.OAIPMHDataLoader; import java.io.*; +import java.net.URL; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.*; @@ -2177,6 +2178,175 @@ public class ItemImport } } + /** + * Given an uploaded file, this method calls the method to instantiate a BTE instance to + * transform the input data and batch import them to DSpace + * @param file The input file to read data from + * @param collections The collections the created items will be inserted to + * @param bteInputType The input type of the data (bibtex, csv, etc.) + * @param context The context + * @throws Exception + */ + public static void processUploadableImport(String url, Collection owningCollection, Collection[] collections, Context context) throws Exception + { + final EPerson eperson = context.getCurrentUser(); + final Collection[] otherCollections = collections; + final Collection theOwningCollection = owningCollection; + final String zipurl = url; + + /*Thread go = new Thread() + { + public void run() + { + Context context = null; +*/ + try { + + // create a new dspace context +// context = new Context(); +// context.setCurrentUser(eperson); +// context.setIgnoreAuthorization(true); + + InputStream is = new URL(zipurl).openStream(); + + String importDir = ConfigurationManager.getProperty("org.dspace.app.batchitemimport.work.dir") + File.separator + "batchuploads" + File.separator + context.getCurrentUser().getID() + File.separator + (new GregorianCalendar()).getTimeInMillis(); + File importDirFile = new File(importDir); + if (!importDirFile.exists()){ + boolean success = importDirFile.mkdirs(); + if (!success) { + log.info("Cannot create batch import directory!"); + throw new Exception("Cannot create batch import directory!"); + } + } + + String dataZipPath = importDirFile + File.separator + "data.zip"; + + OutputStream os = new FileOutputStream(dataZipPath); + + byte[] b = new byte[2048]; + int length; + + while ((length = is.read(b)) != -1) { + os.write(b, 0, length); + } + + is.close(); + os.close(); + + + String mapFilePath = importDirFile + File.separator + "mapfile"; + + context.complete(); + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + try { + throw new Exception(e.getMessage()); + } catch (Exception e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } +// } + +// }; + +// go.isDaemon(); +// go.start(); + // if the file exists + /*if (file.exists()) + { + Thread go = new Thread() + { + public void run() + { + Context context = null; + ItemIterator iitems = null; + try + { + // create a new dspace context + context = new Context(); + context.setCurrentUser(eperson); + context.setIgnoreAuthorization(true); + + File importDir = new File(ConfigurationManager.getProperty("org.dspace.app.batchitemimport.work.dir")); + if (!importDir.exists()){ + boolean success = importDir.mkdir(); + if (!success) { + log.info("Cannot create batch import directory!"); + throw new Exception(); + } + } + //Generate a random filename for the subdirectory of the specific import in case + //more that one batch imports take place at the same time + String subDirName = generateRandomFilename(false); + String workingDir = importDir.getAbsolutePath() + File.separator + subDirName; + + //Create the import working directory + boolean success = (new File(workingDir)).mkdir(); + if (!success) { + log.info("Cannot create batch import working directory!"); + throw new Exception(); + } + + //Create random mapfile; + String mapfile = workingDir + File.separator+ "mapfile"; + + ItemImport myloader = new ItemImport(); + myloader.addBTEItems(context, mycollections, myFile.getAbsolutePath(), mapfile, template, myBteInputType, workingDir); + + // email message letting user know the file is ready for + // download + emailSuccessMessage(context, eperson, mapfile); + + // return to enforcing auths + context.setIgnoreAuthorization(false); + } + catch (Exception e1) + { + try + { + emailErrorMessage(eperson, e1.getMessage()); + } + catch (Exception e) + { + // wont throw here + } + throw new IllegalStateException(e1); + } + finally + { + if (iitems != null) + { + iitems.close(); + } + + // close the mapfile writer + if (mapOut != null) + { + mapOut.close(); + } + + // Make sure the database connection gets closed in all conditions. + try { + context.complete(); + } catch (SQLException sqle) { + context.abort(); + } + } + } + + }; + + go.isDaemon(); + go.start(); + } + else { + log.error("Unable to find the uploadable file"); + }*/ + } + /** * Since the BTE batch import is done in a new thread we are unable to communicate * with calling method about success or failure. We accomplish this diff --git a/dspace-api/src/main/resources/Messages.properties b/dspace-api/src/main/resources/Messages.properties index c219e8b700..e8db4f8322 100644 --- a/dspace-api/src/main/resources/Messages.properties +++ b/dspace-api/src/main/resources/Messages.properties @@ -1867,3 +1867,10 @@ jsp.request.item.request-free-acess.email = E-mail: org.dspace.app.requestitem.RequestItemMetadataStrategy.unnamed = Corresponding Author org.dspace.app.requestitem.RequestItemHelpdeskStrategy.helpdeskname = Help Desk org.dspace.app.webui.jsptag.ItemTag.restrict =   Request a copy + +jsp.layout.navbar-admin.batchimport = Batch metadata import +jsp.dspace-admin.batchimport.title = Batch metadata import +jsp.dspace-admin.batchmetadataimport.select = Select collection +jsp.dspace-admin.batchmetadataimport.selecturl = Provide the URL of the zip file +jsp.dspace-admin.batchmetadataimport.selectowningcollection = Select the owning collection of the items (Optional) +jsp.dspace-admin.batchmetadataimport.selectothercollections = Select other collections that the items will belong to (Optional) diff --git a/dspace-jspui/src/main/java/org/dspace/app/webui/servlet/BatchMetadataImportServlet.java b/dspace-jspui/src/main/java/org/dspace/app/webui/servlet/BatchMetadataImportServlet.java index 36f1784c12..e992d07933 100644 --- a/dspace-jspui/src/main/java/org/dspace/app/webui/servlet/BatchMetadataImportServlet.java +++ b/dspace-jspui/src/main/java/org/dspace/app/webui/servlet/BatchMetadataImportServlet.java @@ -20,6 +20,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException; +import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; import org.dspace.app.webui.util.JSPManager; import org.dspace.app.webui.util.FileUploadRequest; @@ -57,76 +58,129 @@ public class BatchMetadataImportServlet extends DSpaceServlet HttpServletResponse response) throws ServletException, IOException, SQLException, AuthorizeException { - // First, see if we have a multipart request (uploading a metadata file) - String contentType = request.getContentType(); - if ((contentType != null) && (contentType.indexOf("multipart/form-data") != -1)) - { - String message = null; - - // Process the file uploaded - try { - // Wrap multipart request to get the submission info - FileUploadRequest wrapper = new FileUploadRequest(request); - File f = wrapper.getFile("file"); + + int type = -1; - int colId = Integer.parseInt(wrapper.getParameter("collection")); - Collection collection = Collection.find(context, colId); - - String inputType = wrapper.getParameter("inputType"); - - try { - ItemImport.processUploadableImport(f, new Collection[]{collection}, inputType, context); + String typeS = request.getParameter("type"); + if (typeS != null){ + try { + type = Integer.parseInt(typeS); + } catch (NumberFormatException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + //Get all collections + List collections = null; + String colIdS = request.getParameter("colId"); + if (colIdS!=null){ + collections = new ArrayList(); + collections.add(Collection.find(context, Integer.parseInt(colIdS))); + + } + else { + collections = Arrays.asList(Collection.findAll(context)); + } + + request.setAttribute("collections", collections); + + if (type != 0){ + // First, see if we have a multipart request (uploading a metadata file) + String contentType = request.getContentType(); + if ((contentType != null) && (contentType.indexOf("multipart/form-data") != -1)) + { + String message = null; + + // Process the file uploaded + try { + // Wrap multipart request to get the submission info + FileUploadRequest wrapper = new FileUploadRequest(request); + File f = wrapper.getFile("file"); + + int colId = Integer.parseInt(wrapper.getParameter("collection")); + Collection collection = Collection.find(context, colId); + + String inputType = wrapper.getParameter("inputType"); + + try { + ItemImport.processUploadableImport(f, new Collection[]{collection}, inputType, context); + + request.setAttribute("has-error", "false"); + + } catch (Exception e) { + request.setAttribute("has-error", "true"); + message = e.getMessage(); + e.printStackTrace(); + } + } catch (FileSizeLimitExceededException e) { + request.setAttribute("has-error", "true"); + message = e.getMessage(); + e.printStackTrace(); + } catch (Exception e) { + request.setAttribute("has-error", "true"); + message = e.getMessage(); + e.printStackTrace(); + } + + //Get all the possible data loaders from the Spring configuration + BTEBatchImportService dls = new DSpace().getSingletonService(BTEBatchImportService.class); + List inputTypes =dls.getFileDataLoaders(); + + request.setAttribute("input-types", inputTypes); + + request.setAttribute("message", message); + + // Show the upload screen + JSPManager.showJSP(request, response, "/dspace-admin/batchmetadataimport.jsp"); + + } + else + { + request.setAttribute("has-error", "true"); + + // Show the upload screen + JSPManager.showJSP(request, response, "/dspace-admin/batchmetadataimport.jsp"); + } + } + else { + request.setAttribute("type", type); + + String message = null; + + String zipurl = request.getParameter("zipurl"); + if (StringUtils.isEmpty(zipurl)) { + request.setAttribute("has-error", "true"); + } + else { + + Collection owningCollection = null; + if (request.getParameter("collection") != null) { + int colId = Integer.parseInt(request.getParameter("collection")); + if (colId > 0) + owningCollection = Collection.find(context, colId); + } + + //request.getParameter("collections") + Collection[] otherCollections = new Collection[0]; + + try { + + ItemImport.processUploadableImport(zipurl, owningCollection, otherCollections, context); request.setAttribute("has-error", "false"); } catch (Exception e) { request.setAttribute("has-error", "true"); - message = e.getMessage(); - e.printStackTrace(); + message = e.getMessage(); + e.printStackTrace(); } - } catch (FileSizeLimitExceededException e) { - request.setAttribute("has-error", "true"); - message = e.getMessage(); - e.printStackTrace(); - } catch (Exception e) { - request.setAttribute("has-error", "true"); - message = e.getMessage(); - e.printStackTrace(); - } - - //Get all the possible data loaders from the Spring configuration - BTEBatchImportService dls = new DSpace().getSingletonService(BTEBatchImportService.class); - List inputTypes =dls.getFileDataLoaders(); - - request.setAttribute("input-types", inputTypes); - - //Get all collections - List collections = null; - String colIdS = request.getParameter("colId"); - if (colIdS!=null){ - collections = new ArrayList(); - collections.add(Collection.find(context, Integer.parseInt(colIdS))); - - } - else { - collections = Arrays.asList(Collection.findAll(context)); - } - - request.setAttribute("collections", collections); - - request.setAttribute("message", message); - - // Show the upload screen - JSPManager.showJSP(request, response, "/dspace-admin/batchmetadataimport.jsp"); - - } - else - { - request.setAttribute("has-error", "true"); - - // Show the upload screen - JSPManager.showJSP(request, response, "/dspace-admin/batchmetadataimport.jsp"); - } + } + + request.setAttribute("message", message); + + JSPManager.showJSP(request, response, "/dspace-admin/batchimport.jsp"); + } } /** @@ -148,27 +202,47 @@ public class BatchMetadataImportServlet extends DSpaceServlet HttpServletResponse response) throws ServletException, IOException, SQLException, AuthorizeException { - //Get all the possible data loaders from the Spring configuration - BTEBatchImportService dls = new DSpace().getSingletonService(BTEBatchImportService.class); - List inputTypes = dls.getFileDataLoaders(); - request.setAttribute("input-types", inputTypes); + + String typeS = request.getParameter("type"); + + int type = -1; + try { + type = Integer.parseInt(typeS); + } catch (NumberFormatException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + request.setAttribute("type", type); //Get all collections - List collections = null; - String colIdS = request.getParameter("colId"); - if (colIdS!=null){ - collections = new ArrayList(); - collections.add(Collection.find(context, Integer.parseInt(colIdS))); + List collections = null; + String colIdS = request.getParameter("colId"); + if (colIdS!=null){ + collections = new ArrayList(); + collections.add(Collection.find(context, Integer.parseInt(colIdS))); + + } + else { + collections = Arrays.asList(Collection.findAll(context)); + } + + request.setAttribute("collections", collections); + + if (type==0){ + // Show the upload screen + JSPManager.showJSP(request, response, "/dspace-admin/batchimport.jsp"); } else { - collections = Arrays.asList(Collection.findAll(context)); + //Get all the possible data loaders from the Spring configuration + BTEBatchImportService dls = new DSpace().getSingletonService(BTEBatchImportService.class); + List inputTypes = dls.getFileDataLoaders(); + request.setAttribute("input-types", inputTypes); + + // Show the upload screen + JSPManager.showJSP(request, response, "/dspace-admin/batchmetadataimport.jsp"); } - - request.setAttribute("collections", collections); - - // Show the upload screen - JSPManager.showJSP(request, response, "/dspace-admin/batchmetadataimport.jsp"); } } \ No newline at end of file diff --git a/dspace-jspui/src/main/webapp/dspace-admin/batchimport.jsp b/dspace-jspui/src/main/webapp/dspace-admin/batchimport.jsp new file mode 100644 index 0000000000..7c1e9b1c0e --- /dev/null +++ b/dspace-jspui/src/main/webapp/dspace-admin/batchimport.jsp @@ -0,0 +1,106 @@ +<%-- + + The contents of this file are subject to the license and copyright + detailed in the LICENSE and NOTICE files at the root of the source + tree and available online at + + http://www.dspace.org/license/ + +--%> +<%-- + - Form to upload a csv metadata file +--%> + +<%@ page contentType="text/html;charset=UTF-8" %> + +<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> + +<%@ taglib uri="http://www.dspace.org/dspace-tags.tld" prefix="dspace" %> + +<%@ page import="java.util.List" %> +<%@ page import="org.dspace.content.Collection" %> + +<% + + List collections = (List)request.getAttribute("collections"); + String hasErrorS = (String)request.getAttribute("has-error"); + boolean hasError = (hasErrorS==null) ? true : (Boolean.parseBoolean((String)request.getAttribute("has-error"))); + + String message = (String)request.getAttribute("message"); + + int type = (Integer)request.getAttribute("type"); + System.out.println("ss: " + type); + +%> + + + +

+ +<% + if (hasErrorS == null){ + + } + else if (hasError && message!=null){ +%> +
<%= message %>
+<% + } + else if (hasError && message==null){ +%> +
+<% + } + else { +%> +
+<% + } +%> + +
+ + + +
+
+ +
+ +
+ + +
+ +
+ + +
+ + " /> + +
+ +
\ No newline at end of file diff --git a/dspace-jspui/src/main/webapp/layout/navbar-admin.jsp b/dspace-jspui/src/main/webapp/layout/navbar-admin.jsp index 8e8f016cd9..f0583e3d99 100644 --- a/dspace-jspui/src/main/webapp/layout/navbar-admin.jsp +++ b/dspace-jspui/src/main/webapp/layout/navbar-admin.jsp @@ -72,6 +72,7 @@
  • +