Example usage for com.google.gson JsonObject toString

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

Introduction

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

Prototype

@Override
public String toString() 

Source Link

Document

Returns a String representation of this element.

Usage

From source file:app.abhijit.iter.data.source.remote.IterApi.java

License:Open Source License

public static String fetchStudentDetails(String studentId) throws Exception {
    JsonObject request = new JsonObject();
    request.addProperty("sid", "studentdetails");
    request.addProperty("instituteid", INSTITUTE_ID);
    request.addProperty("studentid", studentId);
    return Http.post(API_ENDPOINT, "jdata=" + request.toString());
}

From source file:app.abhijit.iter.data.source.remote.IterApi.java

License:Open Source License

public static String fetchStudentSubjects(String studentId) throws Exception {
    JsonObject request = new JsonObject();
    request.addProperty("sid", "attendance");
    request.addProperty("instituteid", INSTITUTE_ID);
    request.addProperty("registrationid", REGISTRATION_ID);
    request.addProperty("studentid", studentId);
    return Http.post(API_ENDPOINT, "jdata=" + request.toString());
}

From source file:app.abhijit.iter.data.source.StudentDataSource.java

License:Open Source License

public void fetch() {
    String selectedRegistrationNumber = mLocalStore.getString(SELECTED_REGISTRATION_NUMBER_KEY, null);
    if (!StringUtils.equals(selectedRegistrationNumber, mSelectedRegistrationNumber)) {
        if (mFetchStudentAsyncTask != null) {
            mFetchStudentAsyncTask.cancel(true);
            mFetchStudentAsyncTask = null;
        }/*from   w  ww .j  av a 2 s.  c om*/
        if (mFetchStudentSubjectsAsyncTask != null) {
            mFetchStudentSubjectsAsyncTask.cancel(true);
            mFetchStudentSubjectsAsyncTask = null;
        }
        mSelectedRegistrationNumber = selectedRegistrationNumber;
    }

    if (mSelectedRegistrationNumber != null && mErrorMessage == null && mFetchStudentAsyncTask == null
            && mFetchStudentSubjectsAsyncTask == null) {
        if (!mLocalStore.contains(mSelectedRegistrationNumber)) {
            JsonObject student = new JsonObject();
            student.addProperty(STUDENT_REGISTRATION_NUMBER_KEY, mSelectedRegistrationNumber);
            mLocalStore.edit().putString(mSelectedRegistrationNumber, student.toString()).apply();
            mFetchStudentAsyncTask = new FetchStudentAsyncTask();
            mFetchStudentAsyncTask.execute();
        } else {
            JsonObject student = mJsonParser.parse(mLocalStore.getString(mSelectedRegistrationNumber, null))
                    .getAsJsonObject();
            if (!student.has(STUDENT_NAME_KEY)) {
                mFetchStudentAsyncTask = new FetchStudentAsyncTask();
                mFetchStudentAsyncTask.execute();
            } else {
                boolean isSubjectDataOld = (new Date().getTime()
                        - student.get(STUDENT_TIMESTAMP_KEY).getAsLong()) > CACHE_TIMEOUT;
                if (isSubjectDataOld || !student.has(STUDENT_SUBJECTS_KEY)) {
                    String studentId = student.get(STUDENT_ID_KEY).getAsString();
                    mFetchStudentSubjectsAsyncTask = new FetchStudentSubjectsAsyncTask();
                    mFetchStudentSubjectsAsyncTask.execute(studentId);
                }
            }
        }
    }

    if (mErrorMessage != null) {
        EventBus.getDefault().post(new Error(mErrorMessage));
        mErrorMessage = null;
    }

    ArrayList<Student> students = new ArrayList<>();

    for (Map.Entry<String, ?> entry : mLocalStore.getAll().entrySet()) {
        if (entry.getKey().equals(SELECTED_REGISTRATION_NUMBER_KEY))
            continue;
        JsonObject student = mJsonParser.parse(mLocalStore.getString(entry.getKey(), null)).getAsJsonObject();
        students.add(generateStudent(student));
    }

    EventBus.getDefault().post(ImmutableList.copyOf(students));
}

From source file:app.abhijit.iter.data.source.StudentDataSource.java

License:Open Source License

public void refresh() {
    if (mSelectedRegistrationNumber != null && mFetchStudentAsyncTask == null
            && mFetchStudentSubjectsAsyncTask == null) {
        JsonObject student = mJsonParser.parse(mLocalStore.getString(mSelectedRegistrationNumber, null))
                .getAsJsonObject();//w ww.j a  va 2  s.co m
        student.addProperty(STUDENT_TIMESTAMP_KEY, 0);
        mLocalStore.edit().putString(mSelectedRegistrationNumber, student.toString()).apply();
    }
    fetch();
}

From source file:app.abhijit.iter.data.source.StudentDataSource.java

License:Open Source License

