Hi Pedro, I will attach two files - the whole program and the pom.xml. The program runs standalone on the server, for every item reads a directory, zips it and creates the item. The program is as I needed it in my case. I think you need a Java developer to make it work for you.
Best regards Evgeni On Tuesday, May 31, 2016 at 2:58:05 PM UTC+3, Pedro Amorim wrote: > > Hello Evgeni, > > Yes that is definetly an option for me, even being an alternative solution. > I was already planning on developing a script in shell to package folders > in zips and run dspace/bin packager for each of them. > > Would you please elaborate on: > > // get parent object > parentObj = HandleManager > .resolveToObject(context, itemParentHandle); > > // create a single item > itemObj = metsIngester.ingest( > context, > parentObj, > zippedItemPath.toFile(), > pkgParams, > null > ); > > I'm not the most experienced Java developer and wouldn't know how to > implement custom code in DSpace. > > Thank you so much for replying. > > Cheers, > > Pedro Amorim > > -- You received this message because you are subscribed to the Google Groups "DSpace Technical Support" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/dspace-tech. For more options, visit https://groups.google.com/d/optout.
// DSPItemCreator.java
//
package bg.nalis.dsptools;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.dspace.content.DSpaceObject;
import org.dspace.content.packager.PackageIngester;
import org.dspace.content.packager.PackageParameters;
import org.dspace.core.ConfigurationManager;
import org.dspace.core.Context;
import org.dspace.core.PluginManager;
import org.dspace.eperson.EPerson;
import org.dspace.handle.HandleManager;
import org.dspace.servicemanager.DSpaceKernelInit;
/**
* The class creates items.
*/
public class DSPItemCreator {
// logWriter
private BufferedWriter logWriter;
// ----------------------------------------------------- Final Fields
private static final String ADMIN_PERSON_EMAIL = "[email protected]";
// directory
private static final String ITEMS_DIR = "/stagingarea/dtlexports/mets/";
// fileNames
private static final String ITEMS_MAP = "items_map.txt";
private static final String COLLECTIONS_MAP = "collections_map.txt";
private static final String ITEM_ZIPPED = "item.zip";
private static final String ERRLOG = "errlog.txt";
private static final Charset FILEENCODING = StandardCharsets.UTF_8;
// ----------------------------------------------------- Fields
private Map<String, String> itemsMap;
private Map<String, String> collectionsMap;
private List<String> itemsList;
// ----------------------------------------------------- Methods
/**
*
*/
public static void main(String[] args) {
new DSPItemCreator().go();
}
/**
*
*/
private void go() {
// try to create logWriter and exit on failure
try {
Path logPath = Paths.get(ERRLOG);
logWriter = Files.newBufferedWriter(logPath, FILEENCODING);
}
catch (IOException e) {
return;
}
try {
createItem();
}
catch (Throwable t) {
t.printStackTrace();
logTrace(t);
}
try {
logWriter.close();
}
catch (Exception ex) {}
}
/**
*
*/
private void createItem() {
// context
Context context = null;
// parameters
PackageParameters pkgParams = new PackageParameters();
pkgParams.setWorkflowEnabled(false);
// ingester
PackageIngester metsIngester = null;
try {
// read files
itemsMap = readFileToMap(ITEMS_MAP, FILEENCODING);
collectionsMap = readFileToMap(COLLECTIONS_MAP, FILEENCODING);
// read directory
Path dirPath = Paths.get(ITEMS_DIR);
File dir = dirPath.toFile();
String[] subDirs = dir.list();
itemsList = Arrays.asList(subDirs);
Collections.sort(itemsList);
//--- start services ---
( DSpaceKernelInit
.getKernel(null) )
.start(ConfigurationManager.getProperty("dspace.dir"));
// get ingester
metsIngester = (PackageIngester) PluginManager
.getNamedPlugin(PackageIngester.class, "METS");
}
catch (Exception e) {
e.printStackTrace();
logTrace(e);
}
// for every item
int tries = 0;
int hits = 0;
for (String line: itemsList) {
tries++;
try {
// get item
String itemName = line;
String itemParent = itemsMap.get(line);
if (itemParent == null) {
// System.out.println(
// "NO PARENT for " +
// "::item::" + itemName
// );
// continue;
itemParent = "***";
}
String itemParentHandle = collectionsMap.get(itemParent);
log(
"processing item::" + itemName +
"::" + itemParent +
"::" + itemParentHandle
);
System.out.println(
"processing item::" + itemName +
"::" + itemParent +
"::" + itemParentHandle
);
// make zipped item file
zipDir(itemName);
// wait a bit
try {
Thread.sleep(1000);
}
catch (Exception e) {
}
// get zipped item file
File zippedItemFile = new File(ITEM_ZIPPED);
// get context
if ( ((tries % 10) == 0) && context!=null ) {
context.complete();
context = null;
}
if (context == null) {
context = getContext();
}
// get parent object
DSpaceObject parentObj = HandleManager
.resolveToObject(context, itemParentHandle);
// create a single item
DSpaceObject itemObj = metsIngester.ingest(
context,
parentObj,
zippedItemFile,
pkgParams,
null
);
// count success
System.out.println(
"object::" + tries +
"::parent::" + parentObj.getHandle() +
"::item::" + itemObj.getHandle()
);
hits++;
}
catch (Exception e) {
e.printStackTrace();
log("***Ingest failed.");
logTrace(e);
}
}
// close context
try {
context.complete();
}
catch (Exception ex) {}
System.out.println("number of items successfully processed::" + hits);
}
/**
* get context
*/
private static Context getContext()
throws Exception {
Context context = null;
EPerson ePerson = null;
try {
context = new Context();
ePerson = EPerson.findByEmail(context, ADMIN_PERSON_EMAIL);
context.setCurrentUser(ePerson);
}
catch (Exception e) {
throw new Exception(e);
}
return context;
}
/**
* help function that reads a file into a Map
*/
private Map<String, String> readFileToMap(String fileName, Charset fileEncoding)
throws IOException {
Map<String, String> map = new TreeMap<String, String>();
String line;
String[] lineParts;
Path path = Paths.get(fileName);
BufferedReader reader = Files.newBufferedReader(path, fileEncoding);
while ( (line=reader.readLine()) != null) {
line = line.trim();
if (!line.isEmpty()) {
lineParts = line.split("~");
map.put(lineParts[0], lineParts[1]);
}
}
try {
reader.close();
}
catch (Exception e) {
}
return map;
}
/**
* help function that zips a directory
*/
private void zipDir(String dir)
throws IOException {
FileOutputStream fos = new FileOutputStream(ITEM_ZIPPED);
ZipOutputStream zos = new ZipOutputStream(fos);
File dirFile = new File(ITEMS_DIR, dir);
File[] streamFiles = dirFile.listFiles();
byte[] buffer = new byte[1024];
for (File streamFile: streamFiles) {
String streamName = streamFile.getName();
ZipEntry ze= new ZipEntry(streamName);
zos.putNextEntry(ze);
FileInputStream fis =
new FileInputStream(streamFile);
int len;
while ((len = fis.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
fis.close();
zos.closeEntry();
}
zos.close();
}
/**
*
*/
private void log(String s) {
try {
logWriter.write(s);
logWriter.newLine();
}
catch (IOException e) {
}
}
/**
*
*/
private void logTrace(Throwable t) {
PrintWriter printWriter = new PrintWriter(logWriter);
t.printStackTrace(printWriter);
printWriter.flush();
}
}
pom.xml
Description: XML document
