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:com.wallellen.wechat.mp.util.json.WxMpMaterialNewsArticleGsonAdapter.java

License:Open Source License

public WxMpMaterialNews.WxMpMaterialNewsArticle deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    JsonObject articleInfo = jsonElement.getAsJsonObject();
    WxMpMaterialNews.WxMpMaterialNewsArticle article = new WxMpMaterialNews.WxMpMaterialNewsArticle();

    JsonElement title = articleInfo.get("title");
    if (title != null && !title.isJsonNull()) {
        article.setTitle(GsonHelper.getAsString(title));
    }//from   w w  w  .  ja v  a2  s  .c  o  m
    JsonElement content = articleInfo.get("content");
    if (content != null && !content.isJsonNull()) {
        article.setContent(GsonHelper.getAsString(content));
    }
    JsonElement contentSourceUrl = articleInfo.get("content_source_url");
    if (contentSourceUrl != null && !contentSourceUrl.isJsonNull()) {
        article.setContentSourceUrl(GsonHelper.getAsString(contentSourceUrl));
    }
    JsonElement author = articleInfo.get("author");
    if (author != null && !author.isJsonNull()) {
        article.setAuthor(GsonHelper.getAsString(author));
    }
    JsonElement digest = articleInfo.get("digest");
    if (digest != null && !digest.isJsonNull()) {
        article.setDigest(GsonHelper.getAsString(digest));
    }
    JsonElement thumbMediaId = articleInfo.get("thumb_media_id");
    if (thumbMediaId != null && !thumbMediaId.isJsonNull()) {
        article.setThumbMediaId(GsonHelper.getAsString(thumbMediaId));
    }
    JsonElement showCoverPic = articleInfo.get("show_cover_pic");
    if (showCoverPic != null && !showCoverPic.isJsonNull()) {
        article.setShowCoverPic(GsonHelper.getAsBoolean(showCoverPic));
    }
    JsonElement url = articleInfo.get("url");
    if (url != null && !url.isJsonNull()) {
        article.setUrl(GsonHelper.getAsString(url));
    }
    return article;
}

From source file:cosmos.records.JsonRecords.java

License:Apache License

public static List<MapRecord> parseAsMap(String json, DocIdGenerator generator) throws JsonSyntaxException {
    Preconditions.checkNotNull(json);//from   w  ww . java2  s  .  co  m
    Preconditions.checkNotNull(generator);

    JsonParser parser = new JsonParser();
    JsonElement topLevelElement = parser.parse(json);

    if (topLevelElement.isJsonNull()) {
        return Collections.emptyList();
    } else if (!topLevelElement.isJsonArray()) {
        throw new JsonSyntaxException("Expected a list of dictionaries");
    }

    final LinkedList<MapRecord> records = Lists.newLinkedList();

    // Walk the top level list
    JsonArray list = topLevelElement.getAsJsonArray();
    for (JsonElement e : list) {
        // Make sure that it's a Json Object
        if (!e.isJsonObject()) {
            throw new JsonSyntaxException("Expected a Json Object");
        }

        // Parse each "Object" (map)
        JsonObject map = e.getAsJsonObject();

        records.add(asMapRecord(map, generator));
    }

    return records;
}

From source file:cosmos.records.JsonRecords.java

License:Apache License

public static List<MultimapRecord> parseAsMultimap(String json, DocIdGenerator generator)
        throws JsonSyntaxException {
    Preconditions.checkNotNull(json);/*from ww w.j  a  v a 2s  . c o m*/
    Preconditions.checkNotNull(generator);

    JsonParser parser = new JsonParser();
    JsonElement topLevelElement = parser.parse(json);

    if (topLevelElement.isJsonNull()) {
        return Collections.emptyList();
    } else if (!topLevelElement.isJsonArray()) {
        throw new JsonSyntaxException("Expected a list of dictionaries");
    }

    final LinkedList<MultimapRecord> records = Lists.newLinkedList();

    // Walk the top level list
    JsonArray list = topLevelElement.getAsJsonArray();
    for (JsonElement e : list) {
        // Make sure that it's a Json Object
        if (!e.isJsonObject()) {
            throw new JsonSyntaxException("Expected a Json Object");
        }

        // Parse each "Object" (map)
        JsonObject map = e.getAsJsonObject();

        records.add(asMultimapRecord(map, generator));
    }

    return records;
}

