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