[ome-devel] Add a file annotation

Matthias Dunda Matthias.Dunda at carus-it.com
Mon Feb 23 08:03:22 GMT 2009


thanks, Jean-Marie, a major oversight.. :-/

but now I get a

Exception in thread "main" omero.UnloadedCollectionException: Error updating collection:annotationLinksSeq
Collection is currently null. This can be seen
by testing "annotationLinksSeqLoaded". This implies
that this collection was unloaded. Please refresh this object
in order to update this collection.

                at omero.model.ProjectI.throwNullCollectionException(ProjectI.java:25)
                at omero.model.ProjectI.linkAnnotation(ProjectI.java:439)
                at omero.model.Project.linkAnnotation(Project.java:191)
                at de.carus.importer.test.UploadFile.main(UploadFile.java:74)


This is my sample code:

package de.carus.importer.test;

import static omero.rtypes.rlong;
import static omero.rtypes.rstring;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.util.List;

import omero.api.RawFileStorePrx;
import omero.api.ServiceFactoryPrx;
import omero.model.FileAnnotation;
import omero.model.FileAnnotationI;
import omero.model.Format;
import omero.model.IObject;
import omero.model.OriginalFile;
import omero.model.OriginalFileI;
import omero.model.Project;

public class UploadFile {
                public static void main(String[] args) throws Exception {
                               omero.client client = new omero.client("localhost");
                               ServiceFactoryPrx sf = client.createSession("root", "ome");
                               try {
                                               File file = new File(
                                                                              "/data/operashare/An_02_Me01_ReferenceData(2008-06-12_12-32-02).res");

                                               RawFileStorePrx rfs = sf.createRawFileStore();
                                               Format f = (Format) sf.getQueryService().findByString(
                                                                              Format.class.getName(), "value", "text/xml");
                                               OriginalFile oFile = new OriginalFileI();
                                               oFile.setName(rstring(file.getName()));
                                               oFile.setPath(rstring(file.getAbsolutePath()));
                                               oFile.setSize(rlong(file.length()));
                                               oFile.setSha1(rstring("pending"));
                                               oFile.setFormat(f);

                                               oFile = (OriginalFile) sf.getUpdateService().saveAndReturnObject(oFile,
                                                                              null);
                                               rfs.setFileId(oFile.getId().getValue());

                                               byte[] buf = new byte[512];
                                               FileInputStream stream = null;
                                               try {
                                                               stream = new FileInputStream(file);
                                                               long pos = 0;
                                                               int rlen;
                                                               ByteBuffer bbuf;
                                                               while ((rlen = stream.read(buf)) > 0) {
                                                                              rfs.write(buf, pos, rlen);
                                                                              pos += rlen;
                                                                              bbuf = ByteBuffer.wrap(buf);
                                                                              bbuf.limit(rlen);
                                                               }
                                                               stream.close();
                                               } catch (Exception e) {
                                                               try {
                                                                              if (stream != null)
                                                                                              stream.close();
                                                               } catch (Exception ex) {
                                                               }

                                                               throw new Exception("Cannot upload the file with path "
                                                                                              + file.getAbsolutePath(), e);
                                               }

                                               FileAnnotation fa = new FileAnnotationI();
                                               fa.setFile(oFile);
                                               List<IObject> results = sf.getQueryService().findAllByString(
                                                                              Project.class.getName(), "name", "AnnoProject", false, null);
                                               // The project "AnnoProject" exists in the DB!
                                               Project p = (Project)results.get(0);

                                               p.linkAnnotation(fa);
                                               sf.getUpdateService().saveObject(p);
                               } finally {
                                               client.closeSession();
                               }
                }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.openmicroscopy.org.uk/pipermail/ome-devel/attachments/20090223/ac1a5d65/attachment-0001.htm 


More information about the ome-devel mailing list