Skip to content

Instantly share code, notes, and snippets.

@joshmoore
Created July 2, 2012 23:02
Show Gist options
  • Save joshmoore/3036255 to your computer and use it in GitHub Desktop.
Save joshmoore/3036255 to your computer and use it in GitHub Desktop.
Java example of how to write a very basic OMERO importer (see https://www.openmicroscopy.org/community/viewtopic.php?f=6&t=1062)
import ome.formats.OMEROMetadataStoreClient;
import ome.formats.importer.IObservable;
import ome.formats.importer.IObserver;
import ome.formats.importer.ImportCandidates;
import ome.formats.importer.ImportConfig;
import ome.formats.importer.ImportContainer;
import ome.formats.importer.ImportEvent;
import static ome.formats.importer.ImportEvent.IMPORT_DONE;
import ome.formats.importer.ImportLibrary;
import ome.formats.importer.OMEROWrapper;
import ome.formats.importer.cli.ErrorHandler;
public class simple_importer {
public static ImportConfig config(String[] args) throws Exception {
try {
final String host = args[0];
final Integer port = Integer.valueOf(args[1]);
final String username = args[2];
final String password = args[3];
final Long datasetId = Long.valueOf(args[4]);
final String atLeastOneFile = args[5];
final ImportConfig config = new ImportConfig();
config.email.set("");
config.sendFiles.set(true);
config.sendReport.set(false);
config.contOnError.set(false);
config.debug.set(false);
config.hostname.set(host);
config.port.set(port);
config.username.set(username);
config.password.set(password);
config.targetClass.set("omero.model.Dataset");
config.targetId.set(datasetId);
return config;
} catch (Exception e) {
System.err.println("Usage: simple_importer host port user pass id file1 [file2 ...]");
System.err.println("---------------------------------------------------------------");
throw e;
}
}
public static void main(String[] args) throws Exception {
ImportConfig config = config(args);
OMEROMetadataStoreClient store = config.createStore();
final String[] paths = new String[args.length-5];
System.arraycopy(args, 5, paths, 0, args.length-5);
try {
OMEROWrapper reader = new OMEROWrapper(config);
ImportLibrary library = new ImportLibrary(store, reader);
ErrorHandler handler = new ErrorHandler(config);
IObserver status = new IObserver() {
public void update(IObservable importLibrary, ImportEvent event) {
if (event instanceof IMPORT_DONE) {
IMPORT_DONE ev = (IMPORT_DONE) event;
System.err.println("Imported pixels:");
for (omero.model.Pixels p : ev.pixels) {
System.out.println(p.getId().getValue());
}
}
}
};
library.addObserver(status);
ImportCandidates candidates = new ImportCandidates(reader, paths, handler);
boolean success = library.importCandidates(config, candidates);
if (!success) {
System.err.println("Failed");
}
} finally {
store.logout();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment