1 package uk.ac.vamsas.objects;
\r
3 import uk.ac.vamsas.client.IClientDocument;
\r
4 import uk.ac.vamsas.objects.core.*;
\r
6 * 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
10 public class DocumentUpdaterEngine {
\r
11 private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(DocumentUpdaterEngine.class);
\r
12 private IDocumentUpdater handler;
\r
14 * initialise the engine with an implementation
\r
18 public DocumentUpdaterEngine(IDocumentUpdater handler) {
\r
20 this.handler = handler;
\r
23 * call the necessary update handlers at
\r
24 * each point on the VamsasDocument OM
\r
25 * TODO: later: Make this more elegant (use reflection and factor to single update(Object) method) ?
\r
26 * - we take the plodding, explicit approach rather than a funky generalised one here
\r
28 public void callHandlers(IClientDocument cdoc) {
\r
30 log.debug("Null IClientDocument instance.");
\r
33 VAMSAS[] roots = cdoc.getVamsasRoots();
\r
35 for (int r=0; r<roots.length; r++) {
\r
36 if (roots[r].isNewInDocument() || roots[r].isUpdated()) {
\r
37 if (!updateRoot(roots[r])) {
\r
38 log.debug("Calling handler(VAMSAS)");
\r
39 handler.update(roots[r]);
\r
44 log.debug("No Document Roots.");
\r
46 // TODO: determine if the User, private or public appData has changed
\r
47 log.debug("Finished.");
\r
49 private boolean updateRoot(VAMSAS vamsas) {
\r
50 boolean called=false;
\r
51 DataSet[] dset = vamsas.getDataSet();
\r
53 for (int ds=0; ds<dset.length; ds++) {
\r
54 if (dset[ds].isNewInDocument() || dset[ds].isUpdated()) {
\r
55 if (!updateDataset(dset[ds])) {
\r
56 log.debug("Calling handler(Dataset)");
\r
57 handler.update(dset[ds]);
\r
65 private boolean updateDataset(DataSet set) {
\r
66 boolean called=false;
\r
68 Sequence[] dseq = set.getSequence();
\r
70 for (int s=0;s<dseq.length; s++) {
\r
71 if (dseq[s].isNewInDocument() || dseq[s].isUpdated()) {
\r
72 if (!updateSequence(dseq[s])) {
\r
73 log.debug("Calling update(Sequence)");
\r
74 handler.update(dseq[s]);
\r
81 DataSetAnnotations[] dann=set.getDataSetAnnotations();
\r
83 for (int a=0; a<dann.length; a++) {
\r
84 if (dann[a].isNewInDocument() || dann[a].isUpdated()) {
\r
85 if (!updateDataSetAnnotation(dann[a])) {
\r
86 log.debug("Calling update(DataSetAnnotation)");
\r
87 handler.update(dann[a]);
\r
94 Alignment[] al=set.getAlignment();
\r
96 for (int a=0;a<al.length;a++) {
\r
97 if (al[a].isNewInDocument() || al[a].isUpdated()) {
\r
98 if (!updateAlignment(al[a])) {
\r
99 log.debug("Calling update(Alignment)");
\r
100 handler.update(al[a]);
\r
106 // Trees associated with dataset sequences
\r
107 if (updateTrees(set.getTree())) {
\r
112 private boolean updateTrees(Tree[] trees) {
\r
113 boolean called=false;
\r
115 for (int t=0;t<trees.length; t++) {
\r
116 if (trees[t].isNewInDocument() || trees[t].isUpdated()) {
\r
117 if (!updateTree(trees[t])) {
\r
118 log.debug("Calling update(tree)");
\r
119 handler.update(trees[t]);
\r
127 private boolean updateDataSetAnnotation(DataSetAnnotations annotations) {
\r
128 boolean called=false;
\r
131 private boolean updateTree(Tree tree) {
\r
135 private boolean updateAlignment(Alignment alignment) {
\r
136 // TODO Auto-generated method stub
\r
139 private boolean updateSequence(Sequence sequence) {
\r
140 // TODO Auto-generated method stub
\r