From source file:cosmos.records.JsonRecords.java

License:Apache License

/**
 * Parses a {@link JsonObject{ into a MapRecord. A duplicate key in the JsonObject will
 * overwrite the previous key./*from   www . j a v a2s  .  c om*/
 * @param map The JsonObject being parsed
 * @param generator Construct to generate a docId for this {@link MapRecord}
 * @return A MapRecord built from the {@link JsonObject}
 */
protected static MapRecord asMapRecord(JsonObject map, DocIdGenerator generator) {
    Map<Column, RecordValue<?>> data = Maps.newHashMap();
    for (Entry<String, JsonElement> entry : map.entrySet()) {
        final Column key = Column.create(entry.getKey());
        final JsonElement value = entry.getValue();

        if (value.isJsonNull()) {
            data.put(key, null);
        } else if (value.isJsonPrimitive()) {
            JsonPrimitive primitive = (JsonPrimitive) value;

            // Numbers
            if (primitive.isNumber()) {
                NumberRecordValue<?> v;

                double d = primitive.getAsDouble();
                if ((int) d == d) {
                    v = new IntegerRecordValue((int) d, Defaults.EMPTY_VIS);
                } else if ((long) d == d) {
                    v = new LongRecordValue((long) d, Defaults.EMPTY_VIS);
                } else {
                    v = new DoubleRecordValue(d, Defaults.EMPTY_VIS);
                }

                data.put(key, v);

            } else if (primitive.isString()) {
                // String
                data.put(key, new StringRecordValue(primitive.getAsString(), Defaults.EMPTY_VIS));

            } else if (primitive.isBoolean()) {
                // Boolean
                data.put(key, new BooleanRecordValue(primitive.getAsBoolean(), Defaults.EMPTY_VIS));

            } else if (primitive.isJsonNull()) {
                // Is this redundant?
                data.put(key, null);
            } else {
                throw new RuntimeException("Unhandled primitive: " + primitive);
            }
        } else {
            throw new RuntimeException("Expected a String, Number or Boolean");
        }
    }

    return new MapRecord(data, generator.getDocId(data), Defaults.EMPTY_VIS);
}

From source file:cosmos.records.JsonRecords.java

License:Apache License

/**
 * Builds a {@link MultimapRecord} from the provided {@link JsonObject} using a docid
 * from the {@link DocIdGenerator}. This method will support lists of values for a key
 * in the {@link JsonObject} but will fail on values which are {@link JsonObject}s and 
 * values which have nested {@link JsonArray}s. 
 * @param map The {@link JsonObject} to build this {@link MultimapRecord} from
 * @param generator {@link DocIdGenerator} to construct a docid for this {@link MultimapRecord}
 * @return A {@link MultimapRecord} built from the provided arguments.
 */// w w  w. j av a 2s.com
