vamsas API version 0.2 - allow new sessions or imported sessions to be created with...
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / VamsasSession.java
index 310c6c2..ffbdfa6 100644 (file)
@@ -35,6 +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.InvalidSessionUrnException;\r
 import uk.ac.vamsas.client.UserHandle;\r
 \r
 /**\r
@@ -228,6 +229,23 @@ 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
@@ -250,12 +268,21 @@ public class VamsasSession {
       }\r
     }\r
     this.sessionDir = sessionDir1;\r
-    if (extVamDoc==null) {\r
-      sessionHandle = new SimpleSessionHandle(new SessionUrn(sessionDir).getSessionUrn(),sessionDir);\r
-    } else {\r
+    // construct session URN\r
+    SessionUrn sessUrn = new SessionUrn(sessionDir);\r
+    if (extVamDoc!=null) {\r
       // Construct Session URN from the original vamsas document.\r
-      sessionHandle = new SimpleSessionHandle(new SessionUrn(extVamDoc).getSessionUrn(),sessionDir);\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
@@ -271,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