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.OleStatisticalSearchingCodes;
10 import org.kuali.ole.loaders.common.bo.OLELoaderResponseBo;
11 import org.kuali.ole.loaders.common.constants.OLELoaderConstants;
12 import org.kuali.ole.loaders.common.service.OLELoaderService;
13 import org.kuali.ole.loaders.common.service.impl.OLELoaderServiceImpl;
14 import org.kuali.ole.loaders.describe.bo.OLEStatisticalSearchingCodeBo;
15 import org.kuali.ole.loaders.describe.service.OLEStatisticalSearchingCodeLoaderHelperService;
16 import org.kuali.rice.krad.service.BusinessObjectService;
17 import org.kuali.rice.krad.service.KRADServiceLocator;
18
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23
24
25
26 public class OLEStatisticalSearchingCodeLoaderHelperServiceImpl implements OLEStatisticalSearchingCodeLoaderHelperService {
27
28 private static final Logger LOG = Logger.getLogger(OLEStatisticalSearchingCodeLoaderHelperServiceImpl.class);
29 private BusinessObjectService businessObjectService;
30 private OLELoaderService oleLoaderService;
31
32
33 public BusinessObjectService getBusinessObjectService() {
34 if(businessObjectService == null){
35 businessObjectService = KRADServiceLocator.getBusinessObjectService();
36 }
37 return businessObjectService;
38 }
39
40 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
41 this.businessObjectService = businessObjectService;
42 }
43
44 public OLELoaderService getOleLoaderService() {
45 if(oleLoaderService == null){
46 oleLoaderService = new OLELoaderServiceImpl();
47 }
48 return oleLoaderService;
49 }
50
51 public void setOleLoaderService(OLELoaderService oleLoaderService) {
52 this.oleLoaderService = oleLoaderService;
53 }
54
55 @Override
56 public OleStatisticalSearchingCodes getStatisticalSearchingCodeById(String statisticalSearchingCodeId) {
57 Map statisticalSearchingCodeMap = new HashMap();
58 statisticalSearchingCodeMap.put("statisticalSearchingCodeId", statisticalSearchingCodeId);
59 return getBusinessObjectService().findByPrimaryKey(OleStatisticalSearchingCodes.class, statisticalSearchingCodeMap);
60 }
61
62 @Override
63 public OleStatisticalSearchingCodes getStatisticalSearchingCodeByCode(String statisticalSearchingCodeCode) {
64 Map statisticalSearchingCodeMap = new HashMap();
65 statisticalSearchingCodeMap.put("statisticalSearchingCode", statisticalSearchingCodeCode);
66 return getBusinessObjectService().findByPrimaryKey(OleStatisticalSearchingCodes.class, statisticalSearchingCodeMap);
67 }
68
69 @Override
70 public List<OleStatisticalSearchingCodes> getAllStatisticalSearchingCode() {
71 return (List<OleStatisticalSearchingCodes>) getBusinessObjectService().findAll(OleStatisticalSearchingCodes.class);
72 }
73
74 @Override
75 public Object formStatisticalSearchingCodeExportResponse(Object object, String statisticalSearchingCodeContext, String uri, boolean addContext) {
76 OleStatisticalSearchingCodes oleStatisticalSearchingCode = (OleStatisticalSearchingCodes) object;
77 JSONObject jsonObject = new JSONObject();
78 try {
79 if(addContext)
80 jsonObject.put("@context",statisticalSearchingCodeContext);
81 jsonObject.put("@id", OLELoaderConstants.STAT_SEARCH_CODE_URI + OLELoaderConstants.SLASH + oleStatisticalSearchingCode.getStatisticalSearchingCodeId());
82 jsonObject.put("code",oleStatisticalSearchingCode.getStatisticalSearchingCode());
83 jsonObject.put("name",oleStatisticalSearchingCode.getStatisticalSearchingName());
84 jsonObject.put("source",oleStatisticalSearchingCode.getSource());
85 jsonObject.put("sourceDate",oleStatisticalSearchingCode.getSourceDate());
86 jsonObject.put("active",oleStatisticalSearchingCode.isActive());
87 } catch (JSONException e) {
88 e.printStackTrace();
89 }
90 return jsonObject;
91 }
92
93 @Override
94 public Object formAllStatisticalSearchingCodeExportResponse(HttpContext context, List<OleStatisticalSearchingCodes> oleStatisticalSearchingCodeList, String statisticalSearchingCodeContext, String uri) {
95 JSONObject jsonResponseObject = new JSONObject();
96 JSONArray paginationArray = new JSONArray();
97 JSONObject paginationObject = new JSONObject();
98 JSONArray jsonArray = new JSONArray();
99 try{
100 jsonResponseObject.put("@context",statisticalSearchingCodeContext);
101 int startIndex = 0;
102 int maxResults = 50;
103 boolean validStartIndex = true;
104 if(context.getRequest().getQueryParameters().containsKey("start")){
105 try{
106 String start = context.getRequest().getQueryParameters().getFirst("start");
107 startIndex = Integer.parseInt(start);
108 if(startIndex < 0)
109 startIndex =0;
110 if(startIndex > oleStatisticalSearchingCodeList.size()){
111 validStartIndex = false;
112 }
113 }catch (Exception e){
114 LOG.info("Invalid Start Index : " + e.getMessage());
115 startIndex = 0;
116 }
117 }
118 if(context.getRequest().getQueryParameters().containsKey("maxResults")){
119 try{
120 String maxCount = context.getRequest().getQueryParameters().getFirst("maxResults");
121 maxResults = Integer.parseInt(maxCount);
122 if(maxResults < 0)
123 maxResults =50;
124 }catch (Exception e){
125 LOG.info("Invalid Max Result count : " + e.getMessage());
126 maxResults = 50;
127 }
128 }
129 int loopIterationEnd = 0;
130 if(startIndex+maxResults > oleStatisticalSearchingCodeList.size())
131 loopIterationEnd = oleStatisticalSearchingCodeList.size();
132 else{
133 loopIterationEnd = startIndex+maxResults;
134 }
135
136 if(validStartIndex){
137 if(startIndex != 0){
138 paginationObject.put("rel","prev");
139 paginationObject.put("href",OLELoaderConstants.STAT_SEARCH_CODE_URI + "?start="+((startIndex-1)-maxResults < 0 ? 0 : (startIndex-1)-maxResults)+"&maxResults="+maxResults);
140 paginationArray.put(paginationObject);
141 }
142 if(loopIterationEnd != oleStatisticalSearchingCodeList.size()){
143 paginationObject = new JSONObject();
144 paginationObject.put("rel","next");
145 paginationObject.put("href",OLELoaderConstants.STAT_SEARCH_CODE_URI + "?start="+(loopIterationEnd+1)+"&maxResults="+maxResults);
146 paginationArray.put(paginationObject);
147 }
148
149 jsonResponseObject.put("links",paginationArray);
150 for(int index = (startIndex == 0 ? 0 : startIndex-1) ; index < loopIterationEnd-1 ; index++){
151 OleStatisticalSearchingCodes oleStatisticalSearchingCode = oleStatisticalSearchingCodeList.get(index);
152 JSONObject jsonObject = new JSONObject();
153 try {
154 jsonObject.put("@id", OLELoaderConstants.STAT_SEARCH_CODE_URI + OLELoaderConstants.SLASH + oleStatisticalSearchingCode.getStatisticalSearchingCodeId());
155 jsonObject.put("code",oleStatisticalSearchingCode.getStatisticalSearchingCode());
156 jsonObject.put("name",oleStatisticalSearchingCode.getStatisticalSearchingName());
157 jsonObject.put("source",oleStatisticalSearchingCode.getSource());
158 jsonObject.put("sourceDate",oleStatisticalSearchingCode.getSourceDate());
159 jsonObject.put("active",oleStatisticalSearchingCode.isActive());
160 } catch (JSONException e) {
161 e.printStackTrace();
162 }
163 jsonArray.put(jsonObject);
164 }
165 jsonResponseObject.put("items",jsonArray);
166 }
167 }catch (Exception e){
168 e.printStackTrace();
169 }
170 return jsonResponseObject;
171 }
172
173 @Override
174 public OLELoaderResponseBo updateOleStatisticalSearchingCode(OleStatisticalSearchingCodes oleInstanceStatisticalSearchingCode, OLEStatisticalSearchingCodeBo oleStatisticalSearchingCodeBo, HttpContext context) {
175 if(StringUtils.isNotBlank(oleStatisticalSearchingCodeBo.getStatisticalSearchingName()))
176 oleInstanceStatisticalSearchingCode.setStatisticalSearchingName(oleStatisticalSearchingCodeBo.getStatisticalSearchingName());
177 if(StringUtils.isNotBlank(oleStatisticalSearchingCodeBo.getSource()))
178 oleInstanceStatisticalSearchingCode.setSource(oleStatisticalSearchingCodeBo.getSource());
179 if(oleStatisticalSearchingCodeBo.getSourceDate() != null)
180 oleInstanceStatisticalSearchingCode.setSourceDate(new java.sql.Date(oleStatisticalSearchingCodeBo.getSourceDate().getTime()));
181 oleInstanceStatisticalSearchingCode.setActive(oleStatisticalSearchingCodeBo.isActive());
182 try{
183 oleInstanceStatisticalSearchingCode = KRADServiceLocator.getBusinessObjectService().save(oleInstanceStatisticalSearchingCode);
184 String details = formStatisticalSearchingCodeExportResponse(oleInstanceStatisticalSearchingCode, OLELoaderConstants.OLELoaderContext.STAT_SEARCH_CODE,
185 context.getRequest().getRequestUri().toASCIIString(), true).toString();
186 return getOleLoaderService().generateResponse(
187 OLELoaderConstants.OLEloaderMessage.STAT_SEARCH_CODE_SUCCESS,
188 OLELoaderConstants.OLEloaderStatus.STAT_SEARCH_CODE_SUCCESS,details);
189 }catch(Exception e){
190 return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.STAT_SEARCH_CODE_FAILED, OLELoaderConstants.OLEloaderStatus.STAT_SEARCH_CODE_FAILED);
191 }
192
193 }
194 }