Example usage for com.google.gson JsonElement isJsonNull

List of usage examples for com.google.gson JsonElement isJsonNull

Introduction

In this page you can find the example usage for com.google.gson JsonElement isJsonNull.

Prototype

public boolean isJsonNull() 

Source Link

Document

provides check for verifying if this element represents a null value or not.

Usage

From source file:dao.QuotationRequestDAO.java

public HashMap<Integer, QuotationRequest> retrieveCompletedQuotationRequests(int staffId, String token)
        throws SQLException, UnsupportedEncodingException, IOException, ParseException {

    HashMap<Integer, QuotationRequest> allQuotationRequests = new HashMap<Integer, QuotationRequest>();
    String url = "http://119.81.43.85/erp/quotation_request/get_completed_quotation_request";

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    // add header
    post.setHeader("User-Agent", USER_AGENT);

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("staff_id", staffId + ""));
    urlParameters.add(new BasicNameValuePair("token", token));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);//w  w  w  . j a  v  a2s  . c om
    }
    String str = result.toString();
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(str);
    JsonObject jobj = element.getAsJsonObject();
    JsonArray arr = jobj.getAsJsonObject("payload").getAsJsonArray("quotation_request_info");
    int arrSize = arr.size();
    if (arrSize > 20) {
        arrSize = 20;
    }
    for (int i = 0; i < arrSize; i++) {
        //        for (int i = 0; i < arr.size(); i++) {
        JsonElement qrElement = arr.get(i);
        JsonObject qrObj = qrElement.getAsJsonObject();
        JsonElement attElement = qrObj.get("service_id");
        int id = 0;
        if (!attElement.isJsonNull()) {
            id = attElement.getAsInt();
        }
        attElement = qrObj.get("service_name");
        String name = "";
        if (!attElement.isJsonNull()) {
            name = attElement.getAsString();
        }
        attElement = qrObj.get("service_details");
        String details = "";
        if (!attElement.isJsonNull()) {
            details = attElement.getAsString();
        }
        attElement = qrObj.get("service_description");
        String description = "";
        if (!attElement.isJsonNull()) {
            description = attElement.getAsString();
        }
        attElement = qrObj.get("service_mileage");
        String mileage = "";
        if (!attElement.isJsonNull()) {
            mileage = attElement.getAsString();
        }

        attElement = qrObj.get("service_urgency");
        String urgency = "";
        if (!attElement.isJsonNull()) {
            urgency = attElement.getAsString();
        }

        attElement = qrObj.get("service_address");
        String address = "";
        if (!attElement.isJsonNull()) {
            address = attElement.getAsString();
        }

        attElement = qrObj.get("service_longitude");
        double longitude = 0.0;
        if (!attElement.isJsonNull()) {
            longitude = attElement.getAsDouble();
        }

        attElement = qrObj.get("service_latitude");
        double latitude = 0.0;
        if (!attElement.isJsonNull()) {
            latitude = attElement.getAsDouble();
        }

        attElement = qrObj.get("service_amenities");
        String amenities = "";
        if (!attElement.isJsonNull()) {
            amenities = attElement.getAsString();
        }

        attElement = qrObj.get("service_photos");
        String photos = "";
        if (!attElement.isJsonNull()) {
            photos = attElement.getAsString();
        }

        attElement = qrObj.get("offer_id");
        int offerId = 0;
        if (attElement != null && !attElement.isJsonNull()) {
            offerId = attElement.getAsInt();
        }

        attElement = qrObj.get("offer_status");
        int offerStatus = 0;
        if (attElement != null && !attElement.isJsonNull()) {
            offerStatus = attElement.getAsInt();
        }

        attElement = qrObj.get("quotation_min_price");
        double initialMinPrice = 0.0;
        if (attElement != null && !attElement.isJsonNull()) {
            initialMinPrice = attElement.getAsDouble();
        }

        attElement = qrObj.get("quotation_max_price");
        double initialMaxPrice = 0.0;
        if (attElement != null && !attElement.isJsonNull()) {
            initialMaxPrice = attElement.getAsDouble();
        }

        attElement = qrObj.get("diagnostic_price");
        double diagnosticPrice = 0.0;
        if (attElement != null && !attElement.isJsonNull()) {
            diagnosticPrice = attElement.getAsDouble();
        }

        attElement = qrObj.get("offer_final_price");
        double finalQuotationPrice = 0.0;
        if (attElement != null && !attElement.isJsonNull()) {
            finalQuotationPrice = attElement.getAsDouble();
        }

        attElement = qrObj.get("offer_est_complete_time");
        Timestamp estCompletionDateTime = null;
        String dateTimeString = "1990-01-01 00:00:00";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date parsedDate = dateFormat.parse(dateTimeString);
        estCompletionDateTime = new java.sql.Timestamp(parsedDate.getTime());
        if (attElement != null && !attElement.isJsonNull()) {
            dateTimeString = attElement.getAsString();
            dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            parsedDate = dateFormat.parse(dateTimeString);
            estCompletionDateTime = new java.sql.Timestamp(parsedDate.getTime());
        }

        attElement = qrObj.get("requested_datetime");
        Timestamp requestDateTime = null;
        dateTimeString = "1990-01-01 00:00:00";
        dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        parsedDate = dateFormat.parse(dateTimeString);
        requestDateTime = new java.sql.Timestamp(parsedDate.getTime());
        if (!attElement.isJsonNull()) {
            dateTimeString = attElement.getAsString();
            dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            parsedDate = dateFormat.parse(dateTimeString);
            requestDateTime = new java.sql.Timestamp(parsedDate.getTime());
        }

        attElement = qrObj.get("shop_id");
        int wsId = 0;
        if (attElement != null && !attElement.isJsonNull()) {
            wsId = attElement.getAsInt();
        }

        attElement = qrObj.get("vehicle_id");
        int vehicleId = 0;
        if (!attElement.isJsonNull()) {
            vehicleId = attElement.getAsInt();
        }

        attElement = qrObj.get("car_make");
        String carMake = "";
        if (!attElement.isJsonNull()) {
            carMake = attElement.getAsString();
        }

        attElement = qrObj.get("car_model");
        String carModel = "";
        if (!attElement.isJsonNull()) {
            carModel = attElement.getAsString();
        }

        attElement = qrObj.get("car_year_manufactured");
        int carYear = 0;
        if (!attElement.isJsonNull()) {
            carYear = attElement.getAsInt();
        }

        attElement = qrObj.get("car_plate_number");
        String carPlate = "";
        if (!attElement.isJsonNull()) {
            carPlate = attElement.getAsString();
        }

        attElement = qrObj.get("car_color");
        String carColor = "";
        if (!attElement.isJsonNull()) {
            carColor = attElement.getAsString();
        }

        attElement = qrObj.get("car_type_of_control_of_car");
        String carControl = "";
        if (!attElement.isJsonNull()) {
            carControl = attElement.getAsString();
        }

        attElement = qrObj.get("customer_id");
        int customerId = 0;
        if (!attElement.isJsonNull()) {
            customerId = attElement.getAsInt();
        }

        attElement = qrObj.get("customer_name");
        String customerName = "";
        if (!attElement.isJsonNull()) {
            customerName = attElement.getAsString();
        }

        attElement = qrObj.get("customer_email");
        String customerEmail = "";
        if (!attElement.isJsonNull()) {
            customerEmail = attElement.getAsString();
        }

        attElement = qrObj.get("customer_mobile_number");
        String customerHpNo = "";
        if (!attElement.isJsonNull()) {
            customerHpNo = attElement.getAsString();
        }

        attElement = qrObj.get("service_category");
        String category = "";
        if (!attElement.isJsonNull()) {
            category = attElement.getAsString();
        }

        attElement = qrObj.get("service_rejection_times");
        int noOfRejections = 0;
        if (!attElement.isJsonNull()) {
            noOfRejections = attElement.getAsInt();
        }
        Vehicle vehicle = new Vehicle(vehicleId, carMake, carModel, carYear, carPlate, customerId, carColor,
                carControl);
        Customer customer = new Customer(customerId, customerEmail, customerName, customerHpNo);
        Offer offer = new Offer(offerId, id, wsId, offerStatus, initialMinPrice, initialMaxPrice,
                diagnosticPrice, finalQuotationPrice, estCompletionDateTime);

        QuotationRequest qr = new QuotationRequest(id, name, details, description, vehicle, mileage, urgency,
                amenities, latitude, longitude, address, photos, requestDateTime, category, noOfRejections,
                wsId, customer, offer);
        allQuotationRequests.put(i, qr);
    }
    return allQuotationRequests;
}

