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