verson 0.2 LGPL licensed source and jars
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / SimpleClientFactory.java
index 43cc4b6..3d9a534 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * This file is part of the Vamsas Client version 0.1. 
- * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, 
+ * This file is part of the Vamsas Client version 0.2. 
+ * Copyright 2010 by Jim Procter, Iain Milne, Pierre Marguerite, 
  *  Andrew Waterhouse and Dominik Lindner.
  * 
  * Earlier versions have also been incorporated into Jalview version 2.4 
@@ -147,15 +147,40 @@ public class SimpleClientFactory implements IClientFactory {
 
   }
 
+  /**
+   * try to locate the sessionUrn within the simpleclient session arena
+   * @param sessionUrn
+   * @return
+   * @throws InvalidSessionUrnException
+   */
   private File convertSessionUrnToFile(String sessionUrn)
       throws InvalidSessionUrnException {
     if (sessionUrn == null) {
       log.debug("Incorrect URN: can not open session.");
-      throw new InvalidSessionUrnException();
+      throw new InvalidSessionUrnException("SessionUrn was null");
     }
 
     SessionUrn urn = new SessionUrn(sessionUrn);
-    return urn.asFile();
+    SimpleSessionHandle[] sh = null;
+    try {
+      sh = getSessionManager().getSessionFor(urn);
+    } catch (IOException e)
+    {
+      log.warn("Ignored IO Exception when trying to access sessionlist.",e);
+    }
+    File sesfile = null;
+    if (sh!=null)
+    {
+      if (sh.length==1)
+      {
+        sesfile = new File(sh[0].getPhysLoc());
+        sh[0] = null;
+      } else {
+        log.error("Raising exception for multiple session files corresponding to single URN (was : "+sessionUrn+")");
+        throw new InvalidSessionUrnException("IMPLEMENTATION ERROR: Multiple session files available for URN ("+sessionUrn+")");
+      }
+    }
+    return sesfile;
 
   }
 
