2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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/>.
18 package jalview.io.vamsas;
20 import jalview.bin.Cache;
21 import jalview.io.VamsasAppDatastore;
23 import java.util.Enumeration;
24 import java.util.Hashtable;
25 import java.util.IdentityHashMap;
26 import java.util.Vector;
28 import uk.ac.vamsas.client.IClientDocument;
29 import uk.ac.vamsas.client.Vobject;
30 import uk.ac.vamsas.client.VorbaId;
31 import uk.ac.vamsas.objects.core.Entry;
32 import uk.ac.vamsas.objects.core.Provenance;
33 import uk.ac.vamsas.objects.core.Seg;
36 * Holds all the common machinery for binding objects to vamsas objects
41 public abstract class DatastoreItem
46 Entry provEntry = null;
52 IdentityHashMap jv2vobj;
54 boolean tojalview = false;
59 protected static org.apache.log4j.Logger log = org.apache.log4j.Logger
60 .getLogger(DatastoreItem.class);
63 * note: this is taken verbatim from jalview.io.VamsasAppDatastore
65 * @return the Vobject bound to Jalview datamodel object
67 protected Vobject getjv2vObj(Object jvobj)
69 if (jv2vobj.containsKey(jvobj))
71 return cdoc.getObject((VorbaId) jv2vobj.get(jvobj));
73 if (Cache.log.isDebugEnabled())
75 Cache.log.debug("Returning null VorbaID binding for jalview object "
84 * @return Jalview datamodel object bound to the vamsas document object
86 protected Object getvObj2jv(uk.ac.vamsas.client.Vobject vobj)
90 VorbaId id = vobj.getVorbaId();
93 id = cdoc.registerObject(vobj);
95 .debug("Registering new object and returning null for getvObj2jv");
98 if (vobj2jv.containsKey(vobj.getVorbaId()))
100 return vobj2jv.get(vobj.getVorbaId());
106 * note: this is taken verbatim from jalview.io.VamsasAppDatastore with added
107 * call to updateRegistryEntry
112 protected void bindjvvobj(Object jvobj, uk.ac.vamsas.client.Vobject vobj)
114 VorbaId id = vobj.getVorbaId();
117 id = cdoc.registerObject(vobj);
118 if (id == null || vobj.getVorbaId() == null
119 || cdoc.getObject(id) != vobj)
121 Cache.log.error("Failed to get id for "
122 + (vobj.isRegisterable() ? "registerable"
123 : "unregisterable") + " object " + vobj);
126 if (vobj2jv.containsKey(vobj.getVorbaId())
127 && !(vobj2jv.get(vobj.getVorbaId())).equals(jvobj))
130 "Warning? Overwriting existing vamsas id binding for "
131 + vobj.getVorbaId(), new Exception(
132 "Overwriting vamsas id binding."));
134 else if (jv2vobj.containsKey(jvobj)
135 && !((VorbaId) jv2vobj.get(jvobj)).equals(vobj.getVorbaId()))
138 "Warning? Overwriting existing jalview object binding for "
139 + jvobj, new Exception(
140 "Overwriting jalview object binding."));
143 * Cache.log.error("Attempt to make conflicting object binding! "+vobj+" id "
144 * +vobj.getVorbaId()+" already bound to "+getvObj2jv(vobj)+" and "+jvobj+"
145 * already bound to "+getjv2vObj(jvobj),new Exception("Excessive call to
148 // we just update the hash's regardless!
149 Cache.log.debug("Binding " + vobj.getVorbaId() + " to " + jvobj);
150 vobj2jv.put(vobj.getVorbaId(), jvobj);
151 // JBPNote - better implementing a hybrid invertible hash.
152 jv2vobj.put(jvobj, vobj.getVorbaId());
153 if (jvobj == this.jvobj || vobj == this.vobj)
155 updateRegistryEntry(jvobj, vobj);
160 * update the vobj and jvobj references and the registry entry for this
161 * datastore object called by bindjvvobj and replacejvobjmapping
163 private void updateRegistryEntry(Object jvobj, Vobject vobj)
165 if (this.jvobj != null && this.vobj != null)
167 Cache.log.debug("updating dsobj registry. ("
168 + this.getClass().getName() + ")");
172 dsReg.registerDsObj(this);
176 * replaces oldjvobject with newjvobject in the Jalview Object <> VorbaID
177 * binding tables note: originally taken verbatim from
178 * jalview.io.VamsasAppDatastore with added call to updateRegistryEntry
182 * (may be null to forget the oldjvobject's document mapping)
185 protected void replaceJvObjMapping(Object oldjvobject, Object newjvobject)
187 Object vobject = jv2vobj.remove(oldjvobject);
191 "IMPLEMENTATION ERROR: old jalview object is not bound ! ("
192 + oldjvobject + ")");
194 if (newjvobject != null)
196 jv2vobj.put(newjvobject, vobject);
197 vobj2jv.put(vobject, newjvobject);
198 updateRegistryEntry(newjvobject, vobj);
202 public DatastoreItem()
207 public DatastoreItem(VamsasAppDatastore datastore)
210 initDatastoreItem(datastore);
211 // TODO Auto-generated constructor stub
215 * construct and initialise datastore object and retrieve object bound to
216 * vobj2 and validate it against boundType
222 public DatastoreItem(VamsasAppDatastore datastore2, Vobject vobj2,
227 jvobj = getvObj2jv(vobj2);
229 if (jvobj != null && !(boundType.isAssignableFrom(jvobj.getClass())))
231 throw new Error("Implementation Error: Vamsas Document Class "
232 + vobj.getClass() + " should bind to a " + boundType
233 + " (found a " + jvobj.getClass() + ")");
235 dsReg.registerDsObj(this);
239 * construct and initialise datastore object and retrieve document object
240 * bound to Jalview object jvobj2 and validate it against boundType
247 * - the document object class that the bound object should be
250 public DatastoreItem(VamsasAppDatastore datastore2, Object jvobj2,
256 vobj = getjv2vObj(jvobj);
257 if (vobj != null && !(boundToType.isAssignableFrom(vobj.getClass())))
259 throw new Error("Implementation Error: Jalview Class "
260 + jvobj2.getClass() + " should bind to a " + boundToType
261 + " (found a " + vobj.getClass() + ")");
263 dsReg.registerDsObj(this);
267 * create a new vobj to be added to the document for the jalview object jvobj
268 * (jvobj!=null, vobj==null)
270 public abstract void addToDocument();
273 * handle a conflict where both an existing vobj has been updated and a local
274 * jalview object has been updated. This method is only called from doSync,
275 * when an incoming update from the vamsas session conflicts with local
276 * modifications made by the Jalview user. (jvobj!=null, vobj!=null)
278 public abstract void conflict();
281 * update an existing vobj in the document with the data and settings from
282 * jvobj (jvobj!=null, vobj!=null)
284 public abstract void updateToDoc();
287 * update the local jalview object with the data from an existing vobj in the
288 * document (jvobj!=null, vobj!=null)
290 public abstract void updateFromDoc();
293 * create a new local jvobj bound to the vobj in the document. (jvobj==null,
296 public abstract void addFromDocument();
298 boolean addtodoc = false, conflicted = false, updated = false,
299 addfromdoc = false, success = false;
301 private boolean updatedtodoc;
303 private boolean updatedfromdoc;
306 * Sync jalview to document. Enact addToDocument, conflict or update dependent
307 * on existence of a vobj bound to the local jvobj.
309 protected void doSync()
311 dsReg.registerDsObj(this);
314 log.debug("adding new vobject to document.");
320 if (vobj.isUpdated())
322 log.debug("Handling update conflict for existing bound vobject.");
328 log.debug("updating existing vobject in document.");
333 // no exceptions were encountered...
338 * Update jalview from document. enact addFromDocument if no local jvobj
339 * exists, or update iff jvobj exists and the vobj.isUpdated() flag is set.
341 protected void doJvUpdate()
343 dsReg.registerDsObj(this);
346 log.debug("adding new vobject to Jalview from Document");
352 if (vobj.isUpdated())
354 log.debug("updating Jalview from existing bound vObject");
355 updatedfromdoc = true;
361 VamsasAppDatastore datastore = null;
364 * object in vamsas document
366 protected Vobject vobj = null;
369 * local jalview object
371 protected Object jvobj = null;
373 protected DatastoreRegistry dsReg;
375 public void initDatastoreItem(VamsasAppDatastore ds)
378 dsReg = ds.getDatastoreRegisty();
379 initDatastoreItem(ds.getProvEntry(), ds.getClientDocument(),
380 ds.getVamsasObjectBinding(), ds.getJvObjectBinding());
383 private void initDatastoreItem(Entry provEntry, IClientDocument cdoc,
384 Hashtable vobj2jv, IdentityHashMap jv2vobj)
386 this.provEntry = provEntry;
388 this.vobj2jv = vobj2jv;
389 this.jv2vobj = jv2vobj;
392 protected boolean isModifiable(String modifiable)
394 return modifiable == null; // TODO: USE VAMSAS LIBRARY OBJECT LOCK METHODS)
397 protected Vector getjv2vObjs(Vector alsq)
399 Vector vObjs = new Vector();
400 Enumeration elm = alsq.elements();
401 while (elm.hasMoreElements())
403 vObjs.addElement(getjv2vObj(elm.nextElement()));
410 * get start<end range of segment, adjusting for inclusivity flag and
414 * @param ensureDirection
415 * when true - always ensure start is less than end.
416 * @return int[] { start, end, direction} where direction==1 for range running
419 public int[] getSegRange(Seg visSeg, boolean ensureDirection)
421 boolean incl = visSeg.getInclusive();
422 // adjust for inclusive flag.
423 int pol = (visSeg.getStart() <= visSeg.getEnd()) ? 1 : -1; // polarity of
425 int start = visSeg.getStart() + (incl ? 0 : pol);
426 int end = visSeg.getEnd() + (incl ? 0 : -pol);
427 if (ensureDirection && pol == -1)
429 // jalview doesn't deal with inverted ranges, yet.
435 { start, end, pol < 0 ? 1 : 0 };
441 protected jalview.datamodel.Provenance getJalviewProvenance(
444 // TODO: fix App and Action entries and check use of provenance in jalview.
445 jalview.datamodel.Provenance jprov = new jalview.datamodel.Provenance();
446 for (int i = 0; i < prov.getEntryCount(); i++)
448 jprov.addEntry(prov.getEntry(i).getUser(), prov.getEntry(i)
449 .getAction(), prov.getEntry(i).getDate(), prov.getEntry(i)
458 * @return default initial provenance list for a Jalview created vamsas
461 Provenance dummyProvenance()
463 return dummyProvenance(null);
466 protected Entry dummyPEntry(String action)
468 Entry entry = new Entry();
469 entry.setApp(this.provEntry.getApp());
472 entry.setAction(action);
476 entry.setAction("created.");
478 entry.setDate(new java.util.Date());
479 entry.setUser(this.provEntry.getUser());
483 protected Provenance dummyProvenance(String action)
485 Provenance prov = new Provenance();
486 prov.addEntry(dummyPEntry(action));
490 protected void addProvenance(Provenance p, String action)
492 p.addEntry(dummyPEntry(action));
496 * @return true if jalview was being updated from the vamsas document
498 public boolean isTojalview()
504 * @return true if addToDocument() was called.
506 public boolean isAddtodoc()
512 * @return true if conflict() was called
514 public boolean isConflicted()
520 * @return true if updateFromDoc() was called
522 public boolean isUpdatedFromDoc()
524 return updatedfromdoc;
528 * @return true if updateToDoc() was called
530 public boolean isUpdatedToDoc()
536 * @return true if addFromDocument() was called.
538 public boolean isAddfromdoc()
544 * @return true if object sync logic completed normally.
546 public boolean isSuccess()
554 public Vobject getVobj()
562 public Object getJvobj()
567 public boolean docWasUpdated()
569 return (this.addtodoc || this.updated) && this.success;
572 public boolean jvWasUpdated()
574 return (success); // TODO : Implement this properly!