From source file:dao.ScheduleDAO.java

public String addSchedule(int staffId, String token, int shopId, String startTime, String endTime)
        throws UnsupportedEncodingException, IOException {
    String url = "http://119.81.43.85/erp/schedule/add_schedule";

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    // add header
    post.setHeader("User-Agent", USER_AGENT);

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("staff_id", staffId + ""));
    urlParameters.add(new BasicNameValuePair("token", token));
    urlParameters.add(new BasicNameValuePair("shop_id", shopId + ""));
    urlParameters.add(new BasicNameValuePair("start_time", startTime));
    urlParameters.add(new BasicNameValuePair("end_time", endTime));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);//  w w w .  j  a v a 2 s.  c  o m
    }

    String str = result.toString();
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(str);
    JsonObject jobj = element.getAsJsonObject();
    JsonElement errMsgEle = jobj.get("error_message");
    String errMsg = "";
    if (errMsgEle != null && !errMsgEle.isJsonNull()) {
        errMsg = errMsgEle.getAsString();
    }
    System.out.println(errMsg);
    return errMsg;
}

From source file:dao.ScheduleDAO.java

public String deleteSchedule(int staffId, String token, int scheduleId)
        throws UnsupportedEncodingException, IOException {
    String url = "http://119.81.43.85/erp/schedule/delete_schedule";

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    // add header
    post.setHeader("User-Agent", USER_AGENT);

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("staff_id", staffId + ""));
    urlParameters.add(new BasicNameValuePair("token", token));
    urlParameters.add(new BasicNameValuePair("schedule_id", scheduleId + ""));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);/* w w w  .j  ava 2 s .co  m*/
    }

    String str = result.toString();
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(str);
    JsonObject jobj = element.getAsJsonObject();
    JsonElement errMsgEle = jobj.get("error_message");
    String errMsg = "";
    if (errMsgEle != null && !errMsgEle.isJsonNull()) {
        errMsg = errMsgEle.getAsString();
    }

    return errMsg;
}

