001package org.kuali.asr.handler;
002
003import org.codehaus.jettison.json.JSONArray;
004import org.codehaus.jettison.json.JSONException;
005import org.codehaus.jettison.json.JSONObject;
006import org.kuali.ole.ncip.bo.OLENCIPConstants;
007
008import java.util.ArrayList;
009import java.util.HashMap;
010import java.util.List;
011import java.util.Map;
012
013/**
014 * Created by pvsubrah on 6/26/15.
015 */
016public class RenewItemRequestHandler extends RequestHandler {
017
018    public Map parseRequest(JSONObject jsonObject) throws JSONException {
019        String patronBarcode = getStringValueFromJsonObject(jsonObject, OLENCIPConstants.PATRON_BARCODE);
020        JSONArray itemBarcodes = (JSONArray) jsonObject.get(OLENCIPConstants.CIRC_ITEM_BARCODES);
021        List<String> items = new ArrayList<>();
022        for (int i = 0; i < itemBarcodes.length(); i++) {
023            Object object = itemBarcodes.get(i);
024            String itemBarcode = getStringValue(object);
025            items.add(itemBarcode);
026        }
027        String requestFormatType = getStringValueFromJsonObject(jsonObject, OLENCIPConstants.REQUEST_FORMAT_TYPE);
028        String responseFormatType = getStringValueFromJsonObject(jsonObject, OLENCIPConstants.RESPONSE_FORMAT_TYPE);
029
030        Map renewParameters = new HashMap();
031        renewParameters.put("patronBarcode", patronBarcode);
032        renewParameters.put("requestFormatType", requestFormatType);
033        renewParameters.put("responseFormatType", responseFormatType);
034        renewParameters.put("itemBarcodes", items);
035        return renewParameters;
036    }
037
038    private String getStringValue(Object object) {
039        String itemBarcode;
040        if(object instanceof Integer)
041            itemBarcode = ((Integer)object).toString();
042        else if(object instanceof Boolean)
043            itemBarcode = ((Boolean)object).toString();
044        else if(object instanceof Double)
045            itemBarcode = ((Double)object).toString();
046        else
047            itemBarcode = (String)object;
048        return itemBarcode;
049    }
050
051
052    public String getStringValueFromJsonObject(JSONObject jsonObject, String key) {
053        String returnValue = null;
054        try {
055            returnValue = jsonObject.getString(key);
056        } catch (JSONException e) {
057            e.printStackTrace();
058        }
059        return returnValue;
060    }
061
062}