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.service.OLELocationLoaderHelperService;
17 import org.kuali.ole.service.OleLocationService;
18 import org.kuali.ole.sys.context.SpringContext;
19 import org.kuali.rice.krad.service.BusinessObjectService;
20 import org.kuali.rice.krad.service.KRADServiceLocator;
21
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26
27
28
29 public class OLELocationLoaderHelperServiceImpl implements OLELocationLoaderHelperService {
30
31 private static final Logger LOG = Logger.getLogger(OLELocationLoaderHelperServiceImpl.class);
32 private BusinessObjectService businessObjectService;
33 private OleLocationService oleLocationService;
34 private OLELoaderService oleLoaderService;
35
36 public BusinessObjectService getBusinessObjectService() {
37 if(businessObjectService == null){
38 businessObjectService = KRADServiceLocator.getBusinessObjectService();
39 }
40 return businessObjectService;
41 }
42
43 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
44 this.businessObjectService = businessObjectService;
45 }
46
47 public OleLocationService getOleLocationService() {
48 if(oleLocationService == null){
49 oleLocationService = (OleLocationService) SpringContext.getBean("oleLocationService");
50 }
51 return oleLocationService;
52 }
53
54 public void setOleLocationService(OleLocationService oleLocationService) {
55 this.oleLocationService = oleLocationService;
56 }
57
58 public OLELoaderService getOleLoaderService() {
59 if(oleLoaderService == null ){
60 oleLoaderService = new OLELoaderServiceImpl();
61 }
62 return oleLoaderService;
63 }
64
65 public void setOleLoaderService(OLELoaderService oleLoaderService) {
66 this.oleLoaderService = oleLoaderService;
67 }
68
69 @Override
70 public boolean isLocationLevelExistById(String locationLevelId) {
71 Map locationLevelCodeMap = new HashMap();
72 locationLevelCodeMap.put("levelId",locationLevelId);
73 List<OleLocationLevel> locationLevel = (List<OleLocationLevel>)getBusinessObjectService().findMatching(OleLocationLevel.class,locationLevelCodeMap);
74 if(locationLevel.size()>0)
75 return false;
76 else
77 return true;
78 }
79
80 @Override
81 public boolean isParentLocationLevelExist(String parentLocationLevelId) {
82 if(StringUtils.isNotBlank(parentLocationLevelId)){
83 Map parentLocationLevelMap = new HashMap();
84 parentLocationLevelMap.put("levelId",parentLocationLevelId);
85 List<OleLocationLevel> locationLevel = (List<OleLocationLevel>)getBusinessObjectService().findMatching(OleLocationLevel.class,parentLocationLevelMap);
86 if(locationLevel.size()>0)
87 return false;
88 else
89 return true;
90 }
91 return false;
92 }
93
94 @Override
95 public boolean isParentLocationExist(String parentLocationId) {
96 if(StringUtils.isNotBlank(parentLocationId)){
97 Map parentLocationCodeMap = new HashMap();
98 parentLocationCodeMap.put("locationId",parentLocationId);
99 List<OleLocation> locationLevel = (List<OleLocation>)getBusinessObjectService().findMatching(OleLocation.class,parentLocationCodeMap);
100 if(locationLevel.size()>0)
101 return false;
102 else
103 return true;
104 }
105 return false;
106 }
107
108 @Override
109 public boolean isLocationExistByCode(String locationCode) {
110 Map localtionCodeMap = new HashMap();
111 localtionCodeMap.put("locationCode", locationCode);
112 List<OleLocation> location = (List<OleLocation>)getBusinessObjectService().findMatching(OleLocation.class,localtionCodeMap);
113 if(location.size()>0){
114 return true;
115 }
116 return false;
117 }
118
119 @Override
120 public OleLocation createOleLocation(OLELocationBo oleLocationBo) {
121 LOG.info("Inside Create OleLocation. Location Code : " + oleLocationBo.getLocationCode());
122 OleLocation oleLocation = new OleLocation();
123 oleLocation.setLocationCode(oleLocationBo.getLocationCode());
124 oleLocation.setLocationName(oleLocationBo.getLocationName());
125 String locationLevelId = oleLocationBo.getLocationLevelId();
126 OleLocationLevel oleLocationLevel;
127 Map localtionLevelCd = new HashMap();
128 localtionLevelCd.put("levelId",locationLevelId);
129 List<OleLocationLevel> levelList = (List<OleLocationLevel>)getBusinessObjectService().findMatching(OleLocationLevel.class,localtionLevelCd);
130 if(levelList.size()>0){
131 oleLocationLevel = levelList.get(0);
132 oleLocation.setLevelId(oleLocationLevel.getLevelId());
133 } else{
134 throw new RuntimeException();
135 }
136 String parentLocationId = oleLocationBo.getParentLocationId();
137 OleLocation oleLocations;
138 if(StringUtils.isNotBlank(parentLocationId)){
139 Map parentLevelcd = new HashMap();
140 parentLevelcd.put("locationId",parentLocationId);
141 List<OleLocation> parentList = (List<OleLocation>)getBusinessObjectService().findMatching(OleLocation.class,parentLevelcd);
142 if(parentList.size()>0){
143 oleLocation.setParentLocationId(parentList.get(0).getLocationId());
144 oleLocation.setOleLocation(parentList.get(0));
145 }else {
146 oleLocation.setParentLocationId(null);
147 }
148 }else{
149 oleLocation.setParentLocationId(null);
150 }
151
152 getOleLocationService().createLocation(oleLocation);
153 return oleLocation;
154 }
155
156 @Override
157 public OLELoaderResponseBo updateOleLocation(OleLocation oleLocation, OLELocationBo oleLocationBo,HttpContext context) {
158 LOG.info("Inside update OleLocation. Location Code : " + oleLocationBo.getLocationCode());
159 if(StringUtils.isNotBlank(oleLocationBo.getLocationCode()))
160 oleLocation.setLocationCode(oleLocationBo.getLocationCode());
161 if(StringUtils.isNotBlank(oleLocationBo.getLocationName()))
162 oleLocation.setLocationName(oleLocationBo.getLocationName());
163 String locationLevelId = oleLocationBo.getLocationLevelId();
164 if(StringUtils.isNotBlank(locationLevelId)){
165 OleLocationLevel oleLocationLevel;
166 Map localtionLevelCd = new HashMap();
167 localtionLevelCd.put("levelId",locationLevelId);
168 List<OleLocationLevel> levelList = (List<OleLocationLevel>)getBusinessObjectService().findMatching(OleLocationLevel.class,localtionLevelCd);
169 if(levelList.size()>0){
170 oleLocationLevel = levelList.get(0);
171 oleLocation.setLevelId(oleLocationLevel.getLevelId());
172 oleLocation.setOleLocationLevel(oleLocationLevel);
173 } else{
174 return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderCode.LOCATION_LEVEL_NOT_EXIST,OLELoaderConstants.OLEloaderMessage.LOCATION_LEVEL_NOT_EXIST, OLELoaderConstants.OLEloaderStatus.LOCATION_LEVEL_NOT_EXIST);
175 }
176 }
177
178 String parentLocationId = oleLocationBo.getLocationLevelId();
179 if(StringUtils.isNotBlank(parentLocationId)){
180 Map parentLevelcd = new HashMap();
181 parentLevelcd.put("locationId",parentLocationId);
182 List<OleLocation> parentList = (List<OleLocation>)getBusinessObjectService().findMatching(OleLocation.class, parentLevelcd);
183 if(parentList.size()>0){
184 oleLocation.setParentLocationId(parentList.get(0).getLocationId());
185 oleLocation.setOleLocation(parentList.get(0));
186 }else {
187 return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderCode.PARENT_LOCATION_NOT_EXIST,OLELoaderConstants.OLEloaderMessage.PARENT_LOCATION_NOT_EXIST, OLELoaderConstants.OLEloaderStatus.PARENT_LOCATION_NOT_EXIST);
188 }
189 }
190 try{
191 if(getOleLocationService().updateLocation(oleLocation)){
192 String details = formLocationExportResponse(oleLocation,OLELoaderConstants.OLELoaderContext.LOCATION,context.getRequest().getRequestUri().toASCIIString()).toString();
193 return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderCode.LOCATION_SUCCESS,OLELoaderConstants.OLEloaderMessage.LOCATION_SUCCESS, OLELoaderConstants.OLEloaderStatus.LOCATION_SUCCESS,details);
194 }else{
195 return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderCode.LOCATION_FAILED,OLELoaderConstants.OLEloaderMessage.LOCATION_FAILED, OLELoaderConstants.OLEloaderStatus.LOCATION_FAILED);
196 }
197 }catch(Exception e){
198 return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderCode.LOCATION_FAILED,OLELoaderConstants.OLEloaderMessage.LOCATION_FAILED, OLELoaderConstants.OLEloaderStatus.LOCATION_FAILED);
199 }
200 }
201
202 @Override
203 public OleLocation getLocationById(String locationId) {
204 Map locationMap = new HashMap();
205 locationMap.put("locationId", locationId);
206 return getBusinessObjectService().findByPrimaryKey(OleLocation.class, locationMap);
207 }
208
209 @Override
210 public OleLocation getLocationByCode(String locationCode) {
211 Map locationMap = new HashMap();
212 locationMap.put("locationCode", locationCode);
213 List<OleLocation> oleLocationList = (List<OleLocation>) getBusinessObjectService().findMatching(OleLocation.class, locationMap);
214 return (oleLocationList != null && oleLocationList.size() > 0) ? oleLocationList.get(0) : null;
215 }
216
217 @Override
218 public List<OleLocation> getAllLocation() {
219 return (List<OleLocation>) getBusinessObjectService().findAll(OleLocation.class);
220 }
221
222 public Object formLocationExportResponse(Object object, String locationContext, String uri){
223 OleLocation oleLocation = (OleLocation) object;
224 JSONObject jsonObject = new JSONObject();
225 try {
226 jsonObject.put("@context",locationContext);
227 jsonObject.put("@id",OLELoaderConstants.LOCATION_URI + OLELoaderConstants.SLASH + oleLocation.getLocationId());
228 jsonObject.put("code",oleLocation.getLocationCode());
229 jsonObject.put("name",oleLocation.getLocationName());
230 JSONObject parentJsonObject = new JSONObject();
231 if(oleLocation.getOleLocation() != null){
232 parentJsonObject.put("@id",OLELoaderConstants.LOCATION_URI + OLELoaderConstants.SLASH + oleLocation.getOleLocation().getLocationId());
233 parentJsonObject.put("name",oleLocation.getOleLocation().getLocationName());
234 }
235 jsonObject.put("parent",parentJsonObject);
236 JSONObject locationLevelJsonObject = new JSONObject();
237 if(oleLocation.getOleLocationLevel() != null){
238 locationLevelJsonObject.put("@context", OLELoaderConstants.OLELoaderContext.LOCATION_LEVEL);
239 locationLevelJsonObject.put("@id",OLELoaderConstants.LOCATION_LEVEL_URI + OLELoaderConstants.SLASH + oleLocation.getOleLocationLevel().getLevelId());
240 locationLevelJsonObject.put("name",oleLocation.getOleLocationLevel().getLevelName());
241 }
242 jsonObject.put("level",locationLevelJsonObject);
243 } catch (JSONException e) {
244 e.printStackTrace();
245 }
246 return jsonObject;
247 }
248
249 @Override
250 public Object formAllLocationExportResponse(HttpContext context,List<OleLocation> oleLocationList, String locationContext, String uri) {
251 JSONObject jsonResponseObject = new JSONObject();
252 JSONArray paginationArray = new JSONArray();
253 JSONObject paginationObject = new JSONObject();
254 JSONArray jsonArray = new JSONArray();
255 try{
256 jsonResponseObject.put("@context",locationContext);
257 int startIndex = 0;
258 int maxResults = 50;
259 boolean validStartIndex = true;
260 if(context.getRequest().getQueryParameters().containsKey("start")){
261 try{
262 String start = context.getRequest().getQueryParameters().getFirst("start");
263 startIndex = Integer.parseInt(start);
264 if(startIndex < 0)
265 startIndex =0;
266 if(startIndex > oleLocationList.size()){
267 validStartIndex = false;
268 }
269 }catch (Exception e){
270 LOG.info("Invalid Start Index : " + e.getMessage());
271 startIndex = 0;
272 }
273 }
274 if(context.getRequest().getQueryParameters().containsKey("maxResults")){
275 try{
276 String maxCount = context.getRequest().getQueryParameters().getFirst("maxResults");
277 maxResults = Integer.parseInt(maxCount);
278 if(maxResults < 0)
279 maxResults =50;
280 }catch (Exception e){
281 LOG.info("Invalid Max Result count : " + e.getMessage());
282 maxResults = 50;
283 }
284 }
285 int loopIterationEnd = 0;
286 if(startIndex+maxResults > oleLocationList.size())
287 loopIterationEnd = oleLocationList.size();
288 else{
289 loopIterationEnd = startIndex+maxResults;
290 }
291
292 if(validStartIndex){
293 if(startIndex != 0){
294 paginationObject.put("rel","prev");
295 paginationObject.put("href",OLELoaderConstants.LOCATION_URI + "?start="+((startIndex-1)-maxResults < 0 ? 0 : (startIndex-1)-maxResults)+"&maxResults="+maxResults);
296 paginationArray.put(paginationObject);
297 }
298 if(loopIterationEnd != oleLocationList.size()){
299 paginationObject = new JSONObject();
300 paginationObject.put("rel","next");
301 paginationObject.put("href",OLELoaderConstants.LOCATION_URI + "?start="+(loopIterationEnd+1)+"&maxResults="+maxResults);
302 paginationArray.put(paginationObject);
303 }
304
305 jsonResponseObject.put("links",paginationArray);
306 for(int index = (startIndex == 0 ? 0 : startIndex-1) ; index < loopIterationEnd-1 ; index++){
307 OleLocation oleLocation = oleLocationList.get(index);
308 JSONObject jsonObject = new JSONObject();
309 try {
310 jsonObject.put("@id",OLELoaderConstants.LOCATION_URI + OLELoaderConstants.SLASH + oleLocation.getLocationId());
311 jsonObject.put("code",oleLocation.getLocationCode());
312 jsonObject.put("name",oleLocation.getLocationName());
313 JSONObject parentJsonObject = new JSONObject();
314 if(oleLocation.getOleLocation() != null){
315 parentJsonObject.put("@id",OLELoaderConstants.LOCATION_URI + OLELoaderConstants.SLASH + oleLocation.getOleLocation().getLocationId());
316 parentJsonObject.put("name",oleLocation.getOleLocation().getLocationName());
317 }
318 jsonObject.put("parent",parentJsonObject);
319 JSONObject locationLevelJsonObject = new JSONObject();
320 if(oleLocation.getOleLocationLevel() != null){
321 locationLevelJsonObject.put("@context", OLELoaderConstants.OLELoaderContext.LOCATION_LEVEL);
322 locationLevelJsonObject.put("@id",OLELoaderConstants.LOCATION_LEVEL_URI + OLELoaderConstants.SLASH + oleLocation.getOleLocationLevel().getLevelId());
323 locationLevelJsonObject.put("name",oleLocation.getOleLocationLevel().getLevelName());
324 }
325 jsonObject.put("level",locationLevelJsonObject);
326 } catch (JSONException e) {
327 e.printStackTrace();
328 }
329 jsonArray.put(jsonObject);
330 }
331 jsonResponseObject.put("items",jsonArray);
332 }
333 }catch (Exception e){
334 e.printStackTrace();
335 }
336 return jsonResponseObject;
337 }
338
339 @Override
340 public OleLocationLevel getLocationLevelById(String locationLevelId) {
341 Map locationLevelMap = new HashMap();
342 locationLevelMap.put("levelId", locationLevelId);
343 return getBusinessObjectService().findByPrimaryKey(OleLocationLevel.class, locationLevelMap);
344 }
345
346 @Override
347 public OleLocationLevel getLocationLevelByCode(String locationLevelCode) {
348 Map locationLevelMap = new HashMap();
349 locationLevelMap.put("levelCode", locationLevelCode);
350 return getBusinessObjectService().findByPrimaryKey(OleLocationLevel.class, locationLevelMap);
351 }
352
353 @Override
354 public Object formLocationLevelExportResponse(Object object, String locationLevelContext, String uri) {
355 OleLocationLevel oleLocationLevel = (OleLocationLevel) object;
356 JSONObject jsonObject = new JSONObject();
357 try {
358 jsonObject.put("@context",locationLevelContext);
359 jsonObject.put("@id",OLELoaderConstants.LOCATION_LEVEL_URI + OLELoaderConstants.SLASH + oleLocationLevel.getLevelId());
360 jsonObject.put("code",oleLocationLevel.getLevelCode());
361 jsonObject.put("name",oleLocationLevel.getLevelName());
362 JSONObject parentJsonObject = new JSONObject();
363 if(oleLocationLevel.getOleLocationLevel() != null){
364 parentJsonObject.put("@id",OLELoaderConstants.LOCATION_LEVEL_URI + OLELoaderConstants.SLASH + oleLocationLevel.getOleLocationLevel().getLevelId());
365 parentJsonObject.put("name",oleLocationLevel.getOleLocationLevel().getLevelName());
366 }
367 jsonObject.put("parent",parentJsonObject);
368 } catch (JSONException e) {
369 e.printStackTrace();
370 }
371 return jsonObject;
372 }
373 }