merge from 2_4_Release branch
[jalview.git] / src / jalview / io / vamsas / DatastoreItem.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.io.vamsas;
20
21 import jalview.bin.Cache;
22 import jalview.gui.TreePanel;
23 import jalview.io.VamsasAppDatastore;
24
25 import java.util.Enumeration;
26 import java.util.Hashtable;
27 import java.util.IdentityHashMap;
28 import java.util.Vector;
29
30 import uk.ac.vamsas.client.IClientDocument;
31 import uk.ac.vamsas.client.Vobject;
32 import uk.ac.vamsas.client.VorbaId;
33 import uk.ac.vamsas.objects.core.Entry;
34 import uk.ac.vamsas.objects.core.Provenance;
35 import uk.ac.vamsas.objects.core.Seg;
36
37 /**
38  * Holds all the common machinery for binding objects to vamsas objects
39  * 
40  * @author JimP
41  * 
42  */
43 public class DatastoreItem
44 {
45   /**
46    * 
47    */
48   Entry provEntry = null;
49
50   IClientDocument cdoc;
51
52   Hashtable vobj2jv;
53
54   IdentityHashMap jv2vobj;
55
56   /**
57    * @return the Vobject bound to Jalview datamodel object
58    */
59   protected Vobject getjv2vObj(Object jvobj)
60   {
61     if (jv2vobj.containsKey(jvobj))
62     {
63       return cdoc.getObject((VorbaId) jv2vobj.get(jvobj));
64     }
65     if (Cache.log.isDebugEnabled())
66     {
67       Cache.log.debug("Returning null VorbaID binding for jalview object "
68               + jvobj);
69     }
70     return null;
71   }
72
73   /**
74    * 
75    * @param vobj
76    * @return Jalview datamodel object bound to the vamsas document object
77    */
78   protected Object getvObj2jv(uk.ac.vamsas.client.Vobject vobj)
79   {
80     if (vobj2jv == null)
81       return null;
82     VorbaId id = vobj.getVorbaId();
83     if (id == null)
84     {
85       id = cdoc.registerObject(vobj);
86       Cache.log
87               .debug("Registering new object and returning null for getvObj2jv");
88       return null;
89     }
90     if (vobj2jv.containsKey(vobj.getVorbaId()))
91     {
92       return vobj2jv.get(vobj.getVorbaId());
93     }
94     return null;
95   }
96
97   protected void bindjvvobj(Object jvobj, uk.ac.vamsas.client.Vobject vobj)
98   {
99     VorbaId id = vobj.getVorbaId();
100     if (id == null)
101     {
102       id = cdoc.registerObject(vobj);
103       if (id == null || vobj.getVorbaId() == null
104               || cdoc.getObject(id) != vobj)
105       {
106         Cache.log.error("Failed to get id for "
107                 + (vobj.isRegisterable() ? "registerable"
108                         : "unregisterable") + " object " + vobj);
109       }
110     }
111
112     if (vobj2jv.containsKey(vobj.getVorbaId())
113             && !((VorbaId) vobj2jv.get(vobj.getVorbaId())).equals(jvobj))
114     {
115       Cache.log.debug(
116               "Warning? Overwriting existing vamsas id binding for "
117                       + vobj.getVorbaId(), new Exception(
118                       "Overwriting vamsas id binding."));
119     }
120     else if (jv2vobj.containsKey(jvobj)
121             && !((VorbaId) jv2vobj.get(jvobj)).equals(vobj.getVorbaId()))
122     {
123       Cache.log.debug(
124               "Warning? Overwriting existing jalview object binding for "
125                       + jvobj, new Exception(
126                       "Overwriting jalview object binding."));
127     }
128     /*
129      * Cache.log.error("Attempt to make conflicting object binding! "+vobj+" id "
130      * +vobj.getVorbaId()+" already bound to "+getvObj2jv(vobj)+" and "+jvobj+"
131      * already bound to "+getjv2vObj(jvobj),new Exception("Excessive call to
132      * bindjvvobj")); }
133      */
134     // we just update the hash's regardless!
135     Cache.log.debug("Binding " + vobj.getVorbaId() + " to " + jvobj);
136     vobj2jv.put(vobj.getVorbaId(), jvobj);
137     // JBPNote - better implementing a hybrid invertible hash.
138     jv2vobj.put(jvobj, vobj.getVorbaId());
139   }
140
141   public DatastoreItem()
142   {
143     super();
144   }
145
146   public DatastoreItem(VamsasAppDatastore datastore)
147   {
148     this();
149     initDatastoreItem(datastore);
150     // TODO Auto-generated constructor stub
151   }
152
153   VamsasAppDatastore datastore = null;
154
155   public void initDatastoreItem(VamsasAppDatastore ds)
156   {
157     datastore = ds;
158     initDatastoreItem(ds.getProvEntry(), ds.getClientDocument(), ds
159             .getVamsasObjectBinding(), ds.getJvObjectBinding());
160   }
161
162   public void initDatastoreItem(Entry provEntry, IClientDocument cdoc,
163           Hashtable vobj2jv, IdentityHashMap jv2vobj)
164   {
165     this.provEntry = provEntry;
166     this.cdoc = cdoc;
167     this.vobj2jv = vobj2jv;
168     this.jv2vobj = jv2vobj;
169   }
170
171   protected boolean isModifiable(String modifiable)
172   {
173     return modifiable == null; // TODO: USE VAMSAS LIBRARY OBJECT LOCK METHODS)
174   }
175
176   protected Vector getjv2vObjs(Vector alsq)
177   {
178     Vector vObjs = new Vector();
179     Enumeration elm = alsq.elements();
180     while (elm.hasMoreElements())
181     {
182       vObjs.addElement(getjv2vObj(elm.nextElement()));
183     }
184     return vObjs;
185   }
186
187   // utility functions
188   /**
189    * get start<end range of segment, adjusting for inclusivity flag and
190    * polarity.
191    * 
192    * @param visSeg
193    * @param ensureDirection
194    *                when true - always ensure start is less than end.
195    * @return int[] { start, end, direction} where direction==1 for range running
196    *         from end to start.
197    */
198   public int[] getSegRange(Seg visSeg, boolean ensureDirection)
199   {
200     boolean incl = visSeg.getInclusive();
201     // adjust for inclusive flag.
202     int pol = (visSeg.getStart() <= visSeg.getEnd()) ? 1 : -1; // polarity of
203     // region.
204     int start = visSeg.getStart() + (incl ? 0 : pol);
205     int end = visSeg.getEnd() + (incl ? 0 : -pol);
206     if (ensureDirection && pol == -1)
207     {
208       // jalview doesn't deal with inverted ranges, yet.
209       int t = end;
210       end = start;
211       start = t;
212     }
213     return new int[]
214     { start, end, pol < 0 ? 1 : 0 };
215   }
216
217   /**
218    * provenance bits
219    */
220   protected jalview.datamodel.Provenance getJalviewProvenance(
221           Provenance prov)
222   {
223     // TODO: fix App and Action entries and check use of provenance in jalview.
224     jalview.datamodel.Provenance jprov = new jalview.datamodel.Provenance();
225     for (int i = 0; i < prov.getEntryCount(); i++)
226     {
227       jprov.addEntry(prov.getEntry(i).getUser(), prov.getEntry(i)
228               .getAction(), prov.getEntry(i).getDate(), prov.getEntry(i)
229               .getId());
230     }
231
232     return jprov;
233   }
234
235   /**
236    * 
237    * @return default initial provenance list for a Jalview created vamsas
238    *         object.
239    */
240   Provenance dummyProvenance()
241   {
242     return dummyProvenance(null);
243   }
244
245   protected Entry dummyPEntry(String action)
246   {
247     Entry entry = new Entry();
248     entry.setApp(this.provEntry.getApp());
249     if (action != null)
250     {
251       entry.setAction(action);
252     }
253     else
254     {
255       entry.setAction("created.");
256     }
257     entry.setDate(new java.util.Date());
258     entry.setUser(this.provEntry.getUser());
259     return entry;
260   }
261
262   protected Provenance dummyProvenance(String action)
263   {
264     Provenance prov = new Provenance();
265     prov.addEntry(dummyPEntry(action));
266     return prov;
267   }
268
269   protected void addProvenance(Provenance p, String action)
270   {
271     p.addEntry(dummyPEntry(action));
272   }
273 }