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.OleShelvingScheme;
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.OLEShelvingSchemeBo;
15  import org.kuali.ole.loaders.describe.service.OLEShelvingSchemeLoaderHelperService;
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 OLEShelvingSchemeLoaderHelperServiceImpl implements OLEShelvingSchemeLoaderHelperService {
27  
28      private static final Logger LOG = Logger.getLogger(OLEShelvingSchemeLoaderHelperServiceImpl.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 OleShelvingScheme getShelvingSchemeById(String shelvingSchemeId) {
57          Map shelvingSchemeMap = new HashMap();
58          shelvingSchemeMap.put("shelvingSchemeId", shelvingSchemeId);
59          return getBusinessObjectService().findByPrimaryKey(OleShelvingScheme.class, shelvingSchemeMap);
60      }
61  
62      @Override
63      public OleShelvingScheme getShelvingSchemeByCode(String shelvingSchemeCode) {
64          Map shelvingSchemeMap = new HashMap();
65          shelvingSchemeMap.put("shelvingSchemeCode", shelvingSchemeCode);
66          return getBusinessObjectService().findByPrimaryKey(OleShelvingScheme.class, shelvingSchemeMap);
67      }
68  
69      @Override
70      public List<OleShelvingScheme> getAllShelvingSchemes() {
71          return (List<OleShelvingScheme>) getBusinessObjectService().findAll(OleShelvingScheme.class);
72      }
73  
74      @Override
75      public Object formOleShelvingSchemeExportResponse(Object object, String shelvingSchemeContext, String uri, boolean addContext) {
76          OleShelvingScheme oleShelvingScheme = (OleShelvingScheme) object;
77          JSONObject jsonObject = new JSONObject();
78          try {
79              if(addContext)
80                  jsonObject.put("@context",shelvingSchemeContext);
81              jsonObject.put("@id", OLELoaderConstants.SHELVING_SCHEME_URI + OLELoaderConstants.SLASH + oleShelvingScheme.getShelvingSchemeId());
82              jsonObject.put("code",oleShelvingScheme.getShelvingSchemeCode());
83              jsonObject.put("name",oleShelvingScheme.getShelvingSchemeName());
84              jsonObject.put("source",oleShelvingScheme.getSource());
85              jsonObject.put("sourceDate",oleShelvingScheme.getSourceDate());
86              jsonObject.put("active",oleShelvingScheme.isActive());
87          } catch (JSONException e) {
88              e.printStackTrace();
89          }
90          return jsonObject;
91      }
92  
93      @Override
94      public Object formAllShelvingSchemeExportResponse(HttpContext context, List<OleShelvingScheme> oleShelvingSchemeList, String shelvingSchemeContext, 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",shelvingSchemeContext);
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 > oleShelvingSchemeList.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 > oleShelvingSchemeList.size())
131                 loopIterationEnd = oleShelvingSchemeList.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.SHELVING_SCHEME_URI + "?start="+((startIndex-1)-maxResults < 0 ? 0 : (startIndex-1)-maxResults)+"&maxResults="+maxResults);
140                     paginationArray.put(paginationObject);
141                 }
142                 if(loopIterationEnd != oleShelvingSchemeList.size()){
143                     paginationObject = new JSONObject();
144                     paginationObject.put("rel","next");
145                     paginationObject.put("href",OLELoaderConstants.SHELVING_SCHEME_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                     OleShelvingScheme oleShelvingScheme = oleShelvingSchemeList.get(index);
152                     JSONObject jsonObject = new JSONObject();
153                     try {
154                         jsonObject.put("@id", OLELoaderConstants.SHELVING_SCHEME_URI + OLELoaderConstants.SLASH + oleShelvingScheme.getShelvingSchemeId());
155                         jsonObject.put("code",oleShelvingScheme.getShelvingSchemeCode());
156                         jsonObject.put("name",oleShelvingScheme.getShelvingSchemeName());
157                         jsonObject.put("source",oleShelvingScheme.getSource());
158                         jsonObject.put("sourceDate",oleShelvingScheme.getSourceDate());
159                         jsonObject.put("active",oleShelvingScheme.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 updateOleShelvingScheme(OleShelvingScheme oleShelvingScheme, OLEShelvingSchemeBo oleShelvingSchemeBo, HttpContext context) {
175         if(StringUtils.isNotBlank(oleShelvingSchemeBo.getShelvingSchemeCode()))
176             oleShelvingScheme.setShelvingSchemeCode(oleShelvingSchemeBo.getShelvingSchemeCode());
177         if(StringUtils.isNotBlank(oleShelvingSchemeBo.getShelvingSchemeName()))
178             oleShelvingScheme.setShelvingSchemeName(oleShelvingSchemeBo.getShelvingSchemeName());
179         if(StringUtils.isNotBlank(oleShelvingSchemeBo.getSource()))
180             oleShelvingScheme.setSource(oleShelvingSchemeBo.getSource());
181         if(oleShelvingSchemeBo.getSourceDate() != null)
182             oleShelvingScheme.setSourceDate(new java.sql.Date(oleShelvingSchemeBo.getSourceDate().getTime()));
183         oleShelvingScheme.setActive(oleShelvingSchemeBo.isActive());
184         try{
185             oleShelvingScheme = KRADServiceLocator.getBusinessObjectService().save(oleShelvingScheme);
186             String details = formOleShelvingSchemeExportResponse(oleShelvingScheme, OLELoaderConstants.OLELoaderContext.SHELVING_SCHEME,
187                     context.getRequest().getRequestUri().toASCIIString(), true).toString();
188             return getOleLoaderService().generateResponse(
189                     OLELoaderConstants.OLEloaderMessage.SHELVING_SCHEME_SUCCESS,
190                     OLELoaderConstants.OLEloaderStatus.SHELVING_SCHEME_SUCCESS,details);
191         }catch(Exception e){
192             return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.SHELVING_SCHEME_FAILED, OLELoaderConstants.OLEloaderStatus.SHELVING_SCHEME_FAILED);
193         }
194     }
195 }