View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.common.impex.model.util;
17  
18  import org.kuali.common.impex.model.DataTypeSize;
19  import org.kuali.common.impex.model.compare.ColumnDifference;
20  import org.kuali.common.impex.model.compare.ForeignKeyDifference;
21  import org.kuali.common.impex.model.compare.IndexDifference;
22  import org.kuali.common.impex.model.compare.SchemaDifference;
23  import org.kuali.common.impex.model.compare.SequenceDifference;
24  import org.kuali.common.impex.model.compare.TableDifference;
25  import org.kuali.common.impex.model.compare.TableDifferenceType;
26  import org.kuali.common.impex.model.compare.UniqueConstraintDifference;
27  import org.kuali.common.impex.model.compare.ViewDifference;
28  
29  public class DifferenceUtils {
30  	public static final String LABEL_SEPARATOR = ": ";
31  	public static final String FOUND_IN_LABEL = "FOUND IN: ";
32  	public static final String NOT_FOUND_IN_LABEL = "NOT FOUND IN: ";
33  	public static final String DOT = ".";
34  	public static final String COMMA = ".";
35  	public static final String SPACE = " ";
36  	public static final String NOT_EQUAL_SEPARATOR = " != ";
37  	public static final String OPEN_PAD_PAREN = " ( ";
38  	public static final String CLOSE_PAD_PAREN = " ) ";
39  
40  	public static String buildDifferenceToken(String label, SchemaDifference difference) {
41  		StringBuilder sb = new StringBuilder();
42  
43  		sb.append(label);
44  		sb.append(SPACE);
45  		sb.append(buildSchema1Token(difference));
46  		sb.append(NOT_EQUAL_SEPARATOR);
47  		sb.append(buildSchema2Token(difference));
48  
49  		return sb.toString();
50  	}
51  
52      /**
53       * Build a token to label the first "side" of a difference.
54       *
55       * This method will append applicable labels from instances of subclasses of TableDifference as well.
56       *
57       * @param t the TableDifference containing table data
58       * @return a String representation of the first side of the given table data
59       */
60  	public static String buildTable1DifferenceToken(TableDifference t) {
61  		StringBuilder sb = new StringBuilder();
62  		sb.append(t.getTable1().getName());
63  
64  		if (t instanceof ColumnDifference) {
65  			ColumnDifference c = (ColumnDifference) t;
66  			sb.append(DOT);
67  			sb.append(c.getColumn1().getName());
68  			if (TableDifferenceType.COLUMN_DATA_TYPE.equals(c.getType())) {
69  				sb.append(parenWrapToken(c.getColumn1().getType().toString()));
70  			} else if (TableDifferenceType.COLUMN_DATA_TYPE_SIZE.equals(c.getType())) {
71  				sb.append(parenWrapToken(buildTypeSizeToken(c.getColumn1().getSize())));
72  			}
73  		} else if (t instanceof IndexDifference) {
74  			IndexDifference i = (IndexDifference) t;
75  			sb.append(DOT);
76  			sb.append(i.getIndex1().getName());
77  		} else if (t instanceof UniqueConstraintDifference) {
78  			UniqueConstraintDifference u = (UniqueConstraintDifference) t;
79  			sb.append(DOT);
80  			sb.append(u.getUnique1().getName());
81  		}
82  
83  		return sb.toString();
84  	}
85  
86      /**
87       * Build a token to label the first "side" of a schema difference.
88       *
89       * @param difference the SchemaDifference containing difference data
90       * @return a String representation of the first side of the given difference data
91       */
92  	public static String buildSchema1Token(SchemaDifference difference) {
93  		StringBuilder sb = new StringBuilder();
94  		sb.append(difference.getSchema1().getName());
95  		sb.append(DOT);
96  		if (difference instanceof TableDifference) {
97  			sb.append(buildTable1DifferenceToken((TableDifference) difference));
98  		}
99  
100 		if (difference instanceof ViewDifference) {
101 			sb.append(((ViewDifference) difference).getView1().getName());
102 		}
103 
104 		if (difference instanceof ForeignKeyDifference) {
105 			sb.append(((ForeignKeyDifference) difference).getForeignKey1().getName());
106 		}
107 
108 		if (difference instanceof SequenceDifference) {
109 			sb.append(((SequenceDifference) difference).getSequence1().getName());
110 		}
111 
112 		return sb.toString();
113 	}
114 
115     /**
116      * Build a token to label the second "side" of a schema difference.
117      *
118      * @param difference the SchemaDifference containing difference data
119      * @return a String representation of the second side of the given difference data
120      */
121 	public static String buildSchema2Token(SchemaDifference difference) {
122 		StringBuilder sb = new StringBuilder();
123 		sb.append(difference.getSchema2().getName());
124 		sb.append(DOT);
125 		if (difference instanceof TableDifference) {
126 			sb.append(buildTable2DifferenceToken((TableDifference) difference));
127 		}
128 
129 		if (difference instanceof ViewDifference) {
130 			sb.append(((ViewDifference) difference).getView2().getName());
131 		}
132 
133 		if (difference instanceof ForeignKeyDifference) {
134 			sb.append(((ForeignKeyDifference) difference).getForeignKey2().getName());
135 		}
136 
137 		if (difference instanceof SequenceDifference) {
138 			sb.append(((SequenceDifference) difference).getSequence2().getName());
139 		}
140 
141 		return sb.toString();
142 	}
143 
144     /**
145      * Build a token to label the second "side" of a difference.
146      *
147      * This method will append applicable labels from instances of subclasses of TableDifference as well.
148      *
149      * @param t the TableDifference containing table data
150      * @return a String representation of the second side of the given table data
151      */
152 	public static String buildTable2DifferenceToken(TableDifference t) {
153 		StringBuilder sb = new StringBuilder();
154 		sb.append(t.getTable2().getName());
155 
156 		if (t instanceof ColumnDifference) {
157 			ColumnDifference c = (ColumnDifference) t;
158 			sb.append(DOT);
159 			sb.append(c.getColumn2().getName());
160 			if (TableDifferenceType.COLUMN_DATA_TYPE.equals(c.getType())) {
161 				sb.append(parenWrapToken(c.getColumn2().getType().toString()));
162 			} else if (TableDifferenceType.COLUMN_DATA_TYPE_SIZE.equals(c.getType())) {
163 				sb.append(parenWrapToken(buildTypeSizeToken(c.getColumn2().getSize())));
164 			}
165 		} else if (t instanceof IndexDifference) {
166 			IndexDifference i = (IndexDifference) t;
167 			sb.append(DOT);
168 			sb.append(i.getIndex2().getName());
169 		} else if (t instanceof UniqueConstraintDifference) {
170 			UniqueConstraintDifference u = (UniqueConstraintDifference) t;
171 			sb.append(DOT);
172 			sb.append(u.getUnique2().getName());
173 		}
174 
175 		return sb.toString();
176 	}
177 
178     /**
179      * Build a token representing a missing schema element
180      *
181      * @param typeLabel label for the type of difference
182      * @param foundToken token labeling the found element
183      * @param notFoundToken token labeling the missing element
184      * @return a token describing with the following format;
185      *
186      * [typeLabel] : FOUND IN : [foundToken] NOT FOUND IN: [notFoundToken]
187      *
188      */
189 	public static String buildMissingElementToken(String typeLabel, String foundToken, String notFoundToken) {
190 		StringBuilder sb = new StringBuilder();
191 
192 		sb.append(typeLabel);
193 		sb.append(SPACE);
194 		sb.append(LABEL_SEPARATOR);
195 		sb.append(FOUND_IN_LABEL);
196 		sb.append(foundToken);
197 		sb.append(SPACE);
198 		sb.append(NOT_FOUND_IN_LABEL);
199 		sb.append(notFoundToken);
200 
201 		return sb.toString();
202 	}
203 
204     /**
205      * Wraps a token in space padded parenthesis
206      *
207      * @param s input String
208      * @return the input surrounded by space-padded parentheses
209      */
210 	public static String parenWrapToken(String s) {
211 		return OPEN_PAD_PAREN + s + CLOSE_PAD_PAREN;
212 	}
213 
214 	public static String buildTypeSizeToken(DataTypeSize typeSize) {
215         StringBuilder typeSizeString = new StringBuilder();
216         if (typeSize != null) {
217             typeSizeString.append(typeSize.getValue().toString());
218             if (typeSize.isScaleSet()) {
219                 typeSizeString.append(COMMA).append(typeSize.getScale());
220             }
221         }
222 
223         return typeSizeString.toString();
224     }
225 }