added legacy import of version 0.1 SessionLists
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / VamsasSession.java
index af5ab37..cf139a0 100644 (file)
@@ -1,6 +1,6 @@
 /*\r
- * This file is part of the Vamsas Client version 0.1. \r
- * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
+ * This file is part of the Vamsas Client version 0.2. \r
+ * Copyright 2010 by Jim Procter, Iain Milne, Pierre Marguerite, \r
  *  Andrew Waterhouse and Dominik Lindner.\r
  * \r
  * Earlier versions have also been incorporated into Jalview version 2.4 \r
@@ -22,7 +22,6 @@
 package uk.ac.vamsas.client.simpleclient;\r
 \r
 import java.io.File;\r
-import java.io.FileNotFoundException;\r
 import java.io.IOException;\r
 import java.io.RandomAccessFile;\r
 import java.nio.channels.FileChannel;\r
@@ -36,8 +35,7 @@ import org.apache.log4j.PatternLayout;
 \r
 import uk.ac.vamsas.client.ClientHandle;\r
 import uk.ac.vamsas.client.Events;\r
-import uk.ac.vamsas.client.IClient;\r
-import uk.ac.vamsas.client.SessionHandle;\r
+import uk.ac.vamsas.client.InvalidSessionUrnException;\r
 import uk.ac.vamsas.client.UserHandle;\r
 \r
 /**\r
@@ -217,7 +215,7 @@ public class VamsasSession {
   protected VamsasSession(File sessionDir1) throws IOException {\r
     this(sessionDir1, null);\r
   }\r
-\r
+  private SimpleSessionHandle sessionHandle = null;\r
   /**\r
    * sets up the vamsas session files and watchers in sessionDir1\r
    * \r
@@ -231,6 +229,24 @@ public class VamsasSession {
    *           new data into session in this way)\r
    */\r
   protected VamsasSession(File sessionDir1, File extVamDoc) throws IOException {\r
+    this(sessionDir1,extVamDoc,null);\r
+  }\r
+  /**\r
+   * sets up the vamsas session files and watchers in sessionDir1\r
+   * \r
+   * @param sessionDir1\r
+   * @param extVamDoc\r
+   *          null or an existing archive to initialise the session with\r
+   * @param preferredName - optional string to use to generate a new session URI\r
+   * @throws any\r
+   *           IOExceptions from creating session directory and files.\r
+   * @throws error\r
+   *           if both extVamDoc and sessionDir1 already exist (cannot import\r
+   *           new data into session in this way)\r
+   *           MalformedURI if preferredName cannot be used to derive a URI of the form simpleclient::preferredName\r
+   */\r
+  public VamsasSession(File sessionDir1, File extVamDoc, String preferredName) throws IOException {\r
+    // TODO: refactor to separate extVamDoc path from session URN - enables non-local URLs to be locally bound to sessions.\r
     if (sessionDir1 == null)\r
       throw new Error("Null directory for VamsasSession.");\r
     if (!sessionDir1.exists() && !sessionDir1.mkdir()) {\r
@@ -252,6 +268,21 @@ public class VamsasSession {
       }\r
     }\r
     this.sessionDir = sessionDir1;\r
+    // construct session URN\r
+    SessionUrn sessUrn = new SessionUrn(sessionDir);\r
+    if (extVamDoc!=null) {\r
+      // Construct Session URN from the original vamsas document.\r
+      sessUrn = new SessionUrn(extVamDoc);\r
+    }\r
+    if (preferredName!=null) {\r
+      try {\r
+            sessUrn = new SessionUrn(preferredName);\r
+      } catch (InvalidSessionUrnException e) {\r
+        throw new Error("Malformed URI : preferredName = "+preferredName,e);\r
+      }\r
+    }\r
+    // create the session handle\r
+    sessionHandle = new SimpleSessionHandle(sessUrn.getSessionUrn(),sessionDir);\r
     initSessionObjects();\r
     if (existingSession) {\r
       slog.debug("Initialising additional VamsasSession instance");\r
@@ -267,6 +298,7 @@ public class VamsasSession {
     log.debug("Initialised VamsasSession in " + sessionDir1);\r
   }\r
 \r
+\r
   /**\r
    * tests presence of existing sessionfiles files in dir\r
    * \r
@@ -650,7 +682,7 @@ public class VamsasSession {
     if (closeSession) {\r
       log.debug("Last active client: closing session");\r
       log.info("Closing session");\r
-      getSessionManager().removeSession(client.getSessionHandle());\r
+      closeSession(client.getSessionHandle());\r
     }\r
   }\r
 \r
@@ -859,9 +891,15 @@ public class VamsasSession {
    * @param sessionHandle\r
    *          sessionHandle of the session to remove\r
    */\r
-  private void closeSession(SessionHandle sessionHandle) {\r
-    getSessionManager().removeSession(sessionHandle);\r
-    log.debug("Session removed");\r
+  private void closeSession(uk.ac.vamsas.client.SessionHandle sessionHandle) {\r
+    //if (sessionHandle instanceof SimpleSessionHandle)\r
+    //{\r
+      getSessionManager().removeSession(sessionHandle);\r
+          log.debug("Session removed");\r
+    //} else {\r
+    //  log.error("Cannot close a sessionHandle (URN="+sessionHandle.getSessionUrn()+") which is of type "+sessionHandle.getClass());\r
+     // throw new Error("Tried to close a non-SimpleClient vamsas sessionHandle");\r
+    //}\r
   }\r
 \r
   /**\r
@@ -899,4 +937,8 @@ public class VamsasSession {
     }\r
     return clistWatchElement;\r
   }\r
+\r
+  public uk.ac.vamsas.client.simpleclient.SimpleSessionHandle getSessionUrn() {\r
+    return sessionHandle;\r
+  }\r
 }\r