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.PropertyUtil;
7   import org.kuali.ole.ingest.FileUtil;
8   import org.kuali.ole.ingest.OleLocationObjectGeneratorFromXML;
9   import org.kuali.ole.ingest.OleLocationXMLSchemaValidator;
10  import org.kuali.ole.ingest.pojo.OleLocationGroup;
11  import org.kuali.ole.ingest.pojo.OleLocationIngest;
12  import org.kuali.ole.location.bo.OleLocation;
13  import org.kuali.ole.location.bo.OleLocationIngestSummaryRecord;
14  import org.kuali.ole.location.bo.OleLocationLevel;
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  
45      /**
46       * Sets the businessObjectService attribute value.
47       * @param businessObjectService
48       */
49      public void setBusinessObjectService(BusinessObjectService businessObjectService) {
50          this.businessObjectService = businessObjectService;
51      }
52  
53      /**
54       * Gets the oleLocationService attribute value.
55       * @return oleLocationService.
56       */
57      public OleLocationService getOleLocationService() {
58          return oleLocationService;
59      }
60  
61      /**
62       * Sets the oleLocationService attribute value.
63       * @param oleLocationService
64       */
65      public void setOleLocationService(OleLocationService oleLocationService) {
66          this.oleLocationService = oleLocationService;
67      }
68  
69      /**
70       * Gets the createLocationList attribute value.
71       * @return  createLocationList
72       */
73      public List<OleLocationIngest> getCreateLocationList() {
74          return createLocationList;
75      }
76  
77      /**
78       *  Sets the createLocationList attribute value.
79       * @param createLocationList
80       */
81      public void setCreateLocationList(List<OleLocationIngest> createLocationList) {
82          this.createLocationList = createLocationList;
83      }
84  
85      /**
86       * Gets the updateLocationList attribute value.
87       * @return updateLocationList
88       */
89      public List<OleLocationIngest> getUpdateLocationList() {
90          return updateLocationList;
91      }
92  
93      /**
94       *  Sets the updateLocationList attribute value.
95       * @param updateLocationList
96       */
97      public void setUpdateLocationList(List<OleLocationIngest> updateLocationList) {
98          this.updateLocationList = updateLocationList;
99      }
100 
101     /**
102      * This method persist business object using xml file.
103      * @param fileContent
104      * @param fileName
105      * @return String
106      * @throws IOException
107      * @throws URISyntaxException
108      */
109     public String persistLocationFromFileContent(String fileContent, String fileName) throws IOException, URISyntaxException {
110        Timestamp st = new Timestamp(System.currentTimeMillis());
111        createLocationList = new ArrayList<OleLocationIngest>(0);
112        updateLocationList = new ArrayList<OleLocationIngest>(0);
113        rejectLocationList = new ArrayList<OleLocationIngest>(0);
114         OleLocationIngestSummaryRecord oleLocationIngestSummaryRecord =  new OleLocationIngestSummaryRecord();
115         OleLocationGroup location = getOleLocationObjectGeneratorFromXML().buildLocationFromFileContent(fileContent);
116         int locationTotCount = location.getLocationGroup().size();
117         for(int i = 0;i<location.getLocationGroup().size();i++){
118              OleLocationIngest oleLocationIngest=location.getLocationGroup().get(i);
119             if(isLocationLevelCodeExist(oleLocationIngest))    {
120                   rejectLocationList.add(oleLocationIngest);
121                 }else if(isParentLocationExist(oleLocationIngest)) {
122                   rejectLocationList.add(oleLocationIngest);
123             }
124             else {
125                 if(isLocationExist(oleLocationIngest)){
126                     updateLocationList.add(oleLocationIngest);
127                     updateOleLocation(oleLocationIngest);
128                   } else{
129                     createLocationList.add(oleLocationIngest);
130                     createOleLocation(oleLocationIngest);
131                 }
132             }
133         }
134 
135         if (createLocationList.size() > 0) {
136             oleLocationIngestSummaryRecord.setOleLocationCreateCount(createLocationList.size());
137         }
138         if (updateLocationList.size() > 0) {
139             oleLocationIngestSummaryRecord.setOleLocationUpdateCount(updateLocationList.size());
140         }
141         if(rejectLocationList.size()>0) {
142             oleLocationIngestSummaryRecord.setOleLocationFailedCount(rejectLocationList.size());
143         }
144         oleLocationIngestSummaryRecord.setOleLocationTotCount(locationTotCount);
145         oleLocationIngestSummaryRecord.setFileName(fileName);
146         String principalName="";
147        if(GlobalVariables.getUserSession()!=null && GlobalVariables.getUserSession().getPrincipalName()!=null ){
148          principalName = GlobalVariables.getUserSession().getPrincipalName();
149        }
150         oleLocationIngestSummaryRecord.setOlePrincipalName(principalName);
151         oleLocationIngestSummaryRecord.setDate(new Timestamp(System.currentTimeMillis()));
152         getBusinessObjectService().save(oleLocationIngestSummaryRecord);
153 
154         if(rejectLocationList.size()>0) {
155             String patronXML = getOleLocationObjectGeneratorFromXML().toXML(rejectLocationList);
156             saveFailureRecordsForAttachment(patronXML,oleLocationIngestSummaryRecord.getOleLocationSummaryId());
157         }
158 
159         int successCount=updateLocationList.size()+createLocationList.size();
160         int totalCount=successCount+rejectLocationList.size();
161        return "with Total:"+totalCount+" ,Sucess:"+successCount+" , Failure :"+rejectLocationList.size();
162     }
163 
164     /**
165      *  This method moves failed xml file in to specified failed location.
166      * @param locationXML
167      * @param locationReportId
168      */
169     private void saveFailureRecordsForAttachment(String locationXML,String locationReportId){
170         try {
171             HashMap<String, String> map = new HashMap<String, String>();
172             String directory = PropertyUtil.getPropertyUtil().getProperty(OLEConstants.STAGING_DIRECTORY)+
173                                OLEConstants.LOCATION_ERROR_FILE_PATH;
174             String homeDirectory = System.getProperty(OLEConstants.USER_HOME_DIRECTORY);
175             int reportId = Integer.parseInt(locationReportId);
176             File file = new File(homeDirectory+directory);
177             if(file.isDirectory()){
178                 file = new File(homeDirectory+directory+reportId+OLEConstants.FAILED_LOCATION_RECORD_NAME);
179                 file.createNewFile();
180                 FileUtils.writeStringToFile(file, locationXML);
181             } else{
182                 file.mkdirs();
183                 file = new File(homeDirectory+directory+reportId+OLEConstants.FAILED_LOCATION_RECORD_NAME);
184                 file.createNewFile();
185                 FileUtils.writeStringToFile(file, locationXML);
186             }
187 
188         } catch (IOException e) {
189             e.printStackTrace();
190         }
191     }
192 
193     /**
194      *   This method checks LocationLevelCode using oleLocationIngest.
195      * @param oleLocationIngest
196      * @return  boolean
197      */
198      public boolean isLocationLevelCodeExist( OleLocationIngest oleLocationIngest){
199           String locationLevelCode =  oleLocationIngest.getLocationLevelCode();
200               Map locationLevelCodeMap = new HashMap();
201               locationLevelCodeMap.put("levelCode",locationLevelCode);
202               List<OleLocationLevel> locationLevel = (List<OleLocationLevel>)getBusinessObjectService().findMatching(OleLocationLevel.class,locationLevelCodeMap);
203               if(locationLevel.size()>0)
204                return false;
205               else
206                return true;
207      }
208 
209     /**
210      * This method checks existence of ParentLocation using oleLocationIngest.
211      * @param oleLocationIngest
212      * @return  boolean
213      */
214      public boolean isParentLocationExist(OleLocationIngest oleLocationIngest){
215         String parentLocationCode = oleLocationIngest.getParentLocationCode();
216          if(! "".equals(parentLocationCode) && parentLocationCode != null ){
217          Map  parentLocationCodeMap = new HashMap();
218          parentLocationCodeMap.put("locationCode",parentLocationCode);
219          List<OleLocation> locationLevel = (List<OleLocation>)getBusinessObjectService().findMatching(OleLocation.class,parentLocationCodeMap);
220          if(locationLevel.size()>0)
221                return false;
222               else
223                return true;
224          }
225          return false;
226      }
227 
228     /**
229      *   This method checks Location using oleLocationIngest.
230      * @param oleLocationIngest
231      * @return  boolean
232      */
233      public boolean isLocationExist( OleLocationIngest oleLocationIngest){
234          String locationCode=oleLocationIngest.getLocationCode();
235          Map localtionCodeMap = new HashMap();
236          localtionCodeMap.put("locationCode",locationCode);
237          List<OleLocation> location = (List<OleLocation>)getBusinessObjectService().findMatching(OleLocation.class,localtionCodeMap);
238          if(location.size()>0){
239              oleLocationIngest.setLocationId(location.get(0).getLocationId());
240               return true;
241          }
242          return false;
243     }
244 
245     /**
246      *  This method creates location using oleLocation.
247      * @param oleLocation
248      */
249     public void createOleLocation(OleLocationIngest oleLocation){
250         OleLocation oleLocationpojo = new OleLocation();
251         oleLocationpojo.setLocationCode(oleLocation.getLocationCode());
252         oleLocationpojo.setLocationName(oleLocation.getLocationName());
253         String locationLevelCode = oleLocation.getLocationLevelCode();
254         OleLocationLevel oleLocationLevel;
255         Map localtionLevelCd = new HashMap();
256         localtionLevelCd.put("levelCode",locationLevelCode);
257         List<OleLocationLevel> levelList = (List<OleLocationLevel>)getBusinessObjectService().findMatching(OleLocationLevel.class,localtionLevelCd);
258         if(levelList.size()>0){
259             oleLocationLevel =  levelList.get(0);
260             oleLocationpojo.setLevelId(oleLocationLevel.getLevelId());
261         } else{
262             throw new RuntimeException();
263         }
264         String  parentLocationCode =  oleLocation.getParentLocationCode();
265         OleLocation oleLocations;
266         Map parentLevelcd = new HashMap();
267         parentLevelcd.put("locationCode",parentLocationCode);
268         List<OleLocation> parentList = (List<OleLocation>)getBusinessObjectService().findMatching(OleLocation.class,parentLevelcd);
269             if(parentList.size()>0){
270                 oleLocationpojo.setParentLocationId(parentList.get(0).getLocationId());
271                 persistOleLocation(oleLocation, oleLocationpojo);
272             }else if("".equals(oleLocation.getParentLocationCode()) || oleLocation.getParentLocationCode() == null ){
273             oleLocationpojo.setParentLocationId(null);
274         }
275         getOleLocationService().createLocation(oleLocationpojo);
276     }
277 
278     /**
279      *    This method updates location using oleLocation.
280      * @param oleLocation
281      */
282     public void updateOleLocation(OleLocationIngest oleLocation){
283         Map locationIdMap = new HashMap();
284         locationIdMap.put("locationId",oleLocation.getLocationId());
285         List<OleLocation> oleLocationList = (List<OleLocation>)getBusinessObjectService().findMatching(OleLocation.class,locationIdMap);
286         OleLocation oleLocationpojo = oleLocationList.get(0);
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().updateLocation(oleLocationpojo);
312     }
313 
314     /**
315      *
316      * @param fileContent
317      * @return  OleLocationGroup
318      * @throws URISyntaxException
319      * @throws IOException
320      */
321     public OleLocationGroup buildLocationFromFileContent(String fileContent) throws URISyntaxException, IOException {
322            return getOleLocationObjectGeneratorFromXML().buildLocationFromFileContent(fileContent);
323        }
324 
325     /**
326      *
327      * @param fileName
328      * @return  OleLocationGroup/null
329      * @throws URISyntaxException
330      * @throws IOException
331      * @throws SAXException
332      */
333     public OleLocationGroup buildLocation(String fileName) throws URISyntaxException, IOException, SAXException {
334         URL resource = getClass().getResource(fileName);
335         File file = new File(resource.toURI());
336         String fileContent = new FileUtil().readFile(file);
337         if (validLocationXML(fileContent)) {
338             return getOleLocationObjectGeneratorFromXML().buildLocationFromFileContent(fileContent);
339         }
340         return null;
341     }
342 
343     /**
344      * This method validate the fileContent .
345      * @param fileContent
346      * @return boolean
347      * @throws IOException
348      * @throws SAXException
349      */
350     private boolean validLocationXML(String fileContent) throws IOException, SAXException {
351         return new OleLocationXMLSchemaValidator().validateContentsAgainstSchema(null);
352     }
353 
354     /**
355      *  Persists oleLocation using locationIngest and oleLocation.
356      * @param locationIngest
357      * @param oleLocation
358      */
359     private void persistOleLocation(OleLocationIngest locationIngest, OleLocation oleLocation) {
360 
361             Map criteria = new HashMap<String,String>();
362             criteria.put("levelCode",locationIngest.getLocationLevelCode());
363             List<OleLocationLevel> oleLocationLevelList = (List<OleLocationLevel>) getBusinessObjectService().findMatching(OleLocationLevel.class,criteria);
364             oleLocation.setOleLocationLevel(oleLocationLevelList.get(0));
365     }
366 
367     /**
368      * Gets the businessObjectService attribute value.
369      * @return businessObjectService.
370      */
371     private BusinessObjectService getBusinessObjectService() {
372         if (null == businessObjectService) {
373             businessObjectService = KRADServiceLocator.getBusinessObjectService();
374         }
375         return businessObjectService;
376     }
377 
378     /**
379      * Gets the oleLocationObjectGeneratorFromXML attribute value.
380      * @return  oleLocationObjectGeneratorFromXML.
381      */
382     public OleLocationObjectGeneratorFromXML getOleLocationObjectGeneratorFromXML() {
383         if (null == oleLocationObjectGeneratorFromXML) {
384             oleLocationObjectGeneratorFromXML = new OleLocationObjectGeneratorFromXML();
385         }
386         return oleLocationObjectGeneratorFromXML;
387     }
388 
389     /**
390      * Sets the oleLocationObjectGeneratorFromXML attribute value.
391      * @param oleLocationObjectGeneratorFromXML
392      */
393     public void setOleLocationObjectGeneratorFromXML(OleLocationObjectGeneratorFromXML oleLocationObjectGeneratorFromXML) {
394         this.oleLocationObjectGeneratorFromXML = oleLocationObjectGeneratorFromXML;
395     }
396 
397 }