//
// ImageUploader.java
//

/*
VisBio application for visualization of multidimensional biological
image data. Copyright (C) 2002-@year@ Curtis Rueden and Abraham Sorber.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package loci.visbio.ome;

import java.util.*;
import loci.formats.FormatTools;
import loci.formats.MetadataTools;
import loci.formats.StatusEvent;
import loci.formats.StatusListener;
import loci.formats.meta.MetadataRetrieve;
import loci.formats.meta.MetadataStore;
import loci.formats.ome.OMEWriter;

/**
 * ImageUploader is a helper class for uploading VisBio datasets
 * (OME images) to the Open Microscopy Environment.
 *
 * <dl><dt><b>Source code:</b></dt>
 * <dd><a href="https://skyking.microscopy.wisc.edu/trac/java/browser/trunk/loci/visbio/ome/ImageUploader.java">Trac</a>,
 * <a href="https://skyking.microscopy.wisc.edu/svn/java/trunk/loci/visbio/ome/ImageUploader.java">SVN</a></dd></dl>
 */
public class ImageUploader {

  // -- Fields --

  /** List of objects listening for updates to upload tasks. */
  protected Vector listeners;

  // -- Constructor --

  /** Constructs a new OME image uploader. */
  public ImageUploader() { listeners = new Vector(); }

  // -- ImageUploader API methods --

  /**
   * Uploads the given VisBio dataset (OME image) to the specified
   * OME server, using the given username and password.
   */
  public void upload(loci.visbio.data.Dataset data,
    String server, String username, String password)
  {
    try {
      OMEWriter writer = new OMEWriter();
      MetadataStore store = MetadataTools.createOMEXMLMetadata();
      store.setRoot(data.getOMENode());
      MetadataRetrieve retrieve = (MetadataRetrieve) store;
      writer.setMetadataRetrieve(retrieve);

      String id = server + "?user=" + username + "&password=" + password;
      writer.setId(id);

      int numFiles = data.getFilenames().length;
      int numImages = data.getImagesPerSource();

      for (int i=0; i<numFiles; i++) {
        for (int j=0; j<numImages; j++) {
          int[] coords = FormatTools.getZCTCoords(
            retrieve.getPixelsDimensionOrder(0, 0),
            retrieve.getPixelsSizeZ(0, 0).intValue(),
            retrieve.getPixelsSizeC(0, 0).intValue(),
            retrieve.getPixelsSizeT(0, 0).intValue(),
            numImages*numFiles, numImages*i + j);
          writer.saveImage(
            data.getImage(new int[] {coords[0], coords[1], coords[2], j}),
            i == numFiles - 1 && j == numImages - 1);
        }
      }

      writer.close();
    }
    catch (Exception exc) {
      notifyListeners(new StatusEvent(1, 1,
        "Error uploading (see error console for details)"));
      exc.printStackTrace();
    }
  }

  /** Adds an upload task listener. */
  public void addStatusListener(StatusListener l) {
    synchronized (listeners) { listeners.addElement(l); }
  }

  /** Removes an upload task listener. */
  public void removeStatusListener(StatusListener l) {
    synchronized (listeners) { listeners.removeElement(l); }
  }

  /** Removes all upload task listeners. */
  public void removeAllStatusListeners() {
    synchronized (listeners) { listeners.removeAllElements(); }
  }

  /** Notifies listeners of an upload task update. */
  protected void notifyListeners(StatusEvent e) {
    synchronized (listeners) {
      for (int i=0; i<listeners.size(); i++) {
        StatusListener l = (StatusListener) listeners.elementAt(i);
        l.statusUpdated(e);
      }
    }
  }

}

@@ -2256,12 +2256,15 @@
 tOME
-Node
+XMLRoot
 ());
@@ -2478,244 +2478,22 @@
     
-int numFiles = data.getFilenames().length;%0A      int numImages = data.getImagesPerSource();%0A%0A      for (int i=0; i%3CnumFiles; i++) %7B%0A        for (int j=0; j%3CnumImages; j++) %7B%0A          int%5B%5D coords = FormatTools.getZCTCoords(%0A           
+String order =
  ret
@@ -2527,29 +2527,35 @@
 er(0, 0)
-,
+;
 %0A      
-     
+int sizeZ =
  retriev
@@ -2583,37 +2583,43 @@
 ).intValue()
-,
+;
 %0A      
-     
+int sizeC =
  retrieve.ge
@@ -2629,17 +2629,17 @@
 xelsSize
-C
+Z
 (0, 0).i
@@ -2647,29 +2647,35 @@
 tValue()
-,
+;
 %0A      
-     
+int sizeT =
  retriev
@@ -2689,17 +2689,17 @@
 xelsSize
-T
+Z
 (0, 0).i
@@ -2711,226 +2711,607 @@
 ue()
-,
+;%0A
 %0A      
-      numImages*numFiles, numImages*i + j);%0A          writer.saveImage(%0A            data.getImage(new int%5B%5D %7Bcoords%5B0%5D, coords%5B1%5D, coords%5B2%5D, j%7D),%0A            i == numFiles - 1 && j == numImages - 1);%0A        %7D
+int%5B%5D len = data.getLengths();%0A      int total = FormatTools.getRasterLength(len);%0A      int%5B%5D cLen = new int%5Blen.length - 2%5D;%0A      System.arraycopy(len, 2, cLen, 0, cLen.length);%0A%0A      for (int i=0; i%3Ctotal; i++) %7B%0A        int%5B%5D zct = FormatTools.getZCTCoords(order,%0A          sizeZ, sizeC, sizeT, total, i);%0A        int%5B%5D cPos = FormatTools.rasterToPosition(cLen, zct%5B1%5D);%0A        int%5B%5D pos = new int%5B2 + cPos.length%5D;%0A        pos%5B0%5D = zct%5B2%5D;%0A        pos%5B1%5D = zct%5B0%5D;%0A        System.arraycopy(cPos, 0, pos, 2, cPos.length);%0A        writer.saveImage(data.getImage(pos), i == total - 1);
 %0A   

Update OME uploader to work with new Dataset implementation.