From source file:dao.ScheduleDAO.java

public HashMap<Integer, Schedule> retrieveSchedule(int staffId, String token, int shopId, int month, int year)
        throws UnsupportedEncodingException, IOException, ParseException {
    HashMap<Integer, Schedule> monthlySchedule = new HashMap<Integer, Schedule>();
    String url = "http://119.81.43.85/erp/schedule/retrieve_schedule";

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    // add header
    post.setHeader("User-Agent", USER_AGENT);

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("staff_id", staffId + ""));
    urlParameters.add(new BasicNameValuePair("token", token));
    urlParameters.add(new BasicNameValuePair("shop_id", shopId + ""));
    urlParameters.add(new BasicNameValuePair("month", month + ""));
    urlParameters.add(new BasicNameValuePair("year", year + ""));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);// ww  w.ja va 2s.  co  m
    }
    String str = result.toString();
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(str);
    JsonObject jobj = element.getAsJsonObject();
    JsonArray arr = jobj.getAsJsonObject("payload").getAsJsonArray("schedule");

    for (int i = 0; i < arr.size(); i++) {
        JsonElement qrElement = arr.get(i);
        JsonObject obj = qrElement.getAsJsonObject();
        JsonElement attElement = obj.get("id");
        int id = 0;
        if (!attElement.isJsonNull()) {
            id = attElement.getAsInt();
        }
        attElement = obj.get("service_id");
        int serviceId = 0;
        if (!attElement.isJsonNull()) {
            serviceId = attElement.getAsInt();
        }

        attElement = obj.get("start_time");
        Timestamp startTime = null;
        String dateTimeString = "1990-01-01 00:00:00";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date parsedDate = dateFormat.parse(dateTimeString);
        startTime = new java.sql.Timestamp(parsedDate.getTime());
        if (attElement != null && !attElement.isJsonNull()) {
            dateTimeString = attElement.getAsString();
            dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            parsedDate = dateFormat.parse(dateTimeString);
            startTime = new java.sql.Timestamp(parsedDate.getTime());
        }

        attElement = obj.get("end_time");
        Timestamp endTime = null;
        dateTimeString = "1990-01-01 00:00:00";
        dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        parsedDate = dateFormat.parse(dateTimeString);
        endTime = new java.sql.Timestamp(parsedDate.getTime());
        if (!attElement.isJsonNull()) {
            dateTimeString = attElement.getAsString();
            dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            parsedDate = dateFormat.parse(dateTimeString);
            endTime = new java.sql.Timestamp(parsedDate.getTime());
        }

        Schedule schedule = new Schedule(id, serviceId, shopId, startTime, endTime);
        monthlySchedule.put(i, schedule);
    }
    return monthlySchedule;
}

