Example usage for com.google.gson.reflect TypeToken TypeToken

List of usage examples for com.google.gson.reflect TypeToken TypeToken

Introduction

In this page you can find the example usage for com.google.gson.reflect TypeToken TypeToken.

Prototype

@SuppressWarnings("unchecked")
protected TypeToken() 

Source Link

Document

Constructs a new type literal.

Usage

From source file:c301.AdventureBook.ElasticSearch.ESClient.java

License:Open Source License

/**
 * Consumes the Get operation of the service
 * //  www .j a v a  2s . c  o  m
 * Given a StoryId, It returns a StoryObject from The Server.
 * 
 * @param StoryID
 * @return Story -- Returns Null if no Story Found
 */
public Story getStory(String StoryID) {

    Story story = null;
    try {
        HttpGet getRequest = new HttpGet(WEBSERVICE_URI + STORIES_FOLDER + StoryID);

        getRequest.addHeader("Accept", "application/json");

        HttpResponse response = httpclient.execute(getRequest);

        String status = response.getStatusLine().toString();
        System.out.println(status);

        String json = getEntityContent(response);

        // We have to tell GSON what type we expect
        Type elasticSearchResponseType = new TypeToken<ElasticSearchResponse<Story>>() {
        }.getType();
        // Now we expect to get a Story response
        ElasticSearchResponse<Story> esResponse = gson.fromJson(json, elasticSearchResponseType);

        // We get the Story from it
        story = esResponse.getSource();

        // getRequest.releaseConnection();

    } catch (ClientProtocolException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();
    }

    return story;
}

From source file:c301.AdventureBook.ElasticSearch.ESClient.java

License:Open Source License

/**
 * search by keywords/*from  w  w  w. j av a  2  s .c om*/
 */
public ArrayList<Story> searchStories(String Keyword) {
    try {
        ArrayList<Story> filteredStories = new ArrayList<Story>();

        HttpGet searchRequest = new HttpGet(this.getQueryHttpURI(Keyword));

        searchRequest.setHeader("Accept", "application/json");
        HttpResponse response = httpclient.execute(searchRequest);
        String status = response.getStatusLine().toString();
        System.out.println(status);

        String json = getEntityContent(response);

        Type elasticSearchSearchResponseType = new TypeToken<ElasticSearchSearchResponse<Story>>() {
        }.getType();

        ElasticSearchSearchResponse<Story> esResponse = gson.fromJson(json, elasticSearchSearchResponseType);
        System.err.println(esResponse);
        for (ElasticSearchResponse<Story> r : esResponse.getHits()) {
            Story story = r.getSource();
            filteredStories.add(story);
        }
        // searchRequest.releaseConnection();

        return filteredStories;
    } catch (Exception e) {
        return null;
    }
}

From source file:c301.AdventureBook.ElasticSearch.ESClient.java

License:Open Source License

/**
 * Given an input Story object, this function checks whether the input story
 * is present on the WebServer or not./* w ww.  jav a  2 s  .  c o m*/
 * 
 * @param story
 * @return boolean storyExists
 */
