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