Example usage for com.google.gson JsonObject get

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

Introduction

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

Prototype

public JsonElement get(String memberName) 

Source Link

Document

Returns the member with the specified name.

Usage

From source file:au.edu.ausstage.tweetgatherer.MessageProcessor.java

License:Open Source License

/**
 * A method that is run in the thread/*from  w w  w  . jav a 2  s  .  c  o  m*/
 * Takes a tweet from the queue and processes it
 */
public void run() {
    STweet tweet = null;

    try {

        // infinite loop to keep the thread running
        while (true) {

            // take a tweet from the queue
            tweet = newTweets.take();

            // get the tweet id number
            String tweetId = Long.toString(tweet.getStatusId());

            // get a hash of the tweet id
            String tweetIdHash = HashUtils.hashValue(tweetId);

            // get the user
            STweetUser user = tweet.getUser();

            // get the user id
            String userId = Long.toString(user.getUserId());

            // get a hash of the user id
            String userIdHash = HashUtils.hashValue(userId);

            // get the JSON value of the tweet
            JsonObject jsonTweetObject = sanitiseMessage(tweet.getJSON(), tweetIdHash);

            // get the JSON value of the user
            JsonObject jsonUserObject = sanitiseUser(user.getJSON(), userIdHash);

            // replace the user with the sanitised version
            jsonTweetObject.remove("user");
            jsonTweetObject.addProperty("user", "");

            /*
             * Write the file
             */

            if (FileUtils.writeNewFile(logFiles + "/" + tweetIdHash,
                    jsonTweetObject.toString() + "\n" + jsonUserObject.toString()) == false) {
                System.err.println("ERROR: Unable to write file to the specified log file");
                System.err.println("       twitter message with id '" + tweetId + "' is now lost");
            } else {
                System.out.println("INFO: New Message written to the log directory:");
                System.out.println("      " + tweetIdHash);
            }

            /*
             * get the hash tags from this message
             */

            // get the list of hash tags from the tweet
            JsonElement elem = jsonTweetObject.get("text");
            List<String> hashtags = extractHashTags.extractHashtags(elem.getAsString());

            // convert all of the found hash tags to lower case
            ListIterator<String> hashtagsIterator = hashtags.listIterator();

            while (hashtagsIterator.hasNext() == true) {

                // get the next tage in the list
                String tmp = (String) hashtagsIterator.next();

                tmp = tmp.toLowerCase();

                // replace the value we retrieved with this updated one
                hashtagsIterator.set(tmp);
            }

            // see if this is a company performance
            if (isCompanyPerformance(hashtags, jsonTweetObject, jsonUserObject, tweetIdHash) == false) {
                // this isn't a company performance
                // is it using a performance specific id?
                if (isPerformance(hashtags, jsonTweetObject, jsonUserObject, tweetIdHash) == false) {
                    //this isn't a valid performance
                    System.err.println("ERROR: Unable to find a matching performance for this message");

                    // send an exception report
                    if (emailManager.sendMessageWithAttachment(EMAIL_SUBJECT, EMAIL_MESSAGE,
                            logFiles + "/" + tweetIdHash) == false) {
                        System.err.println("ERROR: Unable to send the exception report");
                    }
                }
            }
        }

    } catch (InterruptedException ex) {
        // thread has been interrupted
        System.out.println("INFO: The Message processing thread has stopped");
    }
}

From source file:au.edu.ausstage.tweetgatherer.MessageProcessor.java

License:Open Source License

/**
 * A private method to sanitise the message
 *
 * @param jsonTweetObject the original message object
 * @param idHash          the hash of the message id
 *
 * @return                the sanitised message object
 *//*from ww  w  .j ava  2 s. c o m*/
