View Javadoc
1   package org.kuali.ole.loaders.describe.service.impl;
2   
3   import com.sun.jersey.api.core.HttpContext;
4   import org.apache.commons.lang.StringUtils;
5   import org.apache.log4j.Logger;
6   import org.codehaus.jettison.json.JSONArray;
7   import org.codehaus.jettison.json.JSONException;
8   import org.codehaus.jettison.json.JSONObject;
9   import org.kuali.ole.describe.bo.OleLocation;
10  import org.kuali.ole.describe.bo.OleLocationLevel;
11  import org.kuali.ole.loaders.common.bo.OLELoaderResponseBo;
12  import org.kuali.ole.loaders.common.constants.OLELoaderConstants;
13  import org.kuali.ole.loaders.common.service.OLELoaderService;
14  import org.kuali.ole.loaders.common.service.impl.OLELoaderServiceImpl;
15  import org.kuali.ole.loaders.describe.bo.OLELocationBo;
16  import org.kuali.ole.loaders.describe.bo.OLELocationLevelBo;
17  import org.kuali.ole.loaders.describe.service.OLELocationLoaderHelperService;
18  import org.kuali.ole.service.OleLocationService;
19  import org.kuali.ole.sys.context.SpringContext;
20  import org.kuali.rice.krad.service.BusinessObjectService;
21  import org.kuali.rice.krad.service.KRADServiceLocator;
22  
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  /**
28   * Created by sheiksalahudeenm on 2/4/15.
29   */
30  public class OLELocationLoaderHelperServiceImpl implements OLELocationLoaderHelperService {
31  
32      private static final Logger LOG = Logger.getLogger(OLELocationLoaderHelperServiceImpl.class);
33      private BusinessObjectService businessObjectService;
34      private OleLocationService oleLocationService;
35      private OLELoaderService oleLoaderService;
36  
37      public BusinessObjectService getBusinessObjectService() {
38          if(businessObjectService == null){
39              businessObjectService = KRADServiceLocator.getBusinessObjectService();
40          }
41          return businessObjectService;
42      }
43  
44      public void setBusinessObjectService(BusinessObjectService businessObjectService) {
45          this.businessObjectService = businessObjectService;
46      }
47  
48      public OleLocationService getOleLocationService() {
49          if(oleLocationService == null){
50              oleLocationService = (OleLocationService) SpringContext.getBean("oleLocationService");
51          }
52          return oleLocationService;
53      }
54  
55      public void setOleLocationService(OleLocationService oleLocationService) {
56          this.oleLocationService = oleLocationService;
57      }
58  
59      public OLELoaderService getOleLoaderService() {
60          if(oleLoaderService == null ){
61              oleLoaderService = new OLELoaderServiceImpl();
62          }
63          return oleLoaderService;
64      }
65  
66      public void setOleLoaderService(OLELoaderService oleLoaderService) {
67          this.oleLoaderService = oleLoaderService;
68      }
69  
70      @Override
71      public boolean isLocationLevelExistById(String locationLevelId) {
72          Map locationLevelCodeMap = new HashMap();
73          locationLevelCodeMap.put("levelId",locationLevelId);
74          List<OleLocationLevel> locationLevel = (List<OleLocationLevel>)getBusinessObjectService().findMatching(OleLocationLevel.class,locationLevelCodeMap);
75          if(locationLevel.size()>0)
76              return false;
77          else
78              return true;
79      }
80  
81      @Override
82      public boolean isParentLocationLevelExist(String parentLocationLevelId) {
83          if(StringUtils.isNotBlank(parentLocationLevelId)){
84              Map  parentLocationLevelMap = new HashMap();
85              parentLocationLevelMap.put("levelId",parentLocationLevelId);
86              List<OleLocationLevel> locationLevel = (List<OleLocationLevel>)getBusinessObjectService().findMatching(OleLocationLevel.class,parentLocationLevelMap);
87              if(locationLevel.size()>0)
88                  return false;
89              else
90                  return true;
91          }
92          return false;
93      }
94  
95      @Override
96      public boolean isParentLocationExist(String parentLocationId) {
97          if(StringUtils.isNotBlank(parentLocationId)){
98              Map  parentLocationCodeMap = new HashMap();
99              parentLocationCodeMap.put("locationId",parentLocationId);
100             List<OleLocation> locationLevel = (List<OleLocation>)getBusinessObjectService().findMatching(OleLocation.class,parentLocationCodeMap);
101             if(locationLevel.size()>0)
102                 return false;
103             else
104                 return true;
105         }
106         return false;
107     }
108 
109     @Override
110     public boolean isLocationExistByCode(String locationCode) {
111         Map localtionCodeMap = new HashMap();
112         localtionCodeMap.put("locationCode", locationCode);
113         List<OleLocation> location = (List<OleLocation>)getBusinessObjectService().findMatching(OleLocation.class,localtionCodeMap);
114         if(location.size()>0){
115             return true;
116         }
117         return false;
118     }
119 
120     @Override
121     public OleLocation createOleLocation(OLELocationBo oleLocationBo) {
122         LOG.info("Inside Create OleLocation. Location Code : " + oleLocationBo.getLocationCode());
123         OleLocation oleLocation = new OleLocation();
124         oleLocation.setLocationCode(oleLocationBo.getLocationCode());
125         oleLocation.setLocationName(oleLocationBo.getLocationName());
126         String locationLevelId = oleLocationBo.getLocationLevelId();
127         OleLocationLevel oleLocationLevel;
128         Map localtionLevelCd = new HashMap();
129         localtionLevelCd.put("levelId",locationLevelId);
130         List<OleLocationLevel> levelList = (List<OleLocationLevel>)getBusinessObjectService().findMatching(OleLocationLevel.class,localtionLevelCd);
131         if(levelList.size()>0){
132             oleLocationLevel =  levelList.get(0);
133             oleLocation.setOleLocationLevel(oleLocationLevel);
134             oleLocation.setLevelId(oleLocationLevel.getLevelId());
135         } else{
136             throw new RuntimeException();
137         }
138         String  parentLocationId =  oleLocationBo.getParentLocationId();
139         OleLocation oleLocations;
140         if(StringUtils.isNotBlank(parentLocationId)){
141             Map parentLevelcd = new HashMap();
142             parentLevelcd.put("locationId",parentLocationId);
143             List<OleLocation> parentList = (List<OleLocation>)getBusinessObjectService().findMatching(OleLocation.class,parentLevelcd);
144             if(parentList.size()>0){
145                 oleLocation.setParentLocationId(parentList.get(0).getLocationId());
146                 oleLocation.setOleLocation(parentList.get(0));
147             }else {
148                 oleLocation.setParentLocationId(null);
149             }
150         }else{
151             oleLocation.setParentLocationId(null);
152         }
153 
154         getOleLocationService().createLocation(oleLocation);
155         return oleLocation;
156     }
157 
158     @Override
159     public OLELoaderResponseBo updateOleLocation(OleLocation oleLocation, OLELocationBo oleLocationBo,HttpContext context) {
160         LOG.info("Inside update OleLocation. Location Code : " + oleLocationBo.getLocationCode());
161         if(StringUtils.isNotBlank(oleLocationBo.getLocationCode()))
162             oleLocation.setLocationCode(oleLocationBo.getLocationCode());
163         if(StringUtils.isNotBlank(oleLocationBo.getLocationName()))
164             oleLocation.setLocationName(oleLocationBo.getLocationName());
165         String locationLevelId = oleLocationBo.getLocationLevelId();
166         if(StringUtils.isNotBlank(locationLevelId)){
167             OleLocationLevel oleLocationLevel;
168             Map localtionLevelCd = new HashMap();
169             localtionLevelCd.put("levelId",locationLevelId);
170             List<OleLocationLevel> levelList = (List<OleLocationLevel>)getBusinessObjectService().findMatching(OleLocationLevel.class,localtionLevelCd);
171             if(levelList.size()>0){
172                 oleLocationLevel =  levelList.get(0);
173                 oleLocation.setLevelId(oleLocationLevel.getLevelId());
174                 oleLocation.setOleLocationLevel(oleLocationLevel);
175             } else{
176                 return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.LOCATION_LEVEL_NOT_EXIST, OLELoaderConstants.OLEloaderStatus.LOCATION_LEVEL_NOT_EXIST);
177             }
178         }
179 
180         String  parentLocationId =  oleLocationBo.getLocationLevelId();
181         if(StringUtils.isNotBlank(parentLocationId)){
182             Map parentLevelcd = new HashMap();
183             parentLevelcd.put("locationId",parentLocationId);
184             List<OleLocation> parentList = (List<OleLocation>)getBusinessObjectService().findMatching(OleLocation.class, parentLevelcd);
185             if(parentList.size()>0){
186                 oleLocation.setParentLocationId(parentList.get(0).getLocationId());
187                 oleLocation.setOleLocation(parentList.get(0));
188             }else {
189                 return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.PARENT_LOCATION_NOT_EXIST, OLELoaderConstants.OLEloaderStatus.PARENT_LOCATION_NOT_EXIST);
190             }
191         }
192         try{
193             if(getOleLocationService().updateLocation(oleLocation)){
194                 String details = formLocationExportResponse(oleLocation,OLELoaderConstants.OLELoaderContext.LOCATION,context.getRequest().getRequestUri().toASCIIString(),true).toString();
195                 return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.LOCATION_SUCCESS, OLELoaderConstants.OLEloaderStatus.LOCATION_SUCCESS,details);
196             }else{
197                 return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.LOCATION_FAILED, OLELoaderConstants.OLEloaderStatus.LOCATION_FAILED);
198             }
199         }catch(Exception e){
200             return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.LOCATION_FAILED, OLELoaderConstants.OLEloaderStatus.LOCATION_FAILED);
201         }
202     }
203 
204     @Override
205     public OleLocation getLocationById(String locationId) {
206         Map locationMap = new HashMap();
207         locationMap.put("locationId", locationId);
208         return getBusinessObjectService().findByPrimaryKey(OleLocation.class, locationMap);
209     }
210 
211     @Override
212     public OleLocation getLocationByCode(String locationCode) {
213         Map locationMap = new HashMap();
214         locationMap.put("locationCode", locationCode);
215         List<OleLocation> oleLocationList = (List<OleLocation>) getBusinessObjectService().findMatching(OleLocation.class, locationMap);
216         return (oleLocationList != null && oleLocationList.size() > 0) ? oleLocationList.get(0) : null;
217     }
218 
219     @Override
220     public List<OleLocation> getAllLocation() {
221         return (List<OleLocation>) getBusinessObjectService().findAll(OleLocation.class);
222     }
223 
224     public Object formLocationExportResponse(Object object, String locationContext, String uri, boolean importContext){
225         OleLocation oleLocation = (OleLocation) object;
226         JSONObject jsonObject = new JSONObject();
227         try {
228             if(importContext)
229                 jsonObject.put("@context",locationContext);
230             jsonObject.put("@id",OLELoaderConstants.LOCATION_URI + OLELoaderConstants.SLASH + oleLocation.getLocationId());
231             jsonObject.put("code",oleLocation.getLocationCode());
232             jsonObject.put("name",oleLocation.getLocationName());
233             JSONObject parentJsonObject = new JSONObject();
234             if(oleLocation.getOleLocation() != null){
235                 parentJsonObject.put("@id",OLELoaderConstants.LOCATION_URI + OLELoaderConstants.SLASH + oleLocation.getOleLocation().getLocationId());
236                 parentJsonObject.put("name",oleLocation.getOleLocation().getLocationName());
237             }
238             jsonObject.put("parent",parentJsonObject);
239             JSONObject locationLevelJsonObject = new JSONObject();
240             if(oleLocation.getOleLocationLevel() != null){
241                 locationLevelJsonObject.put("@context", OLELoaderConstants.OLELoaderContext.LOCATION_LEVEL);
242                 locationLevelJsonObject.put("@id",OLELoaderConstants.LOCATION_LEVEL_URI  + OLELoaderConstants.SLASH + oleLocation.getOleLocationLevel().getLevelId());
243                 locationLevelJsonObject.put("name",oleLocation.getOleLocationLevel().getLevelName());
244             }
245             jsonObject.put("level",locationLevelJsonObject);
246         } catch (JSONException e) {
247             e.printStackTrace();
248         }
249         return jsonObject;
250     }
251 
252     @Override
253     public Object formAllLocationExportResponse(HttpContext context,List<OleLocation> oleLocationList, String locationContext, String uri) {
254         JSONObject jsonResponseObject = new JSONObject();
255         JSONArray paginationArray = new JSONArray();
256         JSONObject paginationObject = new JSONObject();
257         JSONArray jsonArray = new JSONArray();
258         try{
259             jsonResponseObject.put("@context",locationContext);
260             int startIndex = 0;
261             int maxResults = 50;
262             boolean validStartIndex = true;
263             if(context.getRequest().getQueryParameters().containsKey("start")){
264                 try{
265                     String start = context.getRequest().getQueryParameters().getFirst("start");
266                     startIndex = Integer.parseInt(start);
267                     if(startIndex < 0)
268                         startIndex =0;
269                     if(startIndex > oleLocationList.size()){
270                         validStartIndex = false;
271                     }
272                 }catch (Exception e){
273                     LOG.info("Invalid Start Index : " + e.getMessage());
274                     startIndex = 0;
275                 }
276             }
277             if(context.getRequest().getQueryParameters().containsKey("maxResults")){
278                 try{
279                     String maxCount = context.getRequest().getQueryParameters().getFirst("maxResults");
280                     maxResults = Integer.parseInt(maxCount);
281                     if(maxResults < 0)
282                         maxResults =50;
283                 }catch (Exception e){
284                     LOG.info("Invalid Max Result count : " + e.getMessage());
285                     maxResults = 50;
286                 }
287             }
288             int loopIterationEnd = 0;
289             if(startIndex+maxResults > oleLocationList.size())
290                 loopIterationEnd = oleLocationList.size();
291             else{
292                 loopIterationEnd = startIndex+maxResults;
293             }
294 
295             if(validStartIndex){
296                 if(startIndex != 0){
297                     paginationObject.put("rel","prev");
298                     paginationObject.put("href",OLELoaderConstants.LOCATION_URI + "?start="+((startIndex-1)-maxResults < 0 ? 0 : (startIndex-1)-maxResults)+"&maxResults="+maxResults);
299                     paginationArray.put(paginationObject);
300                 }
301                 if(loopIterationEnd != oleLocationList.size()){
302                     paginationObject = new JSONObject();
303                     paginationObject.put("rel","next");
304                     paginationObject.put("href",OLELoaderConstants.LOCATION_URI + "?start="+(loopIterationEnd+1)+"&maxResults="+maxResults);
305                     paginationArray.put(paginationObject);
306                 }
307 
308                 jsonResponseObject.put("links",paginationArray);
309                 for(int index = (startIndex == 0 ? 0 : startIndex-1) ; index < loopIterationEnd-1 ; index++){
310                     OleLocation oleLocation = oleLocationList.get(index);
311                     JSONObject jsonObject = new JSONObject();
312                     try {
313                         jsonObject.put("@id",OLELoaderConstants.LOCATION_URI + OLELoaderConstants.SLASH + oleLocation.getLocationId());
314                         jsonObject.put("code",oleLocation.getLocationCode());
315                         jsonObject.put("name",oleLocation.getLocationName());
316                         JSONObject parentJsonObject = new JSONObject();
317                         if(oleLocation.getOleLocation() != null){
318                             parentJsonObject.put("@id",OLELoaderConstants.LOCATION_URI + OLELoaderConstants.SLASH + oleLocation.getOleLocation().getLocationId());
319                             parentJsonObject.put("name",oleLocation.getOleLocation().getLocationName());
320                         }
321                         jsonObject.put("parent",parentJsonObject);
322                         JSONObject locationLevelJsonObject = new JSONObject();
323                         if(oleLocation.getOleLocationLevel() != null){
324                             locationLevelJsonObject.put("@context", OLELoaderConstants.OLELoaderContext.LOCATION_LEVEL);
325                             locationLevelJsonObject.put("@id",OLELoaderConstants.LOCATION_LEVEL_URI + OLELoaderConstants.SLASH + oleLocation.getOleLocationLevel().getLevelId());
326                             locationLevelJsonObject.put("name",oleLocation.getOleLocationLevel().getLevelName());
327                         }
328                         jsonObject.put("level",locationLevelJsonObject);
329                     } catch (JSONException e) {
330                         e.printStackTrace();
331                     }
332                     jsonArray.put(jsonObject);
333                 }
334                 jsonResponseObject.put("items",jsonArray);
335             }
336         }catch (Exception e){
337             e.printStackTrace();
338         }
339         return jsonResponseObject;
340     }
341 
342     @Override
343     public OleLocationLevel getLocationLevelById(String locationLevelId) {
344         Map locationLevelMap = new HashMap();
345         locationLevelMap.put("levelId", locationLevelId);
346         return getBusinessObjectService().findByPrimaryKey(OleLocationLevel.class, locationLevelMap);
347     }
348 
349     @Override
350     public OleLocationLevel getLocationLevelByCode(String locationLevelCode) {
351         Map locationLevelMap = new HashMap();
352         locationLevelMap.put("levelCode", locationLevelCode);
353         return getBusinessObjectService().findByPrimaryKey(OleLocationLevel.class, locationLevelMap);
354     }
355 
356     @Override
357     public List<OleLocationLevel> getAllLocationLevel() {
358         return (List<OleLocationLevel>) getBusinessObjectService().findAll(OleLocationLevel.class);
359     }
360 
361     @Override
362     public Object formLocationLevelExportResponse(Object object, String locationLevelContext, String uri, boolean addContext) {
363         OleLocationLevel oleLocationLevel = (OleLocationLevel) object;
364         JSONObject jsonObject = new JSONObject();
365         try {
366             if(addContext)
367                 jsonObject.put("@context",locationLevelContext);
368             jsonObject.put("@id",OLELoaderConstants.LOCATION_LEVEL_URI + OLELoaderConstants.SLASH + oleLocationLevel.getLevelId());
369             jsonObject.put("code",oleLocationLevel.getLevelCode());
370             jsonObject.put("name",oleLocationLevel.getLevelName());
371             JSONObject parentJsonObject = new JSONObject();
372             if(oleLocationLevel.getOleLocationLevel() != null){
373                 parentJsonObject.put("@id",OLELoaderConstants.LOCATION_LEVEL_URI + OLELoaderConstants.SLASH + oleLocationLevel.getOleLocationLevel().getLevelId());
374                 parentJsonObject.put("name",oleLocationLevel.getOleLocationLevel().getLevelName());
375             }
376             jsonObject.put("parent",parentJsonObject);
377         } catch (JSONException e) {
378             e.printStackTrace();
379         }
380         return jsonObject;
381     }
382 
383     @Override
384     public Object formAllLocationLevelExportResponse(HttpContext context, List<OleLocationLevel> oleLocationLevelList, String locationContext, String uri) {
385         JSONObject jsonResponseObject = new JSONObject();
386         JSONArray paginationArray = new JSONArray();
387         JSONObject paginationObject = new JSONObject();
388         JSONArray jsonArray = new JSONArray();
389         try{
390             jsonResponseObject.put("@context",locationContext);
391             int startIndex = 0;
392             int maxResults = 50;
393             boolean validStartIndex = true;
394             if(context.getRequest().getQueryParameters().containsKey("start")){
395                 try{
396                     String start = context.getRequest().getQueryParameters().getFirst("start");
397                     startIndex = Integer.parseInt(start);
398                     if(startIndex < 0)
399                         startIndex =0;
400                     if(startIndex > oleLocationLevelList.size()){
401                         validStartIndex = false;
402                     }
403                 }catch (Exception e){
404                     LOG.info("Invalid Start Index : " + e.getMessage());
405                     startIndex = 0;
406                 }
407             }
408             if(context.getRequest().getQueryParameters().containsKey("maxResults")){
409                 try{
410                     String maxCount = context.getRequest().getQueryParameters().getFirst("maxResults");
411                     maxResults = Integer.parseInt(maxCount);
412                     if(maxResults < 0)
413                         maxResults =50;
414                 }catch (Exception e){
415                     LOG.info("Invalid Max Result count : " + e.getMessage());
416                     maxResults = 50;
417                 }
418             }
419             int loopIterationEnd = 0;
420             if(startIndex+maxResults > oleLocationLevelList.size())
421                 loopIterationEnd = oleLocationLevelList.size();
422             else{
423                 loopIterationEnd = startIndex+maxResults;
424             }
425 
426             if(validStartIndex){
427                 if(startIndex != 0){
428                     paginationObject.put("rel","prev");
429                     paginationObject.put("href",OLELoaderConstants.LOCATION_LEVEL_URI + "?start="+((startIndex-1)-maxResults < 0 ? 0 : (startIndex-1)-maxResults)+"&maxResults="+maxResults);
430                     paginationArray.put(paginationObject);
431                 }
432                 if(loopIterationEnd != oleLocationLevelList.size()){
433                     paginationObject = new JSONObject();
434                     paginationObject.put("rel","next");
435                     paginationObject.put("href",OLELoaderConstants.LOCATION_LEVEL_URI + "?start="+(loopIterationEnd+1)+"&maxResults="+maxResults);
436                     paginationArray.put(paginationObject);
437                 }
438 
439                 jsonResponseObject.put("links",paginationArray);
440                 for(int index = (startIndex == 0 ? 0 : startIndex-1) ; index < loopIterationEnd-1 ; index++){
441                     OleLocationLevel oleLocationLevel = oleLocationLevelList.get(index);
442                     JSONObject jsonObject = new JSONObject();
443                     try {
444                         jsonObject.put("@id",OLELoaderConstants.LOCATION_LEVEL_URI + OLELoaderConstants.SLASH + oleLocationLevel.getLevelId());
445                         jsonObject.put("code",oleLocationLevel.getLevelCode());
446                         jsonObject.put("name",oleLocationLevel.getLevelName());
447                         JSONObject parentJsonObject = new JSONObject();
448                         if(oleLocationLevel.getOleLocationLevel() != null){
449                             parentJsonObject.put("@id",OLELoaderConstants.LOCATION_LEVEL_URI + OLELoaderConstants.SLASH + oleLocationLevel.getOleLocationLevel().getLevelId());
450                             parentJsonObject.put("name",oleLocationLevel.getOleLocationLevel().getLevelName());
451                         }
452                         jsonObject.put("parent",parentJsonObject);
453                     } catch (JSONException e) {
454                         e.printStackTrace();
455                     }
456                     jsonArray.put(jsonObject);
457                 }
458                 jsonResponseObject.put("items",jsonArray);
459             }
460         }catch (Exception e){
461             e.printStackTrace();
462         }
463         return jsonResponseObject;
464     }
465 
466     @Override
467     public OLELoaderResponseBo updateOleLocationLevel(OleLocationLevel oleLocationLevel, OLELocationLevelBo oleLocationLevelBo,HttpContext context) {
468         LOG.info("Inside update OleLocationLevel. LocationLevel Id : " + oleLocationLevelBo.getLevelId());
469         /*if(StringUtils.isNotBlank(oleLocationLevelBo.getLevelCode()))
470             oleLocationLevel.setLevelCode(oleLocationLevelBo.getLevelCode());*/
471         if(StringUtils.isNotBlank(oleLocationLevelBo.getLevelName()))
472             oleLocationLevel.setLevelName(oleLocationLevelBo.getLevelName());
473         try{
474             oleLocationLevel = getBusinessObjectService().save(oleLocationLevel);
475             String details = formLocationLevelExportResponse(oleLocationLevel, OLELoaderConstants.OLELoaderContext.LOCATION_LEVEL, context.getRequest().getRequestUri().toASCIIString(), true).toString();
476             return getOleLoaderService().generateResponse(
477                     OLELoaderConstants.OLEloaderMessage.LOCATION_LEVEL_SUCCESS,
478                     OLELoaderConstants.OLEloaderStatus.LOCATION_LEVEL_SUCCESS,details);
479         }catch(Exception e){
480             return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.LOCATION_LEVEL_FAILED, OLELoaderConstants.OLEloaderStatus.LOCATION_LEVEL_FAILED);
481         }
482     }
483 }