ensured that hash binding vorbaIds is passed to client document object for vorba...
[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.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   }
55   /**
56    * values for keys in this hash can be used to reference the org.vamsas.client.Vobject instance for the VorbaId string.
57    * @return the hash of all VorbaIds
58    */
59   protected Hashtable getVorbaIdHash() {
60     return extantids;
61   }
62   
63   /* (non-Javadoc)
64    * @see org.vamsas.client.VorbaIdFactory#makeVorbaId()
65    */
66   public VorbaId makeVorbaId(Vobject vobject) {
67     if (session==null)
68       throw new Error("makeVorbaId called on improperly initialised IdFactory Vobject!");
69     if (!vobject.isRegisterable())
70       throw new Error("makeVorbaId called on unregisterable object.");
71     if (vobject.isRegistered())
72       throw new Error("makeVorbaId called on already registered object.");
73     String newidstring;
74     do {
75       if (sequence>0) {
76         sequence++;
77       } else {
78         idstring+="1/";
79         sequence=1;
80       }
81       newidstring=idstring+Integer.toString(sequence);
82     } while (extantids.containsKey(newidstring));
83     extantids.put(newidstring, vobject); // hash the Vobject by its new Id
84     VorbaId id = newId(newidstring); // VorbaId.hash()==newidstring.hash() so we can still recover vobject
85     return id;
86   }
87
88   /* (non-Javadoc)
89    * @see org.vamsas.client.VorbaIdFactory#setSession(org.vamsas.client.SessionHandle)
90    */
91   protected void setSession(SessionHandle sessionhandle) {
92     if (sessionhandle!=null)
93       session=sessionhandle;
94     else
95       log.warn("setSession(null) called.");
96   }
97
98   /* (non-Javadoc)
99    * @see org.vamsas.client.VorbaIdFactory#getSessionHandle()
100    */
101   public SessionHandle getSessionHandle() {
102     return session;
103   }
104
105   /* (non-Javadoc)
106    * @see org.vamsas.client.VorbaIdFactory#setClient(org.vamsas.client.ClientHandle)
107    */
108   protected void setClient(ClientHandle appHandle) {
109     if (appHandle!=null)
110       client = appHandle;
111     else
112       log.warn("setClient(null) called.");
113   }
114
115   /* (non-Javadoc)
116    * @see org.vamsas.client.VorbaIdFactory#getClientHandle()
117    */
118   public ClientHandle getClientHandle() {
119     return client;
120   }
121
122   /* (non-Javadoc)
123    * @see org.vamsas.client.VorbaIdFactory#setUser(org.vamsas.client.UserHandle)
124    */
125   protected void setUser(UserHandle userHandle) {
126   if (userHandle!=null)
127     user = userHandle;
128   else
129     log.warn("setUser(null) called.");
130   }
131
132   /* (non-Javadoc)
133    * @see org.vamsas.client.VorbaIdFactory#getUserHandle()
134    */
135   public UserHandle getUserHandle() {
136     return user;
137   }
138   /**
139    * Convenience method used for default behaviour in testing 
140    * and any anonymous internal vamsasDocument unmarshalling
141    * @param clientname
142    * @return
143    */
144   protected  static IdFactory getDummyFactory(String clientname) {
145     if (clientname==null)
146       clientname="org.vamsas.client.simpleclient.IdFactory";
147     return new IdFactory(new SessionHandle("dummy.session"), 
148         new ClientHandle(clientname,VersionEntries.latestVersion()), 
149         new UserHandle(clientname, "Arnold User's Inc."));
150   }
151 }