View Javadoc

1   /*
2    * Copyright 2006 Outsource Cafe, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the 'License')
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an 'AS IS' BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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      // Generator interface methods:
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      // DatabaseVisitor interface methods:
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      // getters and setters:
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 }