use of edu.harvard.iq.dataverse.api.imports.ImportException in project dataverse by IQSS.
the class BatchImport method startBatchJob.
private Response startBatchJob(String fileDir, String parentIdtf, String apiKey, ImportType importType, Boolean createDV) {
if (createDV == null) {
createDV = Boolean.FALSE;
}
try {
DataverseRequest dataverseRequest;
try {
dataverseRequest = createDataverseRequest(findAuthenticatedUserOrDie());
} catch (WrappedResponse wr) {
return wr.getResponse();
}
if (parentIdtf == null) {
parentIdtf = "root";
}
Dataverse owner = findDataverse(parentIdtf);
if (owner == null) {
if (createDV) {
owner = importService.createDataverse(parentIdtf, dataverseRequest);
} else {
return error(Response.Status.NOT_FOUND, "Can't find dataverse with identifier='" + parentIdtf + "'");
}
}
batchService.processFilePath(fileDir, parentIdtf, dataverseRequest, owner, importType, createDV);
} catch (ImportException e) {
return this.error(Response.Status.BAD_REQUEST, "Import Exception, " + e.getMessage());
}
return this.accepted();
}
use of edu.harvard.iq.dataverse.api.imports.ImportException in project dataverse by IQSS.
the class BatchServiceBean method processFilePath.
@Asynchronous
public void processFilePath(String fileDir, String parentIdtf, DataverseRequest dataverseRequest, Dataverse owner, ImportUtil.ImportType importType, Boolean createDV) {
logger.info("BEGIN IMPORT");
PrintWriter validationLog = null;
PrintWriter cleanupLog = null;
try {
JsonArrayBuilder status = Json.createArrayBuilder();
Date timestamp = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
validationLog = new PrintWriter(new FileWriter("../logs/validationLog" + formatter.format(timestamp) + ".txt"));
cleanupLog = new PrintWriter(new FileWriter("../logs/cleanupLog" + formatter.format(timestamp) + ".txt"));
File dir = new File(fileDir);
if (dir.isDirectory()) {
for (File file : dir.listFiles()) {
if (!file.isHidden()) {
if (file.isDirectory()) {
try {
status.add(handleDirectory(dataverseRequest, file, importType, validationLog, cleanupLog, createDV));
} catch (ImportException e) {
logger.log(Level.SEVERE, "Exception in handleDirectory() for " + file.getName(), e);
}
} else {
try {
status.add(importService.handleFile(dataverseRequest, owner, file, importType, validationLog, cleanupLog));
} catch (ImportException e) {
logger.log(Level.SEVERE, "Exception in handleFile() for " + file.getName(), e);
}
}
}
}
} else {
status.add(importService.handleFile(dataverseRequest, owner, dir, importType, validationLog, cleanupLog));
}
} catch (Exception e) {
logger.log(Level.SEVERE, "Exception in processFilePath()", e);
} finally {
validationLog.close();
cleanupLog.close();
}
logger.info("END IMPORT");
}
use of edu.harvard.iq.dataverse.api.imports.ImportException in project dataverse by IQSS.
the class BatchImport method postImport.
/**
* Import a new Dataset with DDI xml data posted in the request
*
* @param body the xml
* @param parentIdtf the dataverse to import into (id or alias)
* @param apiKey user's api key
* @return import status (including id of the dataset created)
*/
@POST
@Path("import")
public Response postImport(String body, @QueryParam("dv") String parentIdtf, @QueryParam("key") String apiKey) {
DataverseRequest dataverseRequest;
try {
dataverseRequest = createDataverseRequest(findAuthenticatedUserOrDie());
} catch (WrappedResponse wr) {
return wr.getResponse();
}
if (parentIdtf == null) {
parentIdtf = "root";
}
Dataverse owner = findDataverse(parentIdtf);
if (owner == null) {
return error(Response.Status.NOT_FOUND, "Can't find dataverse with identifier='" + parentIdtf + "'");
}
try {
// Cleanup log isn't needed for ImportType == NEW. We don't do any data cleanup in this mode.
PrintWriter cleanupLog = null;
// Since this is a single input from a POST, there is no file that we are reading from.
String filename = null;
JsonObjectBuilder status = importService.doImport(dataverseRequest, owner, body, filename, ImportType.NEW, cleanupLog);
return this.ok(status);
} catch (ImportException | IOException e) {
return this.error(Response.Status.BAD_REQUEST, e.getMessage());
}
}
use of edu.harvard.iq.dataverse.api.imports.ImportException in project dataverse by IQSS.
the class BatchServiceBean method handleDirectory.
public JsonArrayBuilder handleDirectory(DataverseRequest dataverseRequest, File dir, ImportUtil.ImportType importType, PrintWriter validationLog, PrintWriter cleanupLog, Boolean createDV) throws ImportException {
JsonArrayBuilder status = Json.createArrayBuilder();
Dataverse owner = dataverseService.findByAlias(dir.getName());
if (owner == null) {
if (createDV) {
System.out.println("creating new dataverse: " + dir.getName());
owner = importService.createDataverse(dir.getName(), dataverseRequest);
} else {
throw new ImportException("Can't find dataverse with identifier='" + dir.getName() + "'");
}
}
for (File file : dir.listFiles()) {
if (!file.isHidden()) {
try {
JsonObjectBuilder fileStatus = importService.handleFile(dataverseRequest, owner, file, importType, validationLog, cleanupLog);
status.add(fileStatus);
} catch (ImportException | IOException e) {
status.add(Json.createObjectBuilder().add("importStatus", "Exception importing " + file.getName() + ", message = " + e.getMessage()));
}
}
}
return status;
}
Aggregations