Example usage for com.google.gson JsonObject getAsJsonObject

List of usage examples for com.google.gson JsonObject getAsJsonObject

Introduction

In this page you can find the example usage for com.google.gson JsonObject getAsJsonObject.

Prototype

public JsonObject getAsJsonObject(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonObject.

Usage

From source file:dao.QuotationRequestDAO.java

public HashMap<Integer, Integer> retrieveStatusSize(int staffId, String token, int givenWsId, int givenStatus,
        String givenCarModel, String orderBy, String order)
        throws SQLException, UnsupportedEncodingException, IOException, ParseException {
    HashMap<Integer, Integer> statusSize = new HashMap<Integer, Integer>();
    int statusArr[] = { 1, 3, 5, 9, 6 };
    for (int i = 0; i < 5; i++) {
        int sta = statusArr[i];
        String url = "http://119.81.43.85/erp/quotation_request/get_quotation_request_info";

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        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", sta + ""));
        urlParameters.add(new BasicNameValuePair("order_by", orderBy));
        urlParameters.add(new BasicNameValuePair("asc_of_desc", order));
        urlParameters.add(new BasicNameValuePair("car_model", givenCarModel));
        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   ww  w .  j ava  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();
        statusSize.put(i, arrSize);//To be changed to the status
    }
    return statusSize;
}

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);// w ww.  ja v  a 2  s  .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.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);//  ww w . ja v a2 s .com
    }
    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> 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 ww  .  ja v a2  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;
}

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);//from   www.  j  a v a2s  .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.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);//from  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();
    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.WorkshopDAO.java

public ArrayList<Workshop> retrieveAllWorkshops(int staffId, String token)
        throws UnsupportedEncodingException, IOException {
    ArrayList<Workshop> allWorkshops = new ArrayList<Workshop>();
    String url = "http://119.81.43.85/erp/workshop/get_all_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));

    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  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("shops");

    for (int i = 0; i < arr.size(); i++) {
        JsonElement workshop = arr.get(i);
        JsonObject shop = workshop.getAsJsonObject();
        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();
        }
        //int status = attElement.getAsInt();
        Workshop ws = new Workshop(id, email, name, description, website, address, openingHour,
                openingHourFormat, latitude, longitude, contact, contact2, location, specialize, category,
                brandsCarried, remark, status);

        allWorkshops.add(ws);
    }
    return allWorkshops;
}

From source file:de.baydev.hueemulator.util.philips.ResponseDeserializer.java

License:Open Source License

@Override
public HueResponse deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context)
        throws JsonParseException {

    HueResponse response = new HueResponse();
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    if (jsonObject.get("success").isJsonObject()) {
        for (Map.Entry<String, JsonElement> entry : jsonObject.getAsJsonObject("success").entrySet()) {
            response.setSuccess(new HashMap<String, Object>() {
                {/*from w  w  w .  ja v  a 2s  .co  m*/
                    put(entry.getKey(), entry.getValue());
                }
            });
        }
    } else {
        response.setSuccessMessage(jsonObject.get("success").getAsString());
    }

    if (jsonObject.has("error") && jsonObject.get("error").isJsonObject()) {
        for (Map.Entry<String, JsonElement> entry : jsonObject.getAsJsonObject("error").entrySet()) {
            response.setError(context.deserialize(entry.getValue(), HueResponse.Error.class));
        }
    }
    return response;
}

From source file:de.behrfried.wikianalyzer.wawebapp.server.service.JsonWikiAccess.java

License:Apache License

