001    package org.kuali.student.enrollment.class1.lui.service.impl;
002    
003    import org.kuali.student.enrollment.class1.lui.dao.LuiDao;
004    import org.kuali.student.enrollment.class1.lui.dao.LuiLuiRelationDao;
005    import org.kuali.student.enrollment.class1.lui.model.*;
006    import org.kuali.student.enrollment.lui.dto.LuiIdentifierInfo;
007    import org.kuali.student.r2.common.dto.AttributeInfo;
008    import org.kuali.student.r2.common.dto.RichTextInfo;
009    import org.kuali.student.r2.common.exceptions.*;
010    import org.kuali.student.r2.common.util.constants.LuiServiceConstants;
011    import org.kuali.student.r2.lum.clu.dto.LuCodeInfo;
012    
013    import java.text.DateFormat;
014    import java.text.ParseException;
015    import java.text.SimpleDateFormat;
016    import java.util.*;
017    
018    public class LuiTestDataLoader {
019    
020    
021        public LuiTestDataLoader(LuiDao luiDao, LuiLuiRelationDao luiLuiRelationDao) {
022            this.luiDao = luiDao;
023            this.luiLuiRelationDao =  luiLuiRelationDao;
024        }
025        private  LuiDao luiDao;
026    
027        private   LuiLuiRelationDao luiLuiRelationDao;
028    
029        private  String principalId = LuiTestDataLoader.class.getSimpleName();
030    
031        public  void loadData() throws DoesNotExistException, InvalidParameterException,
032                MissingParameterException, OperationFailedException, PermissionDeniedException,
033                DataValidationErrorException, ReadOnlyException, VersionMismatchException,
034                AlreadyExistsException, CircularRelationshipException {
035            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");
036            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");
037            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");
038            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");
039            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");
040    
041            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");
042            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");
043            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");
044    
045        }
046    
047        private  void loadLui(String id,
048                                     String name,
049                                     String cluId,
050                                     String atpId,
051                                     String type,
052                                     String state,
053                                     String descrFormatted,
054                                     String descrPlain,
055                                     String effectiveDate,
056                                     String expirationDate,
057                                     Integer maxSeats,
058                                     Integer minSeats,
059                                     String refUrl, String additionIden, String officialIdentifier, String officialIdentName, String ... luiAttributes)
060                throws DoesNotExistException, InvalidParameterException,
061                MissingParameterException, OperationFailedException, PermissionDeniedException,
062                DataValidationErrorException, ReadOnlyException, AlreadyExistsException {
063            LuiEntity luiEntity  = new LuiEntity();
064            luiEntity.setId(id);
065            luiEntity.setCluId(cluId);
066            luiEntity.setAtpId(atpId);
067            luiEntity.setName(name);
068            luiEntity.setLuiType(type);
069            luiEntity.setLuiState(state);
070            luiEntity.setMaxSeats(maxSeats);
071            luiEntity.setMinSeats(minSeats);
072            luiEntity.setReferenceURL(refUrl);
073            luiEntity.setPlain(descrPlain);
074            luiEntity.setCreateId(principalId);
075            Date time;
076                    luiEntity.setCreateTime(time = new Date());
077            
078            luiEntity.setUpdateId(principalId);
079            luiEntity.setUpdateTime(time);
080            
081            luiEntity.setEffectiveDate(time);
082    
083            LuiIdentifierEntity luiIdent = new LuiIdentifierEntity();
084            luiIdent.setLui(luiEntity);
085            luiIdent.setId(additionIden);
086            luiIdent.setCreateId("TESTDATALOADER");
087            luiIdent.setCreateTime(new Date ());
088    
089            LuiIdentifierEntity luiOfficialIdent = new LuiIdentifierEntity();
090            luiOfficialIdent.setLui(luiEntity);
091            luiOfficialIdent.setId(officialIdentifier);
092            luiOfficialIdent.setShortName(officialIdentName);
093            luiOfficialIdent.setType(LuiServiceConstants.LUI_IDENTIFIER_OFFICIAL_TYPE_KEY);        
094            luiOfficialIdent.setCreateId("TESTDATALOADER");
095            luiOfficialIdent.setCreateTime(new Date ());
096            List<LuiIdentifierEntity> luiIdents = new ArrayList<LuiIdentifierEntity>();
097    
098            luiIdents.add(luiIdent);
099            luiIdents.add(luiOfficialIdent);
100            luiEntity.setIdentifiers(luiIdents);
101    
102            //Lu Code
103            LuCodeEntity luCode = new LuCodeEntity();
104            luCode.setId("Lu-Code-" + id);
105            luCode.setCreateId(principalId);
106            luCode.setCreateTime(new Date());
107            ArrayList<LuCodeEntity> luCodes = new ArrayList<LuCodeEntity>();
108            luCodes.add(luCode);
109            luiEntity.setLuiCodes(luCodes);
110    
111            //Result Value Group Key
112            ArrayList<String> resultValueGroupKeys = new ArrayList<String>();
113            resultValueGroupKeys.add("Lu-Rvgr-" + id);
114            luiEntity.setResultValuesGroupKeys(resultValueGroupKeys);
115    
116            //Attributes
117            if (luiAttributes != null && luiAttributes.length > 0){
118                for (String attr:luiAttributes){
119                    LuiAttributeEntity luiAttr = new LuiAttributeEntity(new AttributeInfo(attr, attr), luiEntity);
120                    luiEntity.getAttributes().add(luiAttr);
121                }
122            }
123    
124            luiDao.persist(luiEntity);
125        }
126    
127        private LuiIdentifierInfo getOfficialIdentifier(String luiId) {
128            loadIdentifiers();
129            if (!identifiers.containsKey(luiId)) {
130                return null;
131            }
132            for (LuiIdentifierInfo info : identifiers.get(luiId)) {
133                if (info.getTypeKey().equals(LuiServiceConstants.LUI_IDENTIFIER_OFFICIAL_TYPE_KEY)) {
134                    return info;
135                }
136            }
137            return null;
138        }
139    
140        private List<LuiIdentifierInfo> getAlternateIdentifiers(String luiId) {
141            loadIdentifiers();
142            List<LuiIdentifierInfo> list = new ArrayList<LuiIdentifierInfo>();
143            if (!identifiers.containsKey(luiId)) {
144                return list;
145            }
146            for (LuiIdentifierInfo info : identifiers.get(luiId)) {
147                if (!info.getTypeKey().equals(LuiServiceConstants.LUI_IDENTIFIER_OFFICIAL_TYPE_KEY)) {
148                    list.add(info);
149                }
150            }
151            return list;
152        }
153        private Map<String, List<LuiIdentifierInfo>> identifiers = null;
154    
155        private void loadIdentifiers() {
156            if (identifiers != null) {
157                return;
158            }
159            identifiers = new LinkedHashMap<String, List<LuiIdentifierInfo>>();
160            addIdentifier("LUI-IDENT-1", "CHEM123", "CHEM", "123", "Chemistry 123", "Chem 123", "kuali.lui.identifier.type.official", null, "Lui-1");
161            addIdentifier("LUI-IDENT-2", "CHEM456", "CHEM", "456", "Chemistry 456", "Chem 456", "kuali.lui.identifier.type.cross-listed", null, "Lui-1");
162            addIdentifier("LUI-IDENT-3", "BIO123", "BIO", "123", "Biology 123", "Bio 123", "kuali.lui.identifier.type.official", null, "Lui-2");
163            addIdentifier("LUI-IDENT-4", "BIO456", "BIO", "456", "Biology 456", "Bio 456", "kuali.lui.identifier.type.cross-listed", null, "Lui-2");
164            addIdentifier("LUI-IDENT-5", "GEOG123", "GEOG", "123", "Geography 123", "Geog 123", "kuali.lui.identifier.type.official", null, "Lui-3");
165            addIdentifier("LUI-IDENT-6", "MATH123", "MATH", "123", "Mathematics 123", "Math 123", "kuali.lui.identifier.type.official", null, "Lui-4");
166            addIdentifier("LUI-IDENT-7", "MATH123", "MATH", "456", "Mathematics 456", "Math 456", "kuali.lui.identifier.type.cross-listed", null, "Lui-4");
167        }
168    
169        private void addIdentifier(String id,
170                String code,
171                String division,
172                String suffixCode,
173                String longName,
174                String shortName,
175                String type,
176                String variation,
177                String luiId) {
178            LuiIdentifierInfo info = new LuiIdentifierInfo();
179            info.setId(id);
180            info.setCode(code);
181            info.setDivision(division);
182            info.setSuffixCode(suffixCode);
183            info.setLongName(longName);
184            info.setShortName(shortName);
185            info.setStateKey(LuiServiceConstants.LUI_IDENTIFIER_ACTIVE_STATE_KEY);
186            info.setTypeKey(type);
187            info.setVariation(variation);
188            if (!identifiers.containsKey(luiId)) {
189                identifiers.put(luiId, new ArrayList<LuiIdentifierInfo>());
190            }
191            identifiers.get(luiId).add(info);
192        }
193    
194        private  void loadLuiLuiRel(String id,
195                String effectiveDate,
196                String expirationDate,
197                String state,
198                String descrFormatted,
199                String descrPlain,
200                String luiId,
201                String type,
202                String relatedLuiId)
203                throws DoesNotExistException, InvalidParameterException,
204                MissingParameterException, OperationFailedException, PermissionDeniedException,
205                DataValidationErrorException, ReadOnlyException, AlreadyExistsException,
206                CircularRelationshipException {
207            LuiLuiRelationEntity entity = new LuiLuiRelationEntity();
208            entity.setId(id);
209            entity.setLui(luiDao.find(luiId));
210            entity.setRelatedLui(luiDao.find(relatedLuiId));
211            entity.setLuiLuiRelationType(type);
212            entity.setLuiLuiRelationState(state);
213            entity.setEffectiveDate(str2Date(effectiveDate, id));
214            entity.setExpirationDate(str2Date(expirationDate, id));
215            entity.setDescrPlain(descrPlain);
216            entity.setDescrPlain(descrFormatted);
217    
218            entity.setCreateId(principalId);
219            Date time;
220                    entity.setCreateTime(time = new Date());
221    
222                    entity.setUpdateId(principalId);
223                    entity.setUpdateTime(time);
224    
225            luiLuiRelationDao.persist(entity);
226    
227        }
228    
229        private  Date str2Date(String str, String context) {
230            if (str == null) {
231                return null;
232            }
233            DateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.S");
234            try {
235                Date date = df.parse(str);
236                return date;
237            } catch (ParseException ex) {
238                throw new IllegalArgumentException("Bad date " + str + " in " + context);
239            }
240        }
241    }