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