2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.io.vamsas;
21 import jalview.bin.Cache;
22 import jalview.io.VamsasAppDatastore;
24 import java.util.Enumeration;
25 import java.util.Hashtable;
26 import java.util.IdentityHashMap;
27 import java.util.Vector;
29 import uk.ac.vamsas.client.IClientDocument;
30 import uk.ac.vamsas.client.Vobject;
31 import uk.ac.vamsas.client.VorbaId;
32 import uk.ac.vamsas.objects.core.Entry;
33 import uk.ac.vamsas.objects.core.Provenance;
34 import uk.ac.vamsas.objects.core.Seg;
37 * Holds all the common machinery for binding objects to vamsas objects
42 public abstract class DatastoreItem
47 Entry provEntry = null;
53 IdentityHashMap jv2vobj;
55 boolean tojalview = false;
60 protected static org.apache.log4j.Logger log = org.apache.log4j.Logger
61 .getLogger(DatastoreItem.class);
64 * note: this is taken verbatim from jalview.io.VamsasAppDatastore
66 * @return the Vobject bound to Jalview datamodel object
68 protected Vobject getjv2vObj(Object jvobj)
70 if (jv2vobj.containsKey(jvobj))
72 return cdoc.getObject((VorbaId) jv2vobj.get(jvobj));
74 if (Cache.log.isDebugEnabled())
76 Cache.log.debug("Returning null VorbaID binding for jalview object "
85 * @return Jalview datamodel object bound to the vamsas document object
87 protected Object getvObj2jv(uk.ac.vamsas.client.Vobject vobj)
91 VorbaId id = vobj.getVorbaId();
94 id = cdoc.registerObject(vobj);
96 .debug("Registering new object and returning null for getvObj2jv");
99 if (vobj2jv.containsKey(vobj.getVorbaId()))
101 return vobj2jv.get(vobj.getVorbaId());
107 * note: this is taken verbatim from jalview.io.VamsasAppDatastore with added
108 * call to updateRegistryEntry
113 protected void bindjvvobj(Object jvobj, uk.ac.vamsas.client.Vobject vobj)
115 VorbaId id = vobj.getVorbaId();
118 id = cdoc.registerObject(vobj);
119 if (id == null || vobj.getVorbaId() == null
120 || cdoc.getObject(id) != vobj)
122 Cache.log.error("Failed to get id for "
123 + (vobj.isRegisterable() ? "registerable"
124 : "unregisterable") + " object " + vobj);
127 if (vobj2jv.containsKey(vobj.getVorbaId())
128 && !(vobj2jv.get(vobj.getVorbaId())).equals(jvobj))
131 "Warning? Overwriting existing vamsas id binding for "
132 + vobj.getVorbaId(), new Exception(
133 "Overwriting vamsas id binding."));
135 else if (jv2vobj.containsKey(jvobj)
136 && !((VorbaId) jv2vobj.get(jvobj)).equals(vobj.getVorbaId()))
139 "Warning? Overwriting existing jalview object binding for "
140 + jvobj, new Exception(
141 "Overwriting jalview object binding."));
144 * Cache.log.error("Attempt to make conflicting object binding! "+vobj+" id "
145 * +vobj.getVorbaId()+" already bound to "+getvObj2jv(vobj)+" and "+jvobj+"
146 * already bound to "+getjv2vObj(jvobj),new Exception("Excessive call to
149 // we just update the hash's regardless!
150 Cache.log.debug("Binding " + vobj.getVorbaId() + " to " + jvobj);
151 vobj2jv.put(vobj.getVorbaId(), jvobj);
152 // JBPNote - better implementing a hybrid invertible hash.
153 jv2vobj.put(jvobj, vobj.getVorbaId());
154 if (jvobj == this.jvobj || vobj == this.vobj)
156 updateRegistryEntry(jvobj, vobj);
161 * update the vobj and jvobj references and the registry entry for this
162 * datastore object called by bindjvvobj and replacejvobjmapping
164 private void updateRegistryEntry(Object jvobj, Vobject vobj)
166 if (this.jvobj != null && this.vobj != null)
168 Cache.log.debug("updating dsobj registry. ("
169 + this.getClass().getName() + ")");
173 dsReg.registerDsObj(this);
177 * replaces oldjvobject with newjvobject in the Jalview Object <> VorbaID
178 * binding tables note: originally taken verbatim from
179 * jalview.io.VamsasAppDatastore with added call to updateRegistryEntry
183 * (may be null to forget the oldjvobject's document mapping)
186 protected void replaceJvObjMapping(Object oldjvobject, Object newjvobject)
188 Object vobject = jv2vobj.remove(oldjvobject);
192 "IMPLEMENTATION ERROR: old jalview object is not bound ! ("
193 + oldjvobject + ")");
195 if (newjvobject != null)
197 jv2vobj.put(newjvobject, vobject);
198 vobj2jv.put(vobject, newjvobject);
199 updateRegistryEntry(newjvobject, vobj);
203 public DatastoreItem()
208 public DatastoreItem(VamsasAppDatastore datastore)
211 initDatastoreItem(datastore);
212 // TODO Auto-generated constructor stub
216 * construct and initialise datastore object and retrieve object bound to
217 * vobj2 and validate it against boundType
223 public DatastoreItem(VamsasAppDatastore datastore2, Vobject vobj2,
228 jvobj = getvObj2jv(vobj2);
230 if (jvobj != null && !(boundType.isAssignableFrom(jvobj.getClass())))
232 throw new Error("Implementation Error: Vamsas Document Class "
233 + vobj.getClass() + " should bind to a " + boundType
234 + " (found a " + jvobj.getClass() + ")");
236 dsReg.registerDsObj(this);
240 * construct and initialise datastore object and retrieve document object
241 * bound to Jalview object jvobj2 and validate it against boundType
248 * - the document object class that the bound object should be
251 public DatastoreItem(VamsasAppDatastore datastore2, Object jvobj2,
257 vobj = getjv2vObj(jvobj);
258 if (vobj != null && !(boundToType.isAssignableFrom(vobj.getClass())))
260 throw new Error("Implementation Error: Jalview Class "
261 + jvobj2.getClass() + " should bind to a " + boundToType
262 + " (found a " + vobj.getClass() + ")");
264 dsReg.registerDsObj(this);
268 * create a new vobj to be added to the document for the jalview object jvobj
269 * (jvobj!=null, vobj==null)
271 public abstract void addToDocument();
274 * handle a conflict where both an existing vobj has been updated and a local
275 * jalview object has been updated. This method is only called from doSync,
276 * when an incoming update from the vamsas session conflicts with local
277 * modifications made by the Jalview user. (jvobj!=null, vobj!=null)
279 public abstract void conflict();
282 * update an existing vobj in the document with the data and settings from
283 * jvobj (jvobj!=null, vobj!=null)
285 public abstract void updateToDoc();
288 * update the local jalview object with the data from an existing vobj in the
289 * document (jvobj!=null, vobj!=null)
291 public abstract void updateFromDoc();
294 * create a new local jvobj bound to the vobj in the document. (jvobj==null,
297 public abstract void addFromDocument();
299 boolean addtodoc = false, conflicted = false, updated = false,
300 addfromdoc = false, success = false;
302 private boolean updatedtodoc;
304 private boolean updatedfromdoc;
307 * Sync jalview to document. Enact addToDocument, conflict or update dependent
308 * on existence of a vobj bound to the local jvobj.
310 protected void doSync()
312 dsReg.registerDsObj(this);
315 log.debug("adding new vobject to document.");
321 if (vobj.isUpdated())
323 log.debug("Handling update conflict for existing bound vobject.");
329 log.debug("updating existing vobject in document.");
334 // no exceptions were encountered...
339 * Update jalview from document. enact addFromDocument if no local jvobj
340 * exists, or update iff jvobj exists and the vobj.isUpdated() flag is set.
342 protected void doJvUpdate()
344 dsReg.registerDsObj(this);
347 log.debug("adding new vobject to Jalview from Document");
353 if (vobj.isUpdated())
355 log.debug("updating Jalview from existing bound vObject");
356 updatedfromdoc = true;
362 VamsasAppDatastore datastore = null;
365 * object in vamsas document
367 protected Vobject vobj = null;
370 * local jalview object
372 protected Object jvobj = null;
374 protected DatastoreRegistry dsReg;
376 public void initDatastoreItem(VamsasAppDatastore ds)
379 dsReg = ds.getDatastoreRegisty();
380 initDatastoreItem(ds.getProvEntry(), ds.getClientDocument(),
381 ds.getVamsasObjectBinding(), ds.getJvObjectBinding());
384 private void initDatastoreItem(Entry provEntry, IClientDocument cdoc,
385 Hashtable vobj2jv, IdentityHashMap jv2vobj)
387 this.provEntry = provEntry;
389 this.vobj2jv = vobj2jv;
390 this.jv2vobj = jv2vobj;
393 protected boolean isModifiable(String modifiable)
395 return modifiable == null; // TODO: USE VAMSAS LIBRARY OBJECT LOCK METHODS)
398 protected Vector getjv2vObjs(Vector alsq)
400 Vector vObjs = new Vector();
401 Enumeration elm = alsq.elements();
402 while (elm.hasMoreElements())
404 vObjs.addElement(getjv2vObj(elm.nextElement()));
411 * get start<end range of segment, adjusting for inclusivity flag and
415 * @param ensureDirection
416 * when true - always ensure start is less than end.
417 * @return int[] { start, end, direction} where direction==1 for range running
420 public int[] getSegRange(Seg visSeg, boolean ensureDirection)
422 boolean incl = visSeg.getInclusive();
423 // adjust for inclusive flag.
424 int pol = (visSeg.getStart() <= visSeg.getEnd()) ? 1 : -1; // polarity of
426 int start = visSeg.getStart() + (incl ? 0 : pol);
427 int end = visSeg.getEnd() + (incl ? 0 : -pol);
428 if (ensureDirection && pol == -1)
430 // jalview doesn't deal with inverted ranges, yet.
436 { start, end, pol < 0 ? 1 : 0 };
442 protected jalview.datamodel.Provenance getJalviewProvenance(
445 // TODO: fix App and Action entries and check use of provenance in jalview.
446 jalview.datamodel.Provenance jprov = new jalview.datamodel.Provenance();
447 for (int i = 0; i < prov.getEntryCount(); i++)
449 jprov.addEntry(prov.getEntry(i).getUser(), prov.getEntry(i)
450 .getAction(), prov.getEntry(i).getDate(), prov.getEntry(i)
459 * @return default initial provenance list for a Jalview created vamsas
462 Provenance dummyProvenance()
464 return dummyProvenance(null);
467 protected Entry dummyPEntry(String action)
469 Entry entry = new Entry();
470 entry.setApp(this.provEntry.getApp());
473 entry.setAction(action);
477 entry.setAction("created.");
479 entry.setDate(new java.util.Date());
480 entry.setUser(this.provEntry.getUser());
484 protected Provenance dummyProvenance(String action)
486 Provenance prov = new Provenance();
487 prov.addEntry(dummyPEntry(action));
491 protected void addProvenance(Provenance p, String action)
493 p.addEntry(dummyPEntry(action));
497 * @return true if jalview was being updated from the vamsas document
499 public boolean isTojalview()
505 * @return true if addToDocument() was called.
507 public boolean isAddtodoc()
513 * @return true if conflict() was called
515 public boolean isConflicted()
521 * @return true if updateFromDoc() was called
523 public boolean isUpdatedFromDoc()
525 return updatedfromdoc;
529 * @return true if updateToDoc() was called
531 public boolean isUpdatedToDoc()
537 * @return true if addFromDocument() was called.
539 public boolean isAddfromdoc()
545 * @return true if object sync logic completed normally.
547 public boolean isSuccess()
555 public Vobject getVobj()
563 public Object getJvobj()
568 public boolean docWasUpdated()
570 return (this.addtodoc || this.updated) && this.success;
573 public boolean jvWasUpdated()
575 return (success); // TODO : Implement this properly!