View Javadoc
1   package org.kuali.ole.ncip.servlet;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.converter.OLECheckInItemConverter;
5   import org.kuali.ole.converter.OLELookupUserConverter;
6   import org.kuali.ole.converter.OLERenewItemConverter;
7   import org.kuali.ole.ncip.bo.OLECirculationErrorMessage;
8   import org.kuali.ole.ncip.bo.OLENCIPConstants;
9   import org.kuali.ole.ncip.bo.OLENCIPErrorResponse;
10  import org.kuali.ole.ncip.converter.*;
11  import org.kuali.ole.ncip.service.OLECirculationService;
12  import org.kuali.ole.ncip.service.impl.*;
13  
14  import javax.servlet.ServletException;
15  import javax.servlet.http.HttpServlet;
16  import javax.servlet.http.HttpServletRequest;
17  import javax.servlet.http.HttpServletResponse;
18  import java.io.IOException;
19  import java.io.PrintWriter;
20  import java.util.*;
21  
22  /**
23   * Created with IntelliJ IDEA.
24   * User: sheiksalahudeenm
25   * Date: 8/16/13
26   * Time: 7:59 PM
27   * To change this template use File | Settings | File Templates.
28   */
29  public class OLECirculationServlet extends HttpServlet {
30      final Logger LOG = Logger.getLogger(OLECirculationServlet.class);
31  
32  
33      public void doPost(HttpServletRequest request, HttpServletResponse response)
34              throws ServletException, IOException {
35          OLECirculationService oleCirculationService=new OLECirculationServiceImpl();
36          boolean doProcess=true;
37          String responseString="";
38          Map<String,String[]> parameterMap=new HashMap<String,String[]>();
39          String outputFormat=OLENCIPConstants.XML_FORMAT;
40  
41          parameterMap=request.getParameterMap();
42  
43          if(parameterMap.containsKey(OLENCIPConstants.FORMAT)){
44              if(parameterMap.get(OLENCIPConstants.FORMAT)[0].equalsIgnoreCase(OLENCIPConstants.XML_FORMAT) || parameterMap.get(OLENCIPConstants.FORMAT)[0].equalsIgnoreCase(OLENCIPConstants.JSON_FORMAT)){
45                  outputFormat=parameterMap.get(OLENCIPConstants.FORMAT)[0];
46              }
47              else{
48                  doProcess=false;
49              }
50          }
51  
52          String service=parameterMap.get(OLENCIPConstants.OLE_CIRCULATION_SERVICE)[0];
53          if(service!=null){
54              if(doProcess){
55                  switch (service){
56                      case OLENCIPConstants.PLACEREQUEST_SERVICE:
57                          if(parameterMap.containsKey(OLENCIPConstants.PATRON_BARCODE) &&
58                                  parameterMap.containsKey(OLENCIPConstants.OPERATOR_ID) &&
59                                  parameterMap.containsKey(OLENCIPConstants.ITEM_BARCODE) &&
60                                  parameterMap.containsKey(OLENCIPConstants.REQUEST_TYPE)){
61                              int size=5;
62                              if(parameterMap.containsKey(OLENCIPConstants.FORMAT)){
63                                  size++;
64                              }
65                              if(parameterMap.containsKey(OLENCIPConstants.PICKUP_LOCATION))
66                              {
67                                  size++;
68                              }
69                              if(parameterMap.size()==size){
70                                  String pickupLocation=null;
71                                  if(parameterMap.containsKey(OLENCIPConstants.PICKUP_LOCATION)) {
72                                      pickupLocation=parameterMap.get(OLENCIPConstants.PICKUP_LOCATION)[0];
73                                  }
74                                  responseString=oleCirculationService.placeRequest(parameterMap.get(OLENCIPConstants.PATRON_BARCODE)[0],parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0],parameterMap.get(OLENCIPConstants.ITEM_BARCODE)[0],parameterMap.get(OLENCIPConstants.REQUEST_TYPE)[0],pickupLocation,null,null,"Item Level",null);
75                                  if(outputFormat.equalsIgnoreCase(OLENCIPConstants.JSON_FORMAT)){
76                                      responseString=new OLEPlaceRequestConverter().generatePlaceRequestJson(responseString);
77                                  }
78                                  if(responseString==null){
79                                      OLENCIPErrorResponse olencipErrorResponse=new OLENCIPErrorResponse();
80                                      olencipErrorResponse.getErrorMap().put(OLENCIPConstants.PATRON_BARCODE,parameterMap.get(OLENCIPConstants.PATRON_BARCODE)[0]);
81                                      olencipErrorResponse.getErrorMap().put(OLENCIPConstants.OPERATOR_ID,parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0]);
82                                      olencipErrorResponse.getErrorMap().put(OLENCIPConstants.MESSAGE,OLENCIPConstants.INVALID_INPUT);
83                                      responseString=olencipErrorResponse.getErrorXml(service);
84                                  }
85                              }else{
86                                  responseString=getCirculationErrorMessage(service,OLENCIPConstants.INVALID_PARAMETERS,"501", OLENCIPConstants.PLACEREQUEST,outputFormat);
87                              }
88                          }else{
89                              responseString=getCirculationErrorMessage(service,OLENCIPConstants.PARAMETER_MISSING,"502",OLENCIPConstants.PLACEREQUEST,outputFormat);
90                          }
91                          break;
92                      case OLENCIPConstants.CANCELREQUEST_SERVICE:
93                          if(parameterMap.containsKey(OLENCIPConstants.OPERATOR_ID) &&
94                                  parameterMap.containsKey(OLENCIPConstants.REQUEST_ID)){
95                              if(parameterMap.size()==3 || parameterMap.size()==4){
96                                 responseString=oleCirculationService.cancelRequests(parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0],parameterMap.get(OLENCIPConstants.REQUEST_ID)[0]);
97                                  if(outputFormat.equalsIgnoreCase(OLENCIPConstants.JSON_FORMAT)){
98                                      responseString=new OLECancelRequestConverter().generateCancelRequestJson(responseString);
99                                  }
100                                 if(responseString==null){
101                                     OLENCIPErrorResponse olencipErrorResponse=new OLENCIPErrorResponse();
102                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.OPERATOR_ID,parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0]);
103                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.REQUEST_ID,parameterMap.get(OLENCIPConstants.REQUEST_ID)[0]);
104                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.MESSAGE,OLENCIPConstants.INVALID_INPUT);
105                                     responseString=olencipErrorResponse.getErrorXml(service);
106                                 }
107                             }else{
108                                 responseString=getCirculationErrorMessage(service,OLENCIPConstants.INVALID_PARAMETERS,"501",OLENCIPConstants.CANCELREQUEST,outputFormat);
109                             }
110                         }else{
111                             responseString=getCirculationErrorMessage(service,OLENCIPConstants.PARAMETER_MISSING,"502",OLENCIPConstants.CANCELREQUEST,outputFormat);
112                         }
113                         break;
114                     case OLENCIPConstants.RENEWITEM_SERVICE:
115                         if(parameterMap.containsKey(OLENCIPConstants.PATRON_BARCODE) &&
116                                 parameterMap.containsKey(OLENCIPConstants.OPERATOR_ID) &&
117                                 parameterMap.containsKey(OLENCIPConstants.ITEM_BARCODE)){
118                             if(parameterMap.size()==4 || parameterMap.size()==5){
119 
120                                 Map renewParameters = new HashMap();
121                                 renewParameters.put("patronBarcode", parameterMap.get(OLENCIPConstants.PATRON_BARCODE)[0]);
122                                 renewParameters.put("operatorId", parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0]);
123                                 List<String> items = new ArrayList<>();
124                                 items.add(parameterMap.get(OLENCIPConstants.ITEM_BARCODE)[0]);
125                                 renewParameters.put("itemBarcodes", items);
126                                 renewParameters.put("responseFormatType", outputFormat);
127                                 responseString = new VuFindRenewItemService().renewItems(renewParameters);
128 
129                                 if(outputFormat.equalsIgnoreCase(OLENCIPConstants.JSON_FORMAT)){
130                                     responseString=new OLERenewItemConverter().generateRenewItemJson(responseString);
131                                 }
132                                 if(responseString==null){
133                                     OLENCIPErrorResponse olencipErrorResponse=new OLENCIPErrorResponse();
134                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.PATRON_BARCODE,parameterMap.get(OLENCIPConstants.PATRON_BARCODE)[0]);
135                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.OPERATOR_ID,parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0]);
136                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.MESSAGE,responseString);
137                                     responseString=olencipErrorResponse.getErrorXml(service);
138                                 }
139                             }else{
140                                 responseString=getCirculationErrorMessage(service,OLENCIPConstants.INVALID_PARAMETERS,"501",OLENCIPConstants.RENEWITEM,outputFormat);
141                             }
142                         }else{
143                             responseString=getCirculationErrorMessage(service,OLENCIPConstants.PARAMETER_MISSING,"502",OLENCIPConstants.RENEWITEM,outputFormat);
144                         }
145                         break;
146                     case OLENCIPConstants.RENEWITEMLIST_SERVICE:
147                         if(parameterMap.containsKey(OLENCIPConstants.PATRON_BARCODE) &&
148                                 parameterMap.containsKey(OLENCIPConstants.OPERATOR_ID) &&
149                                 parameterMap.containsKey(OLENCIPConstants.ITEM_BARCODE)){
150                             if(parameterMap.size()==4 || parameterMap.size()==5){
151                                 Long startingTime = System.currentTimeMillis();
152 
153                                 Map renewParameters = new HashMap();
154                                 renewParameters.put("patronBarcode", parameterMap.get(OLENCIPConstants.PATRON_BARCODE)[0]);
155                                 renewParameters.put("operatorId", parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0]);
156                                 String itemBarcodeList = parameterMap.get(OLENCIPConstants.ITEM_BARCODE)[0];
157                                 String[] itemBarcodeArray = itemBarcodeList.split(",");
158                                 List<String> renewalItemList = Arrays.asList(itemBarcodeArray);
159                                 renewParameters.put("itemBarcodes", renewalItemList);
160                                 renewParameters.put("responseFormatType", outputFormat);
161                                 responseString = new VuFindRenewItemService().renewItems(renewParameters);
162 
163                                 Long endTimme = System.currentTimeMillis();
164                                 Long timeTakenForRenewAll = endTimme-startingTime;
165                                 LOG.info("The Time Taken for RenewAll : "+timeTakenForRenewAll);
166                                 if(outputFormat.equalsIgnoreCase(OLENCIPConstants.JSON_FORMAT)){
167                                     responseString=new OLERenewItemConverter().generateRenewItemListJson(responseString);
168                                 }
169                                 if(responseString==null){
170                                     OLENCIPErrorResponse olencipErrorResponse=new OLENCIPErrorResponse();
171                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.PATRON_BARCODE,parameterMap.get(OLENCIPConstants.PATRON_BARCODE)[0]);
172                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.OPERATOR_ID,parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0]);
173                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.MESSAGE,responseString);
174                                     responseString=olencipErrorResponse.getErrorXml(service);
175                                 }
176                             }else{
177                                 responseString=getCirculationErrorMessage(service,OLENCIPConstants.INVALID_PARAMETERS,"501",OLENCIPConstants.RENEWITEM,outputFormat);
178                             }
179                         }else{
180                             responseString=getCirculationErrorMessage(service,OLENCIPConstants.PARAMETER_MISSING,"502",OLENCIPConstants.RENEWITEM,outputFormat);
181                         }
182                         break;
183                     case OLENCIPConstants.ACCEPTITEM_SERVICE:
184                         if(parameterMap.containsKey(OLENCIPConstants.PATRON_BARCODE) &&
185                                 parameterMap.containsKey(OLENCIPConstants.OPERATOR_ID) &&
186                                 parameterMap.containsKey(OLENCIPConstants.ITEM_BARCODE) &&
187                                 parameterMap.containsKey(OLENCIPConstants.CALLNUMBER) &&
188                                 parameterMap.containsKey(OLENCIPConstants.TITLE) &&
189                                 parameterMap.containsKey(OLENCIPConstants.AUTHOR) &&
190                                 parameterMap.containsKey(OLENCIPConstants.ITEM_TYPE) &&
191                                 parameterMap.containsKey(OLENCIPConstants.ITEM_LOCATION) &&
192                                 parameterMap.containsKey(OLENCIPConstants.DATE_EXPIRES) &&
193                                 parameterMap.containsKey(OLENCIPConstants.REQUEST_TYPE)){
194                             int size=11;
195                             if(parameterMap.containsKey(OLENCIPConstants.FORMAT)){
196                                 size++;
197                             }
198                             if(parameterMap.containsKey(OLENCIPConstants.PICKUP_LOCATION))
199                             {
200                                 size++;
201                             }
202 
203                             if(parameterMap.size()==size){
204                                 String pickupLocation=null;
205                                 if(parameterMap.containsKey(OLENCIPConstants.PICKUP_LOCATION)) {
206                                     pickupLocation=parameterMap.get(OLENCIPConstants.PICKUP_LOCATION)[0];
207                                 }
208                                 responseString=oleCirculationService.acceptItem(parameterMap.get(OLENCIPConstants.PATRON_BARCODE)[0],parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0],parameterMap.get(OLENCIPConstants.ITEM_BARCODE)[0],parameterMap.get(OLENCIPConstants.CALLNUMBER)[0],parameterMap.get(OLENCIPConstants.TITLE)[0],parameterMap.get(OLENCIPConstants.AUTHOR)[0],parameterMap.get(OLENCIPConstants.ITEM_TYPE)[0],parameterMap.get(OLENCIPConstants.ITEM_LOCATION)[0],parameterMap.get(OLENCIPConstants.DATE_EXPIRES)[0],parameterMap.get(OLENCIPConstants.REQUEST_TYPE)[0],pickupLocation);
209                                 if(outputFormat.equalsIgnoreCase(OLENCIPConstants.JSON_FORMAT)){
210                                     responseString=new OLEAcceptItemConverter().generateAcceptItemJson(responseString);
211                                 }
212                                 if(responseString==null){
213                                     OLENCIPErrorResponse olencipErrorResponse=new OLENCIPErrorResponse();
214                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.PATRON_BARCODE,parameterMap.get(OLENCIPConstants.PATRON_BARCODE)[0]);
215                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.OPERATOR_ID,parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0]);
216                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.MESSAGE,OLENCIPConstants.INVALID_INPUT);
217                                     responseString=olencipErrorResponse.getErrorXml(service);
218                                 }
219                             }else{
220                                 responseString=getCirculationErrorMessage(service,OLENCIPConstants.INVALID_PARAMETERS,"501",OLENCIPConstants.ACCEPTITEM,outputFormat);
221                             }
222                         }else{
223                             responseString=getCirculationErrorMessage(service,OLENCIPConstants.PARAMETER_MISSING,"502",OLENCIPConstants.ACCEPTITEM,outputFormat);
224                         }
225                         break;
226                     case OLENCIPConstants.CHECKINITEM_SERVICE:
227                         if(parameterMap.containsKey(OLENCIPConstants.PATRON_BARCODE) &&
228                                 parameterMap.containsKey(OLENCIPConstants.OPERATOR_ID) &&
229                                 parameterMap.containsKey(OLENCIPConstants.ITEM_BARCODE) &&
230                                 parameterMap.containsKey(OLENCIPConstants.DELETE_INDICATOR)){
231                             if(parameterMap.size()==5 || parameterMap.size()==6){
232 
233                                 Map checkinParameters = new HashMap();
234                                 checkinParameters.put("operatorId", parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0]);
235                                 checkinParameters.put("itemBarcode", parameterMap.get(OLENCIPConstants.ITEM_BARCODE)[0]);
236                                 checkinParameters.put("responseFormatType", outputFormat);
237                                 checkinParameters.put("deleteIndicator", parameterMap.get(OLENCIPConstants.DELETE_INDICATOR)[0]);
238                                 responseString = new NonSip2CheckinItemServiceImplImpl().checkinItem(checkinParameters);
239 
240                                 if(outputFormat.equalsIgnoreCase(OLENCIPConstants.JSON_FORMAT)){
241                                     responseString=new OLECheckInItemConverter().generateCheckInItemJson(responseString);
242                                 }
243                                 if(responseString==null){
244                                     OLENCIPErrorResponse olencipErrorResponse=new OLENCIPErrorResponse();
245                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.PATRON_BARCODE,parameterMap.get(OLENCIPConstants.PATRON_BARCODE)[0]);
246                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.OPERATOR_ID,parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0]);
247                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.MESSAGE,OLENCIPConstants.INVALID_INPUT);
248                                     responseString=olencipErrorResponse.getErrorXml(service);
249                                 }
250                             }else{
251                                 responseString=getCirculationErrorMessage(service,OLENCIPConstants.INVALID_PARAMETERS,"501",OLENCIPConstants.CHECKINITEM,outputFormat);
252 
253                             }
254                         }else{
255                             responseString=getCirculationErrorMessage(service,OLENCIPConstants.PARAMETER_MISSING,"502",OLENCIPConstants.CHECKINITEM,outputFormat);
256                         }
257                         break;
258                     case OLENCIPConstants.CHECKOUTITEM_SERVICE:
259                         if(parameterMap.containsKey(OLENCIPConstants.PATRON_BARCODE) &&
260                                 parameterMap.containsKey(OLENCIPConstants.OPERATOR_ID) &&
261                                 parameterMap.containsKey(OLENCIPConstants.ITEM_BARCODE)){
262                             if(parameterMap.size()==4 || parameterMap.size()==5){
263 
264                                 Map checkoutParameters = new HashMap();
265                                 checkoutParameters.put("patronBarcode", parameterMap.get(OLENCIPConstants.PATRON_BARCODE)[0]);
266                                 checkoutParameters.put("operatorId", parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0]);
267                                 checkoutParameters.put("itemBarcode", parameterMap.get(OLENCIPConstants.ITEM_BARCODE)[0]);
268                                 checkoutParameters.put("responseFormatType", outputFormat);
269                                 responseString = new VuFindCheckoutItemServiceImplImpl().checkoutItem(checkoutParameters);
270 
271                                 if(responseString==null){
272                                     OLENCIPErrorResponse olencipErrorResponse=new OLENCIPErrorResponse();
273                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.PATRON_BARCODE,parameterMap.get(OLENCIPConstants.PATRON_BARCODE)[0]);
274                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.OPERATOR_ID,parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0]);
275                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.MESSAGE,OLENCIPConstants.INVALID_INPUT);
276                                     responseString=olencipErrorResponse.getErrorXml(service);
277                                 }
278                             }else{
279                                 responseString=getCirculationErrorMessage(service,OLENCIPConstants.INVALID_PARAMETERS,"501",OLENCIPConstants.CHECKOUTITEM,outputFormat);
280                             }
281                         }else{
282                             responseString=getCirculationErrorMessage(service,OLENCIPConstants.PARAMETER_MISSING,"502",OLENCIPConstants.CHECKOUTITEM,outputFormat);
283                         }
284 
285                         break;
286                     default:
287                         LOG.info("Unknown Service Name: "+service+"   Parameter is missing");
288                         responseString=getCirculationErrorMessage(service,OLENCIPConstants.UNKNOWN_SERVICE,"503",null,outputFormat);
289                         response.sendError(405,"Method Not Supported");
290                 }
291             }else{
292                 responseString=getCirculationErrorMessage(service,OLENCIPConstants.INVALID_FORMAT,"504",null,outputFormat);
293             }
294         }
295         else{
296             responseString=OLENCIPConstants.NULL_SERVICE;
297         }
298 
299         LOG.info("Response :"+responseString);
300         if(responseString==null){
301             responseString=OLENCIPConstants.INVALID_DATA;
302         }
303         if(responseString!=null){
304             responseString=responseString.replaceAll("errorMessage",OLENCIPConstants.MESSAGE);
305             responseString=responseString.replaceAll("<br/>","");
306         }
307         responseString=responseString.replaceAll("errorMessage",OLENCIPConstants.MESSAGE);
308         PrintWriter out=response.getWriter();
309         response.setContentType(OLENCIPConstants.XML_CONTENT_TYPE);
310         response.setCharacterEncoding(OLENCIPConstants.XML_CHAR_ENCODING);
311         if(responseString.contains(OLENCIPConstants.INVALID_FORMAT)){
312             response.setStatus(406);
313         } else if(responseString.contains(OLENCIPConstants.PARAMETER_MISSING)){
314             response.setStatus(422);
315         }else{
316             response.setStatus(200);
317         }
318         out.write(responseString);
319     }
320 
321     public void doGet(HttpServletRequest request, HttpServletResponse response)
322             throws ServletException, IOException {
323         OLECirculationService oleCirculationService=new OLECirculationServiceImpl();
324 
325         String responseString="";
326         boolean doProcess=true;
327         Map<String,String[]> parameterMap=new HashMap<String,String[]>();
328         String outputFormat=OLENCIPConstants.XML_FORMAT;
329         String service=null;
330 
331         parameterMap=request.getParameterMap();
332         if(parameterMap.containsKey(OLENCIPConstants.FORMAT)){
333             if(parameterMap.get(OLENCIPConstants.FORMAT)[0].equalsIgnoreCase(OLENCIPConstants.XML_FORMAT) || parameterMap.get(OLENCIPConstants.FORMAT)[0].equalsIgnoreCase(OLENCIPConstants.JSON_FORMAT)){
334                 outputFormat=parameterMap.get(OLENCIPConstants.FORMAT)[0];
335             }
336             else{
337                 doProcess=false;
338             }
339         }
340         if(parameterMap.containsKey(OLENCIPConstants.OLE_CIRCULATION_SERVICE)){
341             service=parameterMap.get(OLENCIPConstants.OLE_CIRCULATION_SERVICE)[0];
342         }
343 
344         if(service!=null){
345             if(doProcess){
346                 if(service.equals(OLENCIPConstants.LOOKUPUSER_SERVICE)){
347                     if(parameterMap.containsKey(OLENCIPConstants.PATRON_BARCODE) &&
348                             parameterMap.containsKey(OLENCIPConstants.OPERATOR_ID)){
349                         if(parameterMap.size()==3 || parameterMap.size()==4){
350 
351                             Map lookupUserParameters = new HashMap();
352                             lookupUserParameters.put("patronBarcode", parameterMap.get(OLENCIPConstants.PATRON_BARCODE)[0]);
353                             lookupUserParameters.put("operatorId", parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0]);
354                             lookupUserParameters.put("responseFormatType", outputFormat);
355                             responseString = new VuFindLookupUserServiceImpl().lookupUser(lookupUserParameters);
356 
357                             if(outputFormat.equalsIgnoreCase(OLENCIPConstants.JSON_FORMAT)){
358                                 responseString=new OLELookupUserConverter().generateLookupUserJson(responseString);
359                             }
360                             if(responseString==null){
361                                 OLENCIPErrorResponse olencipErrorResponse=new OLENCIPErrorResponse();
362                                 olencipErrorResponse.getErrorMap().put(OLENCIPConstants.PATRON_BARCODE,parameterMap.get(OLENCIPConstants.PATRON_BARCODE)[0]);
363                                 olencipErrorResponse.getErrorMap().put(OLENCIPConstants.OPERATOR_ID,parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0]);
364                                 responseString=olencipErrorResponse.getErrorXml(service);
365                             }
366 
367                         }else{
368                             responseString=getCirculationErrorMessage(service,OLENCIPConstants.INVALID_PARAMETERS,"501",OLENCIPConstants.LOOKUPUSER,outputFormat);
369                         }
370 
371 
372                     }else{
373                         responseString=getCirculationErrorMessage(service,OLENCIPConstants.PARAMETER_MISSING,"502",OLENCIPConstants.LOOKUPUSER,outputFormat);
374                     }
375                 }
376                 else if(service.equals(OLENCIPConstants.GETCHECKEDOUTITEM_SERVICE)){
377                     if(parameterMap.containsKey(OLENCIPConstants.PATRON_BARCODE) &&
378                             parameterMap.containsKey(OLENCIPConstants.OPERATOR_ID)){
379                         if(parameterMap.size()==3 || parameterMap.size()==4){
380                             try {
381                                 responseString=oleCirculationService.getCheckedOutItems(parameterMap.get(OLENCIPConstants.PATRON_BARCODE)[0],parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0]);
382                                 if(outputFormat.equalsIgnoreCase(OLENCIPConstants.JSON_FORMAT)){
383                                     responseString=new OLECheckoutItemsConverter().generateGetCheckedOutItemsJson(responseString);
384                                     System.out.println(responseString);
385                                 }
386                                 if(responseString==null){
387                                     OLENCIPErrorResponse olencipErrorResponse=new OLENCIPErrorResponse();
388                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.PATRON_BARCODE,parameterMap.get(OLENCIPConstants.PATRON_BARCODE)[0]);
389                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.OPERATOR_ID,parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0]);
390                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.MESSAGE,OLENCIPConstants.INVALID_INPUT);
391                                     responseString=olencipErrorResponse.getErrorXml(service);
392                                 }
393                             } catch (Exception e) {
394                                 LOG.error(e,e);
395                             }
396                         }else{
397                             responseString=getCirculationErrorMessage(service,OLENCIPConstants.INVALID_PARAMETERS,"501",OLENCIPConstants.GETCHECKOUTITEMS,outputFormat);
398                         }
399                     }else{
400                         responseString=getCirculationErrorMessage(service,OLENCIPConstants.PARAMETER_MISSING,"502",OLENCIPConstants.GETCHECKOUTITEMS,outputFormat);
401                     }
402                 }
403                 else if(service.equals(OLENCIPConstants.FINE_SERVICE)){
404                     if(parameterMap.containsKey(OLENCIPConstants.PATRON_BARCODE) &&
405                             parameterMap.containsKey(OLENCIPConstants.OPERATOR_ID)){
406                         if(parameterMap.size()==3 || parameterMap.size()==4){
407                             try {
408                                 responseString=oleCirculationService.getFine(parameterMap.get(OLENCIPConstants.PATRON_BARCODE)[0], parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0]);
409                                 if(outputFormat.equalsIgnoreCase(OLENCIPConstants.JSON_FORMAT)){
410                                     responseString=new OLEItemFineConverter().generateFineJson(responseString);
411                                     System.out.println(responseString);
412                                 }
413                                 if(responseString==null){
414                                     OLENCIPErrorResponse olencipErrorResponse=new OLENCIPErrorResponse();
415                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.PATRON_BARCODE,parameterMap.get(OLENCIPConstants.PATRON_BARCODE)[0]);
416                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.OPERATOR_ID,parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0]);
417                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.MESSAGE,OLENCIPConstants.INVALID_INPUT);
418                                     responseString=olencipErrorResponse.getErrorXml(service);
419                                 }
420                             } catch (Exception e) {
421                                 LOG.error(e,e);
422                             }
423                         }else{
424                             responseString=getCirculationErrorMessage(service,OLENCIPConstants.INVALID_PARAMETERS,"501",OLENCIPConstants.FINE,outputFormat);
425                         }
426                     }else{
427                         responseString=getCirculationErrorMessage(service,OLENCIPConstants.PARAMETER_MISSING,"502",OLENCIPConstants.FINE,outputFormat);
428                     }
429                 }
430                 else if(service.equals(OLENCIPConstants.HOLDS_SERVICE)){
431                     if(parameterMap.containsKey(OLENCIPConstants.PATRON_BARCODE) &&
432                             parameterMap.containsKey(OLENCIPConstants.OPERATOR_ID)){
433                         if(parameterMap.size()==3 || parameterMap.size()==4){
434                             try {
435                                 responseString=oleCirculationService.getHolds(parameterMap.get(OLENCIPConstants.PATRON_BARCODE)[0], parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0]);
436                                 if(outputFormat.equalsIgnoreCase(OLENCIPConstants.JSON_FORMAT)){
437                                     responseString=new OLEHoldsConverter().generateHoldsJson(responseString);
438                                     System.out.println(responseString);
439                                 }
440                                 if(responseString==null){
441                                     OLENCIPErrorResponse olencipErrorResponse=new OLENCIPErrorResponse();
442                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.PATRON_BARCODE,parameterMap.get(OLENCIPConstants.PATRON_BARCODE)[0]);
443                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.OPERATOR_ID,parameterMap.get(OLENCIPConstants.OPERATOR_ID)[0]);
444                                     olencipErrorResponse.getErrorMap().put(OLENCIPConstants.MESSAGE,OLENCIPConstants.INVALID_INPUT);
445                                     responseString=olencipErrorResponse.getErrorXml(service);
446                                 }
447                             } catch (Exception e) {
448                                 LOG.error(e,e);
449                             }
450                         }else{
451                             responseString=getCirculationErrorMessage(service,OLENCIPConstants.INVALID_PARAMETERS,"501",OLENCIPConstants.HOLDS,outputFormat);
452                         }
453                     }else{
454                         responseString=getCirculationErrorMessage(service,OLENCIPConstants.PARAMETER_MISSING,"502",OLENCIPConstants.HOLDS,outputFormat);
455                     }
456                 }
457                 else{
458                     LOG.info("Unknown Service Name: "+service+"   Parameter is missing");
459                     responseString=getCirculationErrorMessage(service,OLENCIPConstants.UNKNOWN_SERVICE,"503",null,outputFormat);
460                     response.sendError(405,"Method Not Supported");
461                 }
462             }
463             else{
464                 responseString=getCirculationErrorMessage(service,OLENCIPConstants.INVALID_FORMAT,"504",null,outputFormat);
465             }
466         }
467         else{
468             responseString=OLENCIPConstants.NULL_SERVICE;
469         }
470             if(responseString!=null){
471                 responseString=responseString.replaceAll("errorMessage",OLENCIPConstants.MESSAGE);
472                 responseString=responseString.replaceAll("<br/>","");
473             }
474             PrintWriter out=response.getWriter();
475         if(outputFormat.equalsIgnoreCase(OLENCIPConstants.XML_FORMAT)){
476              response.setContentType(OLENCIPConstants.XML_CONTENT_TYPE);
477               response.setCharacterEncoding(OLENCIPConstants.XML_CHAR_ENCODING);
478 
479         }
480         if(responseString.contains(OLENCIPConstants.INVALID_FORMAT)){
481             response.setStatus(406);
482         } else if(responseString.contains(OLENCIPConstants.PARAMETER_MISSING)){
483             response.setStatus(422);
484         }else{
485             response.setStatus(200);
486         }
487 
488 
489             out.write(responseString);
490 
491     }
492 
493 
494     private String getCirculationErrorMessage(String service,String message,String code,String requiredParameters,String outputFormat){
495         OLECirculationErrorMessage oleCirculationErrorMessage=new OLECirculationErrorMessage();
496         OLECirculationErrorMessageConverter oleCirculationErrorMessageConverter=new OLECirculationErrorMessageConverter();
497         oleCirculationErrorMessage.setMessage(message);
498         oleCirculationErrorMessage.setCode(code);
499         oleCirculationErrorMessage.setService(service);
500         oleCirculationErrorMessage.setRequiredParameters(requiredParameters);
501         String errorMessage="";
502         errorMessage=oleCirculationErrorMessageConverter.generateCirculationErrorXml(oleCirculationErrorMessage);
503         if(outputFormat.equalsIgnoreCase(OLENCIPConstants.JSON_FORMAT)){
504             errorMessage=oleCirculationErrorMessageConverter.generateLookupUserJson(errorMessage);
505         }
506         return  errorMessage;
507     }
508 }