2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.io.vamsas;
23 import jalview.bin.Cache;
24 import jalview.io.VamsasAppDatastore;
26 import java.util.Enumeration;
27 import java.util.Hashtable;
28 import java.util.IdentityHashMap;
29 import java.util.Vector;
31 import uk.ac.vamsas.client.IClientDocument;
32 import uk.ac.vamsas.client.Vobject;
33 import uk.ac.vamsas.client.VorbaId;
34 import uk.ac.vamsas.objects.core.Entry;
35 import uk.ac.vamsas.objects.core.Provenance;
36 import uk.ac.vamsas.objects.core.Seg;
39 * Holds all the common machinery for binding objects to vamsas objects
44 public abstract class DatastoreItem
49 Entry provEntry = null;
55 IdentityHashMap jv2vobj;
57 boolean tojalview = false;
62 protected static org.apache.log4j.Logger log = org.apache.log4j.Logger
63 .getLogger(DatastoreItem.class);
66 * note: this is taken verbatim from jalview.io.VamsasAppDatastore
68 * @return the Vobject bound to Jalview datamodel object
70 protected Vobject getjv2vObj(Object jvobj)
72 if (jv2vobj.containsKey(jvobj))
74 return cdoc.getObject((VorbaId) jv2vobj.get(jvobj));
76 if (Cache.log.isDebugEnabled())
78 Cache.log.debug("Returning null VorbaID binding for jalview object "
87 * @return Jalview datamodel object bound to the vamsas document object
89 protected Object getvObj2jv(uk.ac.vamsas.client.Vobject vobj)
93 VorbaId id = vobj.getVorbaId();
96 id = cdoc.registerObject(vobj);
98 .debug("Registering new object and returning null for getvObj2jv");
101 if (vobj2jv.containsKey(vobj.getVorbaId()))
103 return vobj2jv.get(vobj.getVorbaId());
109 * note: this is taken verbatim from jalview.io.VamsasAppDatastore with added
110 * call to updateRegistryEntry
115 protected void bindjvvobj(Object jvobj, uk.ac.vamsas.client.Vobject vobj)
117 VorbaId id = vobj.getVorbaId();
120 id = cdoc.registerObject(vobj);
121 if (id == null || vobj.getVorbaId() == null
122 || cdoc.getObject(id) != vobj)
124 Cache.log.error("Failed to get id for "
125 + (vobj.isRegisterable() ? "registerable"
126 : "unregisterable") + " object " + vobj);
129 if (vobj2jv.containsKey(vobj.getVorbaId())
130 && !(vobj2jv.get(vobj.getVorbaId())).equals(jvobj))
133 "Warning? Overwriting existing vamsas id binding for "
134 + vobj.getVorbaId(), new Exception(
135 "Overwriting vamsas id binding."));
137 else if (jv2vobj.containsKey(jvobj)
138 && !((VorbaId) jv2vobj.get(jvobj)).equals(vobj.getVorbaId()))
141 "Warning? Overwriting existing jalview object binding for "
142 + jvobj, new Exception(
143 "Overwriting jalview object binding."));
146 * Cache.log.error("Attempt to make conflicting object binding! "+vobj+" id "
147 * +vobj.getVorbaId()+" already bound to "+getvObj2jv(vobj)+" and "+jvobj+"
148 * already bound to "+getjv2vObj(jvobj),new Exception("Excessive call to
151 // we just update the hash's regardless!
152 Cache.log.debug("Binding " + vobj.getVorbaId() + " to " + jvobj);
153 vobj2jv.put(vobj.getVorbaId(), jvobj);
154 // JBPNote - better implementing a hybrid invertible hash.
155 jv2vobj.put(jvobj, vobj.getVorbaId());
156 if (jvobj == this.jvobj || vobj == this.vobj)
158 updateRegistryEntry(jvobj, vobj);
163 * update the vobj and jvobj references and the registry entry for this
164 * datastore object called by bindjvvobj and replacejvobjmapping
166 private void updateRegistryEntry(Object jvobj, Vobject vobj)
168 if (this.jvobj != null && this.vobj != null)
170 Cache.log.debug("updating dsobj registry. ("
171 + this.getClass().getName() + ")");
175 dsReg.registerDsObj(this);
179 * replaces oldjvobject with newjvobject in the Jalview Object <> VorbaID
180 * binding tables note: originally taken verbatim from
181 * jalview.io.VamsasAppDatastore with added call to updateRegistryEntry
185 * (may be null to forget the oldjvobject's document mapping)
188 protected void replaceJvObjMapping(Object oldjvobject, Object newjvobject)
190 Object vobject = jv2vobj.remove(oldjvobject);
194 "IMPLEMENTATION ERROR: old jalview object is not bound ! ("
195 + oldjvobject + ")");
197 if (newjvobject != null)
199 jv2vobj.put(newjvobject, vobject);
200 vobj2jv.put(vobject, newjvobject);
201 updateRegistryEntry(newjvobject, vobj);
205 public DatastoreItem()
210 public DatastoreItem(VamsasAppDatastore datastore)
213 initDatastoreItem(datastore);
214 // TODO Auto-generated constructor stub
218 * construct and initialise datastore object and retrieve object bound to
219 * vobj2 and validate it against boundType
225 public DatastoreItem(VamsasAppDatastore datastore2, Vobject vobj2,
230 jvobj = getvObj2jv(vobj2);
232 if (jvobj != null && !(boundType.isAssignableFrom(jvobj.getClass())))
234 throw new Error("Implementation Error: Vamsas Document Class "
235 + vobj.getClass() + " should bind to a " + boundType
236 + " (found a " + jvobj.getClass() + ")");
238 dsReg.registerDsObj(this);
242 * construct and initialise datastore object and retrieve document object
243 * bound to Jalview object jvobj2 and validate it against boundType
250 * - the document object class that the bound object should be
253 public DatastoreItem(VamsasAppDatastore datastore2, Object jvobj2,
259 vobj = getjv2vObj(jvobj);
260 if (vobj != null && !(boundToType.isAssignableFrom(vobj.getClass())))
262 throw new Error("Implementation Error: Jalview Class "
263 + jvobj2.getClass() + " should bind to a " + boundToType
264 + " (found a " + vobj.getClass() + ")");
266 dsReg.registerDsObj(this);
270 * create a new vobj to be added to the document for the jalview object jvobj
271 * (jvobj!=null, vobj==null)
273 public abstract void addToDocument();
276 * handle a conflict where both an existing vobj has been updated and a local
277 * jalview object has been updated. This method is only called from doSync,
278 * when an incoming update from the vamsas session conflicts with local
279 * modifications made by the Jalview user. (jvobj!=null, vobj!=null)
281 public abstract void conflict();
284 * update an existing vobj in the document with the data and settings from
285 * jvobj (jvobj!=null, vobj!=null)
287 public abstract void updateToDoc();
290 * update the local jalview object with the data from an existing vobj in the
291 * document (jvobj!=null, vobj!=null)
293 public abstract void updateFromDoc();
296 * create a new local jvobj bound to the vobj in the document. (jvobj==null,
299 public abstract void addFromDocument();
301 boolean addtodoc = false, conflicted = false, updated = false,
302 addfromdoc = false, success = false;
304 private boolean updatedtodoc;
306 private boolean updatedfromdoc;
309 * Sync jalview to document. Enact addToDocument, conflict or update dependent
310 * on existence of a vobj bound to the local jvobj.
312 protected void doSync()
314 dsReg.registerDsObj(this);
317 log.debug("adding new vobject to document.");
323 if (vobj.isUpdated())
325 log.debug("Handling update conflict for existing bound vobject.");
331 log.debug("updating existing vobject in document.");
336 // no exceptions were encountered...
341 * Update jalview from document. enact addFromDocument if no local jvobj
342 * exists, or update iff jvobj exists and the vobj.isUpdated() flag is set.
344 protected void doJvUpdate()
346 dsReg.registerDsObj(this);
349 log.debug("adding new vobject to Jalview from Document");
355 if (vobj.isUpdated())
357 log.debug("updating Jalview from existing bound vObject");
358 updatedfromdoc = true;
364 VamsasAppDatastore datastore = null;
367 * object in vamsas document
369 protected Vobject vobj = null;
372 * local jalview object
374 protected Object jvobj = null;
376 protected DatastoreRegistry dsReg;
378 public void initDatastoreItem(VamsasAppDatastore ds)
381 dsReg = ds.getDatastoreRegisty();
382 initDatastoreItem(ds.getProvEntry(), ds.getClientDocument(),
383 ds.getVamsasObjectBinding(), ds.getJvObjectBinding());
386 private void initDatastoreItem(Entry provEntry, IClientDocument cdoc,
387 Hashtable vobj2jv, IdentityHashMap jv2vobj)
389 this.provEntry = provEntry;
391 this.vobj2jv = vobj2jv;
392 this.jv2vobj = jv2vobj;
395 protected boolean isModifiable(String modifiable)
397 return modifiable == null; // TODO: USE VAMSAS LIBRARY OBJECT LOCK METHODS)
400 protected Vector getjv2vObjs(Vector alsq)
402 Vector vObjs = new Vector();
403 Enumeration elm = alsq.elements();
404 while (elm.hasMoreElements())
406 vObjs.addElement(getjv2vObj(elm.nextElement()));
413 * get start<end range of segment, adjusting for inclusivity flag and
417 * @param ensureDirection
418 * when true - always ensure start is less than end.
419 * @return int[] { start, end, direction} where direction==1 for range running
422 public int[] getSegRange(Seg visSeg, boolean ensureDirection)
424 boolean incl = visSeg.getInclusive();
425 // adjust for inclusive flag.
426 int pol = (visSeg.getStart() <= visSeg.getEnd()) ? 1 : -1; // polarity of
428 int start = visSeg.getStart() + (incl ? 0 : pol);
429 int end = visSeg.getEnd() + (incl ? 0 : -pol);
430 if (ensureDirection && pol == -1)
432 // jalview doesn't deal with inverted ranges, yet.
438 { start, end, pol < 0 ? 1 : 0 };
444 protected jalview.datamodel.Provenance getJalviewProvenance(
447 // TODO: fix App and Action entries and check use of provenance in jalview.
448 jalview.datamodel.Provenance jprov = new jalview.datamodel.Provenance();
449 for (int i = 0; i < prov.getEntryCount(); i++)
451 jprov.addEntry(prov.getEntry(i).getUser(), prov.getEntry(i)
452 .getAction(), prov.getEntry(i).getDate(), prov.getEntry(i)
461 * @return default initial provenance list for a Jalview created vamsas
464 Provenance dummyProvenance()
466 return dummyProvenance(null);
469 protected Entry dummyPEntry(String action)
471 Entry entry = new Entry();
472 entry.setApp(this.provEntry.getApp());
475 entry.setAction(action);
479 entry.setAction("created.");
481 entry.setDate(new java.util.Date());
482 entry.setUser(this.provEntry.getUser());
486 protected Provenance dummyProvenance(String action)
488 Provenance prov = new Provenance();
489 prov.addEntry(dummyPEntry(action));
493 protected void addProvenance(Provenance p, String action)
495 p.addEntry(dummyPEntry(action));
499 * @return true if jalview was being updated from the vamsas document
501 public boolean isTojalview()
507 * @return true if addToDocument() was called.
509 public boolean isAddtodoc()
515 * @return true if conflict() was called
517 public boolean isConflicted()
523 * @return true if updateFromDoc() was called
525 public boolean isUpdatedFromDoc()
527 return updatedfromdoc;
531 * @return true if updateToDoc() was called
533 public boolean isUpdatedToDoc()
539 * @return true if addFromDocument() was called.
541 public boolean isAddfromdoc()
547 * @return true if object sync logic completed normally.
549 public boolean isSuccess()
557 public Vobject getVobj()
565 public Object getJvobj()
570 public boolean docWasUpdated()
572 return (this.addtodoc || this.updated) && this.success;
575 public boolean jvWasUpdated()
577 return (success); // TODO : Implement this properly!