1 package org.kuali.ole.deliver.rest;
2
3 import org.apache.commons.collections.CollectionUtils;
4 import org.codehaus.jettison.json.JSONObject;
5 import org.kuali.asr.handler.ResponseHandler;
6 import org.kuali.ole.OLEConstants;
7 import org.kuali.ole.describe.bo.OleLocation;
8 import org.kuali.rice.krad.service.BusinessObjectService;
9 import org.kuali.rice.krad.service.KRADServiceLocator;
10 import org.springframework.stereotype.Controller;
11 import org.springframework.web.bind.annotation.RequestBody;
12 import org.springframework.web.bind.annotation.RequestMapping;
13 import org.springframework.web.bind.annotation.RequestMethod;
14 import org.springframework.web.bind.annotation.ResponseBody;
15
16 import javax.ws.rs.core.MediaType;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20
21
22
23
24 @Controller
25 @RequestMapping("/location")
26 public class LocationRestController {
27 private BusinessObjectService businessObjectService;
28 private ResponseHandler responseHandler;
29
30 @RequestMapping(method = RequestMethod.POST, value = "/fullLocation", produces = {MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
31 @ResponseBody
32 public String getFullLocation(@RequestBody String body) throws Exception {
33 String responseString = "";
34
35 JSONObject jsonObject = new JSONObject(body);
36
37 String locationCode = (String) jsonObject.get("locationCode");
38 List<OleLocation> oleLocations = getLocation(locationCode);
39
40 if (null != oleLocations) {
41 String objectToJson = getResponseHandler().marshalObjectToJson(oleLocations.get(0));
42 responseString = objectToJson;
43 }
44 return responseString;
45 }
46
47 private ResponseHandler getResponseHandler() {
48 if (null == responseHandler) {
49 responseHandler = new ResponseHandler();
50 }
51 return responseHandler;
52 }
53
54 public List<OleLocation> getLocation(String locationCode) {
55 Map<String, String> locationMap = new HashMap<String, String>();
56 locationMap.put(OLEConstants.LOC_CD, locationCode);
57 List<OleLocation> oleLocationList = (List<OleLocation>) getBusinessObjectService().findMatching(OleLocation.class, locationMap);
58
59 if(CollectionUtils.isNotEmpty(oleLocationList)){
60 return oleLocationList;
61 }
62 return null;
63 }
64
65 public BusinessObjectService getBusinessObjectService() {
66 if (null == businessObjectService) {
67 businessObjectService = KRADServiceLocator.getBusinessObjectService();
68 }
69 return businessObjectService;
70 }
71
72
73 }