@Override
public ArticleInfo getArticleInfo(String title) throws ArticleNotExistException {
    try {//from  w ww  . j  av  a 2  s .  c  o m
        final int pageid = this.getPageId(title);

        if (pageid == -1) {
            /* article does not exist */
            throw new ArticleNotExistException("Artikel \"" + title + "\" existiert nicht!");
        }

        final List<ArticleInfo.AuthorAndCommits> authorsAndCommits = new ArrayList<ArticleInfo.AuthorAndCommits>();
        final List<ArticleInfo.Revision> revisions = new ArrayList<ArticleInfo.Revision>();
        final List<ArticleInfo.RevsPerDate> revsPerDates = new ArrayList<ArticleInfo.RevsPerDate>();

        int lastRev = 0;

        Date creationDate = null;
        String initialAuthor = null;

        /* get revisions of an article (max 500 are allowed) */
        // http://de.wikipedia.org/w/api.php?action=query&format=xml&prop=revisions&pageids=88112&rvprop=user|ids|timestamp|sha1&rvlimit=10000&rvdiffto=next&rvdir=older
        final Map<String, Integer> authorsAndCommitsTmp = new HashMap<String, Integer>();
        final Map<Long, Integer> revsPerDatesTmp = new HashMap<Long, Integer>();
        while (lastRev != -1) {
            final String response1 = this.requester
                    .getResult(this.convertRequest("action=query&format=json&prop=revisions&rvprop=user|ids"
                            + "|timestamp|comment|size&rvlimit=500&rvdir=newer&rvexcludeuser=127.0.0.1&pageids="
                            + pageid + "&rvstartid=" + lastRev + "&continue="));

            final JsonObject root = this.parser.parse(response1).getAsJsonObject();
            final JsonObject page = root.getAsJsonObject("query").getAsJsonObject("pages")
                    .getAsJsonObject(pageid + "");

            if (!page.has("revisions")) {
                break;
            }

            final JsonArray w = page.getAsJsonArray("revisions");

            /*
             * iterate the revisions
             */
            for (JsonElement obj : w) {

                final JsonObject jsonObj = obj.getAsJsonObject();
                final String author = jsonObj.getAsJsonPrimitive("user").getAsString();
                if (!authorsAndCommitsTmp.containsKey(author)) {
                    authorsAndCommitsTmp.put(author, 1);
                } else {
                    authorsAndCommitsTmp.put(author, authorsAndCommitsTmp.get(author) + 1);
                }

                try {
                    revisions.add(new ArticleInfo.Revision(jsonObj.getAsJsonPrimitive("revid").getAsInt(),
                            jsonObj.getAsJsonPrimitive("parentid").getAsInt(),
                            this.formatter.parse(
                                    jsonObj.getAsJsonPrimitive("timestamp").getAsString().replace('T', '_')),
                            author, jsonObj.getAsJsonPrimitive("comment").getAsString(),
                            jsonObj.getAsJsonPrimitive("size").getAsInt(), 0, false));
                } catch (ParseException e) {
                    this.logger.error(e.getMessage(), e);
                }
            }
            if (lastRev == 0) {
                try {
                    creationDate = this.formatter.parse(w.get(0).getAsJsonObject()
                            .getAsJsonPrimitive("timestamp").getAsString().replace('T', '_'));
                } catch (ParseException e) {
                    this.logger.error(e.getMessage(), e);
                }
                initialAuthor = w.get(0).getAsJsonObject().getAsJsonPrimitive("user").getAsString();
            }
            // lastRev = w.get(w.size() -
            // 1).getAsJsonObject().getAsJsonPrimitive("revid").getAsInt() +
            // 1;
            lastRev = root.has("continue")
                    ? root.getAsJsonObject("continue").getAsJsonPrimitive("rvcontinue").getAsInt()

                    :

                    -1;
        }

        for (Map.Entry<String, Integer> entry : authorsAndCommitsTmp.entrySet()) {
            authorsAndCommits.add(new ArticleInfo.AuthorAndCommits(entry.getKey(), entry.getValue()));
        }
        Collections.sort(authorsAndCommits, new Comparator<ArticleInfo.AuthorAndCommits>() {

            @Override
            public int compare(ArticleInfo.AuthorAndCommits authorAndCommits,
                    ArticleInfo.AuthorAndCommits authorAndCommits2) {
                return authorAndCommits2.getNumOfCommits() - authorAndCommits.getNumOfCommits();
            }
        });

        /* set diffs in revisions and look for edit wars */
        for (int i = 1; i < revisions.size(); i++) {
            revisions.get(i).setDiff(revisions.get(i).getBytes() - revisions.get(i - 1).getBytes());
        }

        Date currentDate = new Date(creationDate.getYear(), creationDate.getMonth(), creationDate.getDate());
        while (currentDate.before(revisions.get(revisions.size() - 1).getTimestamp())) {
            revsPerDatesTmp.put(currentDate.getTime(), 0);
            currentDate = new Date(currentDate.getTime() + 86400000);
        }
        for (final ArticleInfo.Revision revision : revisions) {
            final Date key = new Date(revision.getTimestamp().getYear(), revision.getTimestamp().getMonth(),
                    revision.getTimestamp().getDate());
            long time = key.getTime();
            try {
                if (!revsPerDatesTmp.containsKey(time)) {
                    time -= 3600000;
                }
                if (!revsPerDatesTmp.containsKey(time)) {
                    time += 7200000;
                }
                revsPerDatesTmp.put(time, revsPerDatesTmp.get(time) + 1);
            } catch (Exception e) {
                this.logger.error(e.getMessage());
            }

        }
        for (Map.Entry<Long, Integer> entry : revsPerDatesTmp.entrySet()) {
            revsPerDates.add(new ArticleInfo.RevsPerDate(new Date(entry.getKey()), entry.getValue()));
        }
        Collections.sort(revsPerDates, new Comparator<ArticleInfo.RevsPerDate>() {

            @Override
            public int compare(ArticleInfo.RevsPerDate revsPerDate, ArticleInfo.RevsPerDate revsPerDate2) {
                return revsPerDate.getDate().compareTo(revsPerDate2.getDate());
            }
        });

        /*
         * find edit wars
         */
        final List<ArticleInfo.EditWar> editWars = new ArrayList<ArticleInfo.EditWar>();
        final List<ArticleInfo.Revision> revertedRevs = this.getRevertedRevisions(revisions);
        for (int i = 0; i < revertedRevs.size() - 4; i++) {
            final ArticleInfo.Revision revision = revertedRevs.get(i);
            int startI = i;
            while (i < revertedRevs.size() - 4 && revertedRevs.get(i + 4).getTimestamp().getTime()
                    - revertedRevs.get(i).getTimestamp().getTime() < 100000000) {
                i += 4;
            }
            if (i != startI) {
                final StringBuilder usersStrBldr = new StringBuilder();
                final Map<String, Integer> usersMap = new HashMap<String, Integer>();
                for (int j = startI; j <= i; j++) {
                    final String author = revertedRevs.get(j).getAuthor();
                    if (!usersMap.containsKey(author)) {
                        usersMap.put(author, 0);
                    }
                    usersMap.put(author, usersMap.get(author) + 1);

                }
                for (final Map.Entry<String, Integer> entry : usersMap.entrySet()) {
                    usersStrBldr.append(entry.getKey());
                    usersStrBldr.append(" (");
                    usersStrBldr.append(entry.getValue());
                    usersStrBldr.append("); ");
                }
                editWars.add(new ArticleInfo.EditWar(revertedRevs.get(startI).getTimestamp(),
                        revertedRevs.get(i).getTimestamp(),
                        usersStrBldr.toString().substring(0, usersStrBldr.length() - 2)));
            }
        }

        /* get similiar articles */
        // http://de.wikipedia.org/w/api.php?action=query&format=xml&list=search&srsearch=Maus&srlimit=500
        final String similar = this.requester.getResult(
                this.convertRequest("action=query&format=json&list=search&srlimit=500&srsearch=" + title));
        final JsonArray search = this.parser.parse(similar).getAsJsonObject().getAsJsonObject("query")
                .getAsJsonArray("search");

        final List<ArticleInfo.SimilarArticle> similarArticles = new ArrayList<ArticleInfo.SimilarArticle>(
                search.size());

        for (final JsonElement obj : search) {
            final JsonObject jsonObj = obj.getAsJsonObject();
            final String simTitle = jsonObj.getAsJsonPrimitive("title").getAsString();
            final int simPageid = this.getPageId(simTitle);

            if (simPageid == pageid) {
                continue;
            }

            /* get categories */
            final String categories = this.getCategories(simPageid);

            /* get creation date */
            Date simCreationDate = null;
            final String creationDateStr = this.requester.getResult(this.convertRequest(
                    "action=query&format=json&prop=revisions&rvprop=timestamp&rvlimit=1&rvdir=newer&pageids="
                            + simPageid));
            try {
                simCreationDate = this.formatter.parse(this.parser.parse(creationDateStr).getAsJsonObject()
                        .getAsJsonObject("query").getAsJsonObject("pages").getAsJsonObject(simPageid + "")
                        .getAsJsonArray("revisions").get(0).getAsJsonObject().getAsJsonPrimitive("timestamp")
                        .getAsString().replace('T', '_'));
            } catch (ParseException e) {
                this.logger.error(e.getMessage(), e);
            }

            similarArticles.add(new ArticleInfo.SimilarArticle(simTitle, categories, simCreationDate));

        }

        /* get number of images */
        final String imageStr = this.requester
                .getResult(this.convertRequest("action=query&format=json&prop=images&pageids=" + pageid));
        final JsonObject images = this.parser.parse(imageStr).getAsJsonObject().getAsJsonObject("query")
                .getAsJsonObject("pages").getAsJsonObject(pageid + "");
        int numOfImages = 0;
        if (images.has("images")) {
            numOfImages = images.getAsJsonArray("images").size();
        }

        /*
         * get categories
         */
        final String categoriesStr = this.requester.getResult(this.convertRequest(
                "action=query&format=json&prop=categories&clprop=timestamp&cllimit=500&pageids=" + pageid));
        final JsonObject categoriesJsonObj = this.parser.parse(categoriesStr).getAsJsonObject()
                .getAsJsonObject("query").getAsJsonObject("pages").getAsJsonObject(pageid + "");

        final List<ArticleInfo.Category> categoryList = new ArrayList<ArticleInfo.Category>();
        if (categoriesJsonObj.has("categories")) {
            final JsonArray categoriesJsonArr = categoriesJsonObj.getAsJsonArray("categories");

            for (final JsonElement catElem : categoriesJsonArr) {
                final JsonObject catJson = catElem.getAsJsonObject();
                try {

                    categoryList.add(new ArticleInfo.Category(
                            catJson.getAsJsonPrimitive("title").getAsString().replaceAll("Kategorie:", "")
                                    .replaceAll("Wikipedia:", ""),
                            this.formatter.parse(
                                    catJson.getAsJsonPrimitive("timestamp").getAsString().replace('T', '_'))));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }

        return new ArticleInfo(pageid, title, initialAuthor, creationDate,
                "http://de.wikipedia.org/wiki/" + title.replaceAll(" ", "_"),
                "http://de.wikipedia.org/wiki/Benutzer:" + initialAuthor, numOfImages,
                this.getCategories(pageid), revisions.get(revisions.size() - 1).getBytes(), authorsAndCommits,
                revisions, revsPerDates, editWars, similarArticles, categoryList);

    } catch (Exception e) {
        this.logger.error(e.getMessage(), e);
    }
    return null;
}

From source file:de.behrfried.wikianalyzer.wawebapp.server.service.JsonWikiAccess.java

License:Apache License

private Object[] calcReputation(String userName, int lastUserCommits, boolean blocked) {
    int abuseCnt = 0;
    String abuses = "";
    int abuseCount = 0;
    double abuseCntFactor = 1.0d;
    String tmpDate = "";

    do {/*from  www . j a  va2  s.co m*/
        final String abuselogStr = this.requester
                .getResult(this.convertRequest("action=query&format=json&list=abuselog&afllimit=500&afluser="
                        + userName + "&aflstart=" + tmpDate));

        final JsonObject abuseRoot = this.parser.parse(abuselogStr).getAsJsonObject();
        final JsonArray abuseJsnonArr = abuseRoot.getAsJsonObject("query").getAsJsonArray("abuselog");
        int warnCount = 0, disallowCount = 0, blockCount = 0;
        for (final JsonElement jElem : abuseJsnonArr) {
            final JsonObject jObj = jElem.getAsJsonObject();
            final String abuseResult = jObj.getAsJsonPrimitive("result").getAsString();

            abuseCnt += 1;
            if (abuseResult.contains("warn")) {
                warnCount += 5;
                abuseCntFactor = (abuseCntFactor + 5.0d) / abuseCnt;
            }
            if (abuseResult.contains("disallow")) {
                disallowCount += 10;
                ;
                abuseCntFactor = (abuseCntFactor + 10.0d) / abuseCnt;
            }
            if (abuseResult.contains("block")) {
                blockCount += 15;
                abuseCntFactor = (abuseCntFactor + 15.0d) / abuseCnt;
            }
        }
        abuseCount = warnCount + disallowCount + blockCount;
        abuses = "(" + abuseCount + "): " + "warn(" + warnCount + "); disallow(" + disallowCount + "); block("
                + blockCount + ");";
        tmpDate = "";
        if (abuseRoot.has("query-continue")) {
            tmpDate = abuseRoot.getAsJsonObject("query-continue").getAsJsonObject("abuselog")
                    .getAsJsonPrimitive("aflstart").getAsString();
        }

    } while (!tmpDate.isEmpty());

    this.logger.info("AbuseCntfactor: " + abuseCntFactor);
    double reputation = 1.0d - (1.0d / ((1.0d + lastUserCommits) / (1.0d + abuseCnt))) / (1 / abuseCntFactor);
    if (blocked) {
        reputation = 0;
    }
    if (reputation > 1) {
        reputation = 1;
    } else if (reputation < 0) {
        reputation = 0;
    }
    return new Object[] { new Double(reputation), abuses, abuseCnt };
}