Print mapFile output to confirm screen, also use batchimport temp dir

This commit is contained in:
Peter Dietz
2014-07-28 14:30:23 -04:00
parent 44cd6c0fde
commit 5dafc899ee
4 changed files with 70 additions and 124 deletions

View File

@@ -40,7 +40,6 @@ import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.apache.xpath.XPathAPI; import org.apache.xpath.XPathAPI;
import org.dspace.app.itemexport.ItemExportException;
import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.AuthorizeManager; import org.dspace.authorize.AuthorizeManager;
import org.dspace.authorize.ResourcePolicy; import org.dspace.authorize.ResourcePolicy;
@@ -48,8 +47,6 @@ import org.dspace.content.Bitstream;
import org.dspace.content.BitstreamFormat; import org.dspace.content.BitstreamFormat;
import org.dspace.content.Bundle; import org.dspace.content.Bundle;
import org.dspace.content.Collection; import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.content.FormatIdentifier; import org.dspace.content.FormatIdentifier;
import org.dspace.content.InstallItem; import org.dspace.content.InstallItem;
import org.dspace.content.Item; import org.dspace.content.Item;
@@ -109,9 +106,20 @@ public class ItemImport
private static boolean template = false; private static boolean template = false;
private static PrintWriter mapOut = null; private static final String tempWorkDir = ConfigurationManager.getProperty("org.dspace.app.batchitemimport.work.dir");
private static final String ziptempdir = ConfigurationManager.getProperty("org.dspace.app.itemexport.work.dir"); static {
//Ensure tempWorkDir exists
File tempWorkDirFile = new File(tempWorkDir);
if (!tempWorkDirFile.exists()){
boolean success = tempWorkDirFile.mkdir();
if (success) {
log.info("Created org.dspace.app.batchitemimport.work.dir of: " + tempWorkDir);
} else {
log.error("Cannot create batch import directory! " + tempWorkDir);
}
}
}
// File listing filter to look for metadata files // File listing filter to look for metadata files
private static FilenameFilter metadataFileFilter = new FilenameFilter() private static FilenameFilter metadataFileFilter = new FilenameFilter()
@@ -512,14 +520,6 @@ public class ItemImport
} }
catch (Exception e) catch (Exception e)
{ {
// abort all operations
if (mapOut != null)
{
mapOut.close();
}
mapOut = null;
c.abort(); c.abort();
e.printStackTrace(); e.printStackTrace();
System.out.println(e); System.out.println(e);
@@ -532,19 +532,16 @@ public class ItemImport
if (zip) if (zip)
{ {
System.gc(); System.gc();
System.out.println("Deleting temporary zip directory: " + ziptempdir); System.out.println("Deleting temporary zip directory: " + tempWorkDir);
ItemImport.deleteDirectory(new File(ziptempdir)); ItemImport.deleteDirectory(new File(tempWorkDir));
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
System.out.println("Unable to delete temporary zip archive location: " + ziptempdir); System.out.println("Unable to delete temporary zip archive location: " + tempWorkDir);
} }
if (mapOut != null)
{
mapOut.close();
}
if (isTest) if (isTest)
{ {
@@ -661,6 +658,9 @@ public class ItemImport
// create the mapfile // create the mapfile
File outFile = null; File outFile = null;
PrintWriter mapOut = null;
boolean directoryFileCollections = false; boolean directoryFileCollections = false;
if (mycollections == null) if (mycollections == null)
{ {
@@ -734,6 +734,10 @@ public class ItemImport
c.clearCache(); c.clearCache();
} }
} }
//TODO null check?
mapOut.flush();
mapOut.close();
} }
private void replaceItems(Context c, Collection[] mycollections, private void replaceItems(Context c, Collection[] mycollections,
@@ -841,7 +845,7 @@ public class ItemImport
private Item addItem(Context c, Collection[] mycollections, String path, private Item addItem(Context c, Collection[] mycollections, String path,
String itemname, PrintWriter mapOut, boolean template) throws Exception String itemname, PrintWriter mapOut, boolean template) throws Exception
{ {
String mapOutput = null; String mapOutputString = null;
System.out.println("Adding item from directory " + itemname); System.out.println("Adding item from directory " + itemname);
@@ -889,7 +893,7 @@ public class ItemImport
} }
// send ID to the mapfile // send ID to the mapfile
mapOutput = itemname + " " + myitem.getID(); mapOutputString = itemname + " " + myitem.getID();
} }
} }
else else
@@ -906,7 +910,7 @@ public class ItemImport
// find the handle, and output to map file // find the handle, and output to map file
myhandle = HandleManager.findHandle(c, myitem); myhandle = HandleManager.findHandle(c, myitem);
mapOutput = itemname + " " + myhandle; mapOutputString = itemname + " " + myhandle;
} }
// set permissions if specified in contents file // set permissions if specified in contents file
@@ -932,7 +936,7 @@ public class ItemImport
// made it this far, everything is fine, commit transaction // made it this far, everything is fine, commit transaction
if (mapOut != null) if (mapOut != null)
{ {
mapOut.println(mapOutput); mapOut.println(mapOutputString);
} }
c.commit(); c.commit();
@@ -1979,8 +1983,6 @@ public class ItemImport
} }
public static String unzip(File zipfile) throws IOException { public static String unzip(File zipfile) throws IOException {
log.info("ZIPFILE: " + zipfile.getAbsolutePath());
// 2 // 2
// does the zip file exist and can we write to the temp directory // does the zip file exist and can we write to the temp directory
if (!zipfile.canRead()) if (!zipfile.canRead())
@@ -1989,7 +1991,7 @@ public class ItemImport
} }
File tempdir = new File(ziptempdir); File tempdir = new File(tempWorkDir);
if (!tempdir.isDirectory()) if (!tempdir.isDirectory())
{ {
log.error("'" + ConfigurationManager.getProperty("org.dspace.app.itemexport.work.dir") + log.error("'" + ConfigurationManager.getProperty("org.dspace.app.itemexport.work.dir") +
@@ -1999,10 +2001,10 @@ public class ItemImport
if (!tempdir.exists() && !tempdir.mkdirs()) if (!tempdir.exists() && !tempdir.mkdirs())
{ {
log.error("Unable to create temporary directory"); log.error("Unable to create temporary directory: " + tempdir.getAbsolutePath());
} }
String sourcedir = ziptempdir + System.getProperty("file.separator") + zipfile.getName(); String sourcedir = tempWorkDir + System.getProperty("file.separator") + zipfile.getName();
String zipDir = ziptempdir + System.getProperty("file.separator") + zipfile.getName() + System.getProperty("file.separator"); String zipDir = tempWorkDir + System.getProperty("file.separator") + zipfile.getName() + System.getProperty("file.separator");
// 3 // 3
@@ -2017,7 +2019,7 @@ public class ItemImport
{ {
if (!new File(zipDir + entry.getName()).mkdir()) if (!new File(zipDir + entry.getName()).mkdir())
{ {
log.error("Unable to create contents directory"); log.error("Unable to create contents directory: " + zipDir + entry.getName());
} }
} }
else else
@@ -2034,7 +2036,7 @@ public class ItemImport
File dir = new File(zipDir + entry.getName().substring(0, index)); File dir = new File(zipDir + entry.getName().substring(0, index));
if (!dir.mkdirs()) if (!dir.mkdirs())
{ {
log.error("Unable to create directory"); log.error("Unable to create directory: " + dir.getAbsolutePath());
} }
//Entries could have too many directories, and we need to adjust the sourcedir //Entries could have too many directories, and we need to adjust the sourcedir
@@ -2214,7 +2216,7 @@ public class ItemImport
{ {
iitems.close(); iitems.close();
} }
// close the mapfile writer // close the mapfile writer
if (mapOut != null) if (mapOut != null)
{ {
@@ -2569,4 +2571,8 @@ public class ItemImport
FileDeleteStrategy.FORCE.delete(new File(uploadDir)); FileDeleteStrategy.FORCE.delete(new File(uploadDir));
} }
public static String getTempWorkDir() {
return tempWorkDir;
}
} }

View File

@@ -7,34 +7,25 @@
*/ */
package org.dspace.app.xmlui.aspect.administrative; package org.dspace.app.xmlui.aspect.administrative;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.io.File;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.cocoon.environment.Request; import org.apache.cocoon.environment.Request;
import org.apache.cocoon.servlet.multipart.Part; import org.apache.cocoon.servlet.multipart.Part;
import org.apache.cocoon.servlet.multipart.PartOnDisk; import org.apache.cocoon.servlet.multipart.PartOnDisk;
import org.dspace.app.bulkedit.BulkEditChange; import org.apache.commons.io.FileUtils;
import org.dspace.app.bulkedit.DSpaceCSV; import org.apache.commons.lang.StringUtils;
import org.dspace.app.bulkedit.MetadataImport; import org.apache.log4j.Logger;
import org.dspace.app.bulkedit.MetadataImportException;
import org.dspace.app.bulkedit.MetadataImportInvalidHeadingException;
import org.dspace.app.itemimport.ItemImport; import org.dspace.app.itemimport.ItemImport;
import org.dspace.app.itemimport.ItemImportOptions;
import org.dspace.app.xmlui.utils.HandleUtil;
import org.dspace.app.xmlui.wing.Message; import org.dspace.app.xmlui.wing.Message;
import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Collection; import org.dspace.content.Collection;
import org.dspace.core.Context;
import org.dspace.core.ConfigurationManager; import org.dspace.core.ConfigurationManager;
import org.dspace.core.Context;
import org.dspace.core.LogManager; import org.dspace.core.LogManager;
import org.dspace.eperson.EPerson;
import org.dspace.handle.HandleManager; import org.dspace.handle.HandleManager;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
/** /**
* Utility methods to processes BatchImport actions. These methods are used * Utility methods to processes BatchImport actions. These methods are used
* exclusively from the administrative flow scripts. * exclusively from the administrative flow scripts.
@@ -49,16 +40,12 @@ public class FlowBatchImportUtils {
*/ */
private static final Message T_upload_successful = new Message("default", "xmlui.administrative.batchimport.flow.upload_successful"); private static final Message T_upload_successful = new Message("default", "xmlui.administrative.batchimport.flow.upload_successful");
private static final Message T_upload_failed = new Message("default", "xmlui.administrative.batchimport.flow.upload_failed"); private static final Message T_upload_failed = new Message("default", "xmlui.administrative.batchimport.flow.upload_failed");
private static final Message T_upload_badschema = new Message("default", "xmlui.administrative.batchimport.flow.upload_badschema");
private static final Message T_upload_badelement = new Message("default", "xmlui.administrative.batchimport.flow.upload_badelement");
private static final Message T_import_successful = new Message("default", "xmlui.administrative.batchimport.flow.import_successful"); private static final Message T_import_successful = new Message("default", "xmlui.administrative.batchimport.flow.import_successful");
private static final Message T_import_failed = new Message("default", "xmlui.administrative.batchimport.flow.import_failed"); private static final Message T_import_failed = new Message("default", "xmlui.administrative.batchimport.flow.import_failed");
private static final Message T_over_limit = new Message("default", "xmlui.administrative.batchimport.flow.over_limit");
private static final Message T_no_changes = new Message("default", "xmlui.administrative.batchimport.general.no_changes"); private static final Message T_no_changes = new Message("default", "xmlui.administrative.batchimport.general.no_changes");
private static final Message T_failed_no_collection = new Message("default", "xmlui.administrative.batchimport.flow.failed_no_collection"); private static final Message T_failed_no_collection = new Message("default", "xmlui.administrative.batchimport.flow.failed_no_collection");
// Other variables // Other variables
private static final int limit = ConfigurationManager.getIntProperty("bulkedit", "gui-item-limit", 20);
private static Logger log = Logger.getLogger(FlowBatchImportUtils.class); private static Logger log = Logger.getLogger(FlowBatchImportUtils.class);
public static FlowResult processBatchImport(Context context, Request request) throws SQLException, AuthorizeException, IOException, Exception { public static FlowResult processBatchImport(Context context, Request request) throws SQLException, AuthorizeException, IOException, Exception {
@@ -67,7 +54,6 @@ public class FlowBatchImportUtils {
result.setContinue(false); result.setContinue(false);
String zipFile = (String) request.getSession().getAttribute("zip"); String zipFile = (String) request.getSession().getAttribute("zip");
log.info(zipFile);
if (zipFile != null) { if (zipFile != null) {
// Commit the changes // Commit the changes
@@ -76,6 +62,7 @@ public class FlowBatchImportUtils {
log.debug(LogManager.getHeader(context, "batchimport", " items changed")); log.debug(LogManager.getHeader(context, "batchimport", " items changed"));
//TODO: I don't think this section actually does anything.
if (true) { if (true) {
result.setContinue(true); result.setContinue(true);
result.setOutcome(true); result.setOutcome(true);
@@ -124,17 +111,13 @@ public class FlowBatchImportUtils {
name = name.substring(name.indexOf('\\') + 1); name = name.substring(name.indexOf('\\') + 1);
} }
log.info(LogManager.getHeader(context, "batchimport", "loading file"));
// Process CSV without import // Process CSV without import
ItemImport itemImport = new ItemImport(); ItemImport itemImport = new ItemImport();
String collectionHandle = null; String collectionHandle = String.valueOf(request.get("collectionHandle"));
if (request.get("collectionHandle") != null) { if (StringUtils.isEmpty(collectionHandle) || !collectionHandle.contains("/")) {
collectionHandle = request.get("collectionHandle").toString();
} else {
//fail //fail
log.info("batch fail"); log.error("UIBatchImport failed due to no collection.");
result.setContinue(false); result.setContinue(false);
result.setOutcome(false); result.setOutcome(false);
result.setMessage(T_failed_no_collection); result.setMessage(T_failed_no_collection);
@@ -145,27 +128,12 @@ public class FlowBatchImportUtils {
Collection[] collections = new Collection[1]; Collection[] collections = new Collection[1];
collections[0] = collection; collections[0] = collection;
log.info("Batch to collection: " + collection.getName()); File tempWorkDir = new File(itemImport.getTempWorkDir());
File mapFile = File.createTempFile("batch-", ".map", tempWorkDir);
/* log.info("Attempt UIBatchImport to collection: " + collection.getName()
ItemImportOptions itemImportOptions = new ItemImportOptions(); + ", zip: " + file.getName()
+ ", map: "+ mapFile.getAbsolutePath());
itemImportOptions.setZipFilePath(file);
Collection destCollection = Collection.find(context, 10);
itemImportOptions.setCollection(destCollection);
EPerson epersonSubmitter = EPerson.findByEmail(context, "peter@longsight.com");
itemImportOptions.setEpersonSubmitter(epersonSubmitter);
itemImportOptions.setUseTemplate(true);
*/
File mapFile = File.createTempFile("batch", "map");
/* PrintWriter mapOut = new PrintWriter(new FileWriter(mapFile));
itemImportOptions.setMapFileOut(mapOut);
log.info("MapFile: " + mapFile.getAbsolutePath());
//Process ItemImport with ZIP, and other options.
*/
/* /*
// equivalent command-line would be: // equivalent command-line would be:
@@ -193,35 +161,23 @@ public class FlowBatchImportUtils {
itemImport.addItems(context, collections, sourceBatchDir, mapFile.getAbsolutePath(), true); itemImport.addItems(context, collections, sourceBatchDir, mapFile.getAbsolutePath(), true);
if(true) // Success!
{ // Set session and request attributes
// Success! result.setContinue(true);
// Set session and request attributes result.setOutcome(true);
result.setMessage(T_upload_successful);
result.setCharacters(FileUtils.readFileToString(mapFile));
//request.setAttribute("changes", changes); log.info("Success! UIBatchImport to collection: " + collection.getName()
//request.getSession().setAttribute("csv", csv); + ", zip: " + file.getName()
log.info("batch success"); + ", map: "+ mapFile.getAbsolutePath());
} else {
result.setContinue(true); //No ZIP File, or upload failed
result.setOutcome(true);
result.setMessage(T_upload_successful);
}
else
{
//fail
log.info("batch fail");
result.setContinue(false);
result.setOutcome(false);
result.setMessage(T_no_changes);
}
}
else
{
result.setContinue(false); result.setContinue(false);
result.setOutcome(false); result.setOutcome(false);
result.setMessage(T_upload_failed); result.setMessage(T_upload_failed);
} }
return result; return result;
} }
} }

View File

@@ -44,26 +44,10 @@ public class BatchImportUpload extends AbstractDSpaceTransformer {
/** Language strings */ /** Language strings */
private static final Message T_dspace_home = message("xmlui.general.dspace_home"); private static final Message T_dspace_home = message("xmlui.general.dspace_home");
private static final Message T_submit_return = message("xmlui.general.return");
private static final Message T_trail = message("xmlui.administrative.batchimport.general.trail"); private static final Message T_trail = message("xmlui.administrative.batchimport.general.trail");
private static final Message T_no_changes = message("xmlui.administrative.batchimport.general.no_changes");
private static final Message T_new_item = message("xmlui.administrative.batchimport.general.new_item");
private static final Message T_title = message("xmlui.administrative.batchimport.general.title"); private static final Message T_title = message("xmlui.administrative.batchimport.general.title");
private static final Message T_head1 = message("xmlui.administrative.batchimport.general.head1"); private static final Message T_head1 = message("xmlui.administrative.batchimport.general.head1");
private static final Message T_para = message("xmlui.administrative.batchimport.BatchImportUpload.hint");
private static final Message T_submit_confirm = message("xmlui.administrative.batchimport.BatchImportUpload.submit_confirm");
private static final Message T_changes_pending = message("xmlui.administrative.batchimport.BatchImportUpload.changes_pending");
private static final Message T_item_addition = message("xmlui.administrative.batchimport.BatchImportUpload.item_add");
private static final Message T_item_deletion = message("xmlui.administrative.batchimport.BatchImportUpload.item_remove");
private static final Message T_collection_newowner = message("xmlui.administrative.batchimport.BatchImportUpload.collection_newowner");
private static final Message T_collection_oldowner = message("xmlui.administrative.batchimport.BatchImportUpload.collection_oldowner");
private static final Message T_collection_mapped = message("xmlui.administrative.batchimport.BatchImportUpload.collection_mapped");
private static final Message T_collection_unmapped = message("xmlui.administrative.batchimport.BatchImportUpload.collection_unmapped");
private static final Message T_item_delete = message("xmlui.administrative.batchimport.BatchImportUpload.item_delete");
private static final Message T_item_withdraw = message("xmlui.administrative.batchimport.BatchImportUpload.item_withdraw");
private static final Message T_item_reinstate = message("xmlui.administrative.batchimport.BatchImportUpload.item_reinstate");
public void addPageMeta(PageMeta pageMeta) throws WingException public void addPageMeta(PageMeta pageMeta) throws WingException
{ {
pageMeta.addMetadata("title").addContent(T_title); pageMeta.addMetadata("title").addContent(T_title);

View File

@@ -1454,7 +1454,7 @@
<message key="xmlui.administrative.batchimport.general.no_changes">No changes were detected</message> <message key="xmlui.administrative.batchimport.general.no_changes">No changes were detected</message>
<message key="xmlui.administrative.batchimport.general.new_item">New item</message> <message key="xmlui.administrative.batchimport.general.new_item">New item</message>
<message key="xmlui.administrative.batchimport.flow.upload_successful">Upload successful</message> <message key="xmlui.administrative.batchimport.flow.upload_successful">Upload successful</message>
<message key="xmlui.administrative.batchimport.flow.upload_failed">Upload failed</message> <message key="xmlui.administrative.batchimport.flow.upload_failed">Upload failed or no file selected</message>
<message key="xmlui.administrative.batchimport.flow.upload_badschema">Unknown metadata schema in heading</message> <message key="xmlui.administrative.batchimport.flow.upload_badschema">Unknown metadata schema in heading</message>
<message key="xmlui.administrative.batchimport.flow.upload_badelement">Unknown metadata element in heading</message> <message key="xmlui.administrative.batchimport.flow.upload_badelement">Unknown metadata element in heading</message>
<message key="xmlui.administrative.batchimport.flow.import_successful">Import successful</message> <message key="xmlui.administrative.batchimport.flow.import_successful">Import successful</message>