package uk.ac.vamsas.objects;\r
\r
import uk.ac.vamsas.client.IClientDocument;\r
-\r
+import uk.ac.vamsas.objects.core.*;\r
+/**\r
+ * Implements a depth first traversal over the document tree calling update handlers based on the Vobject.isUpdated() and Vobject.isNewInDocument() state at each backtrack.\r
+ * @author JimP\r
+ *\r
+ */\r
public class DocumentUpdaterEngine {\r
- private IDocumentUpdater hander;\r
+ private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(DocumentUpdaterEngine.class);\r
+ private IDocumentUpdater handler;\r
/**\r
* initialise the engine with an implementation\r
* of the interface.\r
* @param hander\r
*/\r
- public DocumentUpdaterEngine(IDocumentUpdater hander) {\r
+ public DocumentUpdaterEngine(IDocumentUpdater handler) {\r
super();\r
- this.hander = hander;\r
+ this.handler = handler;\r
}\r
/**\r
* call the necessary update handlers at\r
* each point on the VamsasDocument OM\r
+ * TODO: later: Make this more elegant (use reflection and factor to single update(Object) method) ?\r
+ * - we take the plodding, explicit approach rather than a funky generalised one here\r
*/\r
public void callHandlers(IClientDocument cdoc) {\r
- // TODO: walk down the hierarchy until we find an update and call the handler for it.\r
+ if (cdoc==null) {\r
+ log.debug("Null IClientDocument instance.");\r
+ return;\r
+ }\r
+ VAMSAS[] roots = cdoc.getVamsasRoots();\r
+ if (roots!=null) {\r
+ for (int r=0; r<roots.length; r++) {\r
+ if (roots[r].isNewInDocument() || roots[r].isUpdated()) {\r
+ if (!updateRoot(roots[r])) {\r
+ log.debug("Calling handler(VAMSAS)");\r
+ handler.update(roots[r]);\r
+ }\r
+ }\r
+ }\r
+ } else {\r
+ log.debug("No Document Roots.");\r
+ }\r
// TODO: determine if the User, private or public appData has changed\r
- \r
+ log.debug("Finished.");\r
+ }\r
+ private boolean updateRoot(VAMSAS vamsas) {\r
+ boolean called=false;\r
+ DataSet[] dset = vamsas.getDataSet();\r
+ if (dset!=null) {\r
+ for (int ds=0; ds<dset.length; ds++) {\r
+ if (dset[ds].isNewInDocument() || dset[ds].isUpdated()) {\r
+ if (!updateDataset(dset[ds])) {\r
+ log.debug("Calling handler(Dataset)");\r
+ handler.update(dset[ds]);\r
+ called=true;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ return called;\r
+ }\r
+ private boolean updateDataset(DataSet set) {\r
+ boolean called=false;\r
+ // Sequences\r
+ Sequence[] dseq = set.getSequence();\r
+ if (dseq!=null) {\r
+ for (int s=0;s<dseq.length; s++) {\r
+ if (dseq[s].isNewInDocument() || dseq[s].isUpdated()) {\r
+ if (!updateSequence(dseq[s])) {\r
+ log.debug("Calling update(Sequence)");\r
+ handler.update(dseq[s]);\r
+ called=true;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ // Annotations\r
+ DataSetAnnotations[] dann=set.getDataSetAnnotations();\r
+ if (dann!=null) {\r
+ for (int a=0; a<dann.length; a++) {\r
+ if (dann[a].isNewInDocument() || dann[a].isUpdated()) {\r
+ if (!updateDataSetAnnotation(dann[a])) {\r
+ log.debug("Calling update(DataSetAnnotation)");\r
+ handler.update(dann[a]);\r
+ called=true;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ // Alignments\r
+ Alignment[] al=set.getAlignment();\r
+ if (al!=null) {\r
+ for (int a=0;a<al.length;a++) {\r
+ if (al[a].isNewInDocument() || al[a].isUpdated()) {\r
+ if (!updateAlignment(al[a])) {\r
+ log.debug("Calling update(Alignment)");\r
+ handler.update(al[a]);\r
+ called=true;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ // Trees associated with dataset sequences\r
+ if (updateTrees(set.getTree())) {\r
+ called=true;\r
+ }\r
+ return called;\r
+ }\r
+ private boolean updateTrees(Tree[] trees) {\r
+ boolean called=false;\r
+ if (trees!=null) {\r
+ for (int t=0;t<trees.length; t++) {\r
+ if (trees[t].isNewInDocument() || trees[t].isUpdated()) {\r
+ if (!updateTree(trees[t])) {\r
+ log.debug("Calling update(tree)");\r
+ handler.update(trees[t]);\r
+ called=true;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ return called;\r
+ }\r
+ private boolean updateDataSetAnnotation(DataSetAnnotations annotations) {\r
+ boolean called=false;\r
+ return called;\r
+ }\r
+ private boolean updateTree(Tree tree) {\r
+ // TODO: if (\r
+ return false;\r
+ }\r
+ private boolean updateAlignment(Alignment alignment) {\r
+ // TODO Auto-generated method stub\r
+ return false;\r
+ }\r
+ private boolean updateSequence(Sequence sequence) {\r
+ // TODO Auto-generated method stub\r
+ return false;\r
}\r
}\r