half implemented the SimpleClientAppdata interface.
[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 ApplicationData appsglobal=null;
32   protected User usersdata=null;
33   protected byte[] appData=null;
34   protected VamsasArchive archive = null;
35   /**
36    *
37    *  prepare Application-side dataset from the vamsas Document archive
38    * @param doc - the dataset
39    * @param docHandler - the sessionFile IO handler
40    * @param Factory - the source of current and new vorbaIds
41    * @param sclient - the simpleclient instance
42    */
43   protected ClientDocument(VamsasDocument doc, VamsasArchive docHandler, IdFactory Factory, SimpleClient vorba) {
44     super(Factory.getVorbaIdHash(), Factory);
45     
46     /**
47      * prepare Application-side dataset from the vamsas Document archive
48      */
49     this.sclient = vorba;
50     archive = docHandler;
51     this.doc = doc;
52   }
53   
54   /*
55    * (non-Javadoc)
56    * 
57    * @see org.vamsas.client.IClientDocument#getObject(org.vamsas.client.VorbaId)
58    */
59   public Vobject getObject(VorbaId id) {
60     // TODO: look up id in document Vobject
61     // retrieve Vobject and return
62     return null;
63   }
64   
65   /*
66    * (non-Javadoc)
67    * 
68    * @see org.vamsas.client.IClientDocument#getObjects(org.vamsas.client.VorbaId[])
69    */
70   public Vobject[] getObjects(VorbaId[] ids) {
71     // TODO: getObject in bulk
72     return null;
73   }
74   
75   /*
76    * (non-Javadoc)
77    * 
78    * @see org.vamsas.client.IClientDocument#getVamsasRoots()
79    */
80   public VAMSAS[] getVamsasRoots() {
81     if (doc.getVAMSAS() == null)
82       // Make a new one to return to client : TODO: Decide if this is correct
83       return new VAMSAS[] { new VAMSAS() };
84     return doc.getVAMSAS();
85   }
86   
87   /**
88    * update the document with new roots.
89    */
90   public void setVamsasRoots(VAMSAS[] newroots) {
91     // extract root objects
92     if (newroots != null) {
93       // check newroots for objects that were present in the old document
94       // check to see if the 'old' objects have been modified
95       // if they have ? we overwrite them with their new version, ensuring that
96       // provenance is updated.
97       // if they haven't ? do nothing ?
98       
99       for (int i = 0, k = newroots.length; i < k; i++) {
100         if (newroots[i].isRegistered()) {
101           // easy - just check if anything has changed and do provenance
102           Vobject oldversion = getObject(newroots[i].getVorbaId());
103           if (oldversion instanceof VAMSAS) {
104             // LATER: appropriate merging behaviour when two clients have
105             // modified the same registered Vobject independently
106             if (newroots[i].get__last_hash() != newroots[i].hashCode()) {
107               // client has modified this Vobject since last retrieval.
108               if (newroots[i].get__last_hash() != oldversion.get__last_hash()) {
109                 // Vobject has been modified by another client since this
110                 // client's
111                 // last access to document.
112               }
113             }
114           } else {
115             throw new Error(
116                 "SimpleClient error when using setVamsasRoots : The vorbaId for Vobject "
117                 + i
118                 + " does not refer to an Vobject of type VAMSAS in the current document!");
119           }
120         } else {
121           if (!newroots[i].is__stored_in_document()) {
122             // check if Vobject is modified
123             if (newroots[i].get__last_hash() != newroots[i].hashCode()) {
124               // it is - so we add newroots[i] as a new Vobject, with updated
125               // provenance.
126             } else {
127               // do nothing
128               newroots[i] = null;
129             }
130           } else {
131             // just add newroots[i] as a new Vobject in the document
132             // - with appropriate provenance.
133           }
134         }
135       }
136     }
137   }
138   
139   /* (non-Javadoc)
140    * @see org.vamsas.client.IClientDocument#addVamsasRoot(org.vamsas.objects.core.VAMSAS)
141    */
142   public void addVamsasRoot(VAMSAS newroot) {
143     // TODO Auto-generated method stub
144     
145   }
146   
147   /*
148    * (non-Javadoc)
149    * 
150    * @see org.vamsas.client.IClientDocument#registerObjects(org.vamsas.client.Vobject[])
151    */
152   public VorbaId[] registerObjects(Vobject[] unregistered) {
153     if (unregistered!=null) {
154       VorbaId ids[] = new VorbaId[unregistered.length];
155       for (int i=0,k=unregistered.length; i<k; i++)
156         ids[i]=registerObject(unregistered[i]);
157       return ids;
158     }
159     return null;
160   }
161   
162   /* (non-Javadoc)
163    * @see org.vamsas.client.IClientDocument#registerObject(org.vamsas.client.Vobject)
164    */
165   public VorbaId registerObject(Vobject unregistered) {
166     // TODO: add provenance stuff to newly registered Vobject
167     return _registerObject(unregistered);
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 }