View Javadoc
1   package org.kuali.ole.loaders.deliver.service.impl;
2   
3   import com.sun.jersey.api.core.HttpContext;
4   import org.apache.commons.lang.StringUtils;
5   import org.apache.cxf.common.util.SortedArraySet;
6   import org.apache.log4j.Logger;
7   import org.codehaus.jettison.json.JSONArray;
8   import org.codehaus.jettison.json.JSONException;
9   import org.codehaus.jettison.json.JSONObject;
10  import org.kuali.ole.deliver.bo.OlePatronDocument;
11  import org.kuali.ole.ingest.pojo.OlePatron;
12  import org.kuali.ole.loaders.common.bo.OLELoaderImportResponseBo;
13  import org.kuali.ole.loaders.common.bo.OLELoaderResponseBo;
14  import org.kuali.ole.loaders.common.constants.OLELoaderConstants;
15  import org.kuali.ole.loaders.common.service.OLELoaderService;
16  import org.kuali.ole.loaders.common.service.impl.OLELoaderServiceImpl;
17  import org.kuali.ole.loaders.deliver.service.OLEPatronLoaderHelperService;
18  import org.kuali.ole.loaders.deliver.service.OLEPatronLoaderService;
19  import org.kuali.ole.service.OlePatronConverterService;
20  import org.kuali.ole.sys.context.SpringContext;
21  import org.kuali.rice.krad.service.BusinessObjectService;
22  import org.kuali.rice.krad.service.KRADServiceLocator;
23  
24  import java.util.*;
25  
26  /**
27   * Created by sheiksalahudeenm on 27/3/15.
28   */
29  public class OLEPatronLoaderServiceImpl implements OLEPatronLoaderService {
30  
31      private static final Logger LOG = Logger.getLogger(OLEPatronLoaderServiceImpl.class);
32      private OLEPatronLoaderHelperService olePatronLoaderHelperService;
33      private OLELoaderService oleLoaderService;
34      private BusinessObjectService businessObjectService;
35      private OlePatronConverterService olePatronConverterService;
36  
37      public OLEPatronLoaderHelperService getPatronDocumentLoaderHelperService() {
38          if(olePatronLoaderHelperService == null){
39              olePatronLoaderHelperService = new OLEPatronLoaderHelperServiceImpl();
40          }
41          return olePatronLoaderHelperService;
42      }
43  
44      public void setPatronDocumentLoaderHelperService(OLEPatronLoaderHelperService olePatronLoaderHelperService) {
45          this.olePatronLoaderHelperService = olePatronLoaderHelperService;
46      }
47  
48      public OLELoaderService getOleLoaderService() {
49          if(oleLoaderService == null ){
50              oleLoaderService = new OLELoaderServiceImpl();
51          }
52          return oleLoaderService;
53      }
54  
55      public void setOleLoaderService(OLELoaderService oleLoaderService) {
56          this.oleLoaderService = oleLoaderService;
57      }
58  
59      public BusinessObjectService getBusinessObjectService() {
60          if(businessObjectService == null){
61              businessObjectService = KRADServiceLocator.getBusinessObjectService();
62          }
63          return businessObjectService;
64      }
65  
66      public void setBusinessObjectService(BusinessObjectService businessObjectService) {
67          this.businessObjectService = businessObjectService;
68      }
69  
70      public OlePatronConverterService getOlePatronConverterService() {
71          if(olePatronConverterService == null){
72              olePatronConverterService = (OlePatronConverterService) SpringContext.getService("olePatronConverterService");
73          }
74          return olePatronConverterService;
75      }
76  
77      public void setOlePatronConverterService(OlePatronConverterService olePatronConverterService) {
78          this.olePatronConverterService = olePatronConverterService;
79      }
80  
81      @Override
82      public Object importPatrons(String bodyContent, HttpContext context) {
83          LOG.info("Inside importPatrons method.");
84          OLELoaderImportResponseBo oleLoaderImportResponseBo = new OLELoaderImportResponseBo();
85          Set<Integer> rejectPatronList = new SortedArraySet<>();
86          List<JSONObject> createdPatronObject = new ArrayList<JSONObject>();
87          JSONObject requestJsonObject = getOleLoaderService().getJsonObjectFromString(bodyContent);
88          boolean validObject = false;
89          List<OlePatron> failureList = new ArrayList<>();
90          List<OlePatronDocument> createdList = new ArrayList<>();
91          Map<String,Integer> rejectedPatronBarcodeAndIndexMap = new HashMap<>();
92          Map<String,Integer> selectedPatronBarcodeAndIndexMap = new HashMap<>();
93          if(requestJsonObject != null) {
94              if (requestJsonObject.has("items")) {
95                  String items = getOleLoaderService().getStringValueFromJsonObject(requestJsonObject, "items");
96                  if (StringUtils.isNotBlank(items)) {
97                      JSONArray patronJsonArray = getOleLoaderService().getJsonArrayFromString(items);
98                      List<OlePatron> olePatronList = getPatronDocumentLoaderHelperService().formIngestOlePatrons(patronJsonArray,rejectedPatronBarcodeAndIndexMap,selectedPatronBarcodeAndIndexMap);
99                      createdList = getOlePatronConverterService().createOlePatronDocument(olePatronList,failureList);
100                     for(OlePatronDocument olePatronDocument : createdList){
101                         createdPatronObject.add((JSONObject)getPatronDocumentLoaderHelperService().formPatronExportResponse(olePatronDocument,OLELoaderConstants.OLELoaderContext.PATRON, context.getRequest().getRequestUri().toASCIIString(), false));
102                     }
103                     for(OlePatron olePatron : failureList){
104                         rejectPatronList.add(selectedPatronBarcodeAndIndexMap.get(olePatron.getBarcode()));
105                     }
106                     for (Iterator<String> patronBarcodeIndex= rejectedPatronBarcodeAndIndexMap.keySet().iterator(); patronBarcodeIndex.hasNext(); ) {
107                         String barcode =  patronBarcodeIndex.next();
108                         rejectPatronList.add(rejectedPatronBarcodeAndIndexMap.get(barcode));
109                     }
110                 }else{
111                     return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.BAD_REQUEST, OLELoaderConstants.OLEloaderStatus.BAD_REQUEST);
112                 }
113 
114             }
115         }else{
116             return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.BAD_REQUEST, OLELoaderConstants.OLEloaderStatus.BAD_REQUEST);
117         }
118         oleLoaderImportResponseBo.setOleRejectedBos(new ArrayList<Integer>(rejectPatronList));
119         oleLoaderImportResponseBo.setOleCreatedBos(createdPatronObject);
120         return oleLoaderImportResponseBo;
121     }
122 
123    /* @Override
124     public OLELoaderResponseBo updatePatronById(String patronId, String bodyContent, HttpContext context) {
125         LOG.info("Inside updatePatronById method.");
126         OLEPatronBo olePatronBo = new OLEPatronBo();
127         JSONObject jsonObject = getOleLoaderService().getJsonObjectFromString(bodyContent);
128         boolean validObject = false;
129         if (jsonObject != null) {
130             if (jsonObject.has("name")) {
131                 String name = getOleLoaderService().getStringValueFromJsonObject(jsonObject, "name");
132                 if (StringUtils.isNotBlank(name)) {
133                     olePatronBo.setPatronName(name);
134                     validObject = true;
135                 }
136             }
137 
138             if(jsonObject.has("description")){
139                 String description = getOleLoaderService().getStringValueFromJsonObject(jsonObject,"description");
140                 if(StringUtils.isNotBlank(description)){
141                     olePatronBo.setPatronDescription(description);
142                     validObject = true;
143                 }
144             }
145 
146             if(jsonObject.has("active")){
147                 try{
148                     boolean active = Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(jsonObject, "active"));
149                     olePatronBo.setActive(active);
150                     validObject = true;
151                 }catch(Exception e){
152                     e.printStackTrace();
153                     return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.INVALID_BOOLEAN, OLELoaderConstants.OLEloaderStatus.INVALID_BOOLEAN);
154                 }
155 
156             }
157 
158             if (olePatronBo != null && validObject) {
159                 OlePatronDocument olePatron = getPatronDocumentLoaderHelperService().getPatronById(patronId);
160                 if (olePatron != null) {
161                     return getPatronDocumentLoaderHelperService().updateOlePatronDocument(olePatron,olePatronBo,context);
162                 } else {
163                     return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.PATRON_NOT_EXIST, OLELoaderConstants.OLEloaderStatus.PATRON_NOT_EXIST);
164                 }
165             } else {
166                 return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.BAD_REQUEST, OLELoaderConstants.OLEloaderStatus.BAD_REQUEST);
167             }
168         }else {
169             return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.BAD_REQUEST, OLELoaderConstants.OLEloaderStatus.BAD_REQUEST);
170         }
171     }*/
172 
173     @Override
174     public Object exportPatronById(String patronId) {
175         LOG.info("Inside exportPatronById method.");
176         OlePatronDocument olePatron = getPatronDocumentLoaderHelperService().getPatronById(patronId);
177         if(olePatron != null){
178             return olePatron;
179         }else{
180             return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.PATRON_NOT_EXIST, OLELoaderConstants.OLEloaderStatus.PATRON_NOT_EXIST);
181         }
182     }
183 
184     @Override
185     public Object exportPatronByCode(String patronCode) {
186         LOG.info("Inside exportPatronByCode method.");
187         OlePatronDocument olePatron = getPatronDocumentLoaderHelperService().getPatronByCode(patronCode);
188         if(olePatron != null){
189             return olePatron;
190         }else{
191             return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.PATRON_NOT_EXIST, OLELoaderConstants.OLEloaderStatus.PATRON_NOT_EXIST);
192         }
193     }
194 
195     @Override
196     public List<OlePatronDocument> exportAllPatrons() {
197         LOG.info("Inside exportAllPatrons method.");
198         List<OlePatronDocument> olePatrons = getPatronDocumentLoaderHelperService().getAllPatrons();
199         return olePatrons;
200     }
201 }