From source file:DAO.ValidationDAO.ValidationOperationSource.java

public boolean jsonCompare(JsonElement element1, JsonElement element2) {

    //-------------------Caso o Json seja NULO------------------------------
    if (element1.isJsonNull()) {
        //System.out.println("Element is null");

        //---------------Caso o Json seja uma primitiva-------------------------
    } else if (element1.isJsonPrimitive()) {
        //System.out.println("Element is primitive");

        //----------------Caso o Json seja um Objeto----------------------------
    } else if (element1.isJsonObject()) {
        //System.out.println("Element is object");
        Iterator<Map.Entry<String, JsonElement>> CrunchifyIterator = element1.getAsJsonObject().entrySet()
                .iterator();/*from   w  w  w.j  a  v a2  s .  c om*/
        while (CrunchifyIterator.hasNext()) {
            Map.Entry<String, JsonElement> aux = CrunchifyIterator.next();
            //System.out.println("Field:" + aux.getKey());
            if (findElement(element2, aux.getKey()) == false) {
                //System.out.println("NOT FOUND Field:" + aux.getKey());
                return false;
            } else if (findElement(element2, aux.getKey()) == true) {
                //System.out.println("FOUND Field:" + aux.getKey());
                if (jsonCompare(aux.getValue(), element2) == false) {
                    return false;
                }
            }
        }
        //------------Caso o json seja um array---------------------------------
    } else if (element1.isJsonArray()) {
        //System.out.println("Element is array");
        for (JsonElement jsonElement : element1.getAsJsonArray()) {
            if (jsonCompare(jsonElement, element2) == false) {
                return false;
            }
        }
    }
    return true;
}

From source file:DAO.ValidationDAO.ValidationOperationSource.java

public boolean findElement(JsonElement element, String field) {

    //-------------------Caso o Json seja NULO------------------------------
    if (element.isJsonNull()) {
        //System.out.println("Element is null");

        //---------------Caso o Json seja uma primitiva-------------------------
    } else if (element.isJsonPrimitive()) {
        //System.out.println("Element is primitive");

        //----------------Caso o Json seja um Objeto----------------------------
    } else if (element.isJsonObject()) {
        //System.out.println("Element is object");

        if (element.getAsJsonObject().has(field)) {
            //System.out.println("Object found --> " + element.getAsJsonObject().get(field).toString());
            return true;
        } else {//from   w  ww.  j  a v  a  2  s . co m
            Iterator<Map.Entry<String, JsonElement>> CrunchifyIterator = element.getAsJsonObject().entrySet()
                    .iterator();
            while (CrunchifyIterator.hasNext()) {
                if (findElement(CrunchifyIterator.next().getValue(), field) == true) {
                    return true;
                }
            }
        }
        //------------Caso o json seja um array---------------------------------
    } else if (element.isJsonArray()) {
        //System.out.println("Element is array");
        for (JsonElement jsonElement : element.getAsJsonArray()) {
            if (findElement(jsonElement, field) == true) {
                return true;
            }
        }
    }
    return false;
}

