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 jalview.bin.Cache;
24 import jalview.io.VamsasAppDatastore;
25 import jalview.util.MessageManager;
27 import java.util.Enumeration;
28 import java.util.Hashtable;
29 import java.util.IdentityHashMap;
30 import java.util.Vector;
32 import uk.ac.vamsas.client.IClientDocument;
33 import uk.ac.vamsas.client.Vobject;
34 import uk.ac.vamsas.client.VorbaId;
35 import uk.ac.vamsas.objects.core.Entry;
36 import uk.ac.vamsas.objects.core.Provenance;
37 import uk.ac.vamsas.objects.core.Seg;
40 * Holds all the common machinery for binding objects to vamsas objects
45 public abstract class DatastoreItem
50 Entry provEntry = null;
56 IdentityHashMap jv2vobj;
58 boolean tojalview = false;
63 protected static org.apache.log4j.Logger log = org.apache.log4j.Logger
64 .getLogger(DatastoreItem.class);
67 * note: this is taken verbatim from jalview.io.VamsasAppDatastore
69 * @return the Vobject bound to Jalview datamodel object
71 protected Vobject getjv2vObj(Object jvobj)
73 if (jv2vobj.containsKey(jvobj))
75 return cdoc.getObject((VorbaId) jv2vobj.get(jvobj));
77 if (Cache.log.isDebugEnabled())
79 Cache.log.debug("Returning null VorbaID binding for jalview object "
88 * @return Jalview datamodel object bound to the vamsas document object
90 protected Object getvObj2jv(uk.ac.vamsas.client.Vobject vobj)
94 VorbaId id = vobj.getVorbaId();
97 id = cdoc.registerObject(vobj);
99 .debug("Registering new object and returning null for getvObj2jv");
102 if (vobj2jv.containsKey(vobj.getVorbaId()))
104 return vobj2jv.get(vobj.getVorbaId());
110 * note: this is taken verbatim from jalview.io.VamsasAppDatastore with added
111 * call to updateRegistryEntry
116 protected void bindjvvobj(Object jvobj, uk.ac.vamsas.client.Vobject vobj)
118 VorbaId id = vobj.getVorbaId();
121 id = cdoc.registerObject(vobj);
122 if (id == null || vobj.getVorbaId() == null
123 || cdoc.getObject(id) != vobj)
125 Cache.log.error("Failed to get id for "
126 + (vobj.isRegisterable() ? "registerable"
127 : "unregisterable") + " object " + vobj);
130 if (vobj2jv.containsKey(vobj.getVorbaId())
131 && !(vobj2jv.get(vobj.getVorbaId())).equals(jvobj))
134 .debug("Warning? Overwriting existing vamsas id binding for "
138 .getString("exception.overwriting_vamsas_id_binding")));
140 else if (jv2vobj.containsKey(jvobj)
141 && !((VorbaId) jv2vobj.get(jvobj)).equals(vobj.getVorbaId()))
144 .debug("Warning? Overwriting existing jalview object binding for "
148 .getString("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",
200 new String[] { oldjvobject.toString() }));
202 if (newjvobject != null)
204 jv2vobj.put(newjvobject, vobject);
205 vobj2jv.put(vobject, newjvobject);
206 updateRegistryEntry(newjvobject, vobj);
210 public DatastoreItem()
215 public DatastoreItem(VamsasAppDatastore datastore)
218 initDatastoreItem(datastore);
219 // TODO Auto-generated constructor stub
223 * construct and initialise datastore object and retrieve object bound to
224 * vobj2 and validate it against boundType
230 public DatastoreItem(VamsasAppDatastore datastore2, Vobject vobj2,
235 jvobj = getvObj2jv(vobj2);
237 if (jvobj != null && !(boundType.isAssignableFrom(jvobj.getClass())))
242 "error.implementation_error_vamsas_doc_class_should_bind_to_type",
243 new String[] { vobj.getClass().toString(),
244 boundType.toString(),
245 jvobj.getClass().toString() }));
247 dsReg.registerDsObj(this);
251 * construct and initialise datastore object and retrieve document object
252 * bound to Jalview object jvobj2 and validate it against boundType
259 * - the document object class that the bound object should be
262 public DatastoreItem(VamsasAppDatastore datastore2, Object jvobj2,
268 vobj = getjv2vObj(jvobj);
269 if (vobj != null && !(boundToType.isAssignableFrom(vobj.getClass())))
274 "error.implementation_error_vamsas_doc_class_should_bind_to_type",
275 new String[] { jvobj2.getClass().toString(),
276 boundToType.toString(),
277 vobj.getClass().toString() }));
279 dsReg.registerDsObj(this);
283 * create a new vobj to be added to the document for the jalview object jvobj
284 * (jvobj!=null, vobj==null)
286 public abstract void addToDocument();
289 * handle a conflict where both an existing vobj has been updated and a local
290 * jalview object has been updated. This method is only called from doSync,
291 * when an incoming update from the vamsas session conflicts with local
292 * modifications made by the Jalview user. (jvobj!=null, vobj!=null)
294 public abstract void conflict();
297 * update an existing vobj in the document with the data and settings from
298 * jvobj (jvobj!=null, vobj!=null)
300 public abstract void updateToDoc();
303 * update the local jalview object with the data from an existing vobj in the
304 * document (jvobj!=null, vobj!=null)
306 public abstract void updateFromDoc();
309 * create a new local jvobj bound to the vobj in the document. (jvobj==null,
312 public abstract void addFromDocument();
314 boolean addtodoc = false, conflicted = false, updated = false,
315 addfromdoc = false, success = false;
317 private boolean updatedtodoc;
319 private boolean updatedfromdoc;
322 * Sync jalview to document. Enact addToDocument, conflict or update dependent
323 * on existence of a vobj bound to the local jvobj.
325 protected void doSync()
327 dsReg.registerDsObj(this);
330 log.debug("adding new vobject to document.");
336 if (vobj.isUpdated())
338 log.debug("Handling update conflict for existing bound vobject.");
344 log.debug("updating existing vobject in document.");
349 // no exceptions were encountered...
354 * Update jalview from document. enact addFromDocument if no local jvobj
355 * exists, or update iff jvobj exists and the vobj.isUpdated() flag is set.
357 protected void doJvUpdate()
359 dsReg.registerDsObj(this);
362 log.debug("adding new vobject to Jalview from Document");
368 if (vobj.isUpdated())
370 log.debug("updating Jalview from existing bound vObject");
371 updatedfromdoc = true;
377 VamsasAppDatastore datastore = null;
380 * object in vamsas document
382 protected Vobject vobj = null;
385 * local jalview object
387 protected Object jvobj = null;
389 protected DatastoreRegistry dsReg;
391 public void initDatastoreItem(VamsasAppDatastore ds)
394 dsReg = ds.getDatastoreRegisty();
395 initDatastoreItem(ds.getProvEntry(), ds.getClientDocument(),
396 ds.getVamsasObjectBinding(), ds.getJvObjectBinding());
399 private void initDatastoreItem(Entry provEntry, IClientDocument cdoc,
400 Hashtable vobj2jv, IdentityHashMap jv2vobj)
402 this.provEntry = provEntry;
404 this.vobj2jv = vobj2jv;
405 this.jv2vobj = jv2vobj;
408 protected boolean isModifiable(String modifiable)
410 return modifiable == null; // TODO: USE VAMSAS LIBRARY OBJECT LOCK METHODS)
413 protected Vector getjv2vObjs(Vector alsq)
415 Vector vObjs = new Vector();
416 Enumeration elm = alsq.elements();
417 while (elm.hasMoreElements())
419 vObjs.addElement(getjv2vObj(elm.nextElement()));
426 * get start<end range of segment, adjusting for inclusivity flag and
430 * @param ensureDirection
431 * when true - always ensure start is less than end.
432 * @return int[] { start, end, direction} where direction==1 for range running
435 public int[] getSegRange(Seg visSeg, boolean ensureDirection)
437 boolean incl = visSeg.getInclusive();
438 // adjust for inclusive flag.
439 int pol = (visSeg.getStart() <= visSeg.getEnd()) ? 1 : -1; // polarity of
441 int start = visSeg.getStart() + (incl ? 0 : pol);
442 int end = visSeg.getEnd() + (incl ? 0 : -pol);
443 if (ensureDirection && pol == -1)
445 // jalview doesn't deal with inverted ranges, yet.
450 return new int[] { start, end, pol < 0 ? 1 : 0 };
456 protected jalview.datamodel.Provenance getJalviewProvenance(
459 // TODO: fix App and Action entries and check use of provenance in jalview.
460 jalview.datamodel.Provenance jprov = new jalview.datamodel.Provenance();
461 for (int i = 0; i < prov.getEntryCount(); i++)
463 jprov.addEntry(prov.getEntry(i).getUser(), prov.getEntry(i)
464 .getAction(), prov.getEntry(i).getDate(), prov.getEntry(i)
473 * @return default initial provenance list for a Jalview created vamsas
476 Provenance dummyProvenance()
478 return dummyProvenance(null);
481 protected Entry dummyPEntry(String action)
483 Entry entry = new Entry();
484 entry.setApp(this.provEntry.getApp());
487 entry.setAction(action);
491 entry.setAction("created.");
493 entry.setDate(new java.util.Date());
494 entry.setUser(this.provEntry.getUser());
498 protected Provenance dummyProvenance(String action)
500 Provenance prov = new Provenance();
501 prov.addEntry(dummyPEntry(action));
505 protected void addProvenance(Provenance p, String action)
507 p.addEntry(dummyPEntry(action));
511 * @return true if jalview was being updated from the vamsas document
513 public boolean isTojalview()
519 * @return true if addToDocument() was called.
521 public boolean isAddtodoc()
527 * @return true if conflict() was called
529 public boolean isConflicted()
535 * @return true if updateFromDoc() was called
537 public boolean isUpdatedFromDoc()
539 return updatedfromdoc;
543 * @return true if updateToDoc() was called
545 public boolean isUpdatedToDoc()
551 * @return true if addFromDocument() was called.
553 public boolean isAddfromdoc()
559 * @return true if object sync logic completed normally.
561 public boolean isSuccess()
569 public Vobject getVobj()
577 public Object getJvobj()
582 public boolean docWasUpdated()
584 return (this.addtodoc || this.updated) && this.success;
587 public boolean jvWasUpdated()
589 return (success); // TODO : Implement this properly!