1 package jalview.io.vamsas;
\r
3 import jalview.bin.Cache;
\r
4 import jalview.gui.TreePanel;
\r
5 import jalview.io.VamsasAppDatastore;
\r
7 import java.util.Enumeration;
\r
8 import java.util.Hashtable;
\r
9 import java.util.IdentityHashMap;
\r
10 import java.util.Vector;
\r
12 import uk.ac.vamsas.client.IClientDocument;
\r
13 import uk.ac.vamsas.client.Vobject;
\r
14 import uk.ac.vamsas.client.VorbaId;
\r
15 import uk.ac.vamsas.objects.core.Entry;
\r
16 import uk.ac.vamsas.objects.core.Provenance;
\r
17 import uk.ac.vamsas.objects.core.Seg;
\r
20 * Holds all the common machinery for binding objects to vamsas objects
\r
24 public class DatastoreItem
\r
29 Entry provEntry = null;
\r
31 IClientDocument cdoc;
\r
35 IdentityHashMap jv2vobj;
\r
37 * @return the Vobject bound to Jalview datamodel object
\r
39 protected Vobject getjv2vObj(Object jvobj)
\r
41 if (jv2vobj.containsKey(jvobj))
\r
43 return cdoc.getObject( (VorbaId) jv2vobj.get(jvobj));
\r
45 if (Cache.log.isDebugEnabled())
\r
47 Cache.log.debug("Returning null VorbaID binding for jalview object "+jvobj);
\r
55 * @return Jalview datamodel object bound to the vamsas document object
\r
57 protected Object getvObj2jv(uk.ac.vamsas.client.Vobject vobj)
\r
61 VorbaId id = vobj.getVorbaId();
\r
64 id = cdoc.registerObject(vobj);
\r
66 .debug("Registering new object and returning null for getvObj2jv");
\r
69 if (vobj2jv.containsKey(vobj.getVorbaId()))
\r
71 return vobj2jv.get(vobj.getVorbaId());
\r
76 protected void bindjvvobj(Object jvobj, uk.ac.vamsas.client.Vobject vobj)
\r
78 VorbaId id = vobj.getVorbaId();
\r
81 id = cdoc.registerObject(vobj);
\r
82 if (id == null || vobj.getVorbaId() == null || cdoc.getObject(id)!=vobj)
\r
84 Cache.log.error("Failed to get id for " +
\r
85 (vobj.isRegisterable() ? "registerable" :
\r
86 "unregisterable") + " object " + vobj);
\r
90 if (vobj2jv.containsKey(vobj.getVorbaId()) &&
\r
91 ! ( (VorbaId) vobj2jv.get(vobj.getVorbaId())).equals(jvobj))
\r
93 Cache.log.debug("Warning? Overwriting existing vamsas id binding for " +
\r
95 new Exception("Overwriting vamsas id binding."));
\r
97 else if (jv2vobj.containsKey(jvobj) &&
\r
98 ! ( (VorbaId) jv2vobj.get(jvobj)).equals(vobj.getVorbaId()))
\r
101 "Warning? Overwriting existing jalview object binding for " + jvobj,
\r
102 new Exception("Overwriting jalview object binding."));
\r
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"));
\r
106 // we just update the hash's regardless!
\r
107 Cache.log.debug("Binding "+vobj.getVorbaId()+" to "+jvobj);
\r
108 vobj2jv.put(vobj.getVorbaId(), jvobj);
\r
109 // JBPNote - better implementing a hybrid invertible hash.
\r
110 jv2vobj.put(jvobj, vobj.getVorbaId());
\r
113 public DatastoreItem() {
\r
116 public DatastoreItem(VamsasAppDatastore datastore)
\r
119 initDatastoreItem(datastore);
\r
120 // TODO Auto-generated constructor stub
\r
123 public void initDatastoreItem(VamsasAppDatastore ds)
\r
125 initDatastoreItem(ds.getProvEntry(), ds.getClientDocument(), ds.getVamsasObjectBinding(), ds.getJvObjectBinding());
\r
127 public void initDatastoreItem(Entry provEntry, IClientDocument cdoc, Hashtable vobj2jv, IdentityHashMap jv2vobj)
\r
129 this.provEntry = provEntry;
\r
131 this.vobj2jv = vobj2jv;
\r
132 this.jv2vobj = jv2vobj;
\r
135 protected boolean isModifiable(String modifiable)
\r
137 return modifiable==null; // TODO: USE VAMSAS LIBRARY OBJECT LOCK METHODS)
\r
140 protected Vector getjv2vObjs(Vector alsq)
\r
142 Vector vObjs = new Vector();
\r
143 Enumeration elm = alsq.elements();
\r
144 while (elm.hasMoreElements())
\r
146 vObjs.addElement(getjv2vObj(elm.nextElement()));
\r
150 // utility functions
\r
152 * get start<end range of segment, adjusting for inclusivity flag and
\r
156 * @param ensureDirection when true - always ensure start is less than end.
\r
157 * @return int[] { start, end, direction} where direction==1 for range running from end to start.
\r
159 public int[] getSegRange(Seg visSeg, boolean ensureDirection)
\r
161 boolean incl = visSeg.getInclusive();
\r
162 // adjust for inclusive flag.
\r
163 int pol = (visSeg.getStart() <= visSeg.getEnd()) ? 1 : -1; // polarity of
\r
165 int start = visSeg.getStart() + (incl ? 0 : pol);
\r
166 int end = visSeg.getEnd() + (incl ? 0 : -pol);
\r
167 if (ensureDirection && pol == -1)
\r
169 // jalview doesn't deal with inverted ranges, yet.
\r
176 start, end, pol < 0 ? 1 : 0};
\r
181 protected jalview.datamodel.Provenance getJalviewProvenance(Provenance prov)
\r
183 // TODO: fix App and Action entries and check use of provenance in jalview.
\r
184 jalview.datamodel.Provenance jprov = new jalview.datamodel.Provenance();
\r
185 for (int i = 0; i < prov.getEntryCount(); i++)
\r
187 jprov.addEntry(prov.getEntry(i).getUser(), prov.getEntry(i).getAction(),
\r
188 prov.getEntry(i).getDate(),
\r
189 prov.getEntry(i).getId());
\r
197 * @return default initial provenance list for a Jalview created vamsas
\r
200 Provenance dummyProvenance()
\r
202 return dummyProvenance(null);
\r
205 protected Entry dummyPEntry(String action)
\r
207 Entry entry = new Entry();
\r
208 entry.setApp(this.provEntry.getApp());
\r
209 if (action != null)
\r
211 entry.setAction(action);
\r
215 entry.setAction("created.");
\r
217 entry.setDate(new java.util.Date());
\r
218 entry.setUser(this.provEntry.getUser());
\r
222 protected Provenance dummyProvenance(String action)
\r
224 Provenance prov = new Provenance();
\r
225 prov.addEntry(dummyPEntry(action));
\r
229 protected void addProvenance(Provenance p, String action)
\r
231 p.addEntry(dummyPEntry(action));
\r