1 | |
package org.kuali.student.enrollment.acal; |
2 | |
|
3 | |
|
4 | |
import java.text.SimpleDateFormat; |
5 | |
import java.util.Calendar; |
6 | |
import java.util.Date; |
7 | |
import java.util.GregorianCalendar; |
8 | |
import java.util.HashMap; |
9 | |
import java.util.HashSet; |
10 | |
import java.util.Map; |
11 | |
import java.util.Set; |
12 | |
|
13 | 0 | public class SqlGenerator { |
14 | 0 | private String userId = "admin"; |
15 | |
private static final String ATP_STATE = "kuali.atp.state.Official"; |
16 | |
private static final String MILESTONE_STATE = "kuali.milestone.state.Official"; |
17 | |
private static final String ATPATPRELATION_STATE = "kuali.atp.atp.relation.state.active"; |
18 | |
|
19 | 0 | private Set<String> holiCals = new HashSet<String>(); |
20 | |
|
21 | |
public StringBuilder getSqlForAcademicCalendar (StringBuilder builder, Atp acal) { |
22 | 0 | if (null == builder) { |
23 | 0 | builder = new StringBuilder(); |
24 | |
} |
25 | |
|
26 | 0 | Calendar cal = new GregorianCalendar(); |
27 | 0 | cal.setTime(acal.getStartDate()); |
28 | 0 | cal.add(Calendar.YEAR, -1); |
29 | 0 | cal.set(Calendar.MONTH, Calendar.JANUARY); |
30 | 0 | cal.set(Calendar.DATE, 1); |
31 | 0 | Date createDate = cal.getTime(); |
32 | |
|
33 | |
|
34 | 0 | getAtpInsert(builder, acal, createDate); |
35 | |
|
36 | 0 | for (Atp holiCal : acal.getHolidayCalendars()) { |
37 | 0 | if (!holiCals.contains(holiCal.getId())) { |
38 | |
|
39 | 0 | getAtpInsert(builder, holiCal, createDate); |
40 | |
} |
41 | |
|
42 | |
|
43 | 0 | getAtpAtpRelationSql(builder, acal, holiCal, "kuali.atp.atp.relation.associated", createDate); |
44 | |
|
45 | 0 | if (!holiCals.contains(holiCal.getId())) { |
46 | 0 | for (Milestone holiday : holiCal.getMilestones()) { |
47 | |
|
48 | 0 | getMilestoneInsert(builder, holiday, createDate); |
49 | |
|
50 | 0 | getAtpMilestoneRelationSql(builder, holiCal, holiday, createDate); |
51 | |
} |
52 | 0 | holiCals.add(holiCal.getId()); |
53 | |
} |
54 | |
} |
55 | |
|
56 | 0 | for (Atp term : acal.getTerms()) { |
57 | |
|
58 | 0 | getAtpInsert(builder, term, createDate); |
59 | |
|
60 | 0 | getAtpAtpRelationSql(builder, acal, term, "kuali.atp.atp.relation.includes", createDate); |
61 | |
|
62 | 0 | for (Milestone keydate : term.getMilestones()) { |
63 | |
|
64 | 0 | getMilestoneInsert(builder, keydate, createDate); |
65 | |
|
66 | 0 | getAtpMilestoneRelationSql(builder, term, keydate, createDate); |
67 | |
} |
68 | |
|
69 | 0 | for (Atp subTerm : term.getTerms()) { |
70 | |
|
71 | 0 | getAtpInsert(builder, subTerm, createDate); |
72 | |
|
73 | 0 | getAtpAtpRelationSql(builder, term, subTerm, "kuali.atp.atp.relation.includes", createDate); |
74 | |
|
75 | 0 | for (Milestone keydate : subTerm.getMilestones()) { |
76 | |
|
77 | 0 | getMilestoneInsert(builder, keydate, createDate); |
78 | |
|
79 | 0 | getAtpMilestoneRelationSql(builder, subTerm, keydate, createDate); |
80 | |
} |
81 | |
} |
82 | |
} |
83 | |
|
84 | 0 | return builder; |
85 | |
} |
86 | |
|
87 | |
private StringBuilder getAtpInsert(StringBuilder builder, Atp atp, Date created) { |
88 | 0 | StringBuilder statement = new StringBuilder(); |
89 | 0 | statement.append("INSERT INTO KSEN_ATP "); |
90 | 0 | statement.append("( ID"); |
91 | 0 | statement.append(", OBJ_ID"); |
92 | 0 | statement.append(", ATP_TYPE"); |
93 | 0 | statement.append(", ATP_STATE"); |
94 | 0 | statement.append(", NAME"); |
95 | 0 | statement.append(", DESCR_PLAIN"); |
96 | 0 | statement.append(", DESCR_FORMATTED"); |
97 | 0 | statement.append(", ATP_CD"); |
98 | 0 | statement.append(", END_DT"); |
99 | 0 | statement.append(", START_DT"); |
100 | 0 | statement.append(", ADMIN_ORG_ID"); |
101 | 0 | statement.append(", VER_NBR"); |
102 | 0 | statement.append(", CREATETIME"); |
103 | 0 | statement.append(", CREATEID"); |
104 | 0 | statement.append(", UPDATETIME"); |
105 | 0 | statement.append(", UPDATEID"); |
106 | 0 | statement.append(") VALUES ("); |
107 | 0 | statement.append(" ").append(getInput(atp.getId())); |
108 | 0 | statement.append(", ").append(getInput(getUUID())); |
109 | 0 | statement.append(", ").append(getInput(atp.getType())); |
110 | 0 | statement.append(", ").append(getInput(ATP_STATE)); |
111 | 0 | statement.append(", ").append(getInput(atp.getName())); |
112 | 0 | statement.append(", ").append(getInput(atp.getDescriptionPlain())); |
113 | 0 | statement.append(", ").append(getInput(atp.getDescriptionFormatted())); |
114 | 0 | statement.append(", ").append(getInput(atp.getAtpCode())); |
115 | 0 | statement.append(", ").append(getInput(atp.getEndDate())); |
116 | 0 | statement.append(", ").append(getInput(atp.getStartDate())); |
117 | 0 | statement.append(", ").append(getInput(atp.getAdminOrgId())); |
118 | 0 | statement.append(", 0"); |
119 | 0 | statement.append(", ").append(getInput(created)); |
120 | 0 | statement.append(", ").append(getInput(userId)); |
121 | 0 | statement.append(", ").append(getInput(created)); |
122 | 0 | statement.append(", ").append(getInput(userId)); |
123 | 0 | statement.append(")"); |
124 | 0 | statement.append("\n"); |
125 | 0 | statement.append("/\n"); |
126 | |
|
127 | 0 | if (builder == null) { |
128 | 0 | return statement; |
129 | |
} |
130 | 0 | return builder.append(statement); |
131 | |
} |
132 | |
|
133 | |
private StringBuilder getMilestoneInsert(StringBuilder builder, Milestone milestone, Date created) { |
134 | 0 | StringBuilder statement = new StringBuilder(); |
135 | 0 | statement.append("INSERT INTO KSEN_MSTONE "); |
136 | 0 | statement.append("( ID"); |
137 | 0 | statement.append(", OBJ_ID"); |
138 | 0 | statement.append(", MSTONE_TYPE"); |
139 | 0 | statement.append(", MSTONE_STATE"); |
140 | 0 | statement.append(", NAME"); |
141 | 0 | statement.append(", DESCR_PLAIN"); |
142 | 0 | statement.append(", DESCR_FORMATTED"); |
143 | 0 | statement.append(", IS_ALL_DAY"); |
144 | 0 | statement.append(", IS_INSTRCT_DAY"); |
145 | 0 | statement.append(", IS_RELATIVE"); |
146 | 0 | statement.append(", RELATIVE_ANCHOR_MSTONE_ID"); |
147 | 0 | statement.append(", IS_DATE_RANGE"); |
148 | 0 | statement.append(", START_DT"); |
149 | 0 | statement.append(", END_DT"); |
150 | 0 | statement.append(", VER_NBR"); |
151 | 0 | statement.append(", CREATETIME"); |
152 | 0 | statement.append(", CREATEID"); |
153 | 0 | statement.append(", UPDATETIME"); |
154 | 0 | statement.append(", UPDATEID"); |
155 | 0 | statement.append(") VALUES ("); |
156 | 0 | statement.append(" ").append(getInput(milestone.getId())); |
157 | 0 | statement.append(", ").append(getInput(getUUID())); |
158 | 0 | statement.append(", ").append(getInput(milestone.getType())); |
159 | 0 | statement.append(", ").append(getInput(MILESTONE_STATE)); |
160 | 0 | statement.append(", ").append(getInput(milestone.getName())); |
161 | 0 | statement.append(", ").append(getInput(milestone.getDescriptionPlain())); |
162 | 0 | statement.append(", ").append(getInput(milestone.getDescriptionFormatted())); |
163 | 0 | statement.append(", ").append(getInput(milestone.isAllDay())); |
164 | 0 | statement.append(", 0"); |
165 | 0 | statement.append(", ").append(getInput(milestone.isRelative())); |
166 | 0 | statement.append(", ").append(getInput(milestone.getRelativeMilestoneId())); |
167 | 0 | statement.append(", ").append(getInput(milestone.isDateRange())); |
168 | 0 | statement.append(", ").append(getInput(milestone.getStartDate())); |
169 | 0 | statement.append(", ").append(getInput(milestone.getEndDate())); |
170 | 0 | statement.append(", 0"); |
171 | 0 | statement.append(", ").append(getInput(created)); |
172 | 0 | statement.append(", ").append(getInput(userId)); |
173 | 0 | statement.append(", ").append(getInput(created)); |
174 | 0 | statement.append(", ").append(getInput(userId)); |
175 | 0 | statement.append(")"); |
176 | 0 | statement.append("\n"); |
177 | 0 | statement.append("/\n"); |
178 | |
|
179 | 0 | if (builder == null) { |
180 | 0 | return statement; |
181 | |
} |
182 | 0 | return builder.append(statement); |
183 | |
} |
184 | |
|
185 | |
public StringBuilder getAtpAtpRelationSql(StringBuilder builder, Atp atp, Atp relatedAtp, String relationType, Date created) { |
186 | 0 | StringBuilder statement = new StringBuilder(); |
187 | 0 | statement.append("INSERT INTO KSEN_ATPATP_RELTN "); |
188 | 0 | statement.append("( ID"); |
189 | 0 | statement.append(", OBJ_ID"); |
190 | 0 | statement.append(", ATP_TYPE"); |
191 | 0 | statement.append(", ATP_STATE"); |
192 | 0 | statement.append(", ATP_ID"); |
193 | 0 | statement.append(", RELATED_ATP_ID"); |
194 | 0 | statement.append(", EFF_DT"); |
195 | 0 | statement.append(", EXPIR_DT"); |
196 | 0 | statement.append(", VER_NBR"); |
197 | 0 | statement.append(", CREATETIME"); |
198 | 0 | statement.append(", CREATEID"); |
199 | 0 | statement.append(", UPDATETIME"); |
200 | 0 | statement.append(", UPDATEID"); |
201 | 0 | statement.append(") VALUES ("); |
202 | 0 | statement.append(" ").append(getInput(atp.getId() + relatedAtp.getId() + "RELATION")); |
203 | 0 | statement.append(", ").append(getInput(getUUID())); |
204 | 0 | statement.append(", ").append(getInput(relationType)); |
205 | 0 | statement.append(", ").append(getInput(ATPATPRELATION_STATE)); |
206 | 0 | statement.append(", ").append(getInput(atp.getId())); |
207 | 0 | statement.append(", ").append(getInput(relatedAtp.getId())); |
208 | 0 | statement.append(", ").append(getInput(created)); |
209 | 0 | statement.append(", ").append(getInput((Date)null)); |
210 | 0 | statement.append(", 0"); |
211 | 0 | statement.append(", ").append(getInput(created)); |
212 | 0 | statement.append(", ").append(getInput(userId)); |
213 | 0 | statement.append(", ").append(getInput(created)); |
214 | 0 | statement.append(", ").append(getInput(userId)); |
215 | 0 | statement.append(")"); |
216 | 0 | statement.append("\n"); |
217 | 0 | statement.append("/\n"); |
218 | |
|
219 | 0 | if (builder == null) { |
220 | 0 | return statement; |
221 | |
} |
222 | 0 | return builder.append(statement); |
223 | |
} |
224 | |
|
225 | |
public StringBuilder getAtpMilestoneRelationSql(StringBuilder builder, Atp atp, Milestone milestone, Date created) { |
226 | 0 | StringBuilder statement = new StringBuilder(); |
227 | 0 | statement.append("INSERT INTO KSEN_ATPMSTONE_RELTN "); |
228 | 0 | statement.append("( ID"); |
229 | 0 | statement.append(", OBJ_ID"); |
230 | 0 | statement.append(", VER_NBR"); |
231 | 0 | statement.append(", CREATEID"); |
232 | 0 | statement.append(", CREATETIME"); |
233 | 0 | statement.append(", UPDATEID"); |
234 | 0 | statement.append(", UPDATETIME"); |
235 | 0 | statement.append(", ATP_ID"); |
236 | 0 | statement.append(", MSTONE_ID"); |
237 | 0 | statement.append(") VALUES ("); |
238 | 0 | statement.append(" ").append(getInput(atp.getId() + milestone.getId() + "RELATION")); |
239 | 0 | statement.append(", ").append(getInput(getUUID())); |
240 | 0 | statement.append(", 0"); |
241 | 0 | statement.append(", ").append(getInput(userId)); |
242 | 0 | statement.append(", ").append(getInput(created)); |
243 | 0 | statement.append(", ").append(getInput(userId)); |
244 | 0 | statement.append(", ").append(getInput(created)); |
245 | 0 | statement.append(", ").append(getInput(atp.getId())); |
246 | 0 | statement.append(", ").append(getInput(milestone.getId())); |
247 | 0 | statement.append(")"); |
248 | 0 | statement.append("\n"); |
249 | 0 | statement.append("/\n"); |
250 | |
|
251 | 0 | if (builder == null) { |
252 | 0 | return statement; |
253 | |
} |
254 | 0 | return builder.append(statement); |
255 | |
} |
256 | |
|
257 | |
private String getInput(String string) { |
258 | 0 | if (string == null) { |
259 | 0 | return "NULL"; |
260 | |
} |
261 | 0 | string = string.replaceAll("'", "''"); |
262 | 0 | return "'"+string+"'"; |
263 | |
} |
264 | |
|
265 | |
private String getInput(Date date) { |
266 | 0 | if (date == null) { |
267 | 0 | return "NULL"; |
268 | |
} |
269 | 0 | SimpleDateFormat timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); |
270 | |
|
271 | 0 | return "TO_TIMESTAMP('" + timestamp.format(date) + "', 'YYYY-MM-DD HH24:MI:SS.FF')"; |
272 | |
} |
273 | |
|
274 | |
private int getInput(boolean value) { |
275 | 0 | return (value) ? 1 : 0; |
276 | |
} |
277 | |
|
278 | |
private String getUUID() { |
279 | 0 | return java.util.UUID.randomUUID().toString(); |
280 | |
} |
281 | |
} |