04f49909499318d9a1298a07fb86e805af10c672
[vamsas.git] / src / org / vamsas / client / SimpleClient.java
1 /*
2  * Created on 15-Sep-2005
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */
7 package org.vamsas.client;
8
9 import java.beans.EventHandler;
10 import java.beans.PropertyChangeEvent;
11 import java.beans.PropertyChangeListener;
12 import java.beans.PropertyChangeSupport;
13 import java.io.BufferedReader;
14 import java.io.File;
15 import java.io.IOException;
16 import java.io.Reader;
17 import java.io.Writer;
18 import java.lang.reflect.Field;
19 import java.util.Hashtable;
20 import java.util.Vector;
21
22 import org.exolab.castor.xml.IDResolver;
23 import org.exolab.castor.xml.MarshalException;
24 import org.exolab.castor.xml.Marshaller;
25 import org.exolab.castor.xml.UnmarshalListener;
26 import org.exolab.castor.xml.Unmarshaller;
27 import org.exolab.castor.xml.ValidationException;
28 import org.vamsas.objects.core.VamsasDocument;
29
30 /**
31  * @author jimp
32  */
33 public class SimpleClient implements IClient {
34
35   UserHandle user = null;
36
37   SessionHandle session = null;
38
39   ClientHandle client = null;
40   /*
41    * (non-Javadoc)
42    * 
43    * @see org.vamsas.client.IClient#getAbout()
44    */
45   public String getAbout() {
46     return new String("VORBA SimpleClient version $version$ build $build$");
47   }
48
49   /*
50    * (non-Javadoc)
51    * 
52    * @see org.vamsas.client.IClient#getSessionUrn()
53    */
54   public String getSessionUrn() {
55     return session.getSessionUrn();
56   }
57
58   /*
59    * (non-Javadoc)
60    * 
61    * @see org.vamsas.client.IClient#getSessionHandle()
62    */
63   public SessionHandle getSessionHandle() {
64     return session;
65   }
66
67   /*
68    * (non-Javadoc)
69    * 
70    * @see org.vamsas.client.IClient#getClientHandle()
71    */
72   public ClientHandle getClientHandle() {
73     return client;
74   }
75
76   /*
77    * (non-Javadoc)
78    * 
79    * @see org.vamsas.client.IClient#getUserHandle()
80    */
81   public UserHandle getUserHandle() {
82     return user;
83   }
84
85   private Hashtable handlers = initHandlers();
86   private Vector listeners = new Vector();
87   /**
88    * make all the PropertyChangeSupport objects for the
89    * events described in org.vamsas.client.Event
90    * @return
91    */
92   private static Hashtable initHandlers() {
93     Hashtable events = new Hashtable();
94     java.util.Iterator evt = Events.EventList.iterator();
95     while (evt.hasNext()) {
96       Object ths = evt.next();
97       events.put(ths, (Object) new PropertyChangeSupport(ths));
98     }
99     return events;
100   }
101   /*
102    * (non-Javadoc)
103    * 
104    * @see org.vamsas.client.IClient#addDocumentUpdateHandler(java.util.EventListener)
105    */
106   public void addDocumentUpdateHandler(PropertyChangeListener evt) {
107     if (handlers.containsKey(Events.DOCUMENT_UPDATE)) {
108       Object handler, listener;
109       ((PropertyChangeSupport) (handler
110           = handlers.get(Events.DOCUMENT_UPDATE))).addPropertyChangeListener(evt);
111       listeners.add(handler);
112       listeners.add((Object) evt);      
113     }
114   }
115
116   /*
117    * (non-Javadoc)
118    * 
119    * @see org.vamsas.client.IClient#finalizeClient()
120    */
121   public void finalizeClient() {
122     // TODO: raise events
123     // deregister listeners.
124     // mark this instance as finalized
125   }
126   
127   /**
128    * writes the VamsasDocument to the given stream.
129    * @param outstream
130    * @param doc
131    * @throws IOException
132    * @throws MarshalException
133    * @throws ValidationException
134    */
135   private static void setVamsasDocument(Writer outstream, VamsasDocument doc) throws IOException, MarshalException, ValidationException {
136     Marshaller marshaller = new Marshaller(outstream);  
137     marshaller.marshal(doc);
138   }
139   
140   /**
141    * Unmarshals a vamsasDocument object from a stream, registers
142    * any VorbaIds, and completes the org.vamsas.client.object housekeeping fields.
143    * 
144    * @param instream - the XML input stream 
145    * @param factory - the SimpleClient's properly configured VorbaId factory to make new references.
146    * @return null or {(Object) VamsasDocument object, (Object) Hashtable of object references)
147    */
148   private static Object[] getVamsasDocument(Reader instream,
149       IVorbaIdFactory factory) {
150     Unmarshaller unmarshaller = new Unmarshaller(instream);
151     unmarshaller.setIDResolver(new IDResolver() {
152       public Object resolve(String id) {
153         System.err.println("Warning - id " + id
154             + " is not found in the VamsasDocument!");
155         return null;
156       }
157     });
158     Hashtable refbase = new Hashtable();
159     final Hashtable objrefs = refbase;
160     final IVorbaIdFactory vorbafactory = factory;
161     unmarshaller.setUnmarshalListener(new UnmarshalListener() {
162
163       /*
164        * (non-Javadoc)
165        * 
166        * @see org.exolab.castor.xml.UnmarshalListener#attributesProcessed(java.lang.Object)
167        */
168       public void attributesProcessed(Object object) {
169       }
170
171       /*
172        * (non-Javadoc)
173        * 
174        * @see org.exolab.castor.xml.UnmarshalListener#fieldAdded(java.lang.String,
175        *      java.lang.Object, java.lang.Object)
176        */
177       public void fieldAdded(String fieldName, Object parent, Object child) {
178       }
179
180       /*
181        * (non-Javadoc)
182        * 
183        * @see org.exolab.castor.xml.UnmarshalListener#initialized(java.lang.Object)
184        */
185       public void initialized(Object object) {
186       }
187
188       /*
189        * Check if the object has an 'id' field - if it does, copy the value into
190        * the VorbaId field of object, and add the object to the VorbaId hash.
191        * 
192        * @see org.exolab.castor.xml.UnmarshalListener#unmarshalled(java.lang.Object)
193        */
194       public void unmarshalled(Object newobj) {
195         if (newobj instanceof object) {
196           object nobj = (object) newobj;
197           nobj.set__stored_in_document(true);
198           Field fd = null;
199           try {
200             // look for the id field (should be an NCName string)
201             fd = nobj.getClass().getField("id");
202             String idstring;
203             if (fd.getType().getClass().equals("astring".getClass())) {
204               if (fd.get(nobj) != null) {
205                 idstring = (String) fd.get(nobj);
206                 if (idstring.length() > 0)
207                   if (!objrefs.containsKey(idstring)) {
208                     objrefs.put(idstring, nobj);
209                     nobj.setVorbaId(VorbaId.newId(idstring));
210                     nobj.__vorba = vorbafactory;
211                   }
212               }
213             }
214             nobj.doHash();
215           } catch (Exception e) {
216             return;
217           }
218           ;
219         }
220       }
221
222     });
223     // Call the unmarshaller.
224     try {
225       while (instream.ready()) {
226         Object obj = unmarshaller.unmarshal(instream);
227         if (obj instanceof VamsasDocument) {
228           return new Object[] { obj, objrefs};
229         }
230       }
231     } catch (MarshalException e) {
232       // TODO Auto-generated catch block
233       e.printStackTrace();
234     } catch (ValidationException e) {
235       // TODO Auto-generated catch block
236       e.printStackTrace();
237     } catch (IOException e) {
238       // TODO Auto-generated catch block
239       e.printStackTrace();
240     }
241     return null;
242   }
243 /**
244  * extract data appropriate for client, session and user
245  * from vamsas document.
246  * @return application's byte array
247  */
248   private byte[] getApplicationData() {
249     return null;
250   }
251
252   /*
253    * (non-Javadoc)
254    * 
255    * @see org.vamsas.client.IClient#getClientDocument()
256    */
257   public IClientDocument getClientDocument() {
258     Object[] vdoc;//  TODO: = getVamsasDocument(new Reader());
259     ClientDocument cdoc = new ClientDocument(getApplicationData(),
260         ((VamsasDocument) vdoc[0]).getVAMSAS(), (Hashtable) vdoc[1], this);
261     // 
262     return null;
263   }
264
265   /*
266    * (non-Javadoc)
267    * 
268    * @see org.vamsas.client.IClient#updateDocument(org.vamsas.client.IClientDocument)
269    */
270   public void updateDocument(IClientDocument newdoc) {
271     // TODO Auto-generated method stub
272
273   }
274
275   /*
276    * (non-Javadoc)
277    * 
278    * @see org.vamsas.client.IClient#storeDocument(java.io.File)
279    */
280   public void storeDocument(File location) {
281     // TODO Auto-generated method stub
282
283   }
284
285   /*
286    * (non-Javadoc)
287    * 
288    * @see org.vamsas.client.IClient#addVorbaEventHandler(java.lang.String,
289    *      java.beans.PropertyChangeListener)
290    */
291   public void addVorbaEventHandler(String EventChain, PropertyChangeListener evt) {
292     // TODO Auto-generated method stub
293
294   }
295
296   public static void main(String[] args) {
297   }
298 }