started on ClientDocument implementation and Locked IO tests for VamsasArchive
[vamsas.git] / src / org / vamsas / client / simpleclient / ClientDocument.java
1 /*
2  *
3  */
4 package org.vamsas.client.simpleclient;
5
6 import java.util.Hashtable;
7
8 import org.vamsas.client.IClient;
9 import org.vamsas.client.IClientDocument;
10 import org.vamsas.client.VorbaId;
11 import org.vamsas.client.object;
12 import org.vamsas.objects.core.VAMSAS;
13 import org.vamsas.objects.core.VamsasDocument;
14
15 /**
16  * @author jimp Contains a collection of vamsas objects and reference to a
17  *         specified ClientHandle's information.
18  */
19 public class ClientDocument extends org.vamsas.client.ClientDocument implements IClientDocument {
20   protected IClient vorba;
21   protected org.vamsas.objects.core.VAMSAS[] roots;
22   protected byte[] appData;
23   protected VamsasArchive archive = null;
24   /*
25    * (non-Javadoc)
26    * 
27    * @see org.vamsas.client.IClientDocument#getApplicationData()
28    */
29
30   /**
31    */
32   protected ClientDocument(VamsasArchive document, IdFactory Factory, IClient vorba) {
33     super(Factory.getVorbaIdHash(), Factory);
34     this.vorba = vorba;
35     archive = document;
36     roots = null;
37   }
38
39   public byte[] getApplicationData() {
40     // Look up client byte stash using client and user handle
41     return appData;
42   }
43
44   /*
45    * (non-Javadoc)
46    * 
47    * @see org.vamsas.client.IClientDocument#getObject(org.vamsas.client.VorbaId)
48    */
49   public object getObject(VorbaId id) {
50     // TODO: look up id in document object
51     // retrieve object and return
52     return null;
53   }
54
55   /*
56    * (non-Javadoc)
57    * 
58    * @see org.vamsas.client.IClientDocument#getObjects(org.vamsas.client.VorbaId[])
59    */
60   public object[] getObjects(VorbaId[] ids) {
61     // TODO: getObject in bulk
62     return null;
63   }
64
65   /*
66    * (non-Javadoc)
67    * 
68    * @see org.vamsas.client.IClientDocument#getVamsasRoots()
69    */
70   public VAMSAS[] getVamsasRoots() {
71     if (roots == null)
72       // Make a new one to return to client : TODO: Decide if this is correct
73       return new VAMSAS[] { new VAMSAS() };
74     return roots;
75   }
76
77   /**
78    * update the document with new roots.
79    */
80   public void setVamsasRoots(VAMSAS[] newroots) {
81     // extract root objects
82     if (newroots != null) {
83       // check newroots for objects that were present in the old document
84       // check to see if the 'old' objects have been modified
85       // if they have ? we overwrite them with their new version, ensuring that
86       // provenance is updated.
87       // if they haven't ? do nothing ?
88
89       for (int i = 0, k = newroots.length; i < k; i++) {
90         if (newroots[i].isRegistered()) {
91           // easy - just check if anything has changed and do provenance
92           object oldversion = getObject(newroots[i].getVorbaId());
93           if (oldversion instanceof VAMSAS) {
94             // TODO: appropriate merging behaviour when two clients have
95             // modified the same registered object independently
96             if (newroots[i].get__last_hash() != newroots[i].hashCode()) {
97               // client has modified this object since last retrieval.
98               if (newroots[i].get__last_hash() != oldversion.get__last_hash()) {
99                 // object has been modified by another client since this
100                 // client's
101                 // last access to document.
102               }
103             }
104           } else {
105             throw new Error(
106                 "SimpleClient error when using setVamsasRoots : The vorbaId for object "
107                     + i
108                     + " does not refer to an object of type VAMSAS in the current document!");
109           }
110         } else {
111           if (!newroots[i].is__stored_in_document()) {
112             // check if object is modified
113             if (newroots[i].get__last_hash() != newroots[i].hashCode()) {
114               // it is - so we add newroots[i] as a new object, with updated
115               // provenance.
116             } else {
117               // do nothing
118               newroots[i] = null;
119             }
120           } else {
121             // just add newroots[i] as a new object in the document
122             // - with appropriate provenance.
123           }
124         }
125       }
126     }
127   }
128
129   /* (non-Javadoc)
130    * @see org.vamsas.client.IClientDocument#addVamsasRoot(org.vamsas.objects.core.VAMSAS)
131    */
132   public void addVamsasRoot(VAMSAS newroot) {
133     // TODO Auto-generated method stub
134     
135   }
136
137 /*
138    * (non-Javadoc)
139    * 
140    * @see org.vamsas.client.IClientDocument#setApplicationData(byte[])
141    */
142   public void setApplicationData(byte[] newData) {
143     appData = newData;
144   }
145   /*
146    * (non-Javadoc)
147    * 
148    * @see org.vamsas.client.IClientDocument#registerObjects(org.vamsas.client.object[])
149    */
150   public VorbaId[] registerObjects(object[] unregistered) {
151     if (unregistered!=null) {
152       VorbaId ids[] = new VorbaId[unregistered.length];
153       for (int i=0,k=unregistered.length; i<k; i++)
154         ids[i]=registerObject(unregistered[i]);
155       return ids;
156     }
157     return null;
158   }
159
160   /* (non-Javadoc)
161    * @see org.vamsas.client.IClientDocument#registerObject(org.vamsas.client.object)
162    */
163   public VorbaId registerObject(object unregistered) {
164     // TODO: add provenance stuff to newly registered object
165     return _registerObject(unregistered);
166   }
167 }