From source file:DAO.ValidationDAO.ValidationOperationSource.java

public boolean findElementAndValue(JsonElement element, String field, String value) {

    //-------------------Caso o Json seja NULO------------------------------
    if (element.isJsonNull()) {
        //System.out.println("Element is null");

        //---------------Caso o Json seja uma primitiva-------------------------
    } else if (element.isJsonPrimitive()) {
        //System.out.println("Element is primitive");

        //----------------Caso o Json seja um Objeto----------------------------
    } else if (element.isJsonObject()) {
        //System.out.println("Element is object");

        if (element.getAsJsonObject().has(field)
                && element.getAsJsonObject().get(field).toString().contains(value)) {
            //System.out.println("Object found --> " + element.getAsJsonObject().get(field).toString());
            return true;
        } else {/*from w w  w. j a va 2 s  .  c  om*/
            Iterator<Map.Entry<String, JsonElement>> CrunchifyIterator = element.getAsJsonObject().entrySet()
                    .iterator();
            while (CrunchifyIterator.hasNext()) {
                if (findElementAndValue(CrunchifyIterator.next().getValue(), field, value) == true) {
                    return true;
                }
            }
        }
        //------------Caso o json seja um array---------------------------------
    } else if (element.isJsonArray()) {
        //System.out.println("Element is array");
        for (JsonElement jsonElement : element.getAsJsonArray()) {
            if (findElementAndValue(jsonElement, field, value) == true) {
                return true;
            }
        }
    }
    return false;
}

From source file:dao.WorkshopDAO.java

public Workshop retrieveWorkshop(String givenEmail, int staffId, String token)
        throws UnsupportedEncodingException, IOException {
    Workshop ws = null;//w  w  w .  j  a  v  a  2 s. com
    String url = "http://119.81.43.85/erp/workshop/get_shop_by_id_or_email";

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    // add header
    post.setHeader("User-Agent", USER_AGENT);

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("staff_id", staffId + ""));
    urlParameters.add(new BasicNameValuePair("token", token));
    urlParameters.add(new BasicNameValuePair("email", givenEmail));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    String str = result.toString();
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(str);
    JsonObject jobj = element.getAsJsonObject();

    JsonElement shopElement = jobj.get("payload");
    JsonObject shop = null;
    if (shopElement.isJsonNull()) {
        return ws;
    } else {
        shop = shopElement.getAsJsonObject().getAsJsonObject("shop");
        JsonElement attElement = shop.get("id");
        int id = 0;
        if (!attElement.isJsonNull()) {
            id = attElement.getAsInt();
        }
        attElement = shop.get("name");
        String name = "";
        if (!attElement.isJsonNull()) {
            name = attElement.getAsString();
        }
        attElement = shop.get("email");
        String email = "";
        if (!attElement.isJsonNull()) {
            email = attElement.getAsString();
        }
        attElement = shop.get("description");
        String description = "";
        if (!attElement.isJsonNull()) {
            description = attElement.getAsString();
        }
        attElement = shop.get("website");
        String website = "";
        if (!attElement.isJsonNull()) {
            website = attElement.getAsString();
        }
        attElement = shop.get("address");
        String address = "";
        if (!attElement.isJsonNull()) {
            address = attElement.getAsString();
        }
        attElement = shop.get("opening_hour_full");
        String openingHour = "";
        if (!attElement.isJsonNull()) {
            openingHour = attElement.getAsString();
        }
        attElement = shop.get("opening_hour_format");
        String openingHourFormat = "";
        if (!attElement.isJsonNull()) {
            openingHourFormat = attElement.getAsString();
        }
        attElement = shop.get("latitude");
        double latitude = 0.0;
        if (!attElement.isJsonNull()) {
            latitude = attElement.getAsDouble();
        }
        attElement = shop.get("longitude");
        double longitude = 0.0;
        if (!attElement.isJsonNull()) {
            longitude = attElement.getAsDouble();
        }
        attElement = shop.get("contact");
        String contact = "";
        if (!attElement.isJsonNull()) {
            contact = attElement.getAsString();
        }
        attElement = shop.get("contact2");
        String contact2 = "";
        if (!attElement.isJsonNull()) {
            contact2 = attElement.getAsString();
        }
        attElement = shop.get("location");
        String location = "";
        if (!attElement.isJsonNull()) {
            location = attElement.getAsString();
        }
        attElement = shop.get("specialize");
        String specialize = "";
        if (!attElement.isJsonNull()) {
            specialize = attElement.getAsString();
        }
        attElement = shop.get("category");
        String category = "";
        if (!attElement.isJsonNull()) {
            category = attElement.getAsString();
        }
        attElement = shop.get("brand_carried");
        String brandsCarried = "";
        if (!attElement.isJsonNull()) {
            brandsCarried = attElement.getAsString();
        }
        attElement = shop.get("remark");
        String remark = "";
        if (!attElement.isJsonNull()) {
            remark = attElement.getAsString();
        }
        attElement = shop.get("status");
        int status = 0;
        if (!attElement.isJsonNull()) {
            status = attElement.getAsInt();
        }
        ws = new Workshop(id, email, name, description, website, address, openingHour, openingHourFormat,
                latitude, longitude, contact, contact2, location, specialize, category, brandsCarried, remark,
                status);
    }
    return ws;
}