private void processStudent(String[] response) {
    mFetchStudentAsyncTask = null;//  w  w w .  j a  v  a  2s .c o  m

    if (response[0] == null || response[1] == null) {
        mErrorMessage = "Could not load registration number " + mSelectedRegistrationNumber;
        delete(mSelectedRegistrationNumber);
        fetch();
        return;
    }

    String studentId = response[0];

    if (studentId.equals("0")) {
        mErrorMessage = "Registration number " + mSelectedRegistrationNumber + " is not valid";
        delete(mSelectedRegistrationNumber);
        fetch();
        return;
    }

    JsonObject student = new JsonObject();

    try {
        JsonObject studentDetails = mJsonParser.parse(response[1]).getAsJsonArray().get(0).getAsJsonObject();
        if (!studentDetails.has("name")) {
            throw new Exception();
        }
        student.addProperty(STUDENT_ID_KEY, studentId);
        student.addProperty(STUDENT_NAME_KEY, studentDetails.get("name").getAsString());
        student.addProperty(STUDENT_REGISTRATION_NUMBER_KEY, mSelectedRegistrationNumber);
        student.addProperty(STUDENT_TIMESTAMP_KEY, new Date().getTime());
    } catch (Exception e) {
        mErrorMessage = "Could not load registration number " + mSelectedRegistrationNumber;
        delete(mSelectedRegistrationNumber);
        fetch();
        return;
    }

    mLocalStore.edit().putString(mSelectedRegistrationNumber, student.toString()).apply();

    fetch();
}

From source file:app.abhijit.iter.data.source.StudentDataSource.java

License:Open Source License

private void processStudentSubjects(String response) {
    mFetchStudentSubjectsAsyncTask = null;

    JsonObject student = mJsonParser.parse(mLocalStore.getString(mSelectedRegistrationNumber, null))
            .getAsJsonObject();/*from w  w  w.  j  a v  a 2s . c  o  m*/
    JsonObject subjects = new JsonObject();

    try {
        JsonArray studentSubjects = mJsonParser.parse(response).getAsJsonArray();
        for (int i = 0; i < studentSubjects.size(); i++) {
            if (!studentSubjects.get(i).getAsJsonObject().has("subjectcode")
                    || !studentSubjects.get(i).getAsJsonObject().has("subject")
                    || !studentSubjects.get(i).getAsJsonObject().has("totalpresentclass")
                    || !studentSubjects.get(i).getAsJsonObject().has("totalclasses")) {
                continue;
            }
            String code = studentSubjects.get(i).getAsJsonObject().get("subjectcode").getAsString();
            String name = studentSubjects.get(i).getAsJsonObject().get("subject").getAsString();
            int presentClasses = studentSubjects.get(i).getAsJsonObject().get("totalpresentclass").getAsInt();
            int totalClasses = studentSubjects.get(i).getAsJsonObject().get("totalclasses").getAsInt();
            if (totalClasses <= 0 || presentClasses < 0 || presentClasses > totalClasses)
                continue;
            long lastUpdated = new Date().getTime();
            int oldPresentClasses = presentClasses;
            int oldTotalClasses = totalClasses;
            if (student.has(STUDENT_SUBJECTS_KEY)
                    && student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().has(code)) {
                oldPresentClasses = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().get(code)
                        .getAsJsonObject().get(STUDENT_SUBJECT_PRESENT_CLASSES_KEY).getAsInt();
                oldTotalClasses = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().get(code)
                        .getAsJsonObject().get(STUDENT_SUBJECT_TOTAL_CLASSES_KEY).getAsInt();
                if (presentClasses == oldPresentClasses && totalClasses == oldTotalClasses) {
                    lastUpdated = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().get(code)
                            .getAsJsonObject().get(STUDENT_SUBJECT_LAST_UPDATED_KEY).getAsLong();
                }
            }
            JsonObject subject = new JsonObject();
            subject.addProperty(STUDENT_SUBJECT_CODE_KEY, code);
            subject.addProperty(STUDENT_SUBJECT_NAME_KEY, name);
            subject.addProperty(STUDENT_SUBJECT_PRESENT_CLASSES_KEY, presentClasses);
            subject.addProperty(STUDENT_SUBJECT_TOTAL_CLASSES_KEY, totalClasses);
            subject.addProperty(STUDENT_SUBJECT_OLD_PRESENT_CLASSES_KEY, oldPresentClasses);
            subject.addProperty(STUDENT_SUBJECT_OLD_TOTAL_CLASSES_KEY, oldTotalClasses);
            subject.addProperty(STUDENT_SUBJECT_LAST_UPDATED_KEY, lastUpdated);
            subjects.add(code, subject);
        }
    } catch (Exception e) {
        if (student.has(STUDENT_SUBJECTS_KEY)) {
            subjects = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject();
        } else {
            subjects = new JsonObject();
        }
    }

    student.add(STUDENT_SUBJECTS_KEY, subjects);
    student.addProperty(STUDENT_TIMESTAMP_KEY, new Date().getTime());
    mLocalStore.edit().putString(mSelectedRegistrationNumber, student.toString()).apply();

    fetch();
}

From source file:app.abhijit.iter.data.source.StudentDataSource.java

License:Open Source License

