Search in sources :

Example 1 with ImportException

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();
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) ImportException(edu.harvard.iq.dataverse.api.imports.ImportException) Dataverse(edu.harvard.iq.dataverse.Dataverse)

Example 2 with ImportException

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");
}
Also used : ImportException(edu.harvard.iq.dataverse.api.imports.ImportException) FileWriter(java.io.FileWriter) JsonArrayBuilder(javax.json.JsonArrayBuilder) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) Date(java.util.Date) ImportException(edu.harvard.iq.dataverse.api.imports.ImportException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) Asynchronous(javax.ejb.Asynchronous)

Example 3 with ImportException

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());
    }
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) ImportException(edu.harvard.iq.dataverse.api.imports.ImportException) IOException(java.io.IOException) JsonObjectBuilder(javax.json.JsonObjectBuilder) Dataverse(edu.harvard.iq.dataverse.Dataverse) PrintWriter(java.io.PrintWriter) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 4 with ImportException

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;
}
Also used : ImportException(edu.harvard.iq.dataverse.api.imports.ImportException) JsonArrayBuilder(javax.json.JsonArrayBuilder) IOException(java.io.IOException) JsonObjectBuilder(javax.json.JsonObjectBuilder) Dataverse(edu.harvard.iq.dataverse.Dataverse) File(java.io.File)

Aggregations

ImportException (edu.harvard.iq.dataverse.api.imports.ImportException)4 Dataverse (edu.harvard.iq.dataverse.Dataverse)3 IOException (java.io.IOException)3 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)2 File (java.io.File)2 PrintWriter (java.io.PrintWriter)2 JsonArrayBuilder (javax.json.JsonArrayBuilder)2 JsonObjectBuilder (javax.json.JsonObjectBuilder)2 FileWriter (java.io.FileWriter)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Asynchronous (javax.ejb.Asynchronous)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1