Search in sources :

Example 1 with ImportSubstanceException

use of ambit2.rest.exception.ImportSubstanceException in project ambit-mirror by ideaconsult.

the class TaskJSONReporter method processItem.

@Override
public void processItem(UUID item, Writer output) {
    try {
        if (comma != null)
            output.write(comma);
        ITask<ITaskResult, USERID> task = storage.findTask(item);
        String uri = task.getUri() == null ? null : task.getUri().toString();
        StringWriter errorTrace = null;
        if (task.getError() != null) {
            try {
                ResourceException exception = task.getError();
                errorTrace = new StringWriter();
                if (exception instanceof ImportSubstanceException) {
                    errorTrace.write(((ImportSubstanceException) exception).getDetails());
                } else if (exception.getCause() == null)
                    exception.printStackTrace(new PrintWriter(errorTrace));
                else
                    exception.getCause().printStackTrace(new PrintWriter(errorTrace));
            } catch (Exception x) {
            }
        }
        output.write(String.format(format, uri, item.toString(), task.getName() == null ? "" : JSONUtils.jsonEscape(task.getName()), task.getError() == null ? "" : JSONUtils.jsonEscape(task.getError().toString()), task.getPolicyError() == null ? "" : JSONUtils.jsonEscape(task.getPolicyError().toString()), task.getStatus() == null ? "" : task.getStatus(), task.getStarted(), task.getTimeCompleted(), task.getUri() == null ? "" : task.getUri(), errorTrace == null ? null : JSONUtils.jsonQuote(JSONUtils.jsonEscape(errorTrace.toString()))));
        comma = ",";
    } catch (IOException x) {
        Context.getCurrentLogger().severe(x.getMessage());
    }
}
Also used : ImportSubstanceException(ambit2.rest.exception.ImportSubstanceException) StringWriter(java.io.StringWriter) ITaskResult(net.idea.restnet.i.task.ITaskResult) ResourceException(org.restlet.resource.ResourceException) IOException(java.io.IOException) IOException(java.io.IOException) ResourceException(org.restlet.resource.ResourceException) ImportSubstanceException(ambit2.rest.exception.ImportSubstanceException) PrintWriter(java.io.PrintWriter)

Example 2 with ImportSubstanceException

use of ambit2.rest.exception.ImportSubstanceException in project ambit-mirror by ideaconsult.

the class CallableSubstanceI5Query method doCall.

