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.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.javagen.agile.db.model.Column;
24  import org.javagen.agile.db.model.PkColumn;
25  
26  /***
27   * Creates a set of unique column types based on column type, size (i.e. precision) and scale.
28   * 
29   * @author reaster
30   *
31   */
32  public class UniqueColumnTypeVisitor extends DefaultDatabaseVisitor {
33  
34  	private Map<Integer, Column> map = new HashMap<Integer, Column>();
35  	private Column tempColumn = new Column(); //optimization, not thread safe!
36  	private boolean includeDefaultValue = false;
37  	
38      public UniqueColumnTypeVisitor() {
39          this.setItinerary(new String[] {Column.DEFAULT_MODEL_TYPE,PkColumn.DEFAULT_MODEL_TYPE});
40      }
41          
42  	public void visit(Column column) {
43  		tempColumn = typeClone(column, tempColumn);
44  		Column uniqueColumn = map.get(tempColumn.hashCode());
45  		if (uniqueColumn==null) {
46  			uniqueColumn = typeClone(tempColumn, new Column());
47  			map.put(uniqueColumn.hashCode(), uniqueColumn);
48  		}
49  	}
50  
51  	public void visit(PkColumn pkColumn) {
52  		tempColumn = typeClone(pkColumn, tempColumn);
53  		Column uniqueColumn = map.get(tempColumn.hashCode());
54  		if (uniqueColumn==null) {
55  			uniqueColumn = typeClone(tempColumn, new Column());
56  			map.put(uniqueColumn.hashCode(), uniqueColumn);
57  		}
58  	}
59  
60  	private Column typeClone(Column source, Column target) {
61  		source.copyTo(target);
62  		target.setName(null);
63  		target.setNotNull(null);
64  		target.setUnique(null);
65  		if ( ! includeDefaultValue )
66  			target.setDefaultValue(null);
67  		return target;
68  	}
69  	
70  	public String toString() {
71  		StringBuilder sb = new StringBuilder();
72  		for(Column col : map.values()) {
73  			toString(col,sb);
74  			sb.append(" = ").append(col.toString());
75  			sb.append('\n');
76  		}
77  		return sb.toString();
78  	}
79  
80  	public String toString(Column col) {
81  		StringBuilder sb = new StringBuilder();
82  		toString(col, sb);
83  		return sb.toString();
84  	}
85  
86  	public void toString(Column col, StringBuilder sb) {
87  		sb.append(col.getDbType().name());
88  		sb.append('-').append( col.getColumnSize()==null ? "X" : col.getColumnSize().toString());
89  		sb.append('-').append( col.getScale()==null ? "X" : col.getScale().toString());
90  		if (includeDefaultValue && col.getDefaultValue()!=null)
91  			sb.append('-').append(col.getDefaultValue().toString().trim());
92  	}
93  	
94  	public List<Column> getList() {
95  		List<Column> list = new ArrayList<Column>();
96  		for(Column col : map.values()) {
97  			col.setName(toString(col));
98  			list.add(col);
99  		}
100 		return list;
101 	}
102 }