ui support for native batch import

This commit is contained in:
Kostas Stamatis
2014-07-16 16:02:35 +03:00
parent c61ed134f8
commit e4aa641a59
5 changed files with 436 additions and 78 deletions

View File

@@ -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

View File

@@ -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 = <span class="glyphicon glyphicon-lock"> </span>&nbsp; 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)

View File

@@ -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 typeS = request.getParameter("type");
if (typeS != null){
try {
type = Integer.parseInt(typeS);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String inputType = wrapper.getParameter("inputType");
//Get all collections
List<Collection> collections = null;
String colIdS = request.getParameter("colId");
if (colIdS!=null){
collections = new ArrayList<Collection>();
collections.add(Collection.find(context, Integer.parseInt(colIdS)));
try {
ItemImport.processUploadableImport(f, new Collection[]{collection}, inputType, context);
}
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<String> 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<String> inputTypes =dls.getFileDataLoaders();
request.setAttribute("message", message);
request.setAttribute("input-types", inputTypes);
//Get all collections
List<Collection> collections = null;
String colIdS = request.getParameter("colId");
if (colIdS!=null){
collections = new ArrayList<Collection>();
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");
}
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<String> 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<Collection> collections = null;
String colIdS = request.getParameter("colId");
if (colIdS!=null){
collections = new ArrayList<Collection>();
collections.add(Collection.find(context, Integer.parseInt(colIdS)));
List<Collection> collections = null;
String colIdS = request.getParameter("colId");
if (colIdS!=null){
collections = new ArrayList<Collection>();
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<String> 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");
}
}

View File

@@ -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<Collection> collections = (List<Collection>)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);
%>
<dspace:layout style="submission" titlekey="jsp.dspace-admin.batchimport.title"
navbar="admin"
locbar="link"
parenttitlekey="jsp.administer"
parentlink="/dspace-admin"
nocache="true">
<h1><fmt:message key="jsp.dspace-admin.batchimport.title"/></h1>
<%
if (hasErrorS == null){
}
else if (hasError && message!=null){
%>
<div class="alert alert-warning"><%= message %></div>
<%
}
else if (hasError && message==null){
%>
<div class="alert alert-warning"><fmt:message key="jsp.dspace-admin.batchmetadataimport.genericerror"/></div>
<%
}
else {
%>
<div class="alert alert-info"><fmt:message key="jsp.dspace-admin.batchmetadataimport.success"/></div>
<%
}
%>
<form method="post" action="">
<input type="hidden" name=type value="<%= type %>"/>
<div class="form-group">
<label for="collection"><fmt:message key="jsp.dspace-admin.batchmetadataimport.selecturl"/></label><br/>
<input type="text" name="zipurl" class="form-control"/>
</div>
<div class="form-group">
<label for="collection"><fmt:message key="jsp.dspace-admin.batchmetadataimport.selectowningcollection"/></label>
<select class="form-control" name="collection">
<option value="-1"><fmt:message key="jsp.dspace-admin.batchmetadataimport.select"/></option>
<%
for (Collection collection : collections){
%>
<option value="<%= collection.getID() %>"><%= collection.getName() %></option>
<%
}
%>
</select>
</div>
<div class="form-group">
<label for="collection"><fmt:message key="jsp.dspace-admin.batchmetadataimport.selectothercollections"/></label>
<select class="form-control" name="collections" multiple style="height:150px">
<%
for (Collection collection : collections){
%>
<option value="<%= collection.getID() %>"><%= collection.getName() %></option>
<%
}
%>
</select>
</div>
<input class="btn btn-success" type="submit" name="submit" value="<fmt:message key="jsp.dspace-admin.general.upload"/>" />
</form>
</dspace:layout>

View File

@@ -72,6 +72,7 @@
<li><a href="<%= request.getContextPath() %>/dspace-admin/withdrawn"><fmt:message key="jsp.layout.navbar-admin.withdrawn"/></a></li>
<li><a href="<%= request.getContextPath() %>/dspace-admin/privateitems"><fmt:message key="jsp.layout.navbar-admin.privateitems"/></a></li>
<li><a href="<%= request.getContextPath() %>/dspace-admin/metadataimport"><fmt:message key="jsp.layout.navbar-admin.metadataimport"/></a></li>
<li><a href="<%= request.getContextPath() %>/dspace-admin/batchmetadataimport?type=0"><fmt:message key="jsp.layout.navbar-admin.batchimport"/></a></li>
<li><a href="<%= request.getContextPath() %>/dspace-admin/batchmetadataimport"><fmt:message key="jsp.layout.navbar-admin.batchmetadataimport"/></a></li>
</ul>
</li>