View Javadoc
1   package org.kuali.ole.service;
2   
3   import org.apache.commons.io.FileUtils;
4   import org.apache.commons.net.ntp.TimeStamp;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.ingest.FileUtil;
7   import org.kuali.ole.ingest.OleLocationObjectGeneratorFromXML;
8   import org.kuali.ole.ingest.OleLocationXMLSchemaValidator;
9   import org.kuali.ole.ingest.pojo.OleLocationGroup;
10  import org.kuali.ole.ingest.pojo.OleLocationIngest;
11  import org.kuali.ole.describe.bo.OleLocation;
12  import org.kuali.ole.describe.bo.OleLocationIngestSummaryRecord;
13  import org.kuali.ole.describe.bo.OleLocationLevel;
14  import org.kuali.rice.core.api.config.property.ConfigContext;
15  import org.kuali.rice.krad.UserSession;
16  import org.kuali.rice.krad.service.BusinessObjectService;
17  import org.kuali.rice.krad.service.KRADServiceLocator;
18  import org.kuali.rice.krad.util.GlobalVariables;
19  import org.xml.sax.SAXException;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.net.URISyntaxException;
24  import java.net.URL;
25  import java.sql.Date;
26  import java.sql.Timestamp;
27  import java.util.ArrayList;
28  import java.util.HashMap;
29  import java.util.List;
30  import java.util.Map;
31  
32  /**
33   * OleLocationConverterService generates list of location to perform ole location operation.
34   */
35  public class OleLocationConverterService {
36  
37      private BusinessObjectService businessObjectService;
38      private OleLocationObjectGeneratorFromXML oleLocationObjectGeneratorFromXML;
39      private OleLocationService oleLocationService;
40  
41      private List<OleLocationIngest> createLocationList = new ArrayList<OleLocationIngest>(0);
42      private List<OleLocationIngest> updateLocationList = new ArrayList<OleLocationIngest>(0);
43      private List<OleLocationIngest> rejectLocationList = new ArrayList<OleLocationIngest>(0);
44      private String ingestFailureRecord;
45      private int successCount;
46      private int failureCount;
47      /**
48       * Sets the businessObjectService attribute value.
49       * @param businessObjectService
50       */
51      public void setBusinessObjectService(BusinessObjectService businessObjectService) {
52          this.businessObjectService = businessObjectService;
53      }
54  
55      /**
56       * Gets the oleLocationService attribute value.
57       * @return oleLocationService.
58       */
59      public OleLocationService getOleLocationService() {
60          return oleLocationService;
61      }
62  
63      /**
64       * Sets the oleLocationService attribute value.
65       * @param oleLocationService
66       */
67      public void setOleLocationService(OleLocationService oleLocationService) {
68          this.oleLocationService = oleLocationService;
69      }
70  
71      /**
72       * Gets the createLocationList attribute value.
73       * @return  createLocationList
74       */
75      public List<OleLocationIngest> getCreateLocationList() {
76          return createLocationList;
77      }
78  
79      /**
80       *  Sets the createLocationList attribute value.
81       * @param createLocationList
82       */
83      public void setCreateLocationList(List<OleLocationIngest> createLocationList) {
84          this.createLocationList = createLocationList;
85      }
86  
87      /**
88       * Gets the updateLocationList attribute value.
89       * @return updateLocationList
90       */
91      public List<OleLocationIngest> getUpdateLocationList() {
92          return updateLocationList;
93      }
94  
95      /**
96       *  Sets the updateLocationList attribute value.
97       * @param updateLocationList
98       */
99      public void setUpdateLocationList(List<OleLocationIngest> updateLocationList) {
100         this.updateLocationList = updateLocationList;
101     }
102 
103     public List<OleLocationIngest> getRejectLocationList() {
104         return rejectLocationList;
105     }
106 
107     public void setRejectLocationList(List<OleLocationIngest> rejectLocationList) {
108         this.rejectLocationList = rejectLocationList;
109     }
110 
111     public String getIngestFailureRecord() {
112         return ingestFailureRecord;
113     }
114 
115     public void setIngestFailureRecord(String ingestFailureRecord) {
116         this.ingestFailureRecord = ingestFailureRecord;
117     }
118 
119     public int getSuccessCount() {
120         return successCount;
121     }
122 
123     public void setSuccessCount(int successCount) {
124         this.successCount = successCount;
125     }
126 
127     public int getFailureCount() {
128         return failureCount;
129     }
130 
131     public void setFailureCount(int failureCount) {
132         this.failureCount = failureCount;
133     }
134 
135     /**
136      * This method persist business object using xml file.
137      * @param fileContent
138      * @param fileName
139      * @return String
140      * @throws java.io.IOException
141      * @throws java.net.URISyntaxException
142      */
143     public String persistLocationFromFileContent(String fileContent, String fileName) throws IOException, URISyntaxException {
144        Timestamp st = new Timestamp(System.currentTimeMillis());
145        createLocationList = new ArrayList<OleLocationIngest>(0);
146        updateLocationList = new ArrayList<OleLocationIngest>(0);
147        rejectLocationList = new ArrayList<OleLocationIngest>(0);
148         OleLocationIngestSummaryRecord oleLocationIngestSummaryRecord =  new OleLocationIngestSummaryRecord();
149         OleLocationGroup location = getOleLocationObjectGeneratorFromXML().buildLocationFromFileContent(fileContent);
150         int locationTotCount = location.getLocationGroup().size();
151         for(int i = 0;i<location.getLocationGroup().size();i++){
152              OleLocationIngest oleLocationIngest=location.getLocationGroup().get(i);
153             if(isLocationLevelCodeExist(oleLocationIngest))    {
154                   rejectLocationList.add(oleLocationIngest);
155                 }else if(isParentLocationExist(oleLocationIngest)) {
156                   rejectLocationList.add(oleLocationIngest);
157             }
158             else {
159                 if(isLocationExist(oleLocationIngest)){
160                     updateLocationList.add(oleLocationIngest);
161                     updateOleLocation(oleLocationIngest);
162                   } else{
163                     createLocationList.add(oleLocationIngest);
164                     createOleLocation(oleLocationIngest);
165                 }
166             }
167         }
168 
169         if (createLocationList.size() > 0) {
170             oleLocationIngestSummaryRecord.setOleLocationCreateCount(createLocationList.size());
171         }
172         if (updateLocationList.size() > 0) {
173             oleLocationIngestSummaryRecord.setOleLocationUpdateCount(updateLocationList.size());
174         }
175         if(rejectLocationList.size()>0) {
176             oleLocationIngestSummaryRecord.setOleLocationFailedCount(rejectLocationList.size());
177         }
178         oleLocationIngestSummaryRecord.setOleLocationTotCount(locationTotCount);
179         oleLocationIngestSummaryRecord.setFileName(fileName);
180         String principalName="";
181        if(GlobalVariables.getUserSession()!=null && GlobalVariables.getUserSession().getPrincipalName()!=null ){
182          principalName = GlobalVariables.getUserSession().getPrincipalName();
183        }
184         oleLocationIngestSummaryRecord.setOlePrincipalName(principalName);
185         oleLocationIngestSummaryRecord.setDate(new Timestamp(System.currentTimeMillis()));
186         getBusinessObjectService().save(oleLocationIngestSummaryRecord);
187 
188         if(rejectLocationList.size()>0) {
189             String patronXML = getOleLocationObjectGeneratorFromXML().toXML(rejectLocationList);
190             setIngestFailureRecord(patronXML);
191             //saveFailureRecordsForAttachment(patronXML,oleLocationIngestSummaryRecord.getOleLocationSummaryId(),fileName);
192         }
193 
194         int successCount=updateLocationList.size()+createLocationList.size();
195         setSuccessCount(successCount);
196         setFailureCount(rejectLocationList.size());
197         int totalCount=successCount+rejectLocationList.size();
198        return "with Total:"+totalCount+" ,Sucess:"+successCount+" , Failure :"+rejectLocationList.size();
199     }
200 
201     /**
202      *  This method moves failed xml file in to specified failed location.
203      * @param locationXML
204      * @param locationReportId
205      */
206     private void saveFailureRecordsForAttachment(String locationXML,String locationReportId){
207         try {
208             HashMap<String, String> map = new HashMap<String, String>();
209             String directory =ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.LOCATION_ERROR_FILE_PATH);
210             String homeDirectory = System.getProperty(OLEConstants.USER_HOME_DIRECTORY);
211             int reportId = Integer.parseInt(locationReportId);
212             File file = new File(directory);
213             if(file.isDirectory()){
214                 file = new File(directory+reportId+OLEConstants.FAILED_LOCATION_RECORD_NAME);
215                 file.createNewFile();
216                 FileUtils.writeStringToFile(file, locationXML);
217             } else{
218                 file.mkdirs();
219                 file = new File(directory+reportId+OLEConstants.FAILED_LOCATION_RECORD_NAME);
220                 file.createNewFile();
221                 FileUtils.writeStringToFile(file, locationXML);
222             }
223 
224         } catch (IOException e) {
225             e.printStackTrace();
226         }
227     }
228 
229     /**
230      *   This method checks LocationLevelCode using oleLocationIngest.
231      * @param oleLocationIngest
232      * @return  boolean
233      */
234      public boolean isLocationLevelCodeExist( OleLocationIngest oleLocationIngest){
235           String locationLevelCode =  oleLocationIngest.getLocationLevelCode();
236               Map locationLevelCodeMap = new HashMap();
237               locationLevelCodeMap.put("levelCode",locationLevelCode);
238               List<OleLocationLevel> locationLevel = (List<OleLocationLevel>)getBusinessObjectService().findMatching(OleLocationLevel.class,locationLevelCodeMap);
239               if(locationLevel.size()>0)
240                return false;
241               else
242                return true;
243      }
244 
245     /**
246      * This method checks existence of ParentLocation using oleLocationIngest.
247      * @param oleLocationIngest
248      * @return  boolean
249      */
250      public boolean isParentLocationExist(OleLocationIngest oleLocationIngest){
251         String parentLocationCode = oleLocationIngest.getParentLocationCode();
252          if(! "".equals(parentLocationCode) && parentLocationCode != null ){
253          Map  parentLocationCodeMap = new HashMap();
254          parentLocationCodeMap.put("locationCode",parentLocationCode);
255          List<OleLocation> locationLevel = (List<OleLocation>)getBusinessObjectService().findMatching(OleLocation.class,parentLocationCodeMap);
256          if(locationLevel.size()>0)
257                return false;
258               else
259                return true;
260          }
261          return false;
262      }
263 
264     /**
265      *   This method checks Location using oleLocationIngest.
266      * @param oleLocationIngest
267      * @return  boolean
268      */
269      public boolean isLocationExist( OleLocationIngest oleLocationIngest){
270          String locationCode=oleLocationIngest.getLocationCode();
271          Map localtionCodeMap = new HashMap();
272          localtionCodeMap.put("locationCode",locationCode);
273          List<OleLocation> location = (List<OleLocation>)getBusinessObjectService().findMatching(OleLocation.class,localtionCodeMap);
274          if(location.size()>0){
275              oleLocationIngest.setLocationId(location.get(0).getLocationId());
276               return true;
277          }
278          return false;
279     }
280 
281     /**
282      *  This method creates location using oleLocation.
283      * @param oleLocation
284      */
285     public void createOleLocation(OleLocationIngest oleLocation){
286         OleLocation oleLocationpojo = new OleLocation();
287         oleLocationpojo.setLocationCode(oleLocation.getLocationCode());
288         oleLocationpojo.setLocationName(oleLocation.getLocationName());
289         String locationLevelCode = oleLocation.getLocationLevelCode();
290         OleLocationLevel oleLocationLevel;
291         Map localtionLevelCd = new HashMap();
292         localtionLevelCd.put("levelCode",locationLevelCode);
293         List<OleLocationLevel> levelList = (List<OleLocationLevel>)getBusinessObjectService().findMatching(OleLocationLevel.class,localtionLevelCd);
294         if(levelList.size()>0){
295             oleLocationLevel =  levelList.get(0);
296             oleLocationpojo.setLevelId(oleLocationLevel.getLevelId());
297         } else{
298             throw new RuntimeException();
299         }
300         String  parentLocationCode =  oleLocation.getParentLocationCode();
301         OleLocation oleLocations;
302         Map parentLevelcd = new HashMap();
303         parentLevelcd.put("locationCode",parentLocationCode);
304         List<OleLocation> parentList = (List<OleLocation>)getBusinessObjectService().findMatching(OleLocation.class,parentLevelcd);
305             if(parentList.size()>0){
306                 oleLocationpojo.setParentLocationId(parentList.get(0).getLocationId());
307                 persistOleLocation(oleLocation, oleLocationpojo);
308             }else if("".equals(oleLocation.getParentLocationCode()) || oleLocation.getParentLocationCode() == null ){
309             oleLocationpojo.setParentLocationId(null);
310         }
311         getOleLocationService().createLocation(oleLocationpojo);
312     }
313 
314     /**
315      *    This method updates location using oleLocation.
316      * @param oleLocation
317      */
318     public void updateOleLocation(OleLocationIngest oleLocation){
319         Map locationIdMap = new HashMap();
320         locationIdMap.put("locationId",oleLocation.getLocationId());
321         List<OleLocation> oleLocationList = (List<OleLocation>)getBusinessObjectService().findMatching(OleLocation.class,locationIdMap);
322         OleLocation oleLocationpojo = oleLocationList.get(0);
323         oleLocationpojo.setLocationCode(oleLocation.getLocationCode());
324         oleLocationpojo.setLocationName(oleLocation.getLocationName());
325         String locationLevelCode = oleLocation.getLocationLevelCode();
326         OleLocationLevel oleLocationLevel;
327         Map localtionLevelCd = new HashMap();
328         localtionLevelCd.put("levelCode",locationLevelCode);
329         List<OleLocationLevel> levelList = (List<OleLocationLevel>)getBusinessObjectService().findMatching(OleLocationLevel.class,localtionLevelCd);
330         if(levelList.size()>0){
331             oleLocationLevel =  levelList.get(0);
332             oleLocationpojo.setLevelId(oleLocationLevel.getLevelId());
333         } else{
334             throw new RuntimeException();
335         }
336         String  parentLocationCode =  oleLocation.getParentLocationCode();
337         OleLocation oleLocations;
338         Map parentLevelcd = new HashMap();
339         parentLevelcd.put("locationCode",parentLocationCode);
340         List<OleLocation> parentList = (List<OleLocation>)getBusinessObjectService().findMatching(OleLocation.class, parentLevelcd);
341         if(parentList.size()>0){
342              oleLocationpojo.setParentLocationId(parentList.get(0).getLocationId());
343              persistOleLocation(oleLocation, oleLocationpojo);
344          }else if("".equals(oleLocation.getParentLocationCode()) || oleLocation.getParentLocationCode() == null ){
345             oleLocationpojo.setParentLocationId(null);
346          }
347          getOleLocationService().updateLocation(oleLocationpojo);
348     }
349 
350     /**
351      *
352      * @param fileContent
353      * @return  OleLocationGroup
354      * @throws java.net.URISyntaxException
355      * @throws java.io.IOException
356      */
357     public OleLocationGroup buildLocationFromFileContent(String fileContent) throws URISyntaxException, IOException {
358            return getOleLocationObjectGeneratorFromXML().buildLocationFromFileContent(fileContent);
359        }
360 
361     /**
362      *
363      * @param fileName
364      * @return  OleLocationGroup/null
365      * @throws java.net.URISyntaxException
366      * @throws java.io.IOException
367      * @throws org.xml.sax.SAXException
368      */
369     public OleLocationGroup buildLocation(String fileName) throws URISyntaxException, IOException, SAXException {
370         URL resource = getClass().getResource(fileName);
371         File file = new File(resource.toURI());
372         String fileContent = new FileUtil().readFile(file);
373         if (validLocationXML(fileContent)) {
374             return getOleLocationObjectGeneratorFromXML().buildLocationFromFileContent(fileContent);
375         }
376         return null;
377     }
378 
379     /**
380      * This method validate the fileContent .
381      * @param fileContent
382      * @return boolean
383      * @throws java.io.IOException
384      * @throws org.xml.sax.SAXException
385      */
386     private boolean validLocationXML(String fileContent) throws IOException, SAXException {
387         return new OleLocationXMLSchemaValidator().validateContentsAgainstSchema(null);
388     }
389 
390     /**
391      *  Persists oleLocation using locationIngest and oleLocation.
392      * @param locationIngest
393      * @param oleLocation
394      */
395     private void persistOleLocation(OleLocationIngest locationIngest, OleLocation oleLocation) {
396 
397             Map criteria = new HashMap<String,String>();
398             criteria.put("levelCode",locationIngest.getLocationLevelCode());
399             List<OleLocationLevel> oleLocationLevelList = (List<OleLocationLevel>) getBusinessObjectService().findMatching(OleLocationLevel.class,criteria);
400             oleLocation.setOleLocationLevel(oleLocationLevelList.get(0));
401     }
402 
403     /**
404      * Gets the businessObjectService attribute value.
405      * @return businessObjectService.
406      */
407     private BusinessObjectService getBusinessObjectService() {
408         if (null == businessObjectService) {
409             businessObjectService = KRADServiceLocator.getBusinessObjectService();
410         }
411         return businessObjectService;
412     }
413 
414     /**
415      * Gets the oleLocationObjectGeneratorFromXML attribute value.
416      * @return  oleLocationObjectGeneratorFromXML.
417      */
418     public OleLocationObjectGeneratorFromXML getOleLocationObjectGeneratorFromXML() {
419         if (null == oleLocationObjectGeneratorFromXML) {
420             oleLocationObjectGeneratorFromXML = new OleLocationObjectGeneratorFromXML();
421         }
422         return oleLocationObjectGeneratorFromXML;
423     }
424 
425     /**
426      * Sets the oleLocationObjectGeneratorFromXML attribute value.
427      * @param oleLocationObjectGeneratorFromXML
428      */
429     public void setOleLocationObjectGeneratorFromXML(OleLocationObjectGeneratorFromXML oleLocationObjectGeneratorFromXML) {
430         this.oleLocationObjectGeneratorFromXML = oleLocationObjectGeneratorFromXML;
431     }
432 
433 }