a6bb78c731c7e19c4a8b03404a9a2d1806865088
[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.object;
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 IClient vorba;
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 vorba - the client instance
42    */
43   protected ClientDocument(VamsasDocument doc, VamsasArchive docHandler, IdFactory Factory, IClient vorba) {
44     super(Factory.getVorbaIdHash(), Factory);
45     
46     /**
47      * prepare Application-side dataset from the vamsas Document archive
48      */
49     this.vorba = vorba;
50     archive = docHandler;
51     this.doc = doc;
52     extractAppData();
53   }
54   /**
55    * gets appropriate app data for the application, if it exists in this dataset
56    * 
57    */
58   private void extractAppData() {
59     Vector apldataset = AppDataReference.getUserandApplicationsData(
60         doc, vorbafactory.getUserHandle(), vorbafactory.getClientHandle());
61     if (apldataset!=null) {
62       if (apldataset.size()>0) {
63         AppData clientdat = (AppData) apldataset.get(0);
64         if (clientdat instanceof ApplicationData) {
65           appsglobal = (ApplicationData) clientdat;
66           if (apldataset.size()>1) {
67             clientdat = (AppData) apldataset.get(1);
68             if (clientdat instanceof User)
69               usersdata = (User) clientdat;
70             if (apldataset.size()>2)
71               log.info("Ignoring additional ("+(apldataset.size()-2)+") AppDatas returned by document appdata query.");
72           } 
73         } else {
74           log.warn("Unexpected entry in AppDataReference query: id="+clientdat.getVorbaId()+" type="+clientdat.getClass().getName());
75         }
76       }
77     }
78   }
79   
80   /*
81    * (non-Javadoc)
82    * 
83    * @see org.vamsas.client.IClientDocument#getObject(org.vamsas.client.VorbaId)
84    */
85   public object getObject(VorbaId id) {
86     // TODO: look up id in document object
87     // retrieve object and return
88     return null;
89   }
90   
91   /*
92    * (non-Javadoc)
93    * 
94    * @see org.vamsas.client.IClientDocument#getObjects(org.vamsas.client.VorbaId[])
95    */
96   public object[] getObjects(VorbaId[] ids) {
97     // TODO: getObject in bulk
98     return null;
99   }
100   
101   /*
102    * (non-Javadoc)
103    * 
104    * @see org.vamsas.client.IClientDocument#getVamsasRoots()
105    */
106   public VAMSAS[] getVamsasRoots() {
107     if (doc.getVAMSAS() == null)
108       // Make a new one to return to client : TODO: Decide if this is correct
109       return new VAMSAS[] { new VAMSAS() };
110     return doc.getVAMSAS();
111   }
112   
113   /**
114    * update the document with new roots.
115    */
116   public void setVamsasRoots(VAMSAS[] newroots) {
117     // extract root objects
118     if (newroots != null) {
119       // check newroots for objects that were present in the old document
120       // check to see if the 'old' objects have been modified
121       // if they have ? we overwrite them with their new version, ensuring that
122       // provenance is updated.
123       // if they haven't ? do nothing ?
124       
125       for (int i = 0, k = newroots.length; i < k; i++) {
126         if (newroots[i].isRegistered()) {
127           // easy - just check if anything has changed and do provenance
128           object oldversion = getObject(newroots[i].getVorbaId());
129           if (oldversion instanceof VAMSAS) {
130             // LATER: appropriate merging behaviour when two clients have
131             // modified the same registered object independently
132             if (newroots[i].get__last_hash() != newroots[i].hashCode()) {
133               // client has modified this object since last retrieval.
134               if (newroots[i].get__last_hash() != oldversion.get__last_hash()) {
135                 // object has been modified by another client since this
136                 // client's
137                 // last access to document.
138               }
139             }
140           } else {
141             throw new Error(
142                 "SimpleClient error when using setVamsasRoots : The vorbaId for object "
143                 + i
144                 + " does not refer to an object of type VAMSAS in the current document!");
145           }
146         } else {
147           if (!newroots[i].is__stored_in_document()) {
148             // check if object is modified
149             if (newroots[i].get__last_hash() != newroots[i].hashCode()) {
150               // it is - so we add newroots[i] as a new object, with updated
151               // provenance.
152             } else {
153               // do nothing
154               newroots[i] = null;
155             }
156           } else {
157             // just add newroots[i] as a new object in the document
158             // - with appropriate provenance.
159           }
160         }
161       }
162     }
163   }
164   
165   /* (non-Javadoc)
166    * @see org.vamsas.client.IClientDocument#addVamsasRoot(org.vamsas.objects.core.VAMSAS)
167    */
168   public void addVamsasRoot(VAMSAS newroot) {
169     // TODO Auto-generated method stub
170     
171   }
172   
173   /*
174    * (non-Javadoc)
175    * 
176    * @see org.vamsas.client.IClientDocument#registerObjects(org.vamsas.client.object[])
177    */
178   public VorbaId[] registerObjects(object[] unregistered) {
179     if (unregistered!=null) {
180       VorbaId ids[] = new VorbaId[unregistered.length];
181       for (int i=0,k=unregistered.length; i<k; i++)
182         ids[i]=registerObject(unregistered[i]);
183       return ids;
184     }
185     return null;
186   }
187   
188   /* (non-Javadoc)
189    * @see org.vamsas.client.IClientDocument#registerObject(org.vamsas.client.object)
190    */
191   public VorbaId registerObject(object unregistered) {
192     // TODO: add provenance stuff to newly registered object
193     return _registerObject(unregistered);
194   }
195   /* (non-Javadoc)
196    * @see org.vamsas.client.IClientDocument#getClientAppdata()
197    */
198   public IClientAppdata getClientAppdata() {
199     // TODO Auto-generated method stub
200     return null;
201   }
202 }