SimpleClientAppdata implemented but untested.
[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 import java.util.Vector;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.vamsas.client.IClient;
12 import org.vamsas.client.IClientAppdata;
13 import org.vamsas.client.IClientDocument;
14 import org.vamsas.client.VorbaId;
15 import org.vamsas.client.Vobject;
16 import org.vamsas.objects.core.AppData;
17 import org.vamsas.objects.core.ApplicationData;
18 import org.vamsas.objects.core.User;
19 import org.vamsas.objects.core.VAMSAS;
20 import org.vamsas.objects.core.VamsasDocument;
21 import org.vamsas.objects.utils.AppDataReference;
22
23 /**
24  * @author jimp Contains a collection of vamsas objects and reference to a
25  *         specified ClientHandle's information.
26  */
27 public class ClientDocument extends org.vamsas.client.ClientDocument implements IClientDocument {
28   private static Log log = LogFactory.getLog(ClientDocument.class);
29   private VamsasDocument doc;
30   protected SimpleClient sclient;
31   protected VamsasArchive archive = null;
32   /**
33    *
34    *  prepare Application-side dataset from the vamsas Document archive
35    * @param doc - the dataset
36    * @param docHandler - the sessionFile IO handler
37    * @param Factory - the source of current and new vorbaIds
38    * @param sclient - the simpleclient instance
39    */
40   protected ClientDocument(VamsasDocument doc, VamsasArchive docHandler, IdFactory Factory, SimpleClient vorba) {
41     super(Factory.getVorbaIdHash(), Factory);
42     
43     /**
44      * prepare Application-side dataset from the vamsas Document archive
45      */
46     this.sclient = vorba;
47     archive = docHandler;
48     this.doc = doc;
49   }
50   
51   /*
52    * (non-Javadoc)
53    * 
54    * @see org.vamsas.client.IClientDocument#getObject(org.vamsas.client.VorbaId)
55    */
56   public Vobject getObject(VorbaId id) {
57     // TODO: look up id in document Vobject
58     // retrieve Vobject 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 Vobject[] getObjects(VorbaId[] ids) {
68     // TODO: getObject in bulk
69     return null;
70   }
71   
72   /*
73    * (non-Javadoc)
74    * 
75    * @see org.vamsas.client.IClientDocument#getVamsasRoots()
76    */
77   public VAMSAS[] getVamsasRoots() {
78     if (doc.getVAMSAS() == null)
79       // Make a new one to return to client : TODO: Decide if this is correct
80       return new VAMSAS[] { new VAMSAS() };
81     return doc.getVAMSAS();
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           Vobject oldversion = getObject(newroots[i].getVorbaId());
100           if (oldversion instanceof VAMSAS) {
101             // LATER: appropriate merging behaviour when two clients have
102             // modified the same registered Vobject independently
103             if (newroots[i].get__last_hash() != newroots[i].hashCode()) {
104               // client has modified this Vobject since last retrieval.
105               if (newroots[i].get__last_hash() != oldversion.get__last_hash()) {
106                 // Vobject 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 Vobject "
114                 + i
115                 + " does not refer to an Vobject of type VAMSAS in the current document!");
116           }
117         } else {
118           if (!newroots[i].is__stored_in_document()) {
119             // check if Vobject is modified
120             if (newroots[i].get__last_hash() != newroots[i].hashCode()) {
121               // it is - so we add newroots[i] as a new Vobject, with updated
122               // provenance.
123             } else {
124               // do nothing
125               newroots[i] = null;
126             }
127           } else {
128             // just add newroots[i] as a new Vobject in the document
129             // - with appropriate provenance.
130           }
131         }
132       }
133     }
134   }
135   
136   /* (non-Javadoc)
137    * @see org.vamsas.client.IClientDocument#addVamsasRoot(org.vamsas.objects.core.VAMSAS)
138    */
139   public void addVamsasRoot(VAMSAS newroot) {
140     // TODO Auto-generated method stub
141     
142   }
143   
144   /*
145    * (non-Javadoc)
146    * 
147    * @see org.vamsas.client.IClientDocument#registerObjects(org.vamsas.client.Vobject[])
148    */
149   public VorbaId[] registerObjects(Vobject[] unregistered) {
150     if (unregistered!=null) {
151       VorbaId ids[] = new VorbaId[unregistered.length];
152       for (int i=0,k=unregistered.length; i<k; i++)
153         ids[i]=registerObject(unregistered[i]);
154       return ids;
155     }
156     return null;
157   }
158   
159   /* (non-Javadoc)
160    * @see org.vamsas.client.IClientDocument#registerObject(org.vamsas.client.Vobject)
161    */
162   public VorbaId registerObject(Vobject unregistered) {
163     // TODO: add provenance stuff to newly registered Vobject
164     return _registerObject(unregistered);
165   }
166   /**
167    * IClientAppdata instance - if it exists.
168    */
169   SimpleClientAppdata scappd = null;
170   /* (non-Javadoc)
171    * @see org.vamsas.client.IClientDocument#getClientAppdata()
172    */
173   public IClientAppdata getClientAppdata() {
174     if (scappd==null) {
175       log.debug("Creating new SimpleClientAppdata instance for "+sclient.getSessionHandle());
176       scappd = new SimpleClientAppdata(this);
177       if (scappd==null) {
178         // LATER: may not need this as a warning message.
179         log.warn("Null appdata object for "+sclient.getSessionHandle());
180       } else {
181         log.debug("Created SimpleClientAppdata successfully.");
182       }
183     } else {
184       log.debug("Returning existing SimpleClientAppdata reference.");
185     }
186     return scappd;
187   }
188   /* (non-Javadoc)
189    * @see java.lang.Object#finalize()
190    */
191   protected void finalize() throws Throwable {
192     log.debug("Garbage collecting on ClientDocument instance.");
193     if (scappd!=null) {
194       scappd.finalize();
195       scappd = null;
196     }
197     
198     super.finalize();
199   }
200   /**
201    * access the vamsas document
202    * @return the session's vamsas document
203    */
204   protected VamsasDocument getVamsasDocument() {
205     // TODO: IMPLEMENT
206     return null;
207   }
208   /**
209    * returns the read-only IO interface for the vamsas document Jar file
210    * @return
211    */
212   protected VamsasArchiveReader getVamsasArchiveReader() {
213     // TODO: IMPLEMENT getVamsasArchiveReader
214     return null;
215   }
216 }