private JsonObject sanitiseMessage(JsonObject jsonTweetObject, String idHash) {

    // remove the id and replace it with the hash
    jsonTweetObject.remove("id");
    jsonTweetObject.addProperty("id", idHash);

    // replace the "in_reply_to_user_id" field with a hash if present
    if (jsonTweetObject.has("in_reply_to_user_id") == true) {

        // get a hash of the value
        JsonElement elem = jsonTweetObject.get("in_reply_to_user_id");
        if (elem.isJsonNull() == false) {
            String hash = HashUtils.hashValue(elem.getAsString());

            // update the value
            jsonTweetObject.remove("in_reply_to_user_id");
            jsonTweetObject.addProperty("in_reply_to_user_id", hash);
        }
    }

    // replace the "in_reply_to_status_id" field with a hash if present
    if (jsonTweetObject.has("in_reply_to_status_id") == true) {

        // get a hash of the value
        JsonElement elem = jsonTweetObject.get("in_reply_to_status_id");
        if (elem.isJsonNull() == false) {
            String hash = HashUtils.hashValue(elem.getAsString());

            // update the value
            jsonTweetObject.remove("in_reply_to_status_id");
            jsonTweetObject.addProperty("in_reply_to_status_id", hash);
        }
    }

    // replace the "retweeted_status" field with a hash if present
    if (jsonTweetObject.has("retweeted_status") == true) {

        // get a hash of the value
        JsonElement elem = jsonTweetObject.get("retweeted_status");
        if (elem.isJsonNull() == false) {
            String hash = HashUtils.hashValue(elem.getAsString());

            // update the value
            jsonTweetObject.remove("retweeted_status");
            jsonTweetObject.addProperty("retweeted_status", hash);
        }
    }

    // return the sanitised object
    return jsonTweetObject;

}

From source file:au.edu.ausstage.tweetgatherer.MessageProcessor.java

License:Open Source License

/**
 * A private method to process a message containing a company hash tag
 *
 * @param hashtags        a list of hashtags associated with this message
 * @param jsonTweetObject the message/*w  w  w . ja v a  2  s  .c  o  m*/
 * @param jsonUserObject  the user object
 * @param tweetIdHash     the hash of the message id
 *
 * @return                true if, and only if, the message was processed successfully
 */
private boolean isPerformance(List<String> hashtags, JsonObject jsonTweetObject, JsonObject jsonUserObject,
        String tweetIdHash) {

    /*
     * get the performance and question id
     */
    String performanceId = null;
    String questionId = null;

    JsonElement elem = jsonTweetObject.get("created_at");
    DateTime messageCreated = inputDateTimeFormat.parseDateTime(elem.getAsString());

    String selectSql = "SELECT performance_id, question_id, SUBSTR(hash_tag, 2) " + "FROM mob_performances "
            + "WHERE deprecated_hash_tag = 'N'";

    // get the data
    DbObjects results = database.executeStatement(selectSql);

    // double check the results
    if (results == null) {
        System.err.println("ERROR: Unable to execute the SQL to lookup performance specific hashtags");

        // send an exception report
        if (emailManager.sendMessageWithAttachment(EMAIL_SUBJECT, EMAIL_MESSAGE,
                logFiles + "/" + tweetIdHash) == false) {
            System.err.println("ERROR: Unable to send the exception report");
        }

        return false;

    } else {
        // look for performances matching the hash tag from the message
        ResultSet resultSet = results.getResultSet();

        // loop through the resultset
        boolean found = false; // exit the loop early
        String[] sqlParameters = new String[7]; // store the parameters

        try {

            while (resultSet.next() && found == false) {

                // see if the hashtags in the message match one in the DB
                if (hashtags.contains(resultSet.getString(3)) == true) {
                    // yep
                    sqlParameters[0] = resultSet.getString(1);
                    sqlParameters[1] = resultSet.getString(2);
                    found = true;
                }
            }
        } catch (java.sql.SQLException ex) {
            found = false;
        }

        if (found == false) {
            // no performance with a matching company id was found
            return false;
        } else {

            /*
             * write the message to the database
             */

            // define the sql
            String insertSql = "INSERT INTO mob_feedback "
                    + "(performance_id, question_id, source_type, received_date_time, received_from, source_id, short_content) "
                    + "VALUES (?,?,?, TO_DATE(?, '" + DB_DATE_TIME_FORMAT + "'),?,?,?)";

            // use the source id from the constant
            sqlParameters[2] = SOURCE_ID;

            // add the date and time
            sqlParameters[3] = dateTimeFormat.print(messageCreated);

            // add the user id
            elem = jsonUserObject.get("id");
            sqlParameters[4] = elem.getAsString();

            // add the source id
            elem = jsonTweetObject.get("id");
            sqlParameters[5] = elem.getAsString();

            // add the message
            elem = jsonUserObject.get("text");
            sqlParameters[6] = elem.getAsString();

            // insert the data
            if (database.executePreparedInsertStatement(insertSql, sqlParameters) == false) {
                System.err.println("ERROR: Unable to add the message to the database");

                // send an exception report
                if (emailManager.sendMessageWithAttachment(EMAIL_SUBJECT,
                        "Exception Report: The Tweet Gatherer was unable to store this message in the database",
                        logFiles + "/" + tweetIdHash) == false) {
                    System.err.println("ERROR: Unable to send the exception report");
                }

            } else {
                System.out.println("INFO: Successfully added the message to the database");
                return true;
            }
        }
    } // end performance check

    // if we get this far, something bad happend
    return false;
}

