com.epsi.wks.api_wks_back.User.java Source code

Java tutorial

Introduction

Here is the source code for com.epsi.wks.api_wks_back.User.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.epsi.wks.api_wks_back;

import bdd.TweetDao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import metier.Tweet;
import org.json.simple.JSONObject;
import twitter4j.PagableResponseList;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import util.UtilConfig;

/**
 *
 * @author Nautile
 */
@Path("/user/{id}")
public class User {

    @GET
    @Produces("text/plain")
    public String getUser(@PathParam("id") long userID) {
        return "user" + String.valueOf(userID);
    }

    @GET
    @Path("/following")
    @Produces("text/plain")
    public String getUserFollowings(@PathParam("id") long userID) {
        return "user_following" + String.valueOf(userID);
    }

    @GET
    @Path("/following/tweet")
    @Produces("application/json")
    public List<JSONObject> getUserFollowingsTweets(@PathParam("id") long userID) {
        Twitter twitter = UtilConfig.getTwitterInstance();

        List<JSONObject> followingTweet = new ArrayList<>();
        try {
            PagableResponseList<twitter4j.User> userList = twitter.getFriendsList(userID, -1);

            for (twitter4j.User us : userList) {
                List<Status> statusFollower = twitter.getUserTimeline(us.getId());
                for (Status sts : statusFollower) {
                    JSONObject tweet = new JSONObject();
                    tweet.put("id", sts.getId());
                    tweet.put("text", sts.getText());
                    tweet.put("screen_name", sts.getUser().getScreenName());
                    tweet.put("name", sts.getUser().getName());
                    tweet.put("image_url", sts.getUser().getProfileImageURL());
                    followingTweet.add(tweet);
                }
            }
        } catch (TwitterException e) {
        }
        System.out.println("array" + String.valueOf(followingTweet.size()));
        return followingTweet;
    }

    @DELETE
    @Path("/tweet/{tweet_id}")
    @Produces("application/json")
    public JSONObject getUserFollowingsTweets(@PathParam("id") long userID, @PathParam("tweet_id") long tweetID)
            throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        JSONObject result = new JSONObject();
        result.put("success", true);
        result.put("message", "No need");

        Tweet tweet = new Tweet(tweetID, userID, 2);

        try {
            if (!TweetDao.alreadyRegistered(tweetID, userID)) {
                TweetDao.insert(tweet);
            } else {
                TweetDao.update(tweet);
            }
            result.put("success", true);
            result.put("message", "No need");
        } catch (SQLException ex) {
            result.put("success", false);
            result.put("message", ex);
        }
        return result;
    }
}