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 "Warning? Overwriting existing vamsas id binding for "
135 + vobj.getVorbaId(), new Exception(MessageManager.getString("exception.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(MessageManager.getString("exception.overwriting_jalview_id_binding")));
145 * Cache.log.error("Attempt to make conflicting object binding! "+vobj+" id "
146 * +vobj.getVorbaId()+" already bound to "+getvObj2jv(vobj)+" and "+jvobj+"
147 * already bound to "+getjv2vObj(jvobj),new Exception("Excessive call to
150 // we just update the hash's regardless!
151 Cache.log.debug("Binding " + vobj.getVorbaId() + " to " + jvobj);
152 vobj2jv.put(vobj.getVorbaId(), jvobj);
153 // JBPNote - better implementing a hybrid invertible hash.
154 jv2vobj.put(jvobj, vobj.getVorbaId());
155 if (jvobj == this.jvobj || vobj == this.vobj)
157 updateRegistryEntry(jvobj, vobj);
162 * update the vobj and jvobj references and the registry entry for this
163 * datastore object called by bindjvvobj and replacejvobjmapping
165 private void updateRegistryEntry(Object jvobj, Vobject vobj)
167 if (this.jvobj != null && this.vobj != null)
169 Cache.log.debug("updating dsobj registry. ("
170 + this.getClass().getName() + ")");
174 dsReg.registerDsObj(this);
178 * replaces oldjvobject with newjvobject in the Jalview Object <> VorbaID
179 * binding tables note: originally taken verbatim from
180 * jalview.io.VamsasAppDatastore with added call to updateRegistryEntry
184 * (may be null to forget the oldjvobject's document mapping)
187 protected void replaceJvObjMapping(Object oldjvobject, Object newjvobject)
189 Object vobject = jv2vobj.remove(oldjvobject);
192 throw new Error(MessageManager.formatMessage("error.implementation_error_old_jalview_object_not_bound", new String[]{oldjvobject.toString()}));
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(MessageManager.formatMessage("error.implementation_error_vamsas_doc_class_should_bind_to_type"
232 , new String[]{vobj.getClass().toString(),boundType.toString(),jvobj.getClass().toString()}));
234 dsReg.registerDsObj(this);
238 * construct and initialise datastore object and retrieve document object
239 * bound to Jalview object jvobj2 and validate it against boundType
246 * - the document object class that the bound object should be
249 public DatastoreItem(VamsasAppDatastore datastore2, Object jvobj2,
255 vobj = getjv2vObj(jvobj);
256 if (vobj != null && !(boundToType.isAssignableFrom(vobj.getClass())))
258 throw new Error(MessageManager.formatMessage("error.implementation_error_vamsas_doc_class_should_bind_to_type"
259 , new String[]{jvobj2.getClass().toString(),boundToType.toString(),vobj.getClass().toString()}));
261 dsReg.registerDsObj(this);
265 * create a new vobj to be added to the document for the jalview object jvobj
266 * (jvobj!=null, vobj==null)
268 public abstract void addToDocument();
271 * handle a conflict where both an existing vobj has been updated and a local
272 * jalview object has been updated. This method is only called from doSync,
273 * when an incoming update from the vamsas session conflicts with local
274 * modifications made by the Jalview user. (jvobj!=null, vobj!=null)
276 public abstract void conflict();
279 * update an existing vobj in the document with the data and settings from
280 * jvobj (jvobj!=null, vobj!=null)
282 public abstract void updateToDoc();
285 * update the local jalview object with the data from an existing vobj in the
286 * document (jvobj!=null, vobj!=null)
288 public abstract void updateFromDoc();
291 * create a new local jvobj bound to the vobj in the document. (jvobj==null,
294 public abstract void addFromDocument();
296 boolean addtodoc = false, conflicted = false, updated = false,
297 addfromdoc = false, success = false;
299 private boolean updatedtodoc;
301 private boolean updatedfromdoc;
304 * Sync jalview to document. Enact addToDocument, conflict or update dependent
305 * on existence of a vobj bound to the local jvobj.
307 protected void doSync()
309 dsReg.registerDsObj(this);
312 log.debug("adding new vobject to document.");
318 if (vobj.isUpdated())
320 log.debug("Handling update conflict for existing bound vobject.");
326 log.debug("updating existing vobject in document.");
331 // no exceptions were encountered...
336 * Update jalview from document. enact addFromDocument if no local jvobj
337 * exists, or update iff jvobj exists and the vobj.isUpdated() flag is set.
339 protected void doJvUpdate()
341 dsReg.registerDsObj(this);
344 log.debug("adding new vobject to Jalview from Document");
350 if (vobj.isUpdated())
352 log.debug("updating Jalview from existing bound vObject");
353 updatedfromdoc = true;
359 VamsasAppDatastore datastore = null;
362 * object in vamsas document
364 protected Vobject vobj = null;
367 * local jalview object
369 protected Object jvobj = null;
371 protected DatastoreRegistry dsReg;
373 public void initDatastoreItem(VamsasAppDatastore ds)
376 dsReg = ds.getDatastoreRegisty();
377 initDatastoreItem(ds.getProvEntry(), ds.getClientDocument(),
378 ds.getVamsasObjectBinding(), ds.getJvObjectBinding());
381 private void initDatastoreItem(Entry provEntry, IClientDocument cdoc,
382 Hashtable vobj2jv, IdentityHashMap jv2vobj)
384 this.provEntry = provEntry;
386 this.vobj2jv = vobj2jv;
387 this.jv2vobj = jv2vobj;
390 protected boolean isModifiable(String modifiable)
392 return modifiable == null; // TODO: USE VAMSAS LIBRARY OBJECT LOCK METHODS)
395 protected Vector getjv2vObjs(Vector alsq)
397 Vector vObjs = new Vector();
398 Enumeration elm = alsq.elements();
399 while (elm.hasMoreElements())
401 vObjs.addElement(getjv2vObj(elm.nextElement()));
408 * get start<end range of segment, adjusting for inclusivity flag and
412 * @param ensureDirection
413 * when true - always ensure start is less than end.
414 * @return int[] { start, end, direction} where direction==1 for range running
417 public int[] getSegRange(Seg visSeg, boolean ensureDirection)
419 boolean incl = visSeg.getInclusive();
420 // adjust for inclusive flag.
421 int pol = (visSeg.getStart() <= visSeg.getEnd()) ? 1 : -1; // polarity of
423 int start = visSeg.getStart() + (incl ? 0 : pol);
424 int end = visSeg.getEnd() + (incl ? 0 : -pol);
425 if (ensureDirection && pol == -1)
427 // jalview doesn't deal with inverted ranges, yet.
433 { start, end, pol < 0 ? 1 : 0 };
439 protected jalview.datamodel.Provenance getJalviewProvenance(
442 // TODO: fix App and Action entries and check use of provenance in jalview.
443 jalview.datamodel.Provenance jprov = new jalview.datamodel.Provenance();
444 for (int i = 0; i < prov.getEntryCount(); i++)
446 jprov.addEntry(prov.getEntry(i).getUser(), prov.getEntry(i)
447 .getAction(), prov.getEntry(i).getDate(), prov.getEntry(i)
456 * @return default initial provenance list for a Jalview created vamsas
459 Provenance dummyProvenance()
461 return dummyProvenance(null);
464 protected Entry dummyPEntry(String action)
466 Entry entry = new Entry();
467 entry.setApp(this.provEntry.getApp());
470 entry.setAction(action);
474 entry.setAction("created.");
476 entry.setDate(new java.util.Date());
477 entry.setUser(this.provEntry.getUser());
481 protected Provenance dummyProvenance(String action)
483 Provenance prov = new Provenance();
484 prov.addEntry(dummyPEntry(action));
488 protected void addProvenance(Provenance p, String action)
490 p.addEntry(dummyPEntry(action));
494 * @return true if jalview was being updated from the vamsas document
496 public boolean isTojalview()
502 * @return true if addToDocument() was called.
504 public boolean isAddtodoc()
510 * @return true if conflict() was called
512 public boolean isConflicted()
518 * @return true if updateFromDoc() was called
520 public boolean isUpdatedFromDoc()
522 return updatedfromdoc;
526 * @return true if updateToDoc() was called
528 public boolean isUpdatedToDoc()
534 * @return true if addFromDocument() was called.
536 public boolean isAddfromdoc()
542 * @return true if object sync logic completed normally.
544 public boolean isSuccess()
552 public Vobject getVobj()
560 public Object getJvobj()
565 public boolean docWasUpdated()
567 return (this.addtodoc || this.updated) && this.success;
570 public boolean jvWasUpdated()
572 return (success); // TODO : Implement this properly!