package autoChirp.tweeting;_x000D_
_x000D_
import java.net.MalformedURLException;_x000D_
_x000D_
import javax.annotation.PostConstruct;_x000D_
_x000D_
import org.springframework.beans.factory.annotation.Value;_x000D_
import org.springframework.core.io.Resource;_x000D_
import org.springframework.core.io.UrlResource;_x000D_
import org.springframework.social.twitter.api.StatusDetails;_x000D_
import org.springframework.social.twitter.api.TweetData;_x000D_
import org.springframework.social.twitter.api.Twitter;_x000D_
import org.springframework.social.twitter.api.impl.TwitterTemplate;_x000D_
import org.springframework.stereotype.Component;_x000D_
_x000D_
import autoChirp.DBConnector;_x000D_
import autoChirp.tweetCreation.Tweet;_x000D_
import autoChirp.tweetCreation.TweetFactory;_x000D_
_x000D_
/**_x000D_
 * This class executes the actual twitter status-update using Spring Social_x000D_
 * Twitter API_x000D_
 *_x000D_
 * @author Alena Geduldig_x000D_
 *_x000D_
 */_x000D_
@Component_x000D_
public class TwitterConnection {_x000D_
_x000D_
	@Value("${spring.social.twitter.appId}")_x000D_
	private String appIDProp;_x000D_
_x000D_
	@Value("${spring.social.twitter.appSecret}")_x000D_
	private String appSecretProp;_x000D_
_x000D_
	@Value("${autochirp.domain}")_x000D_
	private String appDomainProp;_x000D_
_x000D_
	@Value("${autochirp.parser.dateformats}")_x000D_
	private String dateformatsProp;_x000D_
_x000D_
	private static String appID;_x000D_
	private static String appSecret;_x000D_
	private static String appDomain;_x000D_
	private static String dateformats;_x000D_
_x000D_
	/**_x000D_
	 * get appToken and appSecret_x000D_
	 */_x000D_
	@PostConstruct_x000D_
	public void initializeConnection() {_x000D_
		this.appID = appIDProp;_x000D_
		this.appSecret = appSecretProp;_x000D_
		this.appDomain = appDomainProp;_x000D_
		this.dateformats = dateformatsProp;_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * updates the users twitter-status to the tweets content. 1. reads the_x000D_
	 * tweet with the given tweetID from the database 2. checks if the related_x000D_
	 * tweetGroup is still enabled and tweet wasn't tweeted already 3. reads the_x000D_
	 * users oAuthToken and oAuthTokenSecret from the database 4. updates the_x000D_
	 * users twitter status to the tweets tweetContent_x000D_
	 *_x000D_
	 * @param userID_x000D_
	 *            userID_x000D_
	 * @param tweetID_x000D_
	 *            tweetID_x000D_
	 */_x000D_
	public void run(int userID, int tweetID) {_x000D_
		// read tweet from DB_x000D_
		Tweet toTweet = DBConnector.getTweetByID(tweetID, userID);_x000D_
_x000D_
		// check if tweet exists_x000D_
		if (toTweet == null) {_x000D_
			return;_x000D_
		}_x000D_
		// check if tweetGroup is still enabled_x000D_
		if (!DBConnector.isEnabledGroup(toTweet.groupID, userID)) {_x000D_
			return;_x000D_
		}_x000D_
		// check if tweet was not tweeted already_x000D_
		if (toTweet.tweeted) {_x000D_
			return;_x000D_
		}_x000D_
		_x000D_
		_x000D_
_x000D_
		// read userConfig (oAuthToken, tokenSecret) from DB_x000D_
		String[] userConfig = DBConnector.getUserConfig(userID);_x000D_
		String token = userConfig[1];_x000D_
		String tokenSecret = userConfig[2];_x000D_
_x000D_
		// tweeting_x000D_
		Twitter twitter = new TwitterTemplate(appID, appSecret, token, tokenSecret);_x000D_
_x000D_
		// TweetData tweetData = new TweetData(toTweet.content);_x000D_
		String tweet = toTweet.content;_x000D_
		TweetData tweetData = new TweetData(tweet);_x000D_
		_x000D_
		//check if tweet is a reply_x000D_
		long replyID = 0;_x000D_
		if(DBConnector.isThreadedGroup(toTweet.groupID, userID)){_x000D_
							System.out.println("is threaded Group");_x000D_
			replyID = DBConnector.getReplyID(tweetID, toTweet.groupID, userID);_x000D_
							System.out.println("replyID: " + replyID);_x000D_
		}_x000D_
_x000D_
		// add image_x000D_
		if (toTweet.imageUrl != null) {_x000D_
			try {_x000D_
				Resource img = new UrlResource(toTweet.imageUrl);_x000D_
				tweetData = tweetData.withMedia(img);_x000D_
			} catch (MalformedURLException e) {_x000D_
				tweetData = new TweetData(tweet + " " + toTweet.imageUrl);_x000D_
			}_x000D_
		}_x000D_
_x000D_
		// add flashcard_x000D_
		if (toTweet.adjustedLength() > Tweet.MAX_TWEET_LENGTH) {_x000D_
			String flashcard = appDomain + "/flashcard/" + toTweet.tweetID;_x000D_
_x000D_
			try {_x000D_
				tweetData = new TweetData(toTweet.trimmedContent()).withMedia(new UrlResource(flashcard));_x000D_
			} catch (MalformedURLException e) {_x000D_
				tweetData = new TweetData(toTweet.trimmedContent()+" " + flashcard);_x000D_
			}_x000D_
		}_x000D_
_x000D_
		// add Geo-Locations_x000D_
		if (toTweet.longitude != 0 || toTweet.latitude != 0) {_x000D_
			tweetData = tweetData.atLocation(toTweet.longitude, toTweet.latitude).displayCoordinates(true);_x000D_
		}_x000D_
		_x000D_
	_x000D_
_x000D_
		// update Tweet-Status in DB_x000D_
		DBConnector.flagAsTweeted(tweetID, userID);_x000D_
_x000D_
		// update Status_x000D_
		org.springframework.social.twitter.api.Tweet statusUpdate = null;_x000D_
		if(replyID > 0){_x000D_
			try{_x000D_
				statusUpdate = twitter.timelineOperations().updateStatus(tweetData.inReplyToStatus(replyID));_x000D_
			}_x000D_
			catch(Exception e){_x000D_
				e.printStackTrace();_x000D_
			}_x000D_
			DBConnector.addStatusID(tweetID, statusUpdate.getId());_x000D_
		}_x000D_
		else{_x000D_
			try{_x000D_
				statusUpdate = twitter.timelineOperations().updateStatus(tweetData);_x000D_
			}_x000D_
			catch(Exception e){_x000D_
				e.printStackTrace();_x000D_
			}_x000D_
			DBConnector.addStatusID(tweetID, statusUpdate.getId());_x000D_
		}_x000D_
_x000D_
	}_x000D_
_x000D_
}_x000D_

/**_x000D_
	 * get appToken and appSecret_x000D_
	 */
package autoChirp.tweeting;_x000D_
_x000D_
import java.net.MalformedURLException;_x000D_
_x000D_
import javax.annotation.PostConstruct;_x000D_
_x000D_
import org.springframework.beans.factory.annotation.Value;_x000D_
import org.springframework.core.io.Resource;_x000D_
import org.springframework.core.io.UrlResource;_x000D_
import org.springframework.social.twitter.api.StatusDetails;_x000D_
import org.springframework.social.twitter.api.TweetData;_x000D_
import org.springframework.social.twitter.api.Twitter;_x000D_
import org.springframework.social.twitter.api.impl.TwitterTemplate;_x000D_
import org.springframework.stereotype.Component;_x000D_
_x000D_
import autoChirp.DBConnector;_x000D_
import autoChirp.tweetCreation.Tweet;_x000D_
import autoChirp.tweetCreation.TweetFactory;_x000D_
_x000D_
/**_x000D_
 * This class executes the actual twitter status-update using Spring Social_x000D_
 * Twitter API_x000D_
 *_x000D_
 * @author Alena Geduldig_x000D_
 *_x000D_
 */_x000D_
@Component_x000D_
public class TwitterConnection {_x000D_
_x000D_
	@Value("${spring.social.twitter.appId}")_x000D_
	private String appIDProp;_x000D_
_x000D_
	@Value("${spring.social.twitter.appSecret}")_x000D_
	private String appSecretProp;_x000D_
_x000D_
	@Value("${autochirp.domain}")_x000D_
	private String appDomainProp;_x000D_
_x000D_
	@Value("${autochirp.parser.dateformats}")_x000D_
	private String dateformatsProp;_x000D_
_x000D_
	private static String appID;_x000D_
	private static String appSecret;_x000D_
	private static String appDomain;_x000D_
	private static String dateformats;_x000D_
_x000D_
	/**_x000D_
	 * get appToken and<SUF>*/_x000D_
	@PostConstruct_x000D_
	public void initializeConnection() {_x000D_
		this.appID = appIDProp;_x000D_
		this.appSecret = appSecretProp;_x000D_
		this.appDomain = appDomainProp;_x000D_
		this.dateformats = dateformatsProp;_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * updates the users twitter-status to the tweets content. 1. reads the_x000D_
	 * tweet with the given tweetID from the database 2. checks if the related_x000D_
	 * tweetGroup is still enabled and tweet wasn't tweeted already 3. reads the_x000D_
	 * users oAuthToken and oAuthTokenSecret from the database 4. updates the_x000D_
	 * users twitter status to the tweets tweetContent_x000D_
	 *_x000D_
	 * @param userID_x000D_
	 *            userID_x000D_
	 * @param tweetID_x000D_
	 *            tweetID_x000D_
	 */_x000D_
	public void run(int userID, int tweetID) {_x000D_
		// read tweet from DB_x000D_
		Tweet toTweet = DBConnector.getTweetByID(tweetID, userID);_x000D_
_x000D_
		// check if tweet exists_x000D_
		if (toTweet == null) {_x000D_
			return;_x000D_
		}_x000D_
		// check if tweetGroup is still enabled_x000D_
		if (!DBConnector.isEnabledGroup(toTweet.groupID, userID)) {_x000D_
			return;_x000D_
		}_x000D_
		// check if tweet was not tweeted already_x000D_
		if (toTweet.tweeted) {_x000D_
			return;_x000D_
		}_x000D_
		_x000D_
		_x000D_
_x000D_
		// read userConfig (oAuthToken, tokenSecret) from DB_x000D_
		String[] userConfig = DBConnector.getUserConfig(userID);_x000D_
		String token = userConfig[1];_x000D_
		String tokenSecret = userConfig[2];_x000D_
_x000D_
		// tweeting_x000D_
		Twitter twitter = new TwitterTemplate(appID, appSecret, token, tokenSecret);_x000D_
_x000D_
		// TweetData tweetData = new TweetData(toTweet.content);_x000D_
		String tweet = toTweet.content;_x000D_
		TweetData tweetData = new TweetData(tweet);_x000D_
		_x000D_
		//check if tweet is a reply_x000D_
		long replyID = 0;_x000D_
		if(DBConnector.isThreadedGroup(toTweet.groupID, userID)){_x000D_
							System.out.println("is threaded Group");_x000D_
			replyID = DBConnector.getReplyID(tweetID, toTweet.groupID, userID);_x000D_
							System.out.println("replyID: " + replyID);_x000D_
		}_x000D_
_x000D_
		// add image_x000D_
		if (toTweet.imageUrl != null) {_x000D_
			try {_x000D_
				Resource img = new UrlResource(toTweet.imageUrl);_x000D_
				tweetData = tweetData.withMedia(img);_x000D_
			} catch (MalformedURLException e) {_x000D_
				tweetData = new TweetData(tweet + " " + toTweet.imageUrl);_x000D_
			}_x000D_
		}_x000D_
_x000D_
		// add flashcard_x000D_
		if (toTweet.adjustedLength() > Tweet.MAX_TWEET_LENGTH) {_x000D_
			String flashcard = appDomain + "/flashcard/" + toTweet.tweetID;_x000D_
_x000D_
			try {_x000D_
				tweetData = new TweetData(toTweet.trimmedContent()).withMedia(new UrlResource(flashcard));_x000D_
			} catch (MalformedURLException e) {_x000D_
				tweetData = new TweetData(toTweet.trimmedContent()+" " + flashcard);_x000D_
			}_x000D_
		}_x000D_
_x000D_
		// add Geo-Locations_x000D_
		if (toTweet.longitude != 0 || toTweet.latitude != 0) {_x000D_
			tweetData = tweetData.atLocation(toTweet.longitude, toTweet.latitude).displayCoordinates(true);_x000D_
		}_x000D_
		_x000D_
	_x000D_
_x000D_
		// update Tweet-Status in DB_x000D_
		DBConnector.flagAsTweeted(tweetID, userID);_x000D_
_x000D_
		// update Status_x000D_
		org.springframework.social.twitter.api.Tweet statusUpdate = null;_x000D_
		if(replyID > 0){_x000D_
			try{_x000D_
				statusUpdate = twitter.timelineOperations().updateStatus(tweetData.inReplyToStatus(replyID));_x000D_
			}_x000D_
			catch(Exception e){_x000D_
				e.printStackTrace();_x000D_
			}_x000D_
			DBConnector.addStatusID(tweetID, statusUpdate.getId());_x000D_
		}_x000D_
		else{_x000D_
			try{_x000D_
				statusUpdate = twitter.timelineOperations().updateStatus(tweetData);_x000D_
			}_x000D_
			catch(Exception e){_x000D_
				e.printStackTrace();_x000D_
			}_x000D_
			DBConnector.addStatusID(tweetID, statusUpdate.getId());_x000D_
		}_x000D_
_x000D_
	}_x000D_
_x000D_
}_x000D_
