applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / IdFactory.java
1 /*\r
2  * This file is part of the Vamsas Client version 0.1. \r
3  * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
4  *  Andrew Waterhouse and Dominik Lindner.\r
5  * \r
6  * Earlier versions have also been incorporated into Jalview version 2.4 \r
7  * since 2008, and TOPALi version 2 since 2007.\r
8  * \r
9  * The Vamsas Client is free software: you can redistribute it and/or modify\r
10  * it under the terms of the GNU Lesser General Public License as published by\r
11  * the Free Software Foundation, either version 3 of the License, or\r
12  * (at your option) any later version.\r
13  *  \r
14  * The Vamsas Client is distributed in the hope that it will be useful,\r
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17  * GNU Lesser General Public License for more details.\r
18  * \r
19  * You should have received a copy of the GNU Lesser General Public License\r
20  * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
21  */\r
22 package uk.ac.vamsas.client.simpleclient;\r
23 \r
24 import org.apache.commons.logging.Log;\r
25 import org.apache.commons.logging.LogFactory;\r
26 \r
27 import uk.ac.vamsas.client.ClientHandle;\r
28 import uk.ac.vamsas.client.SessionHandle;\r
29 import uk.ac.vamsas.client.UserHandle;\r
30 import uk.ac.vamsas.client.Vobject;\r
31 import uk.ac.vamsas.client.VorbaId;\r
32 import uk.ac.vamsas.client.VorbaIdFactory;\r
33 import uk.ac.vamsas.objects.utils.document.VersionEntries;\r
34 \r
35 import java.util.Hashtable;\r
36 import java.util.zip.CRC32;\r
37 \r
38 /**\r
39  * Simplest VorbaId constructor\r
40  * \r
41  * @author jimp\r
42  * \r
43  */\r
44 public class IdFactory extends VorbaIdFactory {\r
45   static Log log = LogFactory.getLog(IdFactory.class);\r
46 \r
47   private SessionHandle session = null;\r
48 \r
49   private ClientHandle client;\r
50 \r
51   private UserHandle user;\r
52 \r
53   private CRC32 unique = new CRC32(); // used to attempt a unique but\r
54                                       // predictable stream for IDs\r
55 \r
56   private String idstring;\r
57 \r
58   int sequence = 1; // incrementing value for next new ID\r
59 \r
60   /**\r
61    * \r
62    */\r
63   public IdFactory() {\r
64     super();\r
65     // TODO Auto-generated constructor stub\r
66   }\r
67 \r
68   /**\r
69    * @param session\r
70    * @param client\r
71    * @param user\r
72    */\r
73   protected IdFactory(SessionHandle session, ClientHandle client,\r
74       UserHandle user) {\r
75     super();\r
76     this.session = session;\r
77     this.client = client;\r
78     this.user = user;\r
79     unique.reset();\r
80     unique.update(new Object[] { session, client, user }.toString().getBytes());\r
81     // TODO: Ensure format of URNs and use standard composition methods.\r
82     idstring = client.getClientNCname() + "_" + unique.getValue() + ".";\r
83     extantids = new Hashtable();\r
84     this.extanthashv = new Hashtable();\r
85   }\r
86 \r
87   /**\r
88    * Create IdFactory with existing object hashes and id set\r
89    * \r
90    * @param session\r
91    * @param client\r
92    * @param user\r
93    * @param extanthashv\r
94    *          hash of existing VorbaIds from a previous read of same document\r
95    */\r
96   protected IdFactory(SessionHandle session, ClientHandle client,\r
97       UserHandle user, Hashtable extanthashv) {\r
98     this(session, client, user);\r
99     this.extanthashv = extanthashv;\r
100   }\r
101 \r
102   /**\r
103    * values for keys in this hash can be used to reference the\r
104    * uk.ac.vamsas.client.Vobject instance for the VorbaId string.\r
105    * \r
106    * @return the hash of all VorbaIds\r
107    */\r
108   protected Hashtable getVorbaIdHash() {\r
109     return extantids;\r
110   }\r
111 \r
112   /**\r
113    * values for keys in this hash are Vobjhash objects created for each Vobj\r
114    * with a VorbaId after this factory has been used to write a vamsas archive.\r
115    * \r
116    * @return the hash of all VorbaIds and their hash values.\r
117    */\r
118   protected Hashtable getVobjhashVals() {\r
119     return extanthashv;\r
120   }\r
121 \r
122   /*\r
123    * (non-Javadoc)\r
124    * \r
125    * @see uk.ac.vamsas.client.VorbaIdFactory#makeVorbaId()\r
126    */\r
127   public VorbaId makeVorbaId(Vobject vobject) {\r
128     if (session == null)\r
129       throw new Error(\r
130           "makeVorbaId called on improperly initialised IdFactory Vobject!");\r
131     if (!vobject.isRegisterable())\r
132       throw new Error("makeVorbaId called on unregisterable object.");\r
133     if (vobject.isRegistered())\r
134       throw new Error("makeVorbaId called on already registered object.");\r
135     String newidstring;\r
136     do {\r
137       if (sequence > 0) {\r
138         sequence++;\r
139       } else {\r
140         idstring += "1/";\r
141         sequence = 1;\r
142       }\r
143       newidstring = idstring + Integer.toString(sequence);\r
144     } while (extantids.containsKey(newidstring));\r
145     VorbaId id = newId(newidstring); // VorbaId.hash()==newidstring.hash() so we\r
146                                      // can still recover vobject\r
147     extantids.put(id, vobject); // hash the Vobject by its new Id\r
148     return id;\r
149   }\r
150 \r
151   /*\r
152    * (non-Javadoc)\r
153    * \r
154    * @see\r
155    * uk.ac.vamsas.client.VorbaIdFactory#setSession(uk.ac.vamsas.client.SessionHandle\r
156    * )\r
157    */\r
158   protected void setSession(SessionHandle sessionhandle) {\r
159     if (sessionhandle != null)\r
160       session = sessionhandle;\r
161     else\r
162       log.warn("setSession(null) called.");\r
163   }\r
164 \r
165   /*\r
166    * (non-Javadoc)\r
167    * \r
168    * @see uk.ac.vamsas.client.VorbaIdFactory#getSessionHandle()\r
169    */\r
170   public SessionHandle getSessionHandle() {\r
171     return session;\r
172   }\r
173 \r
174   /*\r
175    * (non-Javadoc)\r
176    * \r
177    * @see\r
178    * uk.ac.vamsas.client.VorbaIdFactory#setClient(uk.ac.vamsas.client.ClientHandle\r
179    * )\r
180    */\r
181   protected void setClient(ClientHandle appHandle) {\r
182     if (appHandle != null)\r
183       client = appHandle;\r
184     else\r
185       log.warn("setClient(null) called.");\r
186   }\r
187 \r
188   /*\r
189    * (non-Javadoc)\r
190    * \r
191    * @see uk.ac.vamsas.client.VorbaIdFactory#getClientHandle()\r
192    */\r
193   public ClientHandle getClientHandle() {\r
194     return client;\r
195   }\r
196 \r
197   /*\r
198    * (non-Javadoc)\r
199    * \r
200    * @see\r
201    * uk.ac.vamsas.client.VorbaIdFactory#setUser(uk.ac.vamsas.client.UserHandle)\r
202    */\r
203   protected void setUser(UserHandle userHandle) {\r
204     if (userHandle != null)\r
205       user = userHandle;\r
206     else\r
207       log.warn("setUser(null) called.");\r
208   }\r
209 \r
210   /*\r
211    * (non-Javadoc)\r
212    * \r
213    * @see uk.ac.vamsas.client.VorbaIdFactory#getUserHandle()\r
214    */\r
215   public UserHandle getUserHandle() {\r
216     return user;\r
217   }\r
218 \r
219   /**\r
220    * Convenience method used for default behaviour in testing and any anonymous\r
221    * internal vamsasDocument unmarshalling\r
222    * \r
223    * @param clientname\r
224    * @return\r
225    */\r
226   protected static IdFactory getDummyFactory(String clientname) {\r
227     if (clientname == null)\r
228       clientname = "uk.ac.vamsas.client.simpleclient.IdFactory";\r
229     return new IdFactory(new SessionHandle("dummy.session"), new ClientHandle(\r
230         clientname, VersionEntries.latestVersion()), new UserHandle(clientname,\r
231         "Arnold User's Inc."));\r
232   }\r
233 }\r