private void removeOldSubjectData(String registrationNumber, JsonObject student, JsonObject subject) {
    String code = subject.get(STUDENT_SUBJECT_CODE_KEY).getAsString();
    int presentClasses = subject.get(STUDENT_SUBJECT_PRESENT_CLASSES_KEY).getAsInt();
    int totalClasses = subject.get(STUDENT_SUBJECT_TOTAL_CLASSES_KEY).getAsInt();
    subject.addProperty(STUDENT_SUBJECT_OLD_PRESENT_CLASSES_KEY, presentClasses);
    subject.addProperty(STUDENT_SUBJECT_OLD_TOTAL_CLASSES_KEY, totalClasses);
    student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().add(code, subject);
    mLocalStore.edit().putString(registrationNumber, student.toString()).apply();
}

From source file:arces.unibo.SEPA.client.api.SPARQL11SEProtocol.java

License:Open Source License

protected Response parseSPARQL11SEResponse(String response, SPARQL11SEPrimitive op) {
    if (response == null)
        return new ErrorResponse(0, 500, "Response is null");

    JsonObject json = null;
    try {/*  w w w . j  ava  2  s. c  om*/
        json = new JsonParser().parse(response).getAsJsonObject();
    } catch (JsonParseException | IllegalStateException e) {
        //if (op == SPARQL11SEPrimitive.SECUREUPDATE) return new UpdateResponse(response);

        return new ErrorResponse(0, 500, "Unknown response: " + response);
    }

    //Error response
    if (json.get("code") != null)
        if (json.get("code").getAsInt() >= 400)
            return new ErrorResponse(0, json.get("code").getAsInt(), json.get("body").getAsString());

    if (op == SPARQL11SEPrimitive.SECUREQUERY)
        return new QueryResponse(json);
    if (op == SPARQL11SEPrimitive.SECUREUPDATE)
        return new UpdateResponse(response);

    if (op == SPARQL11SEPrimitive.REGISTER) {
        if (json.get("client_id") != null && json.get("client_secret") != null) {
            try {
                properties.setCredentials(json.get("client_id").getAsString(),
                        json.get("client_secret").getAsString());
            } catch (IOException e) {
                return new ErrorResponse(ErrorResponse.NOT_FOUND, "Failed to save credentials");
            }

            return new RegistrationResponse(json.get("client_id").getAsString(),
                    json.get("client_secret").getAsString(), json.get("signature"));
        }
        return new ErrorResponse(0, 401, "Credentials not found in registration response");
    }

    if (op == SPARQL11SEPrimitive.REQUESTTOKEN) {
        if (json.get("access_token") != null && json.get("expires_in") != null
                && json.get("token_type") != null) {
            int seconds = json.get("expires_in").getAsInt();
            Date expires = new Date();
            expires.setTime(expires.getTime() + (1000 * seconds));
            try {
                properties.setJWT(json.get("access_token").getAsString(), expires,
                        json.get("token_type").getAsString());
            } catch (IOException e) {
                return new ErrorResponse(ErrorResponse.NOT_FOUND, "Failed to save JWT");
            }
            return new JWTResponse(json.get("access_token").getAsString(), json.get("token_type").getAsString(),
                    json.get("expires_in").getAsLong());
        } else if (json.get("code") != null && json.get("body") != null)
            return new ErrorResponse(0, json.get("code").getAsInt(), json.get("body").getAsString());
        else if (json.get("code") != null)
            return new ErrorResponse(0, json.get("code").getAsInt(), "");

        return new ErrorResponse(0, 500, "Response not recognized: " + json.toString());
    }

    return new ErrorResponse(0, 500, "Response unknown: " + response);
}

From source file:arces.unibo.SEPA.client.api.WebsocketClientEndpoint.java

License:Open Source License

private void sendSubscribeRequest() {
    if (wsClientSession == null) {
        logger.error("Session is null");
        return;/*from   www  .  j ava2s .  c  o m*/
    }

    try {
        JsonObject request = new JsonObject();
        request.add("subscribe", new JsonPrimitive(sparql));
        if (alias != null)
            request.add("alias", new JsonPrimitive(alias));
        else
            logger.debug("Alias is null");
        if (jwt != null)
            request.add("authorization", new JsonPrimitive(jwt));
        else
            logger.debug("Authorization is null");
        logger.debug(request.toString());

        wsClientSession.getBasicRemote().sendText(request.toString());
    } catch (IOException e) {
        logger.error(e.getMessage());
    }
}

From source file:arces.unibo.SEPA.client.api.WebsocketClientEndpoint.java

License:Open Source License

public void unsubscribe(String spuid, String jwt) throws IOException, URISyntaxException {
    logger.debug("unsubscribe");

    if (!isConnected())
        try {/* w  ww .  j  a v a  2 s  . c om*/
            connect();
        } catch (DeploymentException e) {
            throw new IOException(e.getMessage());
        }

    JsonObject request = new JsonObject();
    if (spuid != null)
        request.add("unsubscribe", new JsonPrimitive(spuid));
    if (jwt != null)
        request.add("authorization", new JsonPrimitive(jwt));

    wsClientSession.getBasicRemote().sendText(request.toString());
}