From source file:au.edu.ausstage.tweetgatherer.MessageProcessor.java

License:Open Source License

/**
 * A private method to process a message containing a company hash tag
 *
 * @param hashtags        a list of hashtags associated with this message
 * @param jsonTweetObject the message/*from www .  j  a  v  a2  s. co m*/
 * @param jsonUserObject  the user object
 * @param tweetIdHash     the hash of the message id
 *
 * @return                true if, and only if, the message was processed successfully
 */
private boolean isCompanyPerformance(List<String> hashtags, JsonObject jsonTweetObject,
        JsonObject jsonUserObject, String tweetIdHash) {

    /*
     * get the date & time that the message was received
     */
    JsonElement elem = jsonTweetObject.get("created_at");
    DateTime messageCreated = inputDateTimeFormat.parseDateTime(elem.getAsString());
    LocalDateTime localMessageCreated = messageCreated.toLocalDateTime();

    /*
     * get the performance and question id
     */
    String performanceId = null;
    String questionId = null;

    // define the sql            
    String selectSql = "SELECT performance_id, question_id, SUBSTR(twitter_hash_tag, 2) "
            + "FROM mob_performances, mob_organisations, orgevlink "
            + "WHERE mob_performances.event_id = orgevlink.eventid "
            + "AND mob_organisations.organisation_id = orgevlink.organisationid "
            + "AND start_date_time < TO_DATE(?, '" + DB_DATE_TIME_FORMAT + "') "
            + "AND end_date_time + 1/12 > TO_DATE(?, '" + DB_DATE_TIME_FORMAT + "') ";

    // define the parameters array
    String[] sqlParameters = new String[2];
    sqlParameters[0] = dateTimeFormat.print(messageCreated);
    sqlParameters[1] = dateTimeFormat.print(messageCreated);

    // get the data
    DbObjects results = database.executePreparedStatement(selectSql, sqlParameters);

    // double check the results
    if (results == null) {
        System.err.println("ERROR: Unable to execute the SQL to lookup performances");

        // send an exception report
        if (emailManager.sendMessageWithAttachment(EMAIL_SUBJECT, EMAIL_MESSAGE,
                logFiles + "/" + tweetIdHash) == false) {
            System.err.println("ERROR: Unable to send the exception report");
        }

        return false;

    } else {

        // look for performances matching the hash tag from the message
        ResultSet resultSet = results.getResultSet();

        // loop through the resultset
        boolean found = false; // exit the loop early
        sqlParameters = new String[7]; // store the parameters

        try {

            while (resultSet.next() && found == false) {

                // see if the hashtags in the message match one in the DB
                if (hashtags.contains(resultSet.getString(3)) == true) {
                    // yep
                    sqlParameters[0] = resultSet.getString(1);
                    sqlParameters[1] = resultSet.getString(2);
                    found = true;
                }
            }
        } catch (java.sql.SQLException ex) {
            found = false;
        }

        if (found == false) {
            // no performance with a matching company id was found
            return false;
        } else {

            /*
             * write the message to the database
             */

            // define the sql
            String insertSql = "INSERT INTO mob_feedback "
                    + "(performance_id, question_id, source_type, received_date_time, received_from, source_id, short_content) "
                    + "VALUES (?,?,?, TO_DATE(?, '" + DB_DATE_TIME_FORMAT + "'),?,?,?)";

            // use the source id from the constant
            sqlParameters[2] = SOURCE_ID;

            // add the date and time
            sqlParameters[3] = dateTimeFormat.print(messageCreated);

            // add the user id
            elem = jsonUserObject.get("id");
            sqlParameters[4] = elem.getAsString();

            // add the source id
            elem = jsonTweetObject.get("id");
            sqlParameters[5] = elem.getAsString();

            // add the message
            elem = jsonUserObject.get("text");
            sqlParameters[6] = elem.getAsString();

            // insert the data
            if (database.executePreparedInsertStatement(insertSql, sqlParameters) == false) {
                System.err.println("ERROR: Unable to add the message to the database");

                // send an exception report
                if (emailManager.sendMessageWithAttachment(EMAIL_SUBJECT,
                        "Exception Report: The Tweet Gatherer was unable to store this message in the database",
                        logFiles + "/" + tweetIdHash) == false) {
                    System.err.println("ERROR: Unable to send the exception report");
                }

            } else {
                System.out.println("INFO: Successfully added the message to the database");
                return true;
            }
        }
    } // end performance check

    // if we get this far, something bad happend
    return false;
}

