2 * This file is part of the Vamsas Client version 0.1.
\r
3 * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite,
\r
4 * Andrew Waterhouse and Dominik Lindner.
\r
6 * Earlier versions have also been incorporated into Jalview version 2.4
\r
7 * since 2008, and TOPALi version 2 since 2007.
\r
9 * The Vamsas Client is free software: you can redistribute it and/or modify
\r
10 * it under the terms of the GNU Lesser General Public License as published by
\r
11 * the Free Software Foundation, either version 3 of the License, or
\r
12 * (at your option) any later version.
\r
14 * The Vamsas Client is distributed in the hope that it will be useful,
\r
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
17 * GNU Lesser General Public License for more details.
\r
19 * You should have received a copy of the GNU Lesser General Public License
\r
20 * along with the Vamsas Client. If not, see <http://www.gnu.org/licenses/>.
\r
22 package uk.ac.vamsas.objects;
\r
24 import uk.ac.vamsas.client.IClientDocument;
\r
25 import uk.ac.vamsas.objects.core.*;
\r
28 * Implements a depth first traversal over the document tree calling update
\r
29 * handlers based on the Vobject.isUpdated() and Vobject.isNewInDocument() state
\r
30 * at each backtrack.
\r
35 public class DocumentUpdaterEngine {
\r
36 private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
\r
37 .getLog(DocumentUpdaterEngine.class);
\r
39 private IDocumentUpdater handler;
\r
42 * initialise the engine with an implementation of the interface.
\r
46 public DocumentUpdaterEngine(IDocumentUpdater handler) {
\r
48 this.handler = handler;
\r
52 * call the necessary update handlers at each point on the VamsasDocument OM
\r
53 * TODO: later: Make this more elegant (use reflection and factor to single
\r
54 * update(Object) method) ? - we take the plodding, explicit approach rather
\r
55 * than a funky generalised one here
\r
57 public void callHandlers(IClientDocument cdoc) {
\r
59 log.debug("Null IClientDocument instance.");
\r
62 VAMSAS[] roots = cdoc.getVamsasRoots();
\r
63 if (roots != null) {
\r
64 for (int r = 0; r < roots.length; r++) {
\r
65 if (roots[r].isNewInDocument() || roots[r].isUpdated()) {
\r
66 if (!updateRoot(roots[r])) {
\r
67 log.debug("Calling handler(VAMSAS)");
\r
68 handler.update(roots[r]);
\r
73 log.debug("No Document Roots.");
\r
75 // TODO: determine if the User, private or public appData has changed
\r
76 log.debug("Finished.");
\r
79 private boolean updateRoot(VAMSAS vamsas) {
\r
80 boolean called = false;
\r
81 DataSet[] dset = vamsas.getDataSet();
\r
83 for (int ds = 0; ds < dset.length; ds++) {
\r
84 if (dset[ds].isNewInDocument() || dset[ds].isUpdated()) {
\r
85 if (!updateDataset(dset[ds])) {
\r
86 log.debug("Calling handler(Dataset)");
\r
87 handler.update(dset[ds]);
\r
96 private boolean updateDataset(DataSet set) {
\r
97 boolean called = false;
\r
99 Sequence[] dseq = set.getSequence();
\r
100 if (dseq != null) {
\r
101 for (int s = 0; s < dseq.length; s++) {
\r
102 if (dseq[s].isNewInDocument() || dseq[s].isUpdated()) {
\r
103 if (!updateSequence(dseq[s])) {
\r
104 log.debug("Calling update(Sequence)");
\r
105 handler.update(dseq[s]);
\r
112 DataSetAnnotations[] dann = set.getDataSetAnnotations();
\r
113 if (dann != null) {
\r
114 for (int a = 0; a < dann.length; a++) {
\r
115 if (dann[a].isNewInDocument() || dann[a].isUpdated()) {
\r
116 if (!updateDataSetAnnotation(dann[a])) {
\r
117 log.debug("Calling update(DataSetAnnotation)");
\r
118 handler.update(dann[a]);
\r
125 Alignment[] al = set.getAlignment();
\r
127 for (int a = 0; a < al.length; a++) {
\r
128 if (al[a].isNewInDocument() || al[a].isUpdated()) {
\r
129 if (!updateAlignment(al[a])) {
\r
130 log.debug("Calling update(Alignment)");
\r
131 handler.update(al[a]);
\r
137 // Trees associated with dataset sequences
\r
138 if (updateTrees(set.getTree())) {
\r
144 private boolean updateTrees(Tree[] trees) {
\r
145 boolean called = false;
\r
146 if (trees != null) {
\r
147 for (int t = 0; t < trees.length; t++) {
\r
148 if (trees[t].isNewInDocument() || trees[t].isUpdated()) {
\r
149 if (!updateTree(trees[t])) {
\r
150 log.debug("Calling update(tree)");
\r
151 handler.update(trees[t]);
\r
160 private boolean updateDataSetAnnotation(DataSetAnnotations annotations) {
\r
161 boolean called = false;
\r
165 private boolean updateTree(Tree tree) {
\r
170 private boolean updateAlignment(Alignment alignment) {
\r
171 // TODO Auto-generated method stub
\r
175 private boolean updateSequence(Sequence sequence) {
\r
176 // TODO Auto-generated method stub
\r