vamsas API version 0.2 - allow new sessions or imported sessions to be created with...
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / SimpleClientFactory.java
index 58e9160..d85127f 100644 (file)
@@ -223,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;
@@ -253,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
@@ -261,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
@@ -269,7 +270,7 @@ public class SimpleClientFactory implements IClientFactory {
     if (vamsasDocument == null) {
       vamsasSession = new VamsasSession(sessdir);
     } else {
-      vamsasSession = new VamsasSession(sessdir, vamsasDocument);
+      vamsasSession = new VamsasSession(sessdir, vamsasDocument,preferredName);
     }
     getSessionManager().addSession(vamsasSession.getSessionUrn());
     if (userId == null) {
@@ -364,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.",
@@ -381,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;
@@ -392,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]",
           "_");
@@ -411,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) {
@@ -449,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);
     }
@@ -459,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);
     }
@@ -482,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);
     }
@@ -493,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);
     }