ui support for batch import delete/resume

This commit is contained in:
Kostas Stamatis
2014-07-18 12:09:43 +03:00
parent f8318c9083
commit 42ad868ee0
6 changed files with 366 additions and 17 deletions

View File

@@ -2196,20 +2196,20 @@ public class ItemImport
final Collection theOwningCollection = owningCollection; final Collection theOwningCollection = owningCollection;
final String zipurl = url; final String zipurl = url;
/* Thread go = new Thread() Thread go = new Thread()
{ {
public void run() public void run()
{ {
Context context = null; Context context = null;
*/
String importDir = null; String importDir = null;
try { try {
// create a new dspace context // create a new dspace context
// context = new Context(); context = new Context();
// context.setCurrentUser(eperson); context.setCurrentUser(eperson);
// context.setIgnoreAuthorization(true); context.setIgnoreAuthorization(true);
InputStream is = new URL(zipurl).openStream(); InputStream is = new URL(zipurl).openStream();
@@ -2255,7 +2255,7 @@ public class ItemImport
} }
else else
{ {
System.out.println("Extracting file: " + entry.getName()); //System.out.println("Extracting file: " + entry.getName());
int index = entry.getName().lastIndexOf('/'); int index = entry.getName().lastIndexOf('/');
if (index == -1) if (index == -1)
{ {
@@ -2352,12 +2352,190 @@ public class ItemImport
context.abort(); context.abort();
} }
} }
/* } }
}; };
go.isDaemon(); go.isDaemon();
go.start();*/ go.start();
}
public static void processResumableImport(String url, Collection owningCollection, Collection[] collections, String resumeDir, Context context) throws Exception
{
final EPerson eperson = context.getCurrentUser();
final Collection[] otherCollections = collections;
final Collection theOwningCollection = owningCollection;
final String zipurl = url;
final String resumePath = resumeDir;
Thread go = new Thread()
{
public void run()
{
Context context = null;
String importDir = null;
try {
// create a new dspace context
context = new Context();
context.setCurrentUser(eperson);
context.setIgnoreAuthorization(true);
InputStream is = new URL(zipurl).openStream();
importDir = ConfigurationManager.getProperty("org.dspace.app.batchitemimport.work.dir") + File.separator + "batchuploads" + File.separator + context.getCurrentUser().getID() + File.separator + resumePath;
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";
String dataZipDir = importDirFile + File.separator + "data_unzipped" + File.separator;
//Clear these files, since it is a resume
(new File(dataZipPath)).delete();
(new File(dataZipDir)).delete();
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();
ZipFile zf = new ZipFile(dataZipPath);
ZipEntry entry;
Enumeration<? extends ZipEntry> entries = zf.entries();
while (entries.hasMoreElements())
{
entry = entries.nextElement();
if (entry.isDirectory())
{
if (!new File(dataZipDir + entry.getName()).mkdir())
{
log.error("Unable to create contents directory");
}
}
else
{
//System.out.println("Extracting file: " + entry.getName());
int index = entry.getName().lastIndexOf('/');
if (index == -1)
{
// Was it created on Windows instead?
index = entry.getName().lastIndexOf('\\');
}
if (index > 0)
{
File dir = new File(dataZipDir + entry.getName().substring(0, index));
if (!dir.mkdirs())
{
log.error("Unable to create directory");
}
}
byte[] buffer = new byte[1024];
int len;
InputStream in = zf.getInputStream(entry);
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(dataZipDir + entry.getName()));
while((len = in.read(buffer)) >= 0)
{
out.write(buffer, 0, len);
}
in.close();
out.close();
}
}
zf.close();
String sourcePath = dataZipDir;
String mapFilePath = importDirFile + File.separator + "mapfile";
ItemImport myloader = new ItemImport();
myloader.isResume = true;
Collection[] finalCollections = null;
if (theOwningCollection != null){
finalCollections = new Collection[otherCollections.length + 1];
finalCollections[0] = theOwningCollection;
for (int i=0; i<otherCollections.length; i++){
finalCollections[i+1] = otherCollections[i];
}
}
myloader.addItems(context, finalCollections, sourcePath, mapFilePath, template);
// email message letting user know the file is ready for
// download
emailSuccessMessage(context, eperson, mapFilePath);
context.complete();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
// abort all operations
if (mapOut != null)
{
mapOut.close();
}
mapOut = null;
//Delete file
if (importDir != null){
//FileDeleteStrategy.FORCE.delete(new File(importDir));
}
try
{
emailErrorMessage(eperson, e.getMessage());
throw new Exception(e.getMessage());
}
catch (Exception e2)
{
// wont throw here
}
}
finally
{
// 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();
} }
/** /**
@@ -2468,4 +2646,19 @@ public class ItemImport
return uploadDir + File.separator + "batchuploads" + File.separator + ePersonID; return uploadDir + File.separator + "batchuploads" + File.separator + ePersonID;
} }
public void deleteButchUpload(Context c, String uploadId) throws Exception
{
String uploadDir = null;
String mapFilePath = null;
uploadDir = ItemImport.getImportUploadableDirectory(c.getCurrentUser().getID()) + File.separator + uploadId;
mapFilePath = uploadDir + File.separator + "mapfile";
this.deleteItems(c, mapFilePath);
FileDeleteStrategy.FORCE.delete(new File(uploadDir));
// complete all transactions
c.commit();
}
} }

View File

@@ -1875,4 +1875,11 @@ jsp.dspace-admin.batchmetadataimport.select = Select collection
jsp.dspace-admin.batchmetadataimport.selecturl = Provide the URL of the zip file 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.selectowningcollection = Select the owning collection of the items (Optional)
jsp.dspace-admin.batchmetadataimport.selectothercollections = Select other collections that the items will belong to (Optional) jsp.dspace-admin.batchmetadataimport.selectothercollections = Select other collections that the items will belong to (Optional)
jsp.layout.navbar-admin.batchimport.owningcollection = You need to provide an owning collection since you have provided secondary ones! jsp.layout.navbar-admin.batchimport.owningcollection = You need to provide an owning collection since you have provide secondary ones!
jsp.dspace-admin.batchimport.success = Success
jsp.dspace-admin.batchimport.failure = Failure
jsp.dspace-admin.batchimport.itemstobeimported = Items to be imported
jsp.dspace-admin.batchimport.itemsimported = Items imported
jsp.dspace-admin.batchimport.downloadmapfile = Download mapfile
jsp.dspace-admin.batchimport.deleteitems = Delete uploaded items & remove import
jsp.dspace-admin.batchimport.resume = Resume upload

View File

@@ -149,6 +149,11 @@ public class BatchMetadataImportServlet extends DSpaceServlet
String message = null; String message = null;
String uploadId = request.getParameter("uploadid");
if (uploadId != null){
request.setAttribute("uploadid", uploadId);
}
String zipurl = request.getParameter("zipurl"); String zipurl = request.getParameter("zipurl");
if (StringUtils.isEmpty(zipurl)) { if (StringUtils.isEmpty(zipurl)) {
request.setAttribute("has-error", "true"); request.setAttribute("has-error", "true");
@@ -196,14 +201,22 @@ public class BatchMetadataImportServlet extends DSpaceServlet
try { try {
ItemImport.processUploadableImport(zipurl, owningCollection, otherCollections, context); //Decide if it is a new upload or a resume one!
if (uploadId != null){ //resume upload
ItemImport.processResumableImport(zipurl, owningCollection, otherCollections, uploadId, context);
}
else { //New upload
ItemImport.processUploadableImport(zipurl, owningCollection, otherCollections, context);
}
request.setAttribute("has-error", "false"); request.setAttribute("has-error", "false");
} catch (Exception e) { } catch (Exception e) {
request.setAttribute("has-error", "true"); request.setAttribute("has-error", "true");
message = e.getMessage(); message = e.getMessage();
e.printStackTrace(); e.printStackTrace();
} }
} }

View File

@@ -7,12 +7,18 @@
*/ */
package org.dspace.app.webui.servlet; package org.dspace.app.webui.servlet;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@@ -75,6 +81,9 @@ public class MyDSpaceServlet extends DSpaceServlet
/** The "request export migrate archive for download" page */ /** The "request export migrate archive for download" page */
public static final int REQUEST_MIGRATE_ARCHIVE = 6; public static final int REQUEST_MIGRATE_ARCHIVE = 6;
public static final int REQUEST_BATCH_IMPORT_ACTION = 7;
protected void doDSGet(Context context, HttpServletRequest request, protected void doDSGet(Context context, HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException, HttpServletResponse response) throws ServletException, IOException,
SQLException, AuthorizeException SQLException, AuthorizeException
@@ -126,6 +135,11 @@ public class MyDSpaceServlet extends DSpaceServlet
break; break;
case REQUEST_BATCH_IMPORT_ACTION:
processBatchImportAction(context, request, response);
break;
default: default:
log.warn(LogManager.getHeader(context, "integrity_error", UIUtil log.warn(LogManager.getHeader(context, "integrity_error", UIUtil
.getRequestLogInfo(request))); .getRequestLogInfo(request)));
@@ -676,6 +690,81 @@ public class MyDSpaceServlet extends DSpaceServlet
} }
private void processBatchImportAction(Context context, HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException,
SQLException, AuthorizeException
{
String buttonPressed = UIUtil.getSubmitButton(request, "submit_mapfile");
String uploadId = request.getParameter("uploadid");
if (buttonPressed.equals("submit_mapfile")){
String mapFilePath = null;
try {
mapFilePath = ItemImport.getImportUploadableDirectory(context.getCurrentUser().getID()) + File.separator + uploadId + File.separator + "mapfile";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
showMainPage(context, request, response);
return;
}
File file = new File(mapFilePath);
int length = 0;
ServletOutputStream outStream = response.getOutputStream();
String mimetype ="application/octet-stream";
response.setContentType(mimetype);
response.setContentLength((int)file.length());
String fileName = file.getName();
// sets HTTP header
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
byte[] byteBuffer = new byte[1024];
DataInputStream in = new DataInputStream(new FileInputStream(file));
// reads the file's bytes and writes them to the response stream
while ((in != null) && ((length = in.read(byteBuffer)) != -1))
{
outStream.write(byteBuffer,0,length);
}
in.close();
outStream.close();
}
else if (buttonPressed.equals("submit_delete")){
ItemImport itemImport = new ItemImport();
try {
itemImport.deleteButchUpload(context, uploadId);
showMainPage(context, request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if (buttonPressed.equals("submit_resume")){
// Set attributes
request.setAttribute("uploadId", uploadId);
request.setAttribute("type", 0);
//Get all collections
List<Collection> collections = Arrays.asList(Collection.findAll(context));
request.setAttribute("collections", collections);
// Forward to main mydspace page
JSPManager.showJSP(request, response, "/dspace-admin/batchimport.jsp");
}
else {
showMainPage(context, request, response);
}
}
// **************************************************************** // ****************************************************************
// **************************************************************** // ****************************************************************
// METHODS FOR SHOWING FORMS // METHODS FOR SHOWING FORMS

View File

@@ -24,7 +24,9 @@
List<Collection> collections = (List<Collection>)request.getAttribute("collections"); List<Collection> collections = (List<Collection>)request.getAttribute("collections");
String hasErrorS = (String)request.getAttribute("has-error"); String hasErrorS = (String)request.getAttribute("has-error");
boolean hasError = (hasErrorS==null) ? true : (Boolean.parseBoolean((String)request.getAttribute("has-error"))); boolean hasError = (hasErrorS==null) ? false : (Boolean.parseBoolean((String)request.getAttribute("has-error")));
String uploadId = (String)request.getAttribute("uploadId");
String message = (String)request.getAttribute("message"); String message = (String)request.getAttribute("message");
@@ -63,10 +65,14 @@
} }
%> %>
<form method="post" action=""> <form method="post" action="<%= request.getContextPath() %>/dspace-admin/batchmetadataimport">
<input type="hidden" name=type value="<%= type %>"/> <input type="hidden" name=type value="<%= type %>"/>
<% if (uploadId != null) { %>
<input type="hidden" name=uploadid value="<%= uploadId %>"/>
<% } %>
<div class="form-group"> <div class="form-group">
<label for="collection"><fmt:message key="jsp.dspace-admin.batchmetadataimport.selecturl"/></label><br/> <label for="collection"><fmt:message key="jsp.dspace-admin.batchmetadataimport.selecturl"/></label><br/>
<input type="text" name="zipurl" class="form-control"/> <input type="text" name="zipurl" class="form-control"/>

View File

@@ -405,13 +405,54 @@
<%if(importsAvailable!=null && importsAvailable.size()>0){ %> <%if(importsAvailable!=null && importsAvailable.size()>0){ %>
<h3><fmt:message key="jsp.mydspace.main.heading8"/></h3> <h3><fmt:message key="jsp.mydspace.main.heading8"/></h3>
<ol class="exportArchives"> <ul class="exportArchives" style="list-style-type: none;">
<%for(BatchUpload batchUpload : importsAvailable){%> <% int i=0;
<li><%= batchUpload.getDateFormatted() + " --- " + (batchUpload.isSuccessful()?"Success":"Unsuccess") %></li> for(BatchUpload batchUpload : importsAvailable){
<% } %> %>
</ol> <li style="padding-top:5px; margin-top:10px">
<div style="float:left"><b><%= batchUpload.getDateFormatted() %></b></div>
<% if (batchUpload.isSuccessful()){ %>
<div style= "float:left">&nbsp;&nbsp;--> <span style="color:green"><fmt:message key="jsp.dspace-admin.batchimport.success"/></span></div>
<% } else { %>
<div style= "float:left;">&nbsp;&nbsp;--> <span style="color:red"><fmt:message key="jsp.dspace-admin.batchimport.failure"/></span></div>
<% } %>
<div style="float:left; padding-left:20px">
<a id="a2_<%= i%>" style="display:none; font-size:12px" href="javascript:showMoreClicked(<%= i%>);"><i>(hide)</i></a>
<a id="a1_<%= i%>" style="font-size:12px" href="javascript:showMoreClicked(<%= i%>);"><i>(show more)</i></a>
</div><br/>
<div id="moreinfo_<%= i%>" style="clear:both; display:none; margin-top:15px; padding:10px; border:1px solid; border-radius:4px; border-color:#bbb">
<div><fmt:message key="jsp.dspace-admin.batchimport.itemstobeimported"/>: <b><%= batchUpload.getTotalItems() %></b></div>
<div><fmt:message key="jsp.dspace-admin.batchimport.itemsimported"/>: <b><%= batchUpload.getItemsImported() %></b></div>
<div style="margin-top:10px">
<form action="<%= request.getContextPath() %>/mydspace" method="post">
<input type="hidden" name="step" value="7">
<input type="hidden" name="uploadid" value="<%= batchUpload.getDir().getName() %>">
<input class="btn btn-info" type="submit" name="submit_mapfile" value="<fmt:message key="jsp.dspace-admin.batchimport.downloadmapfile"/>">
<% if (!batchUpload.isSuccessful()){ %>
<input class="btn btn-warning" type="submit" name="submit_resume" value="<fmt:message key="jsp.dspace-admin.batchimport.resume"/>">
<% } %>
<input class="btn btn-danger" type="submit" name="submit_delete" value="<fmt:message key="jsp.dspace-admin.batchimport.deleteitems"/>">
</form>
<div>
</div>
<br/>
</li>
<% i++;
}
%>
</ul>
<%} %> <%} %>
<script>
function showMoreClicked(index){
$('#moreinfo_'+index).toggle( "slow", function() {
// Animation complete.
});
$('#a1_'+index).toggle();
$('#a2_'+index).toggle();
}
</script>
</div> </div>
</div> </div>
</dspace:layout> </dspace:layout>