--- /dev/null
+/**
+ *
+ */
+package uk.ac.vamsas.client.simpleclient;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.vamsas.client.ClientHandle;
+import org.vamsas.client.SessionHandle;
+import org.vamsas.client.UserHandle;
+import org.vamsas.client.VorbaId;
+import org.vamsas.client.VorbaIdFactory;
+import org.vamsas.client.Vobject;
+import org.vamsas.objects.utils.document.VersionEntries;
+
+import java.util.Hashtable;
+import java.util.zip.CRC32;
+/**
+ * Simplest VorbaId constructor
+ * @author jimp
+ *
+ */
+public class IdFactory extends VorbaIdFactory {
+ static Log log = LogFactory.getLog(IdFactory.class);
+ private SessionHandle session=null;
+ private ClientHandle client;
+ private UserHandle user;
+ private CRC32 unique=new CRC32(); // used to attempt a unique but predictable stream for IDs
+ private String idstring;
+ int sequence=1; // incrementing value for next new ID
+ /**
+ *
+ */
+ public IdFactory() {
+ super();
+ // TODO Auto-generated constructor stub
+ }
+
+ /**
+ * @param session
+ * @param client
+ * @param user
+ */
+ protected IdFactory(SessionHandle session, ClientHandle client, UserHandle user) {
+ super();
+ this.session = session;
+ this.client = client;
+ this.user = user;
+ unique.reset();
+ unique.update(new Object[] { session, client, user}.toString().getBytes());
+ // TODO: Ensure format of URNs and use standard composition methods.
+ idstring = client.getClientName()+":"+unique.getValue()+".";
+ extantids=new Hashtable();
+ this.extanthashv=new Hashtable();
+ }
+ /**
+ * values for keys in this hash can be used to reference the org.vamsas.client.Vobject instance for the VorbaId string.
+ * @return the hash of all VorbaIds
+ */
+ protected Hashtable getVorbaIdHash() {
+ return extantids;
+ }
+ /**
+ * values for keys in this hash are Vobjhash objects created for each Vobj with a VorbaId
+ * after this factory has been used to write a vamsas archive.
+ * @return the hash of all VorbaIds and their hash values.
+ */
+ protected Hashtable getVobjhashVals() {
+ return extanthashv;
+ }
+ /* (non-Javadoc)
+ * @see org.vamsas.client.VorbaIdFactory#makeVorbaId()
+ */
+ public VorbaId makeVorbaId(Vobject vobject) {
+ if (session==null)
+ throw new Error("makeVorbaId called on improperly initialised IdFactory Vobject!");
+ if (!vobject.isRegisterable())
+ throw new Error("makeVorbaId called on unregisterable object.");
+ if (vobject.isRegistered())
+ throw new Error("makeVorbaId called on already registered object.");
+ String newidstring;
+ do {
+ if (sequence>0) {
+ sequence++;
+ } else {
+ idstring+="1/";
+ sequence=1;
+ }
+ newidstring=idstring+Integer.toString(sequence);
+ } while (extantids.containsKey(newidstring));
+ extantids.put(newidstring, vobject); // hash the Vobject by its new Id
+ VorbaId id = newId(newidstring); // VorbaId.hash()==newidstring.hash() so we can still recover vobject
+ return id;
+ }
+
+ /* (non-Javadoc)
+ * @see org.vamsas.client.VorbaIdFactory#setSession(org.vamsas.client.SessionHandle)
+ */
+ protected void setSession(SessionHandle sessionhandle) {
+ if (sessionhandle!=null)
+ session=sessionhandle;
+ else
+ log.warn("setSession(null) called.");
+ }
+
+ /* (non-Javadoc)
+ * @see org.vamsas.client.VorbaIdFactory#getSessionHandle()
+ */
+ public SessionHandle getSessionHandle() {
+ return session;
+ }
+
+ /* (non-Javadoc)
+ * @see org.vamsas.client.VorbaIdFactory#setClient(org.vamsas.client.ClientHandle)
+ */
+ protected void setClient(ClientHandle appHandle) {
+ if (appHandle!=null)
+ client = appHandle;
+ else
+ log.warn("setClient(null) called.");
+ }
+
+ /* (non-Javadoc)
+ * @see org.vamsas.client.VorbaIdFactory#getClientHandle()
+ */
+ public ClientHandle getClientHandle() {
+ return client;
+ }
+
+ /* (non-Javadoc)
+ * @see org.vamsas.client.VorbaIdFactory#setUser(org.vamsas.client.UserHandle)
+ */
+ protected void setUser(UserHandle userHandle) {
+ if (userHandle!=null)
+ user = userHandle;
+ else
+ log.warn("setUser(null) called.");
+ }
+
+ /* (non-Javadoc)
+ * @see org.vamsas.client.VorbaIdFactory#getUserHandle()
+ */
+ public UserHandle getUserHandle() {
+ return user;
+ }
+ /**
+ * Convenience method used for default behaviour in testing
+ * and any anonymous internal vamsasDocument unmarshalling
+ * @param clientname
+ * @return
+ */
+ protected static IdFactory getDummyFactory(String clientname) {
+ if (clientname==null)
+ clientname="uk.ac.vamsas.client.simpleclient.IdFactory";
+ return new IdFactory(new SessionHandle("dummy.session"),
+ new ClientHandle(clientname,VersionEntries.latestVersion()),
+ new UserHandle(clientname, "Arnold User's Inc."));
+ }
+}