protected static MultimapRecord asMultimapRecord(JsonObject map, DocIdGenerator generator) {
    Multimap<Column, RecordValue<?>> data = HashMultimap.create();
    for (Entry<String, JsonElement> entry : map.entrySet()) {
        final Column key = Column.create(entry.getKey());
        final JsonElement value = entry.getValue();

        if (value.isJsonNull()) {
            data.put(key, null);
        } else if (value.isJsonPrimitive()) {
            JsonPrimitive primitive = (JsonPrimitive) value;

            // Numbers
            if (primitive.isNumber()) {
                NumberRecordValue<?> v;

                double d = primitive.getAsDouble();
                if ((int) d == d) {
                    v = new IntegerRecordValue((int) d, Defaults.EMPTY_VIS);
                } else if ((long) d == d) {
                    v = new LongRecordValue((long) d, Defaults.EMPTY_VIS);
                } else {
                    v = new DoubleRecordValue(d, Defaults.EMPTY_VIS);
                }

                data.put(key, v);

            } else if (primitive.isString()) {
                // String
                data.put(key, new StringRecordValue(primitive.getAsString(), Defaults.EMPTY_VIS));

            } else if (primitive.isBoolean()) {
                // Boolean
                data.put(key, new BooleanRecordValue(primitive.getAsBoolean(), Defaults.EMPTY_VIS));

            } else if (primitive.isJsonNull()) {
                // Is this redundant?
                data.put(key, null);
            } else {
                throw new RuntimeException("Unhandled primitive: " + primitive);
            }
        } else if (value.isJsonArray()) {

            // Multimaps should handle the multiple values, not fail
            JsonArray values = value.getAsJsonArray();
            for (JsonElement element : values) {
                if (element.isJsonNull()) {
                    data.put(key, null);
                } else if (element.isJsonPrimitive()) {

                    JsonPrimitive primitive = (JsonPrimitive) element;

                    // Numbers
                    if (primitive.isNumber()) {
                        NumberRecordValue<?> v;

                        double d = primitive.getAsDouble();
                        if ((int) d == d) {
                            v = new IntegerRecordValue((int) d, Defaults.EMPTY_VIS);
                        } else if ((long) d == d) {
                            v = new LongRecordValue((long) d, Defaults.EMPTY_VIS);
                        } else {
                            v = new DoubleRecordValue(d, Defaults.EMPTY_VIS);
                        }

                        data.put(key, v);

                    } else if (primitive.isString()) {
                        // String
                        data.put(key, new StringRecordValue(primitive.getAsString(), Defaults.EMPTY_VIS));

                    } else if (primitive.isBoolean()) {
                        // Boolean
                        data.put(key, new BooleanRecordValue(primitive.getAsBoolean(), Defaults.EMPTY_VIS));

                    } else if (primitive.isJsonNull()) {
                        // Is this redundant?
                        data.put(key, null);
                    } else {
                        throw new RuntimeException("Unhandled Json primitive: " + primitive);
                    }
                } else {
                    throw new RuntimeException("Expected a Json primitive");
                }
            }

        } else {
            throw new RuntimeException("Expected a String, Number or Boolean");
        }
    }

    return new MultimapRecord(data, generator.getDocId(data), Defaults.EMPTY_VIS);

}

From source file:dao.ChatDAO.java

public HashMap<Integer, Chat> getChatList(int staffId, String token)
        throws UnsupportedEncodingException, IOException, ParseException {
    HashMap<Integer, Chat> chatList = new HashMap<Integer, Chat>();
    //Add URL here
    String url = "http://119.81.43.85/chat/retrive_chat_list_history";

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

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

    //Add parameters here
    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);/*from w ww.jav  a2  s  . c  om*/
    }
    String str = result.toString();
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(str);
    JsonObject jobj = element.getAsJsonObject();
    JsonObject jobj2 = jobj.getAsJsonObject("payload");
    JsonObject jobj3 = jobj2.getAsJsonObject("chat_history_list_result");
    JsonArray arr = jobj3.getAsJsonArray("generalList");
    int arrSize = arr.size();
    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("id");
        int id = 0;
        if (!attElement.isJsonNull()) {
            id = attElement.getAsInt();
        }
        attElement = qrObj.get("service_id");
        int service_id = 0;
        if (!attElement.isJsonNull()) {
            service_id = attElement.getAsInt();
        }
        attElement = qrObj.get("user_id");
        int user_id = 0;
        if (!attElement.isJsonNull()) {
            user_id = attElement.getAsInt();
        }
        attElement = qrObj.get("shop_id");
        int shop_id = 0;
        if (!attElement.isJsonNull()) {
            shop_id = attElement.getAsInt();
        }
        attElement = qrObj.get("last_message");
        String last_message = "";
        if (!attElement.isJsonNull()) {
            last_message = attElement.getAsString();
        }
        attElement = qrObj.get("modified");
        Timestamp modified = null;
        String dateTimeString = "1990-01-01 00:00:00";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date parsedDate = dateFormat.parse(dateTimeString);
        modified = 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);
            modified = new java.sql.Timestamp(parsedDate.getTime());
        }
        attElement = qrObj.get("created");
        Timestamp created = null;
        dateTimeString = "1990-01-01 00:00:00";
        dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        parsedDate = dateFormat.parse(dateTimeString);
        created = 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);
            created = new java.sql.Timestamp(parsedDate.getTime());
        }
        attElement = qrObj.get("deleted_at");
        Timestamp deleted_at = null;
        dateTimeString = "1990-01-01 00:00:00";
        dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        parsedDate = dateFormat.parse(dateTimeString);
        deleted_at = 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);
            deleted_at = new java.sql.Timestamp(parsedDate.getTime());
        }
        attElement = qrObj.get("user_name");
        String user_name = "";
        if (!attElement.isJsonNull()) {
            user_name = attElement.getAsString();
        }

        Chat chat = new Chat(id, service_id, user_id, shop_id, last_message, modified, created, deleted_at,
                user_name);
        chatList.put(i, chat);
    }
    return chatList;
}

