1 package org.kuali.student.enrollment.class1.lui.service.impl;
2
3 import org.kuali.student.enrollment.class1.lui.dao.LuiDao;
4 import org.kuali.student.enrollment.class1.lui.dao.LuiLuiRelationDao;
5 import org.kuali.student.enrollment.class1.lui.model.LuCodeEntity;
6 import org.kuali.student.enrollment.class1.lui.model.LuiAttributeEntity;
7 import org.kuali.student.enrollment.class1.lui.model.LuiEntity;
8 import org.kuali.student.enrollment.class1.lui.model.LuiIdentifierEntity;
9 import org.kuali.student.enrollment.class1.lui.model.LuiLuiRelationEntity;
10 import org.kuali.student.enrollment.lui.dto.LuiIdentifierInfo;
11 import org.kuali.student.r2.common.dto.AttributeInfo;
12 import org.kuali.student.r2.common.exceptions.AlreadyExistsException;
13 import org.kuali.student.r2.common.exceptions.CircularRelationshipException;
14 import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
15 import org.kuali.student.r2.common.exceptions.DoesNotExistException;
16 import org.kuali.student.r2.common.exceptions.InvalidParameterException;
17 import org.kuali.student.r2.common.exceptions.MissingParameterException;
18 import org.kuali.student.r2.common.exceptions.OperationFailedException;
19 import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
20 import org.kuali.student.r2.common.exceptions.ReadOnlyException;
21 import org.kuali.student.r2.common.exceptions.VersionMismatchException;
22 import org.kuali.student.r2.common.util.constants.LuiServiceConstants;
23 import org.kuali.student.r2.common.util.date.DateFormatters;
24
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 public class LuiTestDataLoader {
32
33
34 public LuiTestDataLoader(LuiDao luiDao, LuiLuiRelationDao luiLuiRelationDao) {
35 this.luiDao = luiDao;
36 this.luiLuiRelationDao = luiLuiRelationDao;
37 }
38
39 private LuiDao luiDao;
40
41 private LuiLuiRelationDao luiLuiRelationDao;
42
43 private String principalId = LuiTestDataLoader.class.getSimpleName();
44
45 public void loadData() throws DoesNotExistException, InvalidParameterException,
46 MissingParameterException, OperationFailedException, PermissionDeniedException,
47 DataValidationErrorException, ReadOnlyException, VersionMismatchException,
48 AlreadyExistsException, CircularRelationshipException {
49 loadLui("Lui-1", "Lui one", "cluId1", "atpId1", "kuali.lui.type.course.offering", "kuali.lui.state.draft", "<p>Lui Desc 101</p>", "Lui Desc 101", "2011-01-01 00:00:00.0", "2011-12-31 00:00:00.0", 200, 50, "ref.url", "LUI-IDENT-2", "lui_one_official", "Chem 123", "attr1", "attr2");
50 loadLui("Lui-2", "Lui rwo", "cluId2", "atpId2", "kuali.lui.type.activity.offering.lecture", "kuali.lui.state.draft", "<p>Lui Desc 201</p>", "Lui Desc 201", "2011-01-01 00:00:00.0", "2011-12-31 00:00:00.0", 200, 50, "ref.url", "LUI-IDENT-3", "lui_two_official", "Phy 123");
51 loadLui("Lui-3", "Lui three", "cluId3", "atpId3", "kuali.lui.type.course.offering", "kuali.lui.state.draft", "<p>Lui Desc 301</p>", "Lui Desc 301 for deletion", "2011-01-01 00:00:00.0", "2011-12-31 00:00:00.0", 200, 50, "ref.url", "lui_three_additional", "lui_three_official", "Bio 123");
52 loadLui("Lui-4", "Lui four", "cluId4", "atpId4", "kuali.lui.type.activity.offering.lecture", "kuali.lui.state.draft", "<p>Lui Desc 401</p>", "Lui Desc 401 for deletion", "2011-01-01 00:00:00.0", "2011-12-31 00:00:00.0", 200, 50, "ref.url", "lui_four_additional", "lui_four_official", "Phil 123");
53 loadLui("Lui-5", "Lui five", "cluId5", "atpId5", "kuali.lui.type.activity.offering.lab", "kuali.lui.state.draft", "<p>Lui Desc 501</p>", "Lui Desc 501", "2011-01-01 00:00:00.0", "2011-12-31 00:00:00.0", 200, 50, "ref.url", "lui_five_additional", "lui_five_official", "XX");
54
55 loadLuiLuiRel("LUILUIREL-1", "2011-01-01 00:00:00.0", "2100-01-01 00:00:00.0", "kuali.lui.lui.relation.state.active", "<p>LUILUIREL-1 Formatted</p>", "LUILUIREL-1 Plain", "Lui-1", "kuali.lui.lui.relation.associated", "Lui-2");
56 loadLuiLuiRel("LUILUIREL-2", "2011-01-01 00:00:00.0", "2100-01-01 00:00:00.0", "kuali.lui.lui.relation.state.active", "<p>LUILUIREL-2 Formatted</p>", "LUILUIREL-2 Plain", "Lui-3", "kuali.lui.lui.relation.associated", "Lui-4");
57 loadLuiLuiRel("LUILUIREL-3", "2011-01-01 00:00:00.0", "2100-01-01 00:00:00.0", "kuali.lui.lui.relation.state.active", "<p>LUILUIREL-3 Formatted</p>", "LUILUIREL-3 Plain", "Lui-5", "kuali.lui.lui.relation.associated", "Lui-2");
58
59 }
60
61 private void loadLui(String id,
62 String name,
63 String cluId,
64 String atpId,
65 String type,
66 String state,
67 String descrFormatted,
68 String descrPlain,
69 String effectiveDate,
70 String expirationDate,
71 Integer maxSeats,
72 Integer minSeats,
73 String refUrl, String additionIden, String officialIdentifier, String officialIdentName, String... luiAttributes)
74 throws DoesNotExistException, InvalidParameterException,
75 MissingParameterException, OperationFailedException, PermissionDeniedException,
76 DataValidationErrorException, ReadOnlyException, AlreadyExistsException {
77 LuiEntity luiEntity = new LuiEntity();
78 luiEntity.setId(id);
79 luiEntity.setCluId(cluId);
80 luiEntity.setAtpId(atpId);
81 luiEntity.setName(name);
82 luiEntity.setLuiType(type);
83 luiEntity.setLuiState(state);
84 luiEntity.setMaxSeats(maxSeats);
85 luiEntity.setMinSeats(minSeats);
86 luiEntity.setReferenceURL(refUrl);
87 luiEntity.setPlain(descrPlain);
88 luiEntity.setCreateId(principalId);
89 Date time;
90 luiEntity.setCreateTime(time = new Date());
91
92 luiEntity.setUpdateId(principalId);
93 luiEntity.setUpdateTime(time);
94
95 luiEntity.setEffectiveDate(time);
96
97 LuiIdentifierEntity luiIdent = new LuiIdentifierEntity();
98 luiIdent.setLui(luiEntity);
99 luiIdent.setId(additionIden);
100 luiIdent.setCreateId("TESTDATALOADER");
101 luiIdent.setCreateTime(new Date());
102
103 LuiIdentifierEntity luiOfficialIdent = new LuiIdentifierEntity();
104 luiOfficialIdent.setLui(luiEntity);
105 luiOfficialIdent.setId(officialIdentifier);
106 luiOfficialIdent.setShortName(officialIdentName);
107 luiOfficialIdent.setType(LuiServiceConstants.LUI_IDENTIFIER_OFFICIAL_TYPE_KEY);
108 luiOfficialIdent.setCreateId("TESTDATALOADER");
109 luiOfficialIdent.setCreateTime(new Date());
110 List<LuiIdentifierEntity> luiIdents = new ArrayList<LuiIdentifierEntity>();
111
112 luiIdents.add(luiIdent);
113 luiIdents.add(luiOfficialIdent);
114 luiEntity.setIdentifiers(luiIdents);
115
116
117 LuCodeEntity luCode = new LuCodeEntity();
118 luCode.setId("Lu-Code-" + id);
119 luCode.setCreateId(principalId);
120 luCode.setCreateTime(new Date());
121 luCode.setLui(luiEntity);
122
123 ArrayList<LuCodeEntity> luCodes = new ArrayList<LuCodeEntity>();
124 luCodes.add(luCode);
125 luiEntity.setLuiCodes(luCodes);
126
127
128 ArrayList<String> resultValueGroupKeys = new ArrayList<String>();
129 resultValueGroupKeys.add("Lu-Rvgr-" + id);
130 luiEntity.setResultValuesGroupKeys(resultValueGroupKeys);
131
132
133 if (luiAttributes != null && luiAttributes.length > 0) {
134 for (String attr : luiAttributes) {
135 LuiAttributeEntity luiAttr = new LuiAttributeEntity(new AttributeInfo(attr, attr), luiEntity);
136 luiEntity.getAttributes().add(luiAttr);
137 }
138 }
139
140 luiDao.persist(luiEntity);
141 }
142
143 private LuiIdentifierInfo getOfficialIdentifier(String luiId) {
144 loadIdentifiers();
145 if (!identifiers.containsKey(luiId)) {
146 return null;
147 }
148 for (LuiIdentifierInfo info : identifiers.get(luiId)) {
149 if (info.getTypeKey().equals(LuiServiceConstants.LUI_IDENTIFIER_OFFICIAL_TYPE_KEY)) {
150 return info;
151 }
152 }
153 return null;
154 }
155
156 private List<LuiIdentifierInfo> getAlternateIdentifiers(String luiId) {
157 loadIdentifiers();
158 List<LuiIdentifierInfo> list = new ArrayList<LuiIdentifierInfo>();
159 if (!identifiers.containsKey(luiId)) {
160 return list;
161 }
162 for (LuiIdentifierInfo info : identifiers.get(luiId)) {
163 if (!info.getTypeKey().equals(LuiServiceConstants.LUI_IDENTIFIER_OFFICIAL_TYPE_KEY)) {
164 list.add(info);
165 }
166 }
167 return list;
168 }
169
170 private Map<String, List<LuiIdentifierInfo>> identifiers = null;
171
172 private void loadIdentifiers() {
173 if (identifiers != null) {
174 return;
175 }
176 identifiers = new LinkedHashMap<String, List<LuiIdentifierInfo>>();
177 addIdentifier("LUI-IDENT-1", "CHEM123", "CHEM", "123", "Chemistry 123", "Chem 123", "kuali.lui.identifier.type.official", null, "Lui-1");
178 addIdentifier("LUI-IDENT-2", "CHEM456", "CHEM", "456", "Chemistry 456", "Chem 456", "kuali.lui.identifier.type.cross-listed", null, "Lui-1");
179 addIdentifier("LUI-IDENT-3", "BIO123", "BIO", "123", "Biology 123", "Bio 123", "kuali.lui.identifier.type.official", null, "Lui-2");
180 addIdentifier("LUI-IDENT-4", "BIO456", "BIO", "456", "Biology 456", "Bio 456", "kuali.lui.identifier.type.cross-listed", null, "Lui-2");
181 addIdentifier("LUI-IDENT-5", "GEOG123", "GEOG", "123", "Geography 123", "Geog 123", "kuali.lui.identifier.type.official", null, "Lui-3");
182 addIdentifier("LUI-IDENT-6", "MATH123", "MATH", "123", "Mathematics 123", "Math 123", "kuali.lui.identifier.type.official", null, "Lui-4");
183 addIdentifier("LUI-IDENT-7", "MATH123", "MATH", "456", "Mathematics 456", "Math 456", "kuali.lui.identifier.type.cross-listed", null, "Lui-4");
184 }
185
186 private void addIdentifier(String id,
187 String code,
188 String division,
189 String suffixCode,
190 String longName,
191 String shortName,
192 String type,
193 String variation,
194 String luiId) {
195 LuiIdentifierInfo info = new LuiIdentifierInfo();
196 info.setId(id);
197 info.setCode(code);
198 info.setDivision(division);
199 info.setSuffixCode(suffixCode);
200 info.setLongName(longName);
201 info.setShortName(shortName);
202 info.setStateKey(LuiServiceConstants.LUI_IDENTIFIER_ACTIVE_STATE_KEY);
203 info.setTypeKey(type);
204 info.setVariation(variation);
205 if (!identifiers.containsKey(luiId)) {
206 identifiers.put(luiId, new ArrayList<LuiIdentifierInfo>());
207 }
208 identifiers.get(luiId).add(info);
209 }
210
211 private void loadLuiLuiRel(String id,
212 String effectiveDate,
213 String expirationDate,
214 String state,
215 String descrFormatted,
216 String descrPlain,
217 String luiId,
218 String type,
219 String relatedLuiId)
220 throws DoesNotExistException, InvalidParameterException,
221 MissingParameterException, OperationFailedException, PermissionDeniedException,
222 DataValidationErrorException, ReadOnlyException, AlreadyExistsException,
223 CircularRelationshipException {
224 LuiLuiRelationEntity entity = new LuiLuiRelationEntity();
225 entity.setId(id);
226 entity.setLui(luiDao.find(luiId));
227 entity.setRelatedLui(luiDao.find(relatedLuiId));
228 entity.setLuiLuiRelationType(type);
229 entity.setLuiLuiRelationState(state);
230 entity.setEffectiveDate(str2Date(effectiveDate, id));
231 entity.setExpirationDate(str2Date(expirationDate, id));
232 entity.setDescrPlain(descrPlain);
233 entity.setDescrPlain(descrFormatted);
234
235 entity.setCreateId(principalId);
236 Date time;
237 entity.setCreateTime(time = new Date());
238
239 entity.setUpdateId(principalId);
240 entity.setUpdateTime(time);
241
242 luiLuiRelationDao.persist(entity);
243
244 }
245
246 private Date str2Date(String str, String context) {
247 if (str == null) {
248 return null;
249 }
250 try {
251 Date date = DateFormatters.DEFAULT_YEAR_MONTH_24HOUR_MILLISECONDS_FORMATTER.parse(str);
252 return date;
253 } catch (IllegalArgumentException ex) {
254 throw new IllegalArgumentException("Bad date " + str + " in " + context);
255 }
256 }
257 }