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