From source file:au.edu.unsw.soacourse.client.ApplicationLogic.java

static public void getAppsByJobID(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setCharacterEncoding("utf8");
    response.setContentType("application/json");
    PrintWriter out = response.getWriter();
    try {//from ww w.  j  a v  a2 s .  co  m
        StringBuffer sb = new StringBuffer();
        BufferedReader reader = request.getReader();
        String temp = "";
        while ((temp = reader.readLine()) != null) {
            sb.append(temp);
        }
        // System.out.println(sb.toString());
        // convert to object
        Gson gson = new Gson();
        JsonElement je = gson.fromJson(sb.toString(), JsonElement.class);
        JsonObject jo = je.getAsJsonObject();

        String jobID = "";
        if (jo.has("jobID"))
            jobID = jo.get("jobID").getAsString();

        String REST_URI = "http://localhost:8080/RESTfulFoundITService/apps";
        REST_URI += "/jobID/" + jobID;

        WebClient client = WebClient.create(REST_URI);
        String s = client.get(String.class);
        // System.out.println(s);

        // write to json

        out.write(s);
        out.close();
    } catch (Exception e) {
        out.write("failed");
        e.printStackTrace();
        out.close();
    }

}

From source file:au.edu.unsw.soacourse.client.ApplicationLogic.java

static public void getAppsByAppID(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setCharacterEncoding("utf8");
    response.setContentType("application/json");
    PrintWriter out = response.getWriter();
    try {/*from   w w w .  j  a v a 2  s . co m*/
        StringBuffer sb = new StringBuffer();
        BufferedReader reader = request.getReader();
        String temp = "";
        while ((temp = reader.readLine()) != null) {
            sb.append(temp);
        }
        // System.out.println(sb.toString());
        // convert to object
        Gson gson = new Gson();
        JsonElement je = gson.fromJson(sb.toString(), JsonElement.class);
        JsonObject jo = je.getAsJsonObject();

        String appID = "";
        if (jo.has("appID"))
            appID = jo.get("appID").getAsString();

        String REST_URI = "http://localhost:8080/RESTfulFoundITService/apps/";
        REST_URI += appID;

        WebClient client = WebClient.create(REST_URI);
        String s = client.get(String.class);
        // System.out.println(s);

        // write to json

        out.write(s);
        out.close();
    } catch (Exception e) {
        out.write("failed");
        e.printStackTrace();
        out.close();
    }

}

