implementing minimal set : SimpleClientFactory createSession method -> SimpleClient...
[vamsas.git] / src / org / vamsas / client / simpleclient / SimpleClientFactory.java
index 73d7ae9..c6d1687 100644 (file)
@@ -3,6 +3,8 @@ package org.vamsas.client.simpleclient;
 import java.io.File;
 import java.io.IOException;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.vamsas.client.ClientHandle;
 import org.vamsas.client.IClient;
 import org.vamsas.client.IClientFactory;
@@ -14,7 +16,8 @@ import org.vamsas.client.UserHandle;
  *
  */
 public class SimpleClientFactory implements IClientFactory {
+  private static Log log = LogFactory.getLog(SimpleClientFactory.class);
+
   File sessionArena;
   
   /**
@@ -81,7 +84,37 @@ public class SimpleClientFactory implements IClientFactory {
     // create client instance
     return null;
   }
-
+  /**
+   * make a new vamsas session from the data in the archive vamsasdocument 
+   * @param applicationHandle
+   * @param userId
+   * @param vamsasdocument
+   * @return
+   */
+  public IClient openSession(ClientHandle applicationHandle, UserHandle userId, ArchiveUrn vamsasdocument) throws IOException {
+    // verify applicationHandle and userId
+    // TODO: verify applicationHandle and userId
+    // verify vamsasdocument is a valid document.
+    File vamdoc = vamsasdocument.asFile();
+    log.debug("Starting new session with data from "+vamsasdocument.getSessionUrn()+"(File is : "+vamdoc+")");
+    VamsasArchiveReader archive = new VamsasArchiveReader(vamdoc);
+    // TODO: a real validity test. The below just checks it can be read.
+    if (!archive.isValid())
+      throw new IOException(vamsasdocument.getSessionUrn()+" is not a valid vamsasDocument archive.");
+    // create new session directory
+    if (sessionArena==null)
+      throw new Error("Improperly initialised SimpleClientFactory object - null sessionArena.");
+    File sessdir = File.createTempFile("sess", ".simpleclient", sessionArena);
+    if (!(sessdir.delete() && sessdir.mkdir()))
+        throw new IOException("Could not make session directory "+sessdir);
+    VamsasSession sess = new VamsasSession(sessdir);
+    // copy document into session directory
+    sess.setVamsasDocument(vamsasdocument.asFile());
+    // create client instance and return.
+    SimpleClient client = new SimpleClient(userId, applicationHandle, sess);
+    
+    return client;
+  }
   public static void main(String[] args) {
   }
 }