@@ -178,7 +203,7 @@ public class SimpleClientFactory implements IClientFactory {
     // 3. if it is - locate the file and pass to new VamsasSession
 
     try {
-      File sessionDirectory = this.convertSessionUrnToFile(sessionUrn);
+      File sessionDirectory = convertSessionUrnToFile(sessionUrn);
       // create session
       log
           .debug("found session directory "
@@ -198,7 +223,7 @@ public class SimpleClientFactory implements IClientFactory {
        * applicationHandle, vamsasSession);
        */
       client = this.initClient(sessionDirectory, userId, applicationHandle,
-          null);
+          null,null);
     } catch (MalformedURLException e) {
       log.error("error while creating new IClient: incorrect session urn", e);
       client = null;
@@ -228,6 +253,7 @@ public class SimpleClientFactory implements IClientFactory {
    * @param vamsasDocument
    *          null or a document to pass to SimpleCLient to write into the
    *          sessdir
+   * @param preferredName - name to use instead of vamsasDocument path when creating new session URN 
    * @return the client
    * @throws IOException
    *           if there are problems in session or client creation or if the
@@ -236,7 +262,7 @@ public class SimpleClientFactory implements IClientFactory {
    *           for a malformed sessdir
    */
   private IClient initClient(File sessdir, UserHandle userId,
-      ClientHandle clientHandle, File vamsasDocument) throws IOException,
+      ClientHandle clientHandle, File vamsasDocument, String preferredName) throws IOException,
       InvalidSessionUrnException, InvalidSessionDocumentException {
     IClient client = null;
     // create session
@@ -244,11 +270,9 @@ public class SimpleClientFactory implements IClientFactory {
     if (vamsasDocument == null) {
       vamsasSession = new VamsasSession(sessdir);
     } else {
-      vamsasSession = new VamsasSession(sessdir, vamsasDocument);
+      vamsasSession = new VamsasSession(sessdir, vamsasDocument,preferredName);
     }
-
-    this.getSessionManager().addSession(
-        new SessionHandle(new SessionUrn(vamsasSession).getSessionUrn()));
+    getSessionManager().addSession(vamsasSession.getSessionUrn());
     if (userId == null) {
       // create a default userHandle
       // userId = new UserHandle(System.getProperty("user.name",
@@ -341,7 +365,7 @@ public class SimpleClientFactory implements IClientFactory {
     }
     // no session available - create a new one
     try {
-      client = clientInNewSession(userId, clientHandle, null);
+      client = clientInNewSession(userId, clientHandle, null,null);
     } catch (Exception e) {
       throw new Error(
           "IMPLEMENTATION ERROR: unexpected exception when creating a new session to connect to.",
@@ -358,10 +382,11 @@ public class SimpleClientFactory implements IClientFactory {
    * @param clientHandle
    * @param vamsasDocument
    *          null or a document file to copy into the new session
+   *          @param preferredName - name to be used as base for the new session URN
    * @return null or a valid IClient instance
    */
   private IClient clientInNewSession(UserHandle userId,
-      ClientHandle clientHandle, File vamsasDocument)
+      ClientHandle clientHandle, File vamsasDocument, String preferredName)
       throws InvalidSessionDocumentException, InvalidSessionUrnException {
 
     IClient client = null;
@@ -369,8 +394,14 @@ public class SimpleClientFactory implements IClientFactory {
       // try and make a friendly session name
       String sesspref = "";
       if (vamsasDocument != null) {
-        sesspref = vamsasDocument.getName().replaceAll(
-            "([^-A-Za-z0-9]|\\.vdj)", "");
+        if (preferredName!=null)
+        {
+          sesspref = preferredName.replaceAll(
+              "([^-A-Za-z0-9]|\\.vdj)", "");
+        } else {
+          sesspref = vamsasDocument.getName().replaceAll(
+              "([^-A-Za-z0-9]|\\.vdj)", "");
+        }
       }
       sesspref += (new java.util.Date()).toString().replaceAll("[^-A-Za-z0-9]",
           "_");
@@ -388,7 +419,7 @@ public class SimpleClientFactory implements IClientFactory {
       log.debug("Creating new session  directory");
       if (!(sessdir.delete() && sessdir.mkdir()))
         throw new IOException("Could not make session directory " + sessdir);
-      client = initClient(sessdir, userId, clientHandle, vamsasDocument);
+      client = initClient(sessdir, userId, clientHandle, vamsasDocument, preferredName);
     } catch (IOException e) {
       log.error("error while creating new IClient", e);
     } catch (InvalidSessionUrnException e) {
@@ -426,7 +457,7 @@ public class SimpleClientFactory implements IClientFactory {
 
   public IClient getNewSessionIClient(ClientHandle applicationHandle) {
     try {
-      return clientInNewSession(null, applicationHandle, null);
+      return clientInNewSession(null, applicationHandle, null,null);
     } catch (Exception e) {
       log.error("Failed to create new session for app with default user.", e);
     }
@@ -436,7 +467,7 @@ public class SimpleClientFactory implements IClientFactory {
   public IClient getNewSessionIClient(ClientHandle applicationHandle,
       UserHandle userId) {
     try {
-      return clientInNewSession(userId, applicationHandle, null);
+      return clientInNewSession(userId, applicationHandle, null,null);
     } catch (Exception e) {
       log.error("Failed to create new session for app and user.", e);
     }
@@ -459,7 +490,7 @@ public class SimpleClientFactory implements IClientFactory {
       File vamsasDocument) throws InvalidSessionDocumentException {
     checkImportedDocument(vamsasDocument);
     try {
-      return clientInNewSession(null, applicationHandle, vamsasDocument);
+      return clientInNewSession(null, applicationHandle, vamsasDocument,null);
     } catch (InvalidSessionUrnException e) {
       throw new InvalidSessionDocumentException("Unexpected exception", e);
     }
@@ -470,7 +501,29 @@ public class SimpleClientFactory implements IClientFactory {
       throws InvalidSessionDocumentException {
     checkImportedDocument(vamsasDocument);
     try {
-      return clientInNewSession(userId, applicationHandle, vamsasDocument);
+      return clientInNewSession(userId, applicationHandle, vamsasDocument,null);
+    } catch (InvalidSessionUrnException e) {
+      throw new InvalidSessionDocumentException("Unexpected exception", e);
+    }
+  }
+
+  public IClient openAsNewSessionIClient(ClientHandle applicationHandle,
+      File vamsasDocument, String sessionName)
+      throws InvalidSessionDocumentException {
+    checkImportedDocument(vamsasDocument);
+    try {
+      return clientInNewSession(null, applicationHandle, vamsasDocument, sessionName);
+    } catch (InvalidSessionUrnException e) {
+      throw new InvalidSessionDocumentException("Unexpected exception", e);
+    }
+  }
+
+  public IClient openAsNewSessionIClient(ClientHandle applicationHandle,
+      UserHandle userId, File vamsasDocument, String sessionName)
+      throws InvalidSessionDocumentException {
+    checkImportedDocument(vamsasDocument);
+    try {
+      return clientInNewSession(userId, applicationHandle, vamsasDocument, sessionName);
     } catch (InvalidSessionUrnException e) {
       throw new InvalidSessionDocumentException("Unexpected exception", e);
     }