View Javadoc

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         //Lu Code
117         LuCodeEntity luCode = new LuCodeEntity();
118         luCode.setId("Lu-Code-" + id);
119         luCode.setCreateId(principalId);
120         luCode.setCreateTime(new Date());
121         ArrayList<LuCodeEntity> luCodes = new ArrayList<LuCodeEntity>();
122         luCodes.add(luCode);
123         luiEntity.setLuiCodes(luCodes);
124 
125         //Result Value Group Key
126         ArrayList<String> resultValueGroupKeys = new ArrayList<String>();
127         resultValueGroupKeys.add("Lu-Rvgr-" + id);
128         luiEntity.setResultValuesGroupKeys(resultValueGroupKeys);
129 
130         //Attributes
131         if (luiAttributes != null && luiAttributes.length > 0) {
132             for (String attr : luiAttributes) {
133                 LuiAttributeEntity luiAttr = new LuiAttributeEntity(new AttributeInfo(attr, attr), luiEntity);
134                 luiEntity.getAttributes().add(luiAttr);
135             }
136         }
137 
138         luiDao.persist(luiEntity);
139     }
140 
141     private LuiIdentifierInfo getOfficialIdentifier(String luiId) {
142         loadIdentifiers();
143         if (!identifiers.containsKey(luiId)) {
144             return null;
145         }
146         for (LuiIdentifierInfo info : identifiers.get(luiId)) {
147             if (info.getTypeKey().equals(LuiServiceConstants.LUI_IDENTIFIER_OFFICIAL_TYPE_KEY)) {
148                 return info;
149             }
150         }
151         return null;
152     }
153 
154     private List<LuiIdentifierInfo> getAlternateIdentifiers(String luiId) {
155         loadIdentifiers();
156         List<LuiIdentifierInfo> list = new ArrayList<LuiIdentifierInfo>();
157         if (!identifiers.containsKey(luiId)) {
158             return list;
159         }
160         for (LuiIdentifierInfo info : identifiers.get(luiId)) {
161             if (!info.getTypeKey().equals(LuiServiceConstants.LUI_IDENTIFIER_OFFICIAL_TYPE_KEY)) {
162                 list.add(info);
163             }
164         }
165         return list;
166     }
167 
168     private Map<String, List<LuiIdentifierInfo>> identifiers = null;
169 
170     private void loadIdentifiers() {
171         if (identifiers != null) {
172             return;
173         }
174         identifiers = new LinkedHashMap<String, List<LuiIdentifierInfo>>();
175         addIdentifier("LUI-IDENT-1", "CHEM123", "CHEM", "123", "Chemistry 123", "Chem 123", "kuali.lui.identifier.type.official", null, "Lui-1");
176         addIdentifier("LUI-IDENT-2", "CHEM456", "CHEM", "456", "Chemistry 456", "Chem 456", "kuali.lui.identifier.type.cross-listed", null, "Lui-1");
177         addIdentifier("LUI-IDENT-3", "BIO123", "BIO", "123", "Biology 123", "Bio 123", "kuali.lui.identifier.type.official", null, "Lui-2");
178         addIdentifier("LUI-IDENT-4", "BIO456", "BIO", "456", "Biology 456", "Bio 456", "kuali.lui.identifier.type.cross-listed", null, "Lui-2");
179         addIdentifier("LUI-IDENT-5", "GEOG123", "GEOG", "123", "Geography 123", "Geog 123", "kuali.lui.identifier.type.official", null, "Lui-3");
180         addIdentifier("LUI-IDENT-6", "MATH123", "MATH", "123", "Mathematics 123", "Math 123", "kuali.lui.identifier.type.official", null, "Lui-4");
181         addIdentifier("LUI-IDENT-7", "MATH123", "MATH", "456", "Mathematics 456", "Math 456", "kuali.lui.identifier.type.cross-listed", null, "Lui-4");
182     }
183 
184     private void addIdentifier(String id,
185                                String code,
186                                String division,
187                                String suffixCode,
188                                String longName,
189                                String shortName,
190                                String type,
191                                String variation,
192                                String luiId) {
193         LuiIdentifierInfo info = new LuiIdentifierInfo();
194         info.setId(id);
195         info.setCode(code);
196         info.setDivision(division);
197         info.setSuffixCode(suffixCode);
198         info.setLongName(longName);
199         info.setShortName(shortName);
200         info.setStateKey(LuiServiceConstants.LUI_IDENTIFIER_ACTIVE_STATE_KEY);
201         info.setTypeKey(type);
202         info.setVariation(variation);
203         if (!identifiers.containsKey(luiId)) {
204             identifiers.put(luiId, new ArrayList<LuiIdentifierInfo>());
205         }
206         identifiers.get(luiId).add(info);
207     }
208 
209     private void loadLuiLuiRel(String id,
210                                String effectiveDate,
211                                String expirationDate,
212                                String state,
213                                String descrFormatted,
214                                String descrPlain,
215                                String luiId,
216                                String type,
217                                String relatedLuiId)
218             throws DoesNotExistException, InvalidParameterException,
219             MissingParameterException, OperationFailedException, PermissionDeniedException,
220             DataValidationErrorException, ReadOnlyException, AlreadyExistsException,
221             CircularRelationshipException {
222         LuiLuiRelationEntity entity = new LuiLuiRelationEntity();
223         entity.setId(id);
224         entity.setLui(luiDao.find(luiId));
225         entity.setRelatedLui(luiDao.find(relatedLuiId));
226         entity.setLuiLuiRelationType(type);
227         entity.setLuiLuiRelationState(state);
228         entity.setEffectiveDate(str2Date(effectiveDate, id));
229         entity.setExpirationDate(str2Date(expirationDate, id));
230         entity.setDescrPlain(descrPlain);
231         entity.setDescrPlain(descrFormatted);
232 
233         entity.setCreateId(principalId);
234         Date time;
235         entity.setCreateTime(time = new Date());
236 
237         entity.setUpdateId(principalId);
238         entity.setUpdateTime(time);
239 
240         luiLuiRelationDao.persist(entity);
241 
242     }
243 
244     private Date str2Date(String str, String context) {
245         if (str == null) {
246             return null;
247         }
248         try {
249             Date date = DateFormatters.DEFAULT_YEAR_MONTH_24HOUR_MILLISECONDS_FORMATTER.parse(str);
250             return date;
251         } catch (IllegalArgumentException ex) {
252             throw new IllegalArgumentException("Bad date " + str + " in " + context);
253         }
254     }
255 }