private boolean storyExists(Story story) {
    boolean storyExists = false;

    try {
        HttpGet getRequest = new HttpGet(WEBSERVICE_URI + STORIES_FOLDER + story.getStoryId());
        getRequest.addHeader("Accept", "application/json");
        HttpResponse response = httpclient.execute(getRequest);
        String json = getEntityContent(response);

        // We have to tell GSON what type we expect
        Type elasticSearchResponseType = new TypeToken<ElasticSearchResponse<Story>>() {
        }.getType();
        // Now we expect to get a Story response
        ElasticSearchResponse<Story> esResponse = gson.fromJson(json, elasticSearchResponseType);
        // We get the Story from it
        storyExists = esResponse.getExtists();
        // getRequest.releaseConnection();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return storyExists;
}

From source file:ca.barelabs.barecouch.CouchDbClient.java

License:Apache License

public List<String> getAllDatabases() throws IOException {
    return executeAllDatabasesGet().parseAs(new TypeToken<List<String>>() {
    }.getType());//from   www  .j a  v a  2 s. c  om
}

From source file:ca.barrenechea.ticker.data.Event.java

License:Apache License

public List<HistoryEntry> getHistory() {
    if (listHistory == null) {
        Type listType = new TypeToken<LinkedList<HistoryEntry>>() {
        }.getType();/* ww  w  . ja v a  2  s  . co m*/
        listHistory = sGson.fromJson(history, listType);
    }

    return listHistory;
}

From source file:ca.cmput301f13t03.adventure_datetime.model.StoryDB.java

License:Open Source License

/**
 * Creates a StoryFragment from a cursor
 *
 * @param cursor A Cursor pointing to a StoryFragment
 *
 * @return A StoryFragment instance from the Database
 *//*www. jav a2  s .co  m*/
private StoryFragment createStoryFragment(Cursor cursor) {
    UUID storyID, fragmentID;
    String storyText;
    ArrayList<Choice> choices;
    ArrayList<Image> images;
    ArrayList<UUID> uuids;
    storyID = UUID.fromString(cursor.getString(cursor.getColumnIndex(StoryDB.STORYFRAGMENT_COLUMN_STORYID)));
    fragmentID = UUID.fromString(cursor.getString(cursor.getColumnIndex(StoryDB.COLUMN_GUID)));
    storyText = cursor.getString(cursor.getColumnIndex(StoryDB.STORYFRAGMENT_COLUMN_CONTENT));
    String json = cursor.getString(cursor.getColumnIndex(StoryDB.STORYFRAGMENT_COLUMN_CHOICES));
    Gson gson = new Gson();
    Type collectionType = new TypeToken<Collection<Choice>>() {
    }.getType();
    choices = gson.fromJson(json, collectionType);
    json = cursor.getString(cursor.getColumnIndex(STORYFRAGMENT_COLUMN_IMAGES));
    collectionType = new TypeToken<Collection<UUID>>() {
    }.getType();
    uuids = gson.fromJson(json, collectionType);
    images = getImages(uuids);

    return new StoryFragment(storyID, fragmentID, storyText, images, choices);
}

From source file:ca.cmput301w14t09.elasticSearch.ElasticSearchOperations.java

License:GNU General Public License

/**
 * pullThreads returns the list of top comments.  
 * Tested and verified.//from  w ww .  ja va  2  s. c o  m
 * @return list of names of Threads.
 * @throws InterruptedException
 */
public static ArrayList<Comment> pullThreads() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final ArrayList<Comment> commentList = new ArrayList<Comment>();

    if (GSON == null)
        constructGson();

    Thread thread = new Thread() {

        @Override
        public void run() {
            HttpClient client = new DefaultHttpClient();

            try {
                HttpPost searchRequest = new HttpPost(searchAddress);
                String query = "{\"query\" : {\"query_string\" : {\"default_field\" : \"topComment\",\"query\" : \"true\"}}}";

                StringEntity stringentity = new StringEntity(query);
                searchRequest.setEntity(stringentity);

                HttpResponse response = client.execute(searchRequest);
                String json = getEntityContent(response);

                Type elasticSearchSearchResponseType = new TypeToken<ElasticSearchSearchResponse<Comment>>() {
                }.getType();
                ElasticSearchSearchResponse<Comment> esResponse = GSON.fromJson(json,
                        elasticSearchSearchResponseType);

                for (ElasticSearchResponse<Comment> r : esResponse.getHits()) {
                    Comment topComment = r.getSource();
                    commentList.add(topComment);
                }

                // Sort by latest dated element.
                Collections.sort(commentList);
                Collections.reverse(commentList);
                latch.countDown();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    thread.start();
    latch.await();
    return commentList;
}

From source file:ca.cmput301w14t09.elasticSearch.ElasticSearchOperations.java

License:GNU General Public License

/**
 * pullOneThread takes in a thread ID and then
 * returns all comments that contain that thread ID
 * @param threadId//from   w  w  w .  j  a  v  a  2  s .  co  m
 * @return
 * @throws InterruptedException
 */
public static ArrayList<Comment> pullOneThread(final String threadId) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final ArrayList<Comment> commentList = new ArrayList<Comment>();

    if (GSON == null)
        constructGson();

    Thread thread = new Thread() {

        @Override
        public void run() {
            HttpClient client = new DefaultHttpClient();

            try {
                HttpPost searchRequest = new HttpPost(searchAddress);
                String query = "{\"query\" : {\"query_string\" : {\"default_field\" : \"threadId\", \"query\" : \""
                        + threadId + "\"}}}";
                StringEntity stringentity = new StringEntity(query);
                searchRequest.setEntity(stringentity);

                HttpResponse response = client.execute(searchRequest);
                String json = getEntityContent(response);

                Type elasticSearchSearchResponseType = new TypeToken<ElasticSearchSearchResponse<Comment>>() {
                }.getType();
                ElasticSearchSearchResponse<Comment> esResponse = GSON.fromJson(json,
                        elasticSearchSearchResponseType);

                for (ElasticSearchResponse<Comment> r : esResponse.getHits()) {
                    Comment topComment = r.getSource();
                    commentList.add(topComment);
                }

                // Sort by latest dated element.
                Collections.sort(commentList);
                latch.countDown();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    thread.start();
    latch.await();
    return commentList;
}

From source file:ca.cmput301w14t09.elasticSearch.ElasticSearchOperations.java

License:GNU General Public License

/**
 * Pulls the userProfile from the server, uses their uniqueID as a key source of extracting
 * Information./*from   w  ww .j  a  v a 2 s .  c om*/
 * @param uniqueID
 * @return
 * @throws InterruptedException
 */
public static ArrayList<UserProfileModel> pullUserProfile(final String uniqueID) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final ArrayList<UserProfileModel> userProfileList = new ArrayList<UserProfileModel>();

    if (GSON == null)
        constructGson();

    Thread thread = new Thread() {

        @Override
        public void run() {
            HttpClient client = new DefaultHttpClient();
            try {
                HttpPost searchRequest = new HttpPost(profileAddress + "_search?pretty=1");
                String query = "{\"query\" : {\"query_string\" : {\"default_field\" : \"uniqueID\",\"query\" : \""
                        + uniqueID + "\"}}}";

                StringEntity stringentity = new StringEntity(query);
                searchRequest.setEntity(stringentity);

                HttpResponse response = client.execute(searchRequest);
                String json = getEntityContent(response);

                Type elasticSearchSearchResponseType = new TypeToken<ElasticSearchSearchResponse<UserProfileModel>>() {
                }.getType();
                ElasticSearchSearchResponse<UserProfileModel> esResponse = GSON.fromJson(json,
                        elasticSearchSearchResponseType);

                for (ElasticSearchResponse<UserProfileModel> r : esResponse.getHits()) {
                    UserProfileModel userProfileModel = r.getSource();
                    userProfileList.add(userProfileModel);
                }
                latch.countDown();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    thread.start();
    latch.await();
    return userProfileList;
}

From source file:ca.cs.ualberta.localpost.controller.ElasticSearchOperations.java

License:Open Source License

/**
 * Gets all children models from ES/*from   ww w.j a v  a 2  s .co m*/
 * @param uuid Pulls Model from ES using UUID
 * @return   ArrayList of child comment models
 */
public ArrayList<CommentModel> getAllChildren(String uuid) {
    ArrayList<CommentModel> returnArray = new ArrayList<CommentModel>();

    try {
        HttpGet search = new HttpGet(urlChild + "_search");
        HttpResponse response = httpclient.execute(search);

        String status = response.getStatusLine().toString();
        //System.out.println(status);

        String json = getEntityContent(response);

        Type searchType = new TypeToken<ElasticSearchSearchResponse<ChildCommentModel>>() {
        }.getType();
        ElasticSearchSearchResponse<ChildCommentModel> esResponse = gson.fromJson(json, searchType);

        for (ElasticSearchResponse<ChildCommentModel> r : esResponse.getHits()) {
            ChildCommentModel model = r.getSource();
            //deleteComment(model.getPostId(),1);
            if (model.getPostId().toString().equals(uuid)) {
                returnArray.add(model);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return returnArray;
}