@Override
public TaskResult doCall() throws Exception {
    Connection connection = null;
    DBConnection dbc = null;
    IUCLIDLightClient i5 = null;
    DBSubstanceWriter writer = null;
    FileHandler logHandler = null;
    StreamHandler streamHandler = null;
    OutputStream out = new ByteArrayOutputStream();
    try {
        logger.log(Level.INFO, String.format("Logging to %s", logFile));
        logHandler = new FileHandler(logFile);
        localLogger.addHandler(logHandler);
        SimpleFormatter formatter = new SimpleFormatter();
        streamHandler = new StreamHandler(out, formatter);
        localLogger.addHandler(streamHandler);
        logHandler.setFormatter(formatter);
        localLogger.setUseParentHandlers(false);
        localLogger.fine("Start()");
        if (useUUID) {
            if (substanceUUID == null || "".equals(substanceUUID))
                throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Empty UUID");
            dbc = new DBConnection(context);
            if (server == null)
                server = dbc.getProperty(iuclidversion.getProperty(".server"));
            if (user == null)
                user = dbc.getProperty(iuclidversion.getProperty(".user"));
            if (pass == null)
                pass = dbc.getProperty(iuclidversion.getProperty(".pass"));
            try {
                i5 = iuclidversion.createClient(server);
                if (i5.login(user, pass)) {
                    IContainerClient ccli = i5.getContainerClient();
                    substanceUUID = iuclidversion.formatQueryUUID(substanceUUID);
                    localLogger.log(Level.FINE, String.format("Retieving %s from %s", substanceUUID, server));
                    List<IIdentifiableResource<IIdentifier>> container = null;
                    try {
                        container = ccli.get(new Identifier(substanceUUID));
                    } catch (Exception x) {
                        localLogger.log(Level.SEVERE, String.format("Failed to retrieve %s from %s. Error %s", substanceUUID, server, x.getMessage()));
                        streamHandler.flush();
                        String msg = out.toString();
                        throw new ImportSubstanceException(new Status(Status.SERVER_ERROR_BAD_GATEWAY, String.format("Failed to retrieve %s", substanceUUID)), msg);
                    }
                    connection = dbc.getConnection();
                    localLogger.log(Level.INFO, String.format("Importing %s", substanceUUID, server));
                    writer = new DBSubstanceWriter(DBSubstanceWriter.datasetMeta(), new SubstanceRecord(), clearMeasurements, clearComposition);
                    writer.setI5mode(true);
                    writer.setCloseConnection(false);
                    writer.setConnection(connection);
                    writer.open();
                    IIdentifiableResource<IIdentifier> result = processContainer(container, writer, server);
                } else
                    throw new ImportSubstanceException(new Status(Status.SERVER_ERROR_BAD_GATEWAY, "IUCLID5 server login failed"), "IUCLID5 server login failed [" + server + "].");
            } catch (ResourceException x) {
                throw x;
            } catch (Exception x) {
                streamHandler.flush();
                String msg = out.toString();
                throw new ImportSubstanceException(new Status(Status.SERVER_ERROR_BAD_GATEWAY, x.getMessage()), msg);
            }
        } else {
            // query
            if (extIDType == null || extIDValue == null)
                throw new ImportSubstanceException(new Status(Status.CLIENT_ERROR_BAD_REQUEST, "Invalid query"), "Invalid query");
            dbc = new DBConnection(context);
            if (server == null)
                server = dbc.getProperty(iuclidversion.getProperty(".server"));
            if (user == null)
                user = dbc.getProperty(iuclidversion.getProperty(".user"));
            if (pass == null)
                pass = dbc.getProperty(iuclidversion.getProperty(".pass"));
            try {
                i5 = iuclidversion.createClient(server);
                if (i5.login(user, pass)) {
                    IQueryToolClient cli = i5.getQueryToolClient();
                    List<IIdentifiableResource<IIdentifier>> queryResults = cli.executeQuery(iuclidversion.createQuery(extIDValue), extIDType, extIDValue);
                    if (queryResults != null && queryResults.size() > 0) {
                        connection = dbc.getConnection();
                        writer = new DBSubstanceWriter(DBSubstanceWriter.datasetMeta(), new SubstanceRecord(), clearMeasurements, clearComposition);
                        writer.setI5mode(true);
                        writer.setCloseConnection(false);
                        writer.setConnection(connection);
                        writer.open();
                        IContainerClient ccli = i5.getContainerClient();
                        int imported = 0;
                        int importError = 0;
                        for (IIdentifiableResource<IIdentifier> item : queryResults) try {
                            localLogger.log(Level.FINE, item.getResourceIdentifier().toString());
                            List<IIdentifiableResource<IIdentifier>> container = ccli.get(new Identifier(iuclidversion.formatQueryUUID(item.getResourceIdentifier().toString())));
                            processContainer(container, writer, server);
                            imported++;
                        } catch (Exception x) {
                            localLogger.log(Level.SEVERE, x.getMessage());
                            importError++;
                        }
                        String foundMsg = String.format("Found %d substance(s), imported %d substance(s) %d error(s).", queryResults.size(), imported, importError);
                        localLogger.log(Level.INFO, foundMsg);
                        streamHandler.flush();
                        String msg = out.toString();
                        if (importError != 0) {
                            if (imported > 0) {
                                TaskResult result = createReference(connection);
                                msg = msg + String.format("The imported substances are available at %s", result.getUri());
                            }
                            throw new ImportSubstanceException(new Status(Status.SERVER_ERROR_BAD_GATEWAY, foundMsg), msg);
                        } else if (imported == 0) {
                            throw new ImportSubstanceException(new Status(Status.CLIENT_ERROR_NOT_FOUND, foundMsg), msg);
                        }
                    } else {
                        streamHandler.flush();
                        String msg = out.toString();
                        throw new ImportSubstanceException(new Status(Status.CLIENT_ERROR_NOT_FOUND, "No substances found."), msg);
                    }
                } else
                    throw new ImportSubstanceException(new Status(Status.SERVER_ERROR_BAD_GATEWAY, "IUCLID server login failed"), "IUCLID server login failed [" + server + "].");
            } catch (ResourceException x) {
                throw x;
            } catch (Exception x) {
                localLogger.log(Level.SEVERE, x.getMessage());
                streamHandler.flush();
                String msg = out.toString();
                throw new ImportSubstanceException(new Status(Status.SERVER_ERROR_BAD_GATEWAY, "Error when retrieving substances"), msg);
            }
        }
        return createReference(connection);
    } catch (ResourceException x) {
        localLogger.log(Level.SEVERE, x.getMessage(), x);
        throw x;
    } catch (Exception x) {
        localLogger.log(Level.SEVERE, x.getMessage(), x);
        throw x;
    } finally {
        localLogger.fine("Done");
        try {
            if (i5 != null)
                i5.logout();
        } catch (Exception x) {
            localLogger.warning(x.getMessage());
        }
        try {
            if (i5 != null)
                i5.close();
        } catch (Exception x) {
            localLogger.warning(x.getMessage());
        }
        try {
            writer.setConnection(null);
        } catch (Exception x) {
        }
        try {
            writer.close();
        } catch (Exception x) {
        }
        try {
            if (connection != null)
                connection.close();
        } catch (Exception x) {
            localLogger.warning(x.getMessage());
        }
        try {
            if (logHandler != null) {
                localLogger.setUseParentHandlers(true);
                localLogger.removeHandler(logHandler);
                localLogger.removeHandler(streamHandler);
                logHandler.close();
            }
        } catch (Exception x) {
        }
    }
}
Also used : DBConnection(ambit2.rest.DBConnection) DBSubstanceWriter(ambit2.db.substance.processor.DBSubstanceWriter) ImportSubstanceException(ambit2.rest.exception.ImportSubstanceException) IIdentifiableResource(net.idea.opentox.cli.IIdentifiableResource) IIdentifier(net.idea.opentox.cli.id.IIdentifier) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Identifier(net.idea.opentox.cli.id.Identifier) IIdentifier(net.idea.opentox.cli.id.IIdentifier) StreamHandler(java.util.logging.StreamHandler) ResourceException(org.restlet.resource.ResourceException) List(java.util.List) IContainerClient(net.idea.iuclid.cli.IContainerClient) Status(org.restlet.data.Status) SimpleFormatter(java.util.logging.SimpleFormatter) Connection(java.sql.Connection) DBConnection(ambit2.rest.DBConnection) SubstanceRecord(ambit2.base.data.SubstanceRecord) IQueryToolClient(net.idea.iuclid.cli.IQueryToolClient) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) ResourceException(org.restlet.resource.ResourceException) ImportSubstanceException(ambit2.rest.exception.ImportSubstanceException) AmbitIOException(ambit2.base.exceptions.AmbitIOException) FileHandler(java.util.logging.FileHandler) IUCLIDLightClient(net.idea.iuclid.cli.IUCLIDLightClient) TaskResult(ambit2.rest.task.TaskResult)

Aggregations

ImportSubstanceException (ambit2.rest.exception.ImportSubstanceException)2 ResourceException (org.restlet.resource.ResourceException)2 SubstanceRecord (ambit2.base.data.SubstanceRecord)1 AmbitIOException (ambit2.base.exceptions.AmbitIOException)1 DBSubstanceWriter (ambit2.db.substance.processor.DBSubstanceWriter)1 DBConnection (ambit2.rest.DBConnection)1 TaskResult (ambit2.rest.task.TaskResult)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Connection (java.sql.Connection)1 List (java.util.List)1 FileHandler (java.util.logging.FileHandler)1 SimpleFormatter (java.util.logging.SimpleFormatter)1 StreamHandler (java.util.logging.StreamHandler)1 IContainerClient (net.idea.iuclid.cli.IContainerClient)1 IQueryToolClient (net.idea.iuclid.cli.IQueryToolClient)1 IUCLIDLightClient (net.idea.iuclid.cli.IUCLIDLightClient)1 IIdentifiableResource (net.idea.opentox.cli.IIdentifiableResource)1