1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.javagen.agile.db.visitor;
17
18 import java.util.Set;
19
20 import org.javagen.agile.core.Generator;
21 import org.javagen.agile.core.model.Model;
22 import org.javagen.agile.core.util.ModelUtil;
23 import org.javagen.agile.core.util.StringUtil;
24 import org.javagen.agile.db.model.Column;
25 import org.javagen.agile.db.model.ColumnReference;
26 import org.javagen.agile.db.model.Database;
27 import org.javagen.agile.db.model.FkConstraint;
28 import org.javagen.agile.db.model.PkColumn;
29 import org.javagen.agile.db.model.Table;
30 import org.javagen.agile.db.model.UniqueConstraint;
31
32 /***
33 * Walks the database model tree, calling each visitor method for types in the itinerary.
34 *
35 * @author Richard Easterling
36 */
37 public class DefaultDatabaseVisitor implements DatabaseVisitor, Generator {
38
39 protected Set<String> itinerary;
40 protected String relativeInputPath;
41
42 public DefaultDatabaseVisitor() {
43 this.setItinerary(ALL_MODEL_TYPES);
44 }
45
46
47
48
49
50 /***
51 * Walks the database model tree.
52 *
53 * @param input must be either a <code>Database</code> model or if a <code>relativeInputPath</code>
54 * is provided, should result in a <code>Database</code> model after navigating the <code>relativeInputPath</code>
55 * form the input node.
56 * @return the input node
57 */
58 public Model gen(Model input) {
59 Model result = input;
60 if (relativeInputPath!=null)
61 input = ModelUtil.modelPath(input, relativeInputPath);
62 if (input==null)
63 throw new NullPointerException("input type must be: "+Database.class.getName());
64 if ( ! (input instanceof Database) )
65 throw new IllegalArgumentException("input type must be: "+Database.class.getName()+", not "+input);
66 DatabaseWalker.walk( (Database)input, this);
67 return result;
68 }
69
70
71
72
73
74 public Set<String> itinerary() { return itinerary; }
75
76 public void visit(Database database) { }
77
78 public void visit(Table table) { }
79
80 public void visit(Column column) { }
81
82 public void visit(ColumnReference columnReference) { }
83
84 public void visit(FkConstraint fkConstraint) { }
85
86 public void visit(PkColumn pkColumn) { }
87
88 public void visit(UniqueConstraint uniqueConstraint) { }
89
90 public void visit(Model model) { }
91
92
93
94
95
96 public void setItinerary(Set<String> itinerary) {
97 this.itinerary = itinerary;
98 }
99
100 public void setItinerary(String[] modelTypeNames) {
101 setItinerary(StringUtil.toStringSet(modelTypeNames));
102 }
103
104 public String getRelativeInputPath() {
105 return relativeInputPath;
106 }
107
108 public void setRelativeInputPath(String relativeInputPath) {
109 this.relativeInputPath = relativeInputPath;
110 }
111
112 }