2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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 java.util.Enumeration;
24 import java.util.Hashtable;
25 import java.util.IdentityHashMap;
26 import java.util.Vector;
28 import org.apache.logging.log4j.LogManager;
29 import org.apache.logging.log4j.Logger;
31 import jalview.bin.Cache;
32 import jalview.io.VamsasAppDatastore;
33 import jalview.util.MessageManager;
34 import uk.ac.vamsas.client.IClientDocument;
35 import uk.ac.vamsas.client.Vobject;
36 import uk.ac.vamsas.client.VorbaId;
37 import uk.ac.vamsas.objects.core.Entry;
38 import uk.ac.vamsas.objects.core.Provenance;
39 import uk.ac.vamsas.objects.core.Seg;
42 * Holds all the common machinery for binding objects to vamsas objects
47 public abstract class DatastoreItem
52 Entry provEntry = null;
58 IdentityHashMap jv2vobj;
60 boolean tojalview = false;
65 protected static Logger log = LogManager.getLogger(DatastoreItem.class);
68 * note: this is taken verbatim from jalview.io.VamsasAppDatastore
70 * @return the Vobject bound to Jalview datamodel object
72 protected Vobject getjv2vObj(Object jvobj)
74 if (jv2vobj.containsKey(jvobj))
76 return cdoc.getObject((VorbaId) jv2vobj.get(jvobj));
78 if (Cache.log.isDebugEnabled())
81 "Returning null VorbaID binding for jalview object " + jvobj);
89 * @return Jalview datamodel object bound to the vamsas document object
91 protected Object getvObj2jv(uk.ac.vamsas.client.Vobject vobj)
95 VorbaId id = vobj.getVorbaId();
98 id = cdoc.registerObject(vobj);
100 "Registering new object and returning null for getvObj2jv");
103 if (vobj2jv.containsKey(vobj.getVorbaId()))
105 return vobj2jv.get(vobj.getVorbaId());
111 * note: this is taken verbatim from jalview.io.VamsasAppDatastore with added
112 * call to updateRegistryEntry
117 protected void bindjvvobj(Object jvobj, uk.ac.vamsas.client.Vobject vobj)
119 VorbaId id = vobj.getVorbaId();
122 id = cdoc.registerObject(vobj);
123 if (id == null || vobj.getVorbaId() == null
124 || cdoc.getObject(id) != vobj)
126 Cache.log.error("Failed to get id for "
127 + (vobj.isRegisterable() ? "registerable"
129 + " object " + vobj);
132 if (vobj2jv.containsKey(vobj.getVorbaId())
133 && !(vobj2jv.get(vobj.getVorbaId())).equals(jvobj))
136 "Warning? Overwriting existing vamsas id binding for "
138 new Exception(MessageManager.getString(
139 "exception.overwriting_vamsas_id_binding")));
141 else if (jv2vobj.containsKey(jvobj)
142 && !((VorbaId) jv2vobj.get(jvobj)).equals(vobj.getVorbaId()))
145 "Warning? Overwriting existing jalview object binding for "
147 new Exception(MessageManager.getString(
148 "exception.overwriting_jalview_id_binding")));
151 * Cache.log.error("Attempt to make conflicting object binding! "+vobj+" id "
152 * +vobj.getVorbaId()+" already bound to "+getvObj2jv(vobj)+" and "+jvobj+"
153 * already bound to "+getjv2vObj(jvobj),new Exception("Excessive call to
156 // we just update the hash's regardless!
157 Cache.log.debug("Binding " + vobj.getVorbaId() + " to " + jvobj);
158 vobj2jv.put(vobj.getVorbaId(), jvobj);
159 // JBPNote - better implementing a hybrid invertible hash.
160 jv2vobj.put(jvobj, vobj.getVorbaId());
161 if (jvobj == this.jvobj || vobj == this.vobj)
163 updateRegistryEntry(jvobj, vobj);
168 * update the vobj and jvobj references and the registry entry for this
169 * datastore object called by bindjvvobj and replacejvobjmapping
171 private void updateRegistryEntry(Object jvobj, Vobject vobj)
173 if (this.jvobj != null && this.vobj != null)
175 Cache.log.debug("updating dsobj registry. ("
176 + this.getClass().getName() + ")");
180 dsReg.registerDsObj(this);
184 * replaces oldjvobject with newjvobject in the Jalview Object <> VorbaID
185 * binding tables note: originally taken verbatim from
186 * jalview.io.VamsasAppDatastore with added call to updateRegistryEntry
190 * (may be null to forget the oldjvobject's document mapping)
193 protected void replaceJvObjMapping(Object oldjvobject, Object newjvobject)
195 Object vobject = jv2vobj.remove(oldjvobject);
198 throw new Error(MessageManager.formatMessage(
199 "error.implementation_error_old_jalview_object_not_bound",
201 { oldjvobject.toString() }));
203 if (newjvobject != null)
205 jv2vobj.put(newjvobject, vobject);
206 vobj2jv.put(vobject, newjvobject);
207 updateRegistryEntry(newjvobject, vobj);
211 public DatastoreItem()
216 public DatastoreItem(VamsasAppDatastore datastore)
219 initDatastoreItem(datastore);
220 // TODO Auto-generated constructor stub
224 * construct and initialise datastore object and retrieve object bound to
225 * vobj2 and validate it against boundType
231 public DatastoreItem(VamsasAppDatastore datastore2, Vobject vobj2,
236 jvobj = getvObj2jv(vobj2);
238 if (jvobj != null && !(boundType.isAssignableFrom(jvobj.getClass())))
240 throw new Error(MessageManager.formatMessage(
241 "error.implementation_error_vamsas_doc_class_should_bind_to_type",
243 { vobj.getClass().toString(), boundType.toString(),
244 jvobj.getClass().toString() }));
246 dsReg.registerDsObj(this);
250 * construct and initialise datastore object and retrieve document object
251 * bound to Jalview object jvobj2 and validate it against boundType
258 * - the document object class that the bound object should be
261 public DatastoreItem(VamsasAppDatastore datastore2, Object jvobj2,
267 vobj = getjv2vObj(jvobj);
268 if (vobj != null && !(boundToType.isAssignableFrom(vobj.getClass())))
270 throw new Error(MessageManager.formatMessage(
271 "error.implementation_error_vamsas_doc_class_should_bind_to_type",
273 { jvobj2.getClass().toString(), boundToType.toString(),
274 vobj.getClass().toString() }));
276 dsReg.registerDsObj(this);
280 * create a new vobj to be added to the document for the jalview object jvobj
281 * (jvobj!=null, vobj==null)
283 public abstract void addToDocument();
286 * handle a conflict where both an existing vobj has been updated and a local
287 * jalview object has been updated. This method is only called from doSync,
288 * when an incoming update from the vamsas session conflicts with local
289 * modifications made by the Jalview user. (jvobj!=null, vobj!=null)
291 public abstract void conflict();
294 * update an existing vobj in the document with the data and settings from
295 * jvobj (jvobj!=null, vobj!=null)
297 public abstract void updateToDoc();
300 * update the local jalview object with the data from an existing vobj in the
301 * document (jvobj!=null, vobj!=null)
303 public abstract void updateFromDoc();
306 * create a new local jvobj bound to the vobj in the document. (jvobj==null,
309 public abstract void addFromDocument();
311 boolean addtodoc = false, conflicted = false, updated = false,
312 addfromdoc = false, success = false;
314 private boolean updatedtodoc;
316 private boolean updatedfromdoc;
319 * Sync jalview to document. Enact addToDocument, conflict or update dependent
320 * on existence of a vobj bound to the local jvobj.
322 protected void doSync()
324 dsReg.registerDsObj(this);
327 log.debug("adding new vobject to document.");
333 if (vobj.isUpdated())
335 log.debug("Handling update conflict for existing bound vobject.");
341 log.debug("updating existing vobject in document.");
346 // no exceptions were encountered...
351 * Update jalview from document. enact addFromDocument if no local jvobj
352 * exists, or update iff jvobj exists and the vobj.isUpdated() flag is set.
354 protected void doJvUpdate()
356 dsReg.registerDsObj(this);
359 log.debug("adding new vobject to Jalview from Document");
365 if (vobj.isUpdated())
367 log.debug("updating Jalview from existing bound vObject");
368 updatedfromdoc = true;
374 VamsasAppDatastore datastore = null;
377 * object in vamsas document
379 protected Vobject vobj = null;
382 * local jalview object
384 protected Object jvobj = null;
386 protected DatastoreRegistry dsReg;
388 public void initDatastoreItem(VamsasAppDatastore ds)
391 dsReg = ds.getDatastoreRegisty();
392 initDatastoreItem(ds.getProvEntry(), ds.getClientDocument(),
393 ds.getVamsasObjectBinding(), ds.getJvObjectBinding());
396 private void initDatastoreItem(Entry provEntry, IClientDocument cdoc,
397 Hashtable vobj2jv, IdentityHashMap jv2vobj)
399 this.provEntry = provEntry;
401 this.vobj2jv = vobj2jv;
402 this.jv2vobj = jv2vobj;
405 protected boolean isModifiable(String modifiable)
407 return modifiable == null; // TODO: USE VAMSAS LIBRARY OBJECT LOCK METHODS)
410 protected Vector getjv2vObjs(Vector alsq)
412 Vector vObjs = new Vector();
413 Enumeration elm = alsq.elements();
414 while (elm.hasMoreElements())
416 vObjs.addElement(getjv2vObj(elm.nextElement()));
423 * get start<end range of segment, adjusting for inclusivity flag and
427 * @param ensureDirection
428 * when true - always ensure start is less than end.
429 * @return int[] { start, end, direction} where direction==1 for range running
432 public int[] getSegRange(Seg visSeg, boolean ensureDirection)
434 boolean incl = visSeg.getInclusive();
435 // adjust for inclusive flag.
436 int pol = (visSeg.getStart() <= visSeg.getEnd()) ? 1 : -1; // polarity of
438 int start = visSeg.getStart() + (incl ? 0 : pol);
439 int end = visSeg.getEnd() + (incl ? 0 : -pol);
440 if (ensureDirection && pol == -1)
442 // jalview doesn't deal with inverted ranges, yet.
447 return new int[] { start, end, pol < 0 ? 1 : 0 };
453 protected jalview.datamodel.Provenance getJalviewProvenance(
456 // TODO: fix App and Action entries and check use of provenance in jalview.
457 jalview.datamodel.Provenance jprov = new jalview.datamodel.Provenance();
458 for (int i = 0; i < prov.getEntryCount(); i++)
460 jprov.addEntry(prov.getEntry(i).getUser(),
461 prov.getEntry(i).getAction(), prov.getEntry(i).getDate(),
462 prov.getEntry(i).getId());
470 * @return default initial provenance list for a Jalview created vamsas
473 Provenance dummyProvenance()
475 return dummyProvenance(null);
478 protected Entry dummyPEntry(String action)
480 Entry entry = new Entry();
481 entry.setApp(this.provEntry.getApp());
484 entry.setAction(action);
488 entry.setAction("created.");
490 entry.setDate(new java.util.Date());
491 entry.setUser(this.provEntry.getUser());
495 protected Provenance dummyProvenance(String action)
497 Provenance prov = new Provenance();
498 prov.addEntry(dummyPEntry(action));
502 protected void addProvenance(Provenance p, String action)
504 p.addEntry(dummyPEntry(action));
508 * @return true if jalview was being updated from the vamsas document
510 public boolean isTojalview()
516 * @return true if addToDocument() was called.
518 public boolean isAddtodoc()
524 * @return true if conflict() was called
526 public boolean isConflicted()
532 * @return true if updateFromDoc() was called
534 public boolean isUpdatedFromDoc()
536 return updatedfromdoc;
540 * @return true if updateToDoc() was called
542 public boolean isUpdatedToDoc()
548 * @return true if addFromDocument() was called.
550 public boolean isAddfromdoc()
556 * @return true if object sync logic completed normally.
558 public boolean isSuccess()
566 public Vobject getVobj()
574 public Object getJvobj()
579 public boolean docWasUpdated()
581 return (this.addtodoc || this.updated) && this.success;
584 public boolean jvWasUpdated()
586 return (success); // TODO : Implement this properly!