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());
}
}
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) {
}
}
}
Aggregations