edu.usf.cutr.open311client.utils.Open311Parser.java Source code

Java tutorial

Introduction

Here is the source code for edu.usf.cutr.open311client.utils.Open311Parser.java

Source

/*
* Copyright (C) 2014 University of South Florida (sjbarbeau@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package edu.usf.cutr.open311client.utils;

import edu.usf.cutr.open311client.constants.Open311Constants;
import edu.usf.cutr.open311client.constants.Open311Type;
import edu.usf.cutr.open311client.models.Service;
import edu.usf.cutr.open311client.models.ServiceDescription;
import edu.usf.cutr.open311client.models.ServiceInfo;
import edu.usf.cutr.open311client.models.ServiceInfoResponse;
import edu.usf.cutr.open311client.models.ServiceListResponse;
import edu.usf.cutr.open311client.models.ServiceRequestResponse;
import edu.usf.cutr.open311client.settings.Logger;
import edu.usf.cutr.open311client.settings.Settings;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

/**
 * Helper class for parsing responses
 *
 * @author Cagri Cetin
 */
public class Open311Parser {

    private static Logger logger = Logger.getLogger();

    /**
     * Parses json string result for service list
     *
     * @param json string result
     * @return ServiceListResponse object
     */
    public static ServiceListResponse parseServices(String json) {

        if (json == null) {
            return new ServiceListResponse();
        }

        ObjectMapper om = createObjectMapper();

        ArrayList<Service> services;
        ServiceListResponse slr = new ServiceListResponse();
        try {
            services = om.readValue(json, new TypeReference<ArrayList<Service>>() {
            });
            slr.setServiceList(services);
            slr.setResultCode(Open311Constants.RESULT_OK);
        } catch (IOException e) {
            logger.error(e);
        }
        return slr;
    }

    /**
     * Parses json string result for service description
     *
     * @param json string result
     * @return ServiceDescription object
     */
    public static ServiceDescription parseServiceDescription(String json) {

        if (json == null) {
            return new ServiceDescription();
        }

        ObjectMapper om = createObjectMapper();
        ServiceDescription serviceDescription = new ServiceDescription();
        try {
            serviceDescription = om.readValue(json, ServiceDescription.class);
            serviceDescription.setResultCode(Open311Constants.RESULT_OK);
        } catch (IOException e) {
            logger.error(e);
        }
        return serviceDescription;
    }

    /**
     * Parses json string result for service request responses
     *
     * @param json
     * @param open311Type
     * @return
     */
    public static ServiceRequestResponse parseRequestResponse(String json, Open311Type open311Type) {
        ServiceRequestResponse response = null;

        if (json == null) {
            return new ServiceRequestResponse(open311Type);
        }

        try {
            JSONArray jsonArray = new JSONArray(json);

            if (jsonArray.getJSONObject(0) != null) {
                response = new ServiceRequestResponse(jsonArray.getJSONObject(0), open311Type);
            } else {
                response = new ServiceRequestResponse(open311Type);
                response.setResultCode(Open311Constants.RESULT_FAIL);
            }
        } catch (JSONException e) {
            logger.error(e);
            try {
                JSONObject jsonObject = new JSONObject(json);
                response = new ServiceRequestResponse(jsonObject, open311Type);

            } catch (JSONException e1) {
                logger.error(e);
                response = new ServiceRequestResponse(open311Type);
                response.setResultCode(Open311Constants.RESULT_FAIL);
                response.setResultDescription(e.getMessage());
            }
        }
        return response;
    }

    /**
     * Parses json string to Service Info Response
     *
     * @param json
     * @return
     */
    public static ServiceInfoResponse parseServiceInfos(String json) {

        if (json == null) {
            return new ServiceInfoResponse();
        }

        ObjectMapper om = createObjectMapper();
        ArrayList<ServiceInfo> serviceInfos;
        ServiceInfoResponse sir = new ServiceInfoResponse();
        try {
            serviceInfos = om.readValue(json, new TypeReference<ArrayList<ServiceInfo>>() {
            });
            sir.setServiceInfoList(serviceInfos);
            sir.setResultCode(Open311Constants.RESULT_OK);
        } catch (IOException e) {
            logger.error(e);
        }
        return sir;
    }

    private static ObjectMapper createObjectMapper() {
        ObjectMapper om = new ObjectMapper();
        if (!Settings.getSettings().isDebugMode()) {
            om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        }
        return om;
    }
}