From source file:dao.WorkshopDAO.java

public Workshop retrieveWorkshop(int givenID, int staffId, String token) throws IOException {
    Workshop ws = null;//www.j av a  2s .  c o  m
    String url = "http://119.81.43.85/erp/workshop/get_shop_by_id_or_email";

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    // add header
    post.setHeader("User-Agent", USER_AGENT);

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("staff_id", staffId + ""));
    urlParameters.add(new BasicNameValuePair("token", token));
    urlParameters.add(new BasicNameValuePair("id", givenID + ""));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    String str = result.toString();
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(str);
    JsonObject jobj = element.getAsJsonObject();

    JsonElement shopElement = jobj.get("payload");
    JsonObject shop = null;
    if (shopElement.isJsonNull()) {
        return ws;
    } else {
        shop = shopElement.getAsJsonObject().getAsJsonObject("shop");
        JsonElement attElement = shop.get("id");
        int id = 0;
        if (!attElement.isJsonNull()) {
            id = attElement.getAsInt();
        }
        attElement = shop.get("name");
        String name = "";
        if (!attElement.isJsonNull()) {
            name = attElement.getAsString();
        }
        attElement = shop.get("email");
        String email = "";
        if (!attElement.isJsonNull()) {
            email = attElement.getAsString();
        }
        attElement = shop.get("description");
        String description = "";
        if (!attElement.isJsonNull()) {
            description = attElement.getAsString();
        }
        attElement = shop.get("website");
        String website = "";
        if (!attElement.isJsonNull()) {
            website = attElement.getAsString();
        }
        attElement = shop.get("address");
        String address = "";
        if (!attElement.isJsonNull()) {
            address = attElement.getAsString();
        }
        attElement = shop.get("opening_hour_full");
        String openingHour = "";
        if (!attElement.isJsonNull()) {
            openingHour = attElement.getAsString();
        }
        attElement = shop.get("opening_hour_format");
        String openingHourFormat = "";
        if (!attElement.isJsonNull()) {
            openingHourFormat = attElement.getAsString();
        }
        attElement = shop.get("latitude");
        double latitude = 0.0;
        if (!attElement.isJsonNull()) {
            latitude = attElement.getAsDouble();
        }
        attElement = shop.get("longitude");
        double longitude = 0.0;
        if (!attElement.isJsonNull()) {
            longitude = attElement.getAsDouble();
        }
        attElement = shop.get("contact");
        String contact = "";
        if (!attElement.isJsonNull()) {
            contact = attElement.getAsString();
        }
        attElement = shop.get("contact2");
        String contact2 = "";
        if (!attElement.isJsonNull()) {
            contact2 = attElement.getAsString();
        }
        attElement = shop.get("location");
        String location = "";
        if (!attElement.isJsonNull()) {
            location = attElement.getAsString();
        }
        attElement = shop.get("specialize");
        String specialize = "";
        if (!attElement.isJsonNull()) {
            specialize = attElement.getAsString();
        }
        attElement = shop.get("category");
        String category = "";
        if (!attElement.isJsonNull()) {
            category = attElement.getAsString();
        }
        attElement = shop.get("brand_carried");
        String brandsCarried = "";
        if (!attElement.isJsonNull()) {
            brandsCarried = attElement.getAsString();
        }
        attElement = shop.get("remark");
        String remark = "";
        if (!attElement.isJsonNull()) {
            remark = attElement.getAsString();
        }
        attElement = shop.get("status");
        int status = 0;
        if (!attElement.isJsonNull()) {
            status = attElement.getAsInt();
        }
        ws = new Workshop(id, email, name, description, website, address, openingHour, openingHourFormat,
                latitude, longitude, contact, contact2, location, specialize, category, brandsCarried, remark,
                status);
    }
    return ws;
}

