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.OleItemAvailableStatus;
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.OLEItemAvailableStatusBo;
15  import org.kuali.ole.loaders.describe.service.OLEItemAvailableStatusLoaderHelperService;
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   * Created by sheiksalahudeenm on 27/3/15.
25   */
26  public class OLEItemAvailableStatusLoaderHelperServiceImpl implements OLEItemAvailableStatusLoaderHelperService {
27  
28      private static final Logger LOG = Logger.getLogger(OLEItemAvailableStatusLoaderHelperServiceImpl.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 OleItemAvailableStatus getItemAvailableStatusById(String itemAvailableStatusId) {
57          Map itemAvailableStatusMap = new HashMap();
58          itemAvailableStatusMap.put("itemAvailableStatusId", itemAvailableStatusId);
59          return getBusinessObjectService().findByPrimaryKey(OleItemAvailableStatus.class, itemAvailableStatusMap);
60      }
61  
62      @Override
63      public OleItemAvailableStatus getItemAvailableStatusByCode(String itemAvailableStatusCode) {
64          Map itemAvailableStatusMap = new HashMap();
65          itemAvailableStatusMap.put("itemAvailableStatusCode", itemAvailableStatusCode);
66          return getBusinessObjectService().findByPrimaryKey(OleItemAvailableStatus.class, itemAvailableStatusMap);
67      }
68  
69      @Override
70      public List<OleItemAvailableStatus> getAllItemAvailableStatus() {
71          return (List<OleItemAvailableStatus>) getBusinessObjectService().findAll(OleItemAvailableStatus.class);
72      }
73  
74      @Override
75      public Object formItemAvailableStatusExportResponse(Object object, String itemAvailableStatusContext, String uri, boolean addContext) {
76          OleItemAvailableStatus oleItemAvailableStatus = (OleItemAvailableStatus) object;
77          JSONObject jsonObject = new JSONObject();
78          try {
79              if(addContext)
80                  jsonObject.put("@context",itemAvailableStatusContext);
81              jsonObject.put("@id", OLELoaderConstants.ITEM_STATUS_URI + OLELoaderConstants.SLASH + oleItemAvailableStatus.getItemAvailableStatusId());
82              jsonObject.put("code",oleItemAvailableStatus.getItemAvailableStatusCode());
83              jsonObject.put("name",oleItemAvailableStatus.getItemAvailableStatusName());
84              jsonObject.put("active",oleItemAvailableStatus.isActive());
85          } catch (JSONException e) {
86              e.printStackTrace();
87          }
88          return jsonObject;
89      }
90  
91      @Override
92      public Object formAllItemAvailableStatusExportResponse(HttpContext context, List<OleItemAvailableStatus> oleItemAvailableStatusList, String itemAvailableStatusContext, String uri) {
93          JSONObject jsonResponseObject = new JSONObject();
94          JSONArray paginationArray = new JSONArray();
95          JSONObject paginationObject = new JSONObject();
96          JSONArray jsonArray = new JSONArray();
97          try{
98              jsonResponseObject.put("@context",itemAvailableStatusContext);
99              int startIndex = 0;
100             int maxResults = 50;
101             boolean validStartIndex = true;
102             if(context.getRequest().getQueryParameters().containsKey("start")){
103                 try{
104                     String start = context.getRequest().getQueryParameters().getFirst("start");
105                     startIndex = Integer.parseInt(start);
106                     if(startIndex < 0)
107                         startIndex =0;
108                     if(startIndex > oleItemAvailableStatusList.size()){
109                         validStartIndex = false;
110                     }
111                 }catch (Exception e){
112                     LOG.info("Invalid Start Index : " + e.getMessage());
113                     startIndex = 0;
114                 }
115             }
116             if(context.getRequest().getQueryParameters().containsKey("maxResults")){
117                 try{
118                     String maxCount = context.getRequest().getQueryParameters().getFirst("maxResults");
119                     maxResults = Integer.parseInt(maxCount);
120                     if(maxResults < 0)
121                         maxResults =50;
122                 }catch (Exception e){
123                     LOG.info("Invalid Max Result count : " + e.getMessage());
124                     maxResults = 50;
125                 }
126             }
127             int loopIterationEnd = 0;
128             if(startIndex+maxResults > oleItemAvailableStatusList.size())
129                 loopIterationEnd = oleItemAvailableStatusList.size();
130             else{
131                 loopIterationEnd = startIndex+maxResults;
132             }
133 
134             if(validStartIndex){
135                 if(startIndex != 0){
136                     paginationObject.put("rel","prev");
137                     paginationObject.put("href",OLELoaderConstants.ITEM_STATUS_URI + "?start="+((startIndex-1)-maxResults < 0 ? 0 : (startIndex-1)-maxResults)+"&maxResults="+maxResults);
138                     paginationArray.put(paginationObject);
139                 }
140                 if(loopIterationEnd != oleItemAvailableStatusList.size()){
141                     paginationObject = new JSONObject();
142                     paginationObject.put("rel","next");
143                     paginationObject.put("href",OLELoaderConstants.ITEM_STATUS_URI + "?start="+(loopIterationEnd+1)+"&maxResults="+maxResults);
144                     paginationArray.put(paginationObject);
145                 }
146 
147                 jsonResponseObject.put("links",paginationArray);
148                 for(int index = (startIndex == 0 ? 0 : startIndex-1) ; index < loopIterationEnd-1 ; index++){
149                     OleItemAvailableStatus oleItemAvailableStatus = oleItemAvailableStatusList.get(index);
150                     JSONObject jsonObject = new JSONObject();
151                     try {
152                         jsonObject.put("@id", OLELoaderConstants.ITEM_STATUS_URI + OLELoaderConstants.SLASH + oleItemAvailableStatus.getItemAvailableStatusId());
153                         jsonObject.put("code",oleItemAvailableStatus.getItemAvailableStatusCode());
154                         jsonObject.put("name",oleItemAvailableStatus.getItemAvailableStatusName());
155                         jsonObject.put("active",oleItemAvailableStatus.isActive());
156                     } catch (JSONException e) {
157                         e.printStackTrace();
158                     }
159                     jsonArray.put(jsonObject);
160                 }
161                 jsonResponseObject.put("items",jsonArray);
162             }
163         }catch (Exception e){
164             e.printStackTrace();
165         }
166         return jsonResponseObject;
167     }
168 
169     @Override
170     public OLELoaderResponseBo updateOleItemAvailableStatus(OleItemAvailableStatus oleInstanceItemAvailableStatus, OLEItemAvailableStatusBo oleItemAvailableStatusBo, HttpContext context) {
171         if(oleItemAvailableStatusBo.getItemAvailableStatusName() != null)
172             oleInstanceItemAvailableStatus.setItemAvailableStatusName(oleItemAvailableStatusBo.getItemAvailableStatusName());
173         oleInstanceItemAvailableStatus.setActive(oleItemAvailableStatusBo.isActive());
174         try{
175             oleInstanceItemAvailableStatus = KRADServiceLocator.getBusinessObjectService().save(oleInstanceItemAvailableStatus);
176             String details = formItemAvailableStatusExportResponse(oleInstanceItemAvailableStatus, OLELoaderConstants.OLELoaderContext.ITEM_STATUS,
177                     context.getRequest().getRequestUri().toASCIIString(), true).toString();
178             return getOleLoaderService().generateResponse(
179                     OLELoaderConstants.OLEloaderMessage.ITEM_STATUS_SUCCESS,
180                     OLELoaderConstants.OLEloaderStatus.ITEM_STATUS_SUCCESS,details);
181         }catch(Exception e){
182             return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.ITEM_STATUS_FAILED, OLELoaderConstants.OLEloaderStatus.ITEM_STATUS_FAILED);
183         }
184 
185     }
186 }