ascii file
[jalview.git] / src / jalview / io / vamsas / DatastoreItem.java
1 package jalview.io.vamsas;
2
3 import jalview.bin.Cache;
4 import jalview.gui.TreePanel;
5 import jalview.io.VamsasAppDatastore;
6
7 import java.util.Enumeration;
8 import java.util.Hashtable;
9 import java.util.IdentityHashMap;
10 import java.util.Vector;
11
12 import uk.ac.vamsas.client.IClientDocument;
13 import uk.ac.vamsas.client.Vobject;
14 import uk.ac.vamsas.client.VorbaId;
15 import uk.ac.vamsas.objects.core.Entry;
16 import uk.ac.vamsas.objects.core.Provenance;
17 import uk.ac.vamsas.objects.core.Seg;
18
19 /**
20  * Holds all the common machinery for binding objects to vamsas objects
21  * @author JimP
22  *
23  */
24 public class DatastoreItem
25 {
26   /**
27    * 
28    */
29   Entry provEntry = null;
30
31   IClientDocument cdoc;
32
33   Hashtable vobj2jv;
34
35   IdentityHashMap jv2vobj;
36   /**
37    * @return the Vobject bound to Jalview datamodel object
38    */
39   protected Vobject getjv2vObj(Object jvobj)
40   {
41     if (jv2vobj.containsKey(jvobj))
42     {
43       return cdoc.getObject( (VorbaId) jv2vobj.get(jvobj));
44     }
45     if (Cache.log.isDebugEnabled())
46     {
47       Cache.log.debug("Returning null VorbaID binding for jalview object "+jvobj);
48     }
49     return null;
50   }
51
52   /**
53    *
54    * @param vobj
55    * @return Jalview datamodel object bound to the vamsas document object
56    */
57   protected Object getvObj2jv(uk.ac.vamsas.client.Vobject vobj)
58   {
59     if (vobj2jv==null)
60       return null;
61     VorbaId id = vobj.getVorbaId();
62     if (id == null)
63     {
64       id = cdoc.registerObject(vobj);
65       Cache.log
66           .debug("Registering new object and returning null for getvObj2jv");
67       return null;
68     }
69     if (vobj2jv.containsKey(vobj.getVorbaId()))
70     {
71       return vobj2jv.get(vobj.getVorbaId());
72     }
73     return null;
74   }
75
76   protected void bindjvvobj(Object jvobj, uk.ac.vamsas.client.Vobject vobj)
77   {
78     VorbaId id = vobj.getVorbaId();
79     if (id == null)
80     {
81       id = cdoc.registerObject(vobj);
82       if (id == null || vobj.getVorbaId() == null || cdoc.getObject(id)!=vobj)
83       {
84         Cache.log.error("Failed to get id for " +
85                         (vobj.isRegisterable() ? "registerable" :
86                          "unregisterable") + " object " + vobj);
87       }
88     }
89
90     if (vobj2jv.containsKey(vobj.getVorbaId()) &&
91         ! ( (VorbaId) vobj2jv.get(vobj.getVorbaId())).equals(jvobj))
92     {
93       Cache.log.debug("Warning? Overwriting existing vamsas id binding for " +
94                       vobj.getVorbaId(),
95                       new Exception("Overwriting vamsas id binding."));
96     }
97     else if (jv2vobj.containsKey(jvobj) &&
98              ! ( (VorbaId) jv2vobj.get(jvobj)).equals(vobj.getVorbaId()))
99     {
100       Cache.log.debug(
101           "Warning? Overwriting existing jalview object binding for " + jvobj,
102           new Exception("Overwriting jalview object binding."));
103     }
104     /* Cache.log.error("Attempt to make conflicting object binding! "+vobj+" id " +vobj.getVorbaId()+" already bound to "+getvObj2jv(vobj)+" and "+jvobj+" already bound to "+getjv2vObj(jvobj),new Exception("Excessive call to bindjvvobj"));
105          }*/
106     // we just update the hash's regardless!
107     Cache.log.debug("Binding "+vobj.getVorbaId()+" to "+jvobj);
108     vobj2jv.put(vobj.getVorbaId(), jvobj);
109     // JBPNote - better implementing a hybrid invertible hash.
110     jv2vobj.put(jvobj, vobj.getVorbaId());
111   }
112
113   public DatastoreItem() {
114     super();
115   }
116   public DatastoreItem(VamsasAppDatastore datastore)
117   {
118     this();
119     initDatastoreItem(datastore);
120     // TODO Auto-generated constructor stub
121   }
122   VamsasAppDatastore datastore = null;
123   public void initDatastoreItem(VamsasAppDatastore ds)
124   {
125     datastore = ds;
126     initDatastoreItem(ds.getProvEntry(), ds.getClientDocument(), ds.getVamsasObjectBinding(), ds.getJvObjectBinding());
127   }
128   public void initDatastoreItem(Entry provEntry, IClientDocument cdoc, Hashtable vobj2jv, IdentityHashMap jv2vobj)
129   {
130     this.provEntry = provEntry;
131     this.cdoc = cdoc;
132     this.vobj2jv = vobj2jv;
133     this.jv2vobj = jv2vobj;
134   }
135
136   protected boolean isModifiable(String modifiable)
137   {
138     return modifiable==null; // TODO: USE VAMSAS LIBRARY OBJECT LOCK METHODS)
139   }
140
141   protected Vector getjv2vObjs(Vector alsq)
142   {
143     Vector vObjs = new Vector();
144     Enumeration elm = alsq.elements();
145     while (elm.hasMoreElements())
146     {
147       vObjs.addElement(getjv2vObj(elm.nextElement()));
148     }
149     return vObjs;
150   }
151   // utility functions
152   /**
153    * get start<end range of segment, adjusting for inclusivity flag and
154    * polarity.
155    *
156    * @param visSeg
157    * @param ensureDirection when true - always ensure start is less than end.
158    * @return int[] { start, end, direction} where direction==1 for range running from end to start.
159    */
160   public int[] getSegRange(Seg visSeg, boolean ensureDirection)
161   {
162     boolean incl = visSeg.getInclusive();
163     // adjust for inclusive flag.
164     int pol = (visSeg.getStart() <= visSeg.getEnd()) ? 1 : -1; // polarity of
165     // region.
166     int start = visSeg.getStart() + (incl ? 0 : pol);
167     int end = visSeg.getEnd() + (incl ? 0 : -pol);
168     if (ensureDirection && pol == -1)
169     {
170       // jalview doesn't deal with inverted ranges, yet.
171       int t = end;
172       end = start;
173       start = t;
174     }
175     return new int[]
176         {
177         start, end, pol < 0 ? 1 : 0};
178   }
179   /**
180    * provenance bits
181    */
182   protected jalview.datamodel.Provenance getJalviewProvenance(Provenance prov)
183   {
184     // TODO: fix App and Action entries and check use of provenance in jalview.
185     jalview.datamodel.Provenance jprov = new jalview.datamodel.Provenance();
186     for (int i = 0; i < prov.getEntryCount(); i++)
187     {
188       jprov.addEntry(prov.getEntry(i).getUser(), prov.getEntry(i).getAction(),
189                      prov.getEntry(i).getDate(),
190                      prov.getEntry(i).getId());
191     }
192
193     return jprov;
194   }
195
196   /**
197    *
198    * @return default initial provenance list for a Jalview created vamsas
199    *         object.
200    */
201   Provenance dummyProvenance()
202   {
203     return dummyProvenance(null);
204   }
205
206   protected Entry dummyPEntry(String action)
207   {
208     Entry entry = new Entry();
209     entry.setApp(this.provEntry.getApp());
210     if (action != null)
211     {
212       entry.setAction(action);
213     }
214     else
215     {
216       entry.setAction("created.");
217     }
218     entry.setDate(new java.util.Date());
219     entry.setUser(this.provEntry.getUser());
220     return entry;
221   }
222
223   protected Provenance dummyProvenance(String action)
224   {
225     Provenance prov = new Provenance();
226     prov.addEntry(dummyPEntry(action));
227     return prov;
228   }
229
230   protected void addProvenance(Provenance p, String action)
231   {
232     p.addEntry(dummyPEntry(action));
233   }
234 }