beginning of vamsas/local object synchronization pattern implementation.
[jalview.git] / src / jalview / io / vamsas / LocalDocSyncObject.java
1 package jalview.io.vamsas;\r
2 \r
3 import uk.ac.vamsas.client.Vobject;\r
4 \r
5 /**\r
6  * Implement the basic logic for synchronising changes to or from the Vamsas Document\r
7  * @author JimP\r
8  */\r
9 public abstract class LocalDocSyncObject extends DatastoreItem\r
10 {\r
11   /**\r
12    * \r
13    * @return null or the local object that is being worked on. \r
14    */\r
15   public abstract Object getLObject();\r
16   /**\r
17    * \r
18    * @return null or the document object that is being worked on\r
19    */\r
20   public abstract Vobject getVObject();\r
21   \r
22   /**\r
23    * endpoint for synchronize when all opreations are finished.\r
24    */\r
25   public abstract void nextObject();\r
26   /**\r
27    * called if the local object can be safely updated from the bound document object.\r
28    */\r
29   public abstract void updateFromDoc();\r
30   /**\r
31    * called if the associated document object can be safely updated with the local changes\r
32    */\r
33   public abstract void updateToDoc();\r
34   /**\r
35    * @return true if the local object is modified\r
36    */\r
37   public abstract boolean locallyModified();\r
38   /**\r
39    * \r
40    * @return true if the bound document object is modified\r
41    */\r
42   public abstract boolean documentModified();\r
43   /**\r
44    * \r
45    * @return true if the document object is locked  w.r.t. this object's update.\r
46    */\r
47   public abstract boolean documentObjectLocked();\r
48   /**\r
49    * \r
50    * @return a new datastore item instance which binds the local object to a new document object \r
51    */\r
52   public abstract LocalDocSyncObject newDocumentObject(); // could make this constructor(Lobject)\r
53   /**\r
54    * \r
55    * @return a new datastore item instance which binds the document object to a new local object. \r
56    */\r
57   public abstract LocalDocSyncObject newLocalObject(); // make this constructor(Vobject)\r
58   /**\r
59    * apply the update/commit logic as defined in the vamsas paper\r
60    * @param documentIsUpdated true if a document update event is being handled  \r
61    */\r
62   public void synchronize(boolean documentIsUpdated) {\r
63     Object Lobject = getLObject();\r
64     Vobject Vobject = getVObject();\r
65     if (Lobject==null)\r
66     {\r
67       // no local binding for document object\r
68       newLocalObject().synchronize(documentIsUpdated);\r
69       return;\r
70     }\r
71     if (Vobject==null)\r
72     {\r
73       // no document binding for local object\r
74       newDocumentObject().synchronize(documentIsUpdated);\r
75     }\r
76     // Check binding is valid\r
77     if (getjv2vObj(Lobject)!=Vobject)\r
78     {\r
79       // no local binding for document object\r
80       newLocalObject().synchronize(documentIsUpdated);\r
81       // no document binding for local object\r
82       newDocumentObject().synchronize(documentIsUpdated);\r
83     }\r
84   }\r
85 }\r