started on ClientDocument implementation and Locked IO tests for VamsasArchive
[vamsas.git] / src / org / vamsas / client / simpleclient / IdFactory.java
1 /**
2  * 
3  */
4 package org.vamsas.client.simpleclient;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.vamsas.client.ClientHandle;
9 import org.vamsas.client.SessionHandle;
10 import org.vamsas.client.UserHandle;
11 import org.vamsas.client.VorbaId;
12 import org.vamsas.client.VorbaIdFactory;
13 import org.vamsas.objects.utils.document.VersionEntries;
14
15 import java.util.Hashtable;
16 import java.util.zip.CRC32;
17 /**
18  * Simplest VorbaId constructor
19  * @author jimp
20  *
21  */
22 public class IdFactory extends VorbaIdFactory {
23   static Log log = LogFactory.getLog(IdFactory.class);
24   private SessionHandle session=null;
25   private ClientHandle client;
26   private UserHandle user;
27   private CRC32 unique=new CRC32(); // used to attempt a unique but predictable stream for IDs
28   private String idstring;
29   int sequence=1; // incrementing value for next new ID
30   /**
31    * 
32    */
33   public IdFactory() {
34     super();
35     // TODO Auto-generated constructor stub
36   }
37   
38   /**
39    * @param session
40    * @param client
41    * @param user
42    */
43   protected IdFactory(SessionHandle session, ClientHandle client, UserHandle user) {
44     super();
45     this.session = session;
46     this.client = client;
47     this.user = user;
48     unique.reset();
49     unique.update(new Object[] { session, client, user}.toString().getBytes());
50     // TODO: Ensure format of URNs and use standard composition methods.
51     idstring = client.getClientUrn()+":"+unique.getValue()+"/";
52     extantids=new Hashtable();
53   }
54   /**
55    * values for keys in this hash can be used to reference the org.vamsas.client.object instance for the VorbaId string.
56    * @return the hash of all VorbaIds
57    */
58   protected Hashtable getVorbaIdHash() {
59     return extantids;
60   }
61   
62   /* (non-Javadoc)
63    * @see org.vamsas.client.VorbaIdFactory#makeVorbaId()
64    */
65   public VorbaId makeVorbaId() {
66     if (session==null)
67       throw new Error("makeVorbaId called on improperly initialised IdFactory object!");
68     String newidstring;
69     do {
70       if (sequence>0) {
71         sequence++;
72       } else {
73         idstring+="1/";
74         sequence=1;
75       }
76       newidstring=idstring+Integer.toString(sequence);
77     } while (extantids.containsKey(newidstring));
78     VorbaId id = newId(newidstring);
79     return id;
80   }
81
82   /* (non-Javadoc)
83    * @see org.vamsas.client.VorbaIdFactory#setSession(org.vamsas.client.SessionHandle)
84    */
85   protected void setSession(SessionHandle sessionhandle) {
86     if (sessionhandle!=null)
87       session=sessionhandle;
88     else
89       log.warn("setSession(null) called.");
90   }
91
92   /* (non-Javadoc)
93    * @see org.vamsas.client.VorbaIdFactory#getSessionHandle()
94    */
95   public SessionHandle getSessionHandle() {
96     return session;
97   }
98
99   /* (non-Javadoc)
100    * @see org.vamsas.client.VorbaIdFactory#setClient(org.vamsas.client.ClientHandle)
101    */
102   protected void setClient(ClientHandle appHandle) {
103     if (appHandle!=null)
104       client = appHandle;
105     else
106       log.warn("setClient(null) called.");
107   }
108
109   /* (non-Javadoc)
110    * @see org.vamsas.client.VorbaIdFactory#getClientHandle()
111    */
112   public ClientHandle getClientHandle() {
113     return client;
114   }
115
116   /* (non-Javadoc)
117    * @see org.vamsas.client.VorbaIdFactory#setUser(org.vamsas.client.UserHandle)
118    */
119   protected void setUser(UserHandle userHandle) {
120   if (userHandle!=null)
121     user = userHandle;
122   else
123     log.warn("setUser(null) called.");
124   }
125
126   /* (non-Javadoc)
127    * @see org.vamsas.client.VorbaIdFactory#getUserHandle()
128    */
129   public UserHandle getUserHandle() {
130     return user;
131   }
132   /**
133    * Convenience method used for default behaviour in testing 
134    * and any anonymous internal vamsasDocument unmarshalling
135    * @param clientname
136    * @return
137    */
138   protected  static IdFactory getDummyFactory(String clientname) {
139     if (clientname==null)
140       clientname="org.vamsas.client.simpleclient.IdFactory";
141     return new IdFactory(new SessionHandle("dummy.session"), 
142         new ClientHandle(clientname,VersionEntries.latestVersion()), 
143         new UserHandle(clientname, "Arnold User's Inc."));
144   }
145 }