d4b9ec02f193a18d6effa706fd102df98fffde01
[vamsas.git] / src / uk / ac / vamsas / test / simpleclient / simpleapp / VamsasClient.java
1 /**
2  * 
3  */
4 package uk.ac.vamsas.test.simpleclient.simpleapp;
5
6 import java.io.File;
7 import java.io.FileOutputStream;
8 import java.io.OutputStreamWriter;
9 import java.io.PrintWriter;
10 import java.util.Hashtable;
11 import java.util.IdentityHashMap;
12 import java.util.Vector;
13 import java.util.jar.JarOutputStream;
14
15 import javax.swing.JInternalFrame;
16
17
18 import uk.ac.vamsas.client.UserHandle;
19 import uk.ac.vamsas.client.simpleclient.FileWatcher;
20 import uk.ac.vamsas.client.simpleclient.VamsasArchive;
21 import uk.ac.vamsas.client.simpleclient.VamsasFile;
22 import uk.ac.vamsas.objects.core.Entry;
23 import uk.ac.vamsas.objects.core.VamsasDocument;
24 import uk.ac.vamsas.test.simpleclient.ArchiveClient;
25 import uk.ac.vamsas.test.simpleclient.ClientDoc;
26
27 /**
28  * @author jimp
29  *
30  */
31 public class VamsasClient extends ArchiveClient {
32         org.apache.commons.logging.Log log=org.apache.commons.logging.LogFactory.getLog(VamsasClient.class);
33   /**
34    * create a new vamsas client session from the archive at sessionPath.
35    * @param sessionPath
36    */
37   public VamsasClient(File sessionPath) {
38                 super(System.getProperty("user.name"),System.getProperty("host.name"), "SimpleVamsasClientApp","0.1",
39                                 sessionPath);
40         }
41   /**
42    * Called by gui to read anything from the vamsas session into the apps datamodel
43    * after it has started up.
44    *
45    */
46         public void initial_update() {
47                 log.info("Jalview loading the Vamsas Session.");
48                 // load in the vamsas archive for the first time
49                 ClientDoc cdoc = this.getUpdateable();
50                 updateJalview(cdoc);
51                 // TODO: flush any new VorbaIds to the document : updateVamsasClient may actually generate new Vamsas Document data in the form of vamsas element ids - these should be written back to the document. 
52     //doUpdate(cdoc); // JBPNote: this should flush new VorbaIds but I've not tested it yet.
53     cdoc.closeDoc();
54                 // then tell app to update its display based on the datamodel changes.
55         }
56   VamsasClientWatcher watcher=null;
57         /**
58    * Called by app when internal datamodel should exported (syncrhonised outwards) to vamsas document 
59    *
60          */
61   public void push_update() {
62
63     watchForChange=false; // this makes any watch(long) loops return. 
64     // we should also wait arount for this.WATCH_SLEEP to really make sure the watcher thread has stopped. 
65     try {
66       Thread.sleep(WATCH_SLEEP); 
67     } catch (Exception e) {
68       
69     };
70     
71     ClientDoc cdoc = getUpdateable();
72     updateVamsasDocument(cdoc);
73     doUpdate(cdoc);
74     cdoc.closeDoc();
75     cdoc=null;
76     watchForChange=true;
77     startWatcher();
78         }
79         public void end_session() {
80     watchForChange=false; // this makes any watch(long) loops return. 
81     // we should also wait arount for this.WATCH_SLEEP to really make sure the watcher thread has stopped. 
82     try {
83       Thread.sleep(WATCH_SLEEP); 
84     } catch (Exception e) {
85       
86     };
87     
88                 // stop any update/watcher thread.
89                 log.info("VamsasClientApplication disconnecting from the Vamsas Session.");
90         }
91         public void updateJalview(ClientDoc cdoc) {
92                 ensureVamsasBindings();
93     VamsasDatastore vds = new VamsasDatastore(cdoc, vobj2jv, jv2vobj, baseProvEntry());
94     vds.updateToJalview();
95         }
96         private void ensureVamsasBindings() {
97     if (jv2vobj==null) {
98       jv2vobj = new IdentityHashMap();
99       vobj2jv = new Hashtable();
100     }
101         }
102         /**
103          * App's object binding to VorbaIds
104          */
105         IdentityHashMap jv2vobj = null;
106         Hashtable vobj2jv = null;
107         /**
108    * called with a vamsas document which will be updated with new data from the app
109    * @param doc
110          */
111   public void updateVamsasDocument(ClientDoc doc) {
112           ensureVamsasBindings();
113     VamsasDatastore vds = new VamsasDatastore(doc, vobj2jv, jv2vobj, baseProvEntry());
114     // wander through frames 
115     vds.storeVAMSAS(new Object()); // Object is the apps datamodel ;)
116         }
117   /**
118    * 
119    * @return a base provenance entry used by the VamsasDatastore object to get attributes from. this isn't particularly elegant either.
120    */
121         private Entry baseProvEntry() {
122     uk.ac.vamsas.objects.core.Entry pentry = new uk.ac.vamsas.objects.core.Entry();
123     pentry.setUser(this.getProvenanceUser());
124     pentry.setApp(this.getClientHandle().getClientName());
125     pentry.setDate(new java.util.Date());
126     pentry.setAction("created");
127     return pentry;
128         }
129   protected class VamsasClientWatcher extends Thread implements Runnable {
130                 /* (non-Javadoc)
131                  * @see java.lang.Thread#run()
132                  */
133                 VamsasClient client=null;
134                 VamsasClientWatcher(VamsasClient client) {
135                         this.client = client;
136                 }
137     boolean running=false;
138                 public void run() {
139                   running=true;
140                         while (client.watchForChange) {
141                           ClientDoc docio = client.watch(0);
142                           if (docio!=null) {
143           // VamsasClient GUI bits should be disabled whilst an update is in progress so the user doesn't screw anything up.
144           client.disableGui(true);
145           log.debug("Updating VamsasClient app from changed vamsas document.");
146                             client.updateJalview(docio);
147           log.debug("Finished updating from document change.");
148           docio.closeDoc();
149           docio=null;
150           client.disableGui(false);
151                           }
152       }
153       running=false;
154       
155                 }
156                 
157         }
158         /**
159          * @param args
160          */
161         public static void main(String[] args) {
162                 // TODO Auto-generated method stub
163
164         }
165   /**
166    * disable (if b is true) or enable (if b is true) the VamsasClient's vamsas session gui bits whilst a document change is being updated to the app.
167    * @param b 
168    */
169   public void disableGui(boolean b) {
170       // in jalview, we turn off the VAMSAS Session menu : Desktop.instance.setVamsasUpdate(b);    
171   }
172   /**
173    * spawn a new thread to start the VamsasClientWatcher.
174    *
175    */
176   public void startWatcher() {
177     if (watcher==null)
178       watcher=new VamsasClientWatcher(this);
179     Thread thr = new Thread() {
180       public void run() {
181         watcher.start();
182       }
183     };
184     thr.start();
185   }
186
187 }