1
2
3
4
5
6
7
8
9
10
11
12
13
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
54
55
56
57
58
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
88
89
90
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
117
118
119
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
146
147
148
149
150
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
180
181
182
183
184
185
186
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
206
207
208
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 }