From source file:dao.WorkshopDAO.java

public ArrayList<String> addWorkshop(String email, String name, String description, String website,
        String address, String openingHour, String openingHourFormat, double latitude, double longitude,
        String contact, String contact2, String location, String specialize, String category,
        String brandsCarried, String remark, int staffId, String token)
        throws UnsupportedEncodingException, IOException {
    String url = "http://119.81.43.85/erp/workshop/add_shop";

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    // add header
    post.setHeader("User-Agent", USER_AGENT);

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("staff_id", staffId + ""));
    urlParameters.add(new BasicNameValuePair("token", token));
    urlParameters.add(new BasicNameValuePair("name", name));
    urlParameters.add(new BasicNameValuePair("description", description));
    urlParameters.add(new BasicNameValuePair("website", website));
    urlParameters.add(new BasicNameValuePair("address", address));
    urlParameters.add(new BasicNameValuePair("opening_hour_full", openingHour));
    urlParameters.add(new BasicNameValuePair("opening_hour_format", openingHourFormat));
    urlParameters.add(new BasicNameValuePair("latitude", latitude + ""));
    urlParameters.add(new BasicNameValuePair("longitude", longitude + ""));
    urlParameters.add(new BasicNameValuePair("contact", contact));
    urlParameters.add(new BasicNameValuePair("contact2", contact2));
    urlParameters.add(new BasicNameValuePair("location", location));
    urlParameters.add(new BasicNameValuePair("specialize", specialize));
    urlParameters.add(new BasicNameValuePair("category", category));
    urlParameters.add(new BasicNameValuePair("brand_carried", brandsCarried));
    urlParameters.add(new BasicNameValuePair("remark", remark));
    urlParameters.add(new BasicNameValuePair("email", email));
    urlParameters.add(new BasicNameValuePair("status", 1 + ""));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);//ww  w . j a  v  a 2 s . co  m
    }
    String str = result.toString();
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(str);
    JsonObject jobj = element.getAsJsonObject();
    String errorMessage = null;
    ArrayList<String> errors = new ArrayList<String>();
    JsonElement isSuccess = jobj.get("is_success");
    if (isSuccess.getAsString().equals("false")) {
        errorMessage = jobj.get("error_message").getAsString();
        errors.add(errorMessage);
        JsonElement fields = jobj.get("payload");
        if (!fields.isJsonNull()) {
            JsonArray arr = fields.getAsJsonObject().get("error_field").getAsJsonArray();
            for (int i = 0; i < arr.size(); i++) {
                String f = arr.get(i).getAsString();
                errors.add(f);
            }
        }
    }

    return errors;

}