From source file:au.edu.unsw.soacourse.client.ApplicationLogic.java

static public void getApps(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setCharacterEncoding("utf8");
    response.setContentType("application/json");
    PrintWriter out = response.getWriter();
    try {//from  www. j  av a 2 s  .c o  m
        StringBuffer sb = new StringBuffer();
        BufferedReader reader = request.getReader();
        String temp = "";
        while ((temp = reader.readLine()) != null) {
            sb.append(temp);
        }
        // System.out.println(sb.toString());
        // convert to object
        Gson gson = new Gson();
        JsonElement je = gson.fromJson(sb.toString(), JsonElement.class);
        JsonObject jo = je.getAsJsonObject();

        String profileID = "";
        if (jo.has("profileID"))
            profileID = jo.get("profileID").getAsString();

        // System.out.println(profileID);
        String REST_URI = "http://localhost:8080/RESTfulFoundITService/apps";
        REST_URI += "/profileID/" + profileID;

        WebClient client = WebClient.create(REST_URI);
        String s = client.get(String.class);
        // System.out.println(s);
        // write the json
        out.write(s);
        out.close();
    } catch (Exception e) {
        out.write("failed");
        e.printStackTrace();
        out.close();
    }

}

From source file:au.edu.unsw.soacourse.client.RegisterLogic.java

static public void getpro(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setCharacterEncoding("utf8");
    response.setContentType("application/json");
    PrintWriter out = response.getWriter();
    try {/*  w ww. j av  a 2  s.co  m*/
        StringBuffer sb = new StringBuffer();
        BufferedReader reader = request.getReader();
        String temp = "";
        while ((temp = reader.readLine()) != null) {
            sb.append(temp);
        }
        // System.out.println(sb.toString());
        // convert to object
        Gson gson = new Gson();
        JsonElement je = gson.fromJson(sb.toString(), JsonElement.class);
        JsonObject jo = je.getAsJsonObject();

        String profileID = "";
        if (jo.has("profileID"))
            profileID = jo.get("profileID").getAsString();

        String REST_URI = "http://localhost:8080/RESTfulFoundITService/userProfiles";
        REST_URI += "/profile/" + profileID;

        WebClient client = WebClient.create(REST_URI);
        String s = client.get(String.class);
        // System.out.println(s);

        // write the json
        out.write(s);
        out.close();
    } catch (Exception e) {
        out.write("failed");
        e.printStackTrace();
        out.close();
    }

}

From source file:augsburg.se.alltagsguide.common.Author.java

License:Open Source License

@NonNull
public static Author fromJson(@NonNull final JsonObject jsonTag) {
    String login = jsonTag.get("login").getAsString();
    String firstName = jsonTag.get("first_name").getAsString();
    String lastName = jsonTag.get("last_name").getAsString();
    return new Author(login, firstName, lastName);
}

From source file:augsburg.se.alltagsguide.common.Event.java

License:Open Source License

public @NonNull static Event fromJson(@NonNull final JsonObject jsonEvent, int pageId) {
    int id = jsonEvent.get("id").getAsInt();
    String startDate = jsonEvent.get("start_date").getAsString();
    String endDate = jsonEvent.get("end_date").getAsString();
    String startTime = jsonEvent.get("start_time").getAsString();
    String endTime = jsonEvent.get("end_time").getAsString();
    boolean allDay = jsonEvent.get("all_day").getAsInt() == 1;

    long start = -1;
    long end = -1;
    try {/* ww w  . j  av a 2s.c o  m*/
        start = Helper.FROM_DATE_FORMAT.parse(startDate + " " + Objects.emptyIfNull(startTime)).getTime();
        end = Helper.FROM_DATE_FORMAT.parse(endDate + " " + Objects.emptyIfNull(endTime)).getTime();
    } catch (ParseException e) {
        Ln.e(e);

    }
    return new Event(id, start, end, allDay, pageId);
}