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(Vobject vobj)
96 VorbaId id = vobj.getVorbaId();
99 id = cdoc.registerObject(vobj);
101 .debug("Registering new object and returning null for getvObj2jv");
104 if (vobj2jv.containsKey(vobj.getVorbaId()))
106 return vobj2jv.get(vobj.getVorbaId());
112 * note: this is taken verbatim from jalview.io.VamsasAppDatastore with added
113 * call to updateRegistryEntry
118 protected void bindjvvobj(Object jvobj, Vobject vobj)
120 VorbaId id = vobj.getVorbaId();
123 id = cdoc.registerObject(vobj);
124 if (id == null || vobj.getVorbaId() == null
125 || cdoc.getObject(id) != vobj)
127 Cache.log.error("Failed to get id for "
128 + (vobj.isRegisterable() ? "registerable"
129 : "unregisterable") + " object " + vobj);
132 if (vobj2jv.containsKey(vobj.getVorbaId())
133 && !(vobj2jv.get(vobj.getVorbaId())).equals(jvobj))
136 "Warning? Overwriting existing vamsas id binding for "
137 + vobj.getVorbaId(), new Exception(MessageManager.getString("exception.overwriting_vamsas_id_binding")));
139 else if (jv2vobj.containsKey(jvobj)
140 && !((VorbaId) jv2vobj.get(jvobj)).equals(vobj.getVorbaId()))
143 "Warning? Overwriting existing jalview object binding for "
144 + jvobj, new Exception(MessageManager.getString("exception.overwriting_jalview_id_binding")));
147 * Cache.log.error("Attempt to make conflicting object binding! "+vobj+" id "
148 * +vobj.getVorbaId()+" already bound to "+getvObj2jv(vobj)+" and "+jvobj+"
149 * already bound to "+getjv2vObj(jvobj),new Exception("Excessive call to
152 // we just update the hash's regardless!
153 Cache.log.debug("Binding " + vobj.getVorbaId() + " to " + jvobj);
154 vobj2jv.put(vobj.getVorbaId(), jvobj);
155 // JBPNote - better implementing a hybrid invertible hash.
156 jv2vobj.put(jvobj, vobj.getVorbaId());
157 if (jvobj == this.jvobj || vobj == this.vobj)
159 updateRegistryEntry(jvobj, vobj);
164 * update the vobj and jvobj references and the registry entry for this
165 * datastore object called by bindjvvobj and replacejvobjmapping
167 private void updateRegistryEntry(Object jvobj, Vobject vobj)
169 if (this.jvobj != null && this.vobj != null)
171 Cache.log.debug("updating dsobj registry. ("
172 + this.getClass().getName() + ")");
176 dsReg.registerDsObj(this);
180 * replaces oldjvobject with newjvobject in the Jalview Object <> VorbaID
181 * binding tables note: originally taken verbatim from
182 * jalview.io.VamsasAppDatastore with added call to updateRegistryEntry
186 * (may be null to forget the oldjvobject's document mapping)
189 protected void replaceJvObjMapping(Object oldjvobject, Object newjvobject)
191 Object vobject = jv2vobj.remove(oldjvobject);
194 throw new Error(MessageManager.formatMessage("error.implementation_error_old_jalview_object_not_bound", new String[]{oldjvobject.toString()}));
196 if (newjvobject != null)
198 jv2vobj.put(newjvobject, vobject);
199 vobj2jv.put(vobject, newjvobject);
200 updateRegistryEntry(newjvobject, vobj);
204 public DatastoreItem()
209 public DatastoreItem(VamsasAppDatastore datastore)
212 initDatastoreItem(datastore);
213 // TODO Auto-generated constructor stub
217 * construct and initialise datastore object and retrieve object bound to
218 * vobj2 and validate it against boundType
224 public DatastoreItem(VamsasAppDatastore datastore2, Vobject vobj2,
229 jvobj = getvObj2jv(vobj2);
231 if (jvobj != null && !(boundType.isAssignableFrom(jvobj.getClass())))
233 throw new Error(MessageManager.formatMessage("error.implementation_error_vamsas_doc_class_should_bind_to_type"
234 , new String[]{vobj.getClass().toString(),boundType.toString(),jvobj.getClass().toString()}));
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(MessageManager.formatMessage("error.implementation_error_vamsas_doc_class_should_bind_to_type"
261 , new String[]{jvobj2.getClass().toString(),boundToType.toString(),vobj.getClass().toString()}));
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!