2 * This file is part of the Vamsas Client version 0.2.
\r
3 * Copyright 2010 by Jim Procter, Iain Milne, Pierre Marguerite,
\r
4 * Andrew Waterhouse and Dominik Lindner.
\r
6 * Earlier versions have also been incorporated into Jalview version 2.4
\r
7 * since 2008, and TOPALi version 2 since 2007.
\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
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
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
22 package uk.ac.vamsas.client.simpleclient;
\r
24 import org.apache.commons.logging.Log;
\r
25 import org.apache.commons.logging.LogFactory;
\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
35 import java.util.Hashtable;
\r
36 import java.util.zip.CRC32;
\r
39 * Simplest VorbaId constructor
\r
44 public class IdFactory extends VorbaIdFactory {
\r
45 static Log log = LogFactory.getLog(IdFactory.class);
\r
47 private SessionHandle session = null;
\r
49 private ClientHandle client;
\r
51 private UserHandle user;
\r
53 private CRC32 unique = new CRC32(); // used to attempt a unique but
\r
54 // predictable stream for IDs
\r
56 private String idstring;
\r
58 int sequence = 1; // incrementing value for next new ID
\r
63 public IdFactory() {
\r
65 // TODO Auto-generated constructor stub
\r
73 protected IdFactory(SessionHandle session, ClientHandle client,
\r
76 this.session = session;
\r
77 this.client = client;
\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
88 * Create IdFactory with existing object hashes and id set
\r
93 * @param extanthashv
\r
94 * hash of existing VorbaIds from a previous read of same document
\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
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
106 * @return the hash of all VorbaIds
\r
108 protected Hashtable getVorbaIdHash() {
\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
116 * @return the hash of all VorbaIds and their hash values.
\r
118 protected Hashtable getVobjhashVals() {
\r
119 return extanthashv;
\r
125 * @see uk.ac.vamsas.client.VorbaIdFactory#makeVorbaId()
\r
127 public VorbaId makeVorbaId(Vobject vobject) {
\r
128 if (session == null)
\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
137 if (sequence > 0) {
\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
155 * uk.ac.vamsas.client.VorbaIdFactory#setSession(uk.ac.vamsas.client.SessionHandle
\r
158 protected void setSession(SessionHandle sessionhandle) {
\r
159 if (sessionhandle != null)
\r
160 session = sessionhandle;
\r
162 log.warn("setSession(null) called.");
\r
168 * @see uk.ac.vamsas.client.VorbaIdFactory#getSessionHandle()
\r
170 public SessionHandle getSessionHandle() {
\r
178 * uk.ac.vamsas.client.VorbaIdFactory#setClient(uk.ac.vamsas.client.ClientHandle
\r
181 protected void setClient(ClientHandle appHandle) {
\r
182 if (appHandle != null)
\r
183 client = appHandle;
\r
185 log.warn("setClient(null) called.");
\r
191 * @see uk.ac.vamsas.client.VorbaIdFactory#getClientHandle()
\r
193 public ClientHandle getClientHandle() {
\r
201 * uk.ac.vamsas.client.VorbaIdFactory#setUser(uk.ac.vamsas.client.UserHandle)
\r
203 protected void setUser(UserHandle userHandle) {
\r
204 if (userHandle != null)
\r
207 log.warn("setUser(null) called.");
\r
213 * @see uk.ac.vamsas.client.VorbaIdFactory#getUserHandle()
\r
215 public UserHandle getUserHandle() {
\r
220 * Convenience method used for default behaviour in testing and any anonymous
\r
221 * internal vamsasDocument unmarshalling
\r
223 * @param clientname
\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