2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.io.vamsas;
21 import jalview.bin.Cache;
22 import jalview.datamodel.DBRefEntry;
23 import jalview.gui.TreePanel;
24 import jalview.io.VamsasAppDatastore;
26 import java.util.Enumeration;
27 import java.util.Hashtable;
28 import java.util.IdentityHashMap;
29 import java.util.Vector;
31 import org.apache.commons.logging.Log;
33 import uk.ac.vamsas.client.IClientDocument;
34 import uk.ac.vamsas.client.Vobject;
35 import uk.ac.vamsas.client.VorbaId;
36 import uk.ac.vamsas.objects.core.DbRef;
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;
64 protected static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(DatastoreItem.class);
66 * note: this is taken verbatim from jalview.io.VamsasAppDatastore
67 * @return the Vobject bound to Jalview datamodel object
69 protected Vobject getjv2vObj(Object jvobj)
71 if (jv2vobj.containsKey(jvobj))
73 return cdoc.getObject((VorbaId) jv2vobj.get(jvobj));
75 if (Cache.log.isDebugEnabled())
77 Cache.log.debug("Returning null VorbaID binding for jalview object "
86 * @return Jalview datamodel object bound to the vamsas document object
88 protected Object getvObj2jv(uk.ac.vamsas.client.Vobject vobj)
92 VorbaId id = vobj.getVorbaId();
95 id = cdoc.registerObject(vobj);
97 .debug("Registering new object and returning null for getvObj2jv");
100 if (vobj2jv.containsKey(vobj.getVorbaId()))
102 return vobj2jv.get(vobj.getVorbaId());
108 * note: this is taken verbatim from jalview.io.VamsasAppDatastore
113 protected void bindjvvobj(Object jvobj, uk.ac.vamsas.client.Vobject vobj)
115 VorbaId id = vobj.getVorbaId();
118 id = cdoc.registerObject(vobj);
119 if (id == null || vobj.getVorbaId() == null
120 || cdoc.getObject(id) != vobj)
122 Cache.log.error("Failed to get id for "
123 + (vobj.isRegisterable() ? "registerable"
124 : "unregisterable") + " object " + vobj);
128 if (vobj2jv.containsKey(vobj.getVorbaId())
129 && !((VorbaId) vobj2jv.get(vobj.getVorbaId())).equals(jvobj))
132 "Warning? Overwriting existing vamsas id binding for "
133 + vobj.getVorbaId(), new Exception(
134 "Overwriting vamsas id binding."));
136 else if (jv2vobj.containsKey(jvobj)
137 && !((VorbaId) jv2vobj.get(jvobj)).equals(vobj.getVorbaId()))
140 "Warning? Overwriting existing jalview object binding for "
141 + jvobj, new Exception(
142 "Overwriting jalview object 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());
157 * replaces oldjvobject with newjvobject in the Jalview Object <> VorbaID
159 * note: this is taken verbatim from jalview.io.VamsasAppDatastore
161 * @param newjvobject (may be null to forget the oldjvobject's document mapping)
164 protected void replaceJvObjMapping(Object oldjvobject, Object newjvobject)
166 Object vobject = jv2vobj.remove(oldjvobject);
170 "IMPLEMENTATION ERROR: old jalview object is not bound ! ("
171 + oldjvobject + ")");
173 if (newjvobject!=null)
175 jv2vobj.put(newjvobject, vobject);
176 vobj2jv.put(vobject, newjvobject);
180 public DatastoreItem()
185 public DatastoreItem(VamsasAppDatastore datastore)
188 initDatastoreItem(datastore);
189 // TODO Auto-generated constructor stub
192 * construct and initialise datastore object and retrieve object bound to vobj2 and validate it against boundType
197 public DatastoreItem(VamsasAppDatastore datastore2, Vobject vobj2, Class boundType)
201 jvobj = getvObj2jv(vobj2);
203 if (jvobj!=null && !(boundType.isAssignableFrom(jvobj.getClass())))
205 throw new Error("Implementation Error: Vamsas Document Class "+vobj.getClass()+" should bind to a "+boundType+" (found a "+jvobj.getClass()+")");
209 * construct and initialise datastore object and retrieve document object bound to Jalview object jvobj2 and validate it against boundType
210 * @param datastore2 the datastore
211 * @param jvobj2 the jalview object
212 * @param boundToType - the document object class that the bound object should be assignable from
214 public DatastoreItem(VamsasAppDatastore datastore2, Object jvobj2, Class boundToType)
219 vobj = getjv2vObj(jvobj);
220 if (vobj!=null && !(boundToType.isAssignableFrom(vobj.getClass())))
222 throw new Error("Implementation Error: Jalview Class "+jvobj2.getClass()+" should bind to a "+boundToType+" (found a "+vobj.getClass()+")");
226 * create a new vobj to be added to the document
227 * for the jalview object jvobj
228 * (jvobj!=null, vobj==null)
230 public abstract void addToDocument();
232 * handle a conflict where both an existing vobj has been updated
233 * and a local jalview object has been updated. This method
234 * is only called from doSync, when an incoming update from the vamsas
235 * session conflicts with local modifications made by the Jalview user.
236 * (jvobj!=null, vobj!=null)
238 public abstract void conflict();
240 * update an existing vobj in the document with the data and settings from jvobj
241 * (jvobj!=null, vobj!=null)
243 public abstract void updateToDoc();
245 * update the local jalview object with the data from an existing vobj in the document
246 * (jvobj!=null, vobj!=null)
248 public abstract void updateFromDoc();
250 * create a new local jvobj bound to the vobj in the document.
251 * (jvobj==null, vobj!=null)
253 public abstract void addFromDocument();
254 boolean addtodoc=false, conflicted=false,updated=false,addfromdoc=false,success=false;
256 private boolean updatedtodoc;
258 private boolean updatedfromdoc;
260 * Sync jalview to document. Enact addToDocument, conflict or update dependent on
261 * existence of a vobj bound to the local jvobj.
263 protected void doSync()
268 log.debug("adding new vobject to document.");
274 if (vobj.isUpdated())
276 log.debug("Handling update conflict for existing bound vobject.");
282 log.debug("updating existing vobject in document.");
287 // no exceptions were encountered...
291 * Update jalview from document. enact addFromDocument if no local jvobj exists, or update iff jvobj
292 * exists and the vobj.isUpdated() flag is set.
294 protected void doJvUpdate()
298 log.debug("adding new vobject to Jalview from Document");
304 if (vobj.isUpdated())
306 log.debug("updating Jalview from existing bound vObject");
313 VamsasAppDatastore datastore = null;
315 * object in vamsas document
317 protected Vobject vobj = null;
319 * local jalview object
321 protected Object jvobj = null;
323 public void initDatastoreItem(VamsasAppDatastore ds)
326 initDatastoreItem(ds.getProvEntry(), ds.getClientDocument(), ds
327 .getVamsasObjectBinding(), ds.getJvObjectBinding());
330 public void initDatastoreItem(Entry provEntry, IClientDocument cdoc,
331 Hashtable vobj2jv, IdentityHashMap jv2vobj)
333 this.provEntry = provEntry;
335 this.vobj2jv = vobj2jv;
336 this.jv2vobj = jv2vobj;
339 protected boolean isModifiable(String modifiable)
341 return modifiable == null; // TODO: USE VAMSAS LIBRARY OBJECT LOCK METHODS)
344 protected Vector getjv2vObjs(Vector alsq)
346 Vector vObjs = new Vector();
347 Enumeration elm = alsq.elements();
348 while (elm.hasMoreElements())
350 vObjs.addElement(getjv2vObj(elm.nextElement()));
357 * get start<end range of segment, adjusting for inclusivity flag and
361 * @param ensureDirection
362 * when true - always ensure start is less than end.
363 * @return int[] { start, end, direction} where direction==1 for range running
366 public int[] getSegRange(Seg visSeg, boolean ensureDirection)
368 boolean incl = visSeg.getInclusive();
369 // adjust for inclusive flag.
370 int pol = (visSeg.getStart() <= visSeg.getEnd()) ? 1 : -1; // polarity of
372 int start = visSeg.getStart() + (incl ? 0 : pol);
373 int end = visSeg.getEnd() + (incl ? 0 : -pol);
374 if (ensureDirection && pol == -1)
376 // jalview doesn't deal with inverted ranges, yet.
382 { start, end, pol < 0 ? 1 : 0 };
388 protected jalview.datamodel.Provenance getJalviewProvenance(
391 // TODO: fix App and Action entries and check use of provenance in jalview.
392 jalview.datamodel.Provenance jprov = new jalview.datamodel.Provenance();
393 for (int i = 0; i < prov.getEntryCount(); i++)
395 jprov.addEntry(prov.getEntry(i).getUser(), prov.getEntry(i)
396 .getAction(), prov.getEntry(i).getDate(), prov.getEntry(i)
405 * @return default initial provenance list for a Jalview created vamsas
408 Provenance dummyProvenance()
410 return dummyProvenance(null);
413 protected Entry dummyPEntry(String action)
415 Entry entry = new Entry();
416 entry.setApp(this.provEntry.getApp());
419 entry.setAction(action);
423 entry.setAction("created.");
425 entry.setDate(new java.util.Date());
426 entry.setUser(this.provEntry.getUser());
430 protected Provenance dummyProvenance(String action)
432 Provenance prov = new Provenance();
433 prov.addEntry(dummyPEntry(action));
437 protected void addProvenance(Provenance p, String action)
439 p.addEntry(dummyPEntry(action));
444 * @return true if jalview was being updated from the vamsas document
446 public boolean isTojalview()
452 * @return true if addToDocument() was called.
454 public boolean isAddtodoc()
460 * @return true if conflict() was called
462 public boolean isConflicted()
468 * @return true if updateFromDoc() was called
470 public boolean isUpdatedFromDoc()
472 return updatedfromdoc;
475 * @return true if updateToDoc() was called
477 public boolean isUpdatedToDoc()
483 * @return true if addFromDocument() was called.
485 public boolean isAddfromdoc()
491 * @return true if object sync logic completed normally.
493 public boolean isSuccess()
501 public Vobject getVobj()
509 public Object getJvobj()