From source file:dao.QuotationRequestDAO.java

public HashMap<Integer, QuotationRequest> retrieveQuotationRequest(int staffId, String token, int givenID)
        throws SQLException, ParseException, UnsupportedEncodingException, IOException {
    HashMap<Integer, QuotationRequest> allQuotationRequests = new HashMap<Integer, QuotationRequest>();
    String url = "http://119.81.43.85/erp/quotation_request/get_quotation_request_info_by_service_id";

    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("service_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);/*from www  .ja v  a  2s  .c  o m*/
    }
    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.QuotationRequestDAO.java

public HashMap<Integer, QuotationRequest> retrieveAllQuotationRequests(int staffId, String token, int givenWsId,
        int givenStatus, String orderBy, String order)
        throws SQLException, UnsupportedEncodingException, IOException, ParseException {

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

    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("workshop_id", givenWsId + ""));
    urlParameters.add(new BasicNameValuePair("status", givenStatus + ""));
    urlParameters.add(new BasicNameValuePair("order_by", orderBy));
    urlParameters.add(new BasicNameValuePair("asc_of_desc", order));

    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);//from  w  w  w  .ja v  a 2 s.c  o m
    }
    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.QuotationRequestDAO.java

public Offer retrieveOffer(int staffId, String token, int offerId)
        throws SQLException, ParseException, UnsupportedEncodingException, IOException {
    Offer offer = null;/*from   w  w w .ja v  a  2  s.c o  m*/
    String url = "http://119.81.43.85/quotation_request/retrieve_min_max_quotation_or_diagnostic_price";

    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("offer_id", offerId + ""));

    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 oElement = jobj.get("payload");
    JsonObject oObj = null;
    if (oElement.isJsonNull()) {
        return offer;
    }

    oObj = oElement.getAsJsonObject();
    JsonElement attElement = oObj.get("service_id");
    int serviceId = 0;
    if (!attElement.isJsonNull()) {
        serviceId = attElement.getAsInt();
    }

    attElement = oObj.get("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 = oObj.get("final_price");
    double finalPrice = 0.0;
    if (!attElement.isJsonNull()) {
        finalPrice = attElement.getAsDouble();
    }

    attElement = oObj.get("offer_status");
    int status = 0;
    if (!attElement.isJsonNull()) {
        status = attElement.getAsInt();
    }

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

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

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

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

    offer = new Offer(offerId, serviceId, wsId, status, initialMinPrice, initialMaxPrice, diagnosticPrice,
            finalPrice, estCompletionDateTime);
    return offer;

}

From source file:dao.QuotationRequestDAO.java

public HashMap<Integer, QuotationRequest> retrieveQuotationRequestsWithoutOffer(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_request_quotation_without_offer";

    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);//from 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();
    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;
}