1 package org.kuali.asr.handler;
2
3 import org.codehaus.jettison.json.JSONArray;
4 import org.codehaus.jettison.json.JSONException;
5 import org.codehaus.jettison.json.JSONObject;
6 import org.kuali.ole.ncip.bo.OLENCIPConstants;
7
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12
13
14
15
16 public class RenewItemRequestHandler extends RequestHandler {
17
18 public Map parseRequest(JSONObject jsonObject) throws JSONException {
19 String patronBarcode = getStringValueFromJsonObject(jsonObject, OLENCIPConstants.PATRON_BARCODE);
20 JSONArray itemBarcodes = (JSONArray) jsonObject.get(OLENCIPConstants.CIRC_ITEM_BARCODES);
21 List<String> items = new ArrayList<>();
22 for (int i = 0; i < itemBarcodes.length(); i++) {
23 Object object = itemBarcodes.get(i);
24 String itemBarcode = getStringValue(object);
25 items.add(itemBarcode);
26 }
27 String requestFormatType = getStringValueFromJsonObject(jsonObject, OLENCIPConstants.REQUEST_FORMAT_TYPE);
28 String responseFormatType = getStringValueFromJsonObject(jsonObject, OLENCIPConstants.RESPONSE_FORMAT_TYPE);
29
30 Map renewParameters = new HashMap();
31 renewParameters.put("patronBarcode", patronBarcode);
32 renewParameters.put("requestFormatType", requestFormatType);
33 renewParameters.put("responseFormatType", responseFormatType);
34 renewParameters.put("itemBarcodes", items);
35 return renewParameters;
36 }
37
38 private String getStringValue(Object object) {
39 String itemBarcode;
40 if(object instanceof Integer)
41 itemBarcode = ((Integer)object).toString();
42 else if(object instanceof Boolean)
43 itemBarcode = ((Boolean)object).toString();
44 else if(object instanceof Double)
45 itemBarcode = ((Double)object).toString();
46 else
47 itemBarcode = (String)object;
48 return itemBarcode;
49 }
50
51
52 public String getStringValueFromJsonObject(JSONObject jsonObject, String key) {
53 String returnValue = null;
54 try {
55 returnValue = jsonObject.getString(key);
56 } catch (JSONException e) {
57 e.printStackTrace();
58 }
59 return returnValue;
60 }
61
62 }