applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / objects / DocumentUpdaterEngine.java
1 /*\r
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
5  * \r
6  * Earlier versions have also been incorporated into Jalview version 2.4 \r
7  * since 2008, and TOPALi version 2 since 2007.\r
8  * \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
13  *  \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
18  * \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
21  */\r
22 package uk.ac.vamsas.objects;\r
23 \r
24 import uk.ac.vamsas.client.IClientDocument;\r
25 import uk.ac.vamsas.objects.core.*;\r
26 \r
27 /**\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
31  * \r
32  * @author JimP\r
33  * \r
34  */\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
38 \r
39   private IDocumentUpdater handler;\r
40 \r
41   /**\r
42    * initialise the engine with an implementation of the interface.\r
43    * \r
44    * @param hander\r
45    */\r
46   public DocumentUpdaterEngine(IDocumentUpdater handler) {\r
47     super();\r
48     this.handler = handler;\r
49   }\r
50 \r
51   /**\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
56    */\r
57   public void callHandlers(IClientDocument cdoc) {\r
58     if (cdoc == null) {\r
59       log.debug("Null IClientDocument instance.");\r
60       return;\r
61     }\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
69           }\r
70         }\r
71       }\r
72     } else {\r
73       log.debug("No Document Roots.");\r
74     }\r
75     // TODO: determine if the User, private or public appData has changed\r
76     log.debug("Finished.");\r
77   }\r
78 \r
79   private boolean updateRoot(VAMSAS vamsas) {\r
80     boolean called = false;\r
81     DataSet[] dset = vamsas.getDataSet();\r
82     if (dset != null) {\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
88             called = true;\r
89           }\r
90         }\r
91       }\r
92     }\r
93     return called;\r
94   }\r
95 \r
96   private boolean updateDataset(DataSet set) {\r
97     boolean called = false;\r
98     // Sequences\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
106             called = true;\r
107           }\r
108         }\r
109       }\r
110     }\r
111     // Annotations\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
119             called = true;\r
120           }\r
121         }\r
122       }\r
123     }\r
124     // Alignments\r
125     Alignment[] al = set.getAlignment();\r
126     if (al != null) {\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
132             called = true;\r
133           }\r
134         }\r
135       }\r
136     }\r
137     // Trees associated with dataset sequences\r
138     if (updateTrees(set.getTree())) {\r
139       called = true;\r
140     }\r
141     return called;\r
142   }\r
143 \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
152             called = true;\r
153           }\r
154         }\r
155       }\r
156     }\r
157     return called;\r
158   }\r
159 \r
160   private boolean updateDataSetAnnotation(DataSetAnnotations annotations) {\r
161     boolean called = false;\r
162     return called;\r
163   }\r
164 \r
165   private boolean updateTree(Tree tree) {\r
166     // TODO: if (\r
167     return false;\r
168   }\r
169 \r
170   private boolean updateAlignment(Alignment alignment) {\r
171     // TODO Auto-generated method stub\r
172     return false;\r
173   }\r
174 \r
175   private boolean updateSequence(Sequence sequence) {\r
176     // TODO Auto-generated method stub\r
177     return false;\r
178   }\r
179 }\r