/**
  * Get Provider Account Id.
  *
  * @param accountId
  * @param provider
  * @return
  * @throws EnMeNoResultsFoundException
  */
 public Long getProviderAccountId(String accountId, SocialProvider provider)
     throws EnMeNoResultsFoundException {
   final SocialAccount ac = this.getAccountConnection(accountId, provider);
   if (ac != null) {
     return ac.getId();
   } else {
     throw new EnMeNoResultsFoundException("connection not found");
   }
 }
 /**
  * Get Access Token.
  *
  * @param accountId
  * @param provider
  * @return
  * @throws EnMeNoResultsFoundException
  */
 public OAuth1Token getAccessToken(String accountId, SocialProvider provider)
     throws EnMeNoResultsFoundException {
   final SocialAccount ac = this.getAccountConnection(accountId, provider);
   if (ac != null) {
     final OAuth1Token oAuthToken = new OAuth1Token(ac.getAccessToken(), ac.getSecretToken());
     return oAuthToken;
   } else {
     throw new EnMeNoResultsFoundException("connection not found");
   }
 }
 /**
  * Create new social connection
  *
  * @param provider {@link SocialProvider}
  * @param accessGrant {@link AccessGrant}
  * @param socialAccountId social account id.
  * @param userAccount {@link UserAccount}
  * @param providerProfileUrl provider profile url.
  * @return
  */
 public SocialAccount updateSocialAccountConnection(
     final AccessGrant accessGrant, // OAuth2
     final String socialAccountId,
     final SocialAccount currentSocialAccount) {
   log.debug("Add Connection " + accessGrant);
   log.debug("Add Connection " + socialAccountId);
   // reference to social account.
   // connection.setSocialAccount(socialAccount);
   // store oauth provider permissions.
   if (SocialProvider.getTypeAuth(currentSocialAccount.getAccounType()).equals(TypeAuth.OAUTH1)) {
     // OAuth access token.
     // connection.setAccessToken(token.getValue());
     // OAuth1
     // connection.setSecretToken(token.getSecret());
     // TODO: pending OAUTH1.
     log.debug("pending OAUTH1 - OAuth 1 social connection is not available right now.");
   } else if (SocialProvider.getTypeAuth(currentSocialAccount.getAccounType())
       .equals(TypeAuth.OAUTH2)) {
     // OAuth2
     currentSocialAccount.setAccessToken(accessGrant.getAccessToken());
     currentSocialAccount.setRefreshToken(accessGrant.getRefreshToken());
     currentSocialAccount.setExpires(accessGrant.getExpires());
   }
   Assert.assertNotNull(socialAccountId);
   currentSocialAccount.setSocialProfileId(socialAccountId);
   this.saveOrUpdate(currentSocialAccount); // TODO: this seems not save or update properly.
   log.debug("Added Connection:{" + currentSocialAccount.toString());
   return currentSocialAccount;
 }
 /**
  * Get social accounts stats.
  *
  * @param socialAccount {@link SocialAccount}.
  * @return
  */
 @SuppressWarnings("unchecked")
 public HashMap<String, Long> getSocialAccountStats(final SocialAccount socialAccount) {
   final HashMap<String, Long> stats = new HashMap<String, Long>();
   log.debug("getSocialAccountStats " + socialAccount.getId());
   final DetachedCriteria criteria =
       DetachedCriteria.forClass(TweetPollSavedPublishedStatus.class);
   criteria.add(Restrictions.eq("socialAccount", socialAccount));
   criteria.setProjection(Projections.rowCount());
   final List tweetPollstats = getHibernateTemplate().findByCriteria(criteria);
   log.debug("getSocialAccountStats " + tweetPollstats.size());
   log.debug("getSocialAccountStats " + tweetPollstats);
   if (tweetPollstats.size() > 0) {
     stats.put("tweetpoll", Long.valueOf(tweetPollstats.get(0).toString()));
   } else {
     stats.put("tweetpoll", 0L);
   }
   // TODO: in the future we can add another stats.
   stats.put("poll", 0L);
   stats.put("survey", 0L);
   log.debug("getSocialAccountStats stats" + stats);
   return stats;
 }
 /**
  * Public Tweet Poll (OAuth method).
  *
  * @param tweetText tweet text
  * @return status of tweet
  * @throws EnMeExpcetion exception
  */
 public TweetPublishedMetadata publicTweetPoll(
     final String tweetText, final SocialAccount socialAccount, final Set<HashTag> hashtags)
     throws EnMeExpcetion {
   TweetPublishedMetadata published = new TweetPublishedMetadata();
   log.debug("publicTweetPoll:{ " + tweetText);
   if (socialAccount.getAccounType().equals(SocialProvider.TWITTER)) {
     log.debug("Publish on TWITTER");
     final TwitterAPIOperations twitterAPIOperations =
         new TwitterAPITemplate(
             EnMePlaceHolderConfigurer.getProperty("twitter.oauth.consumerSecret"),
             EnMePlaceHolderConfigurer.getProperty("twitter.oauth.consumerKey"),
             socialAccount);
     try {
       published = twitterAPIOperations.updateStatus(tweetText);
     } catch (Exception e) {
       log.error(e);
       e.printStackTrace();
     }
   } else if (socialAccount.getAccounType().equals(SocialProvider.IDENTICA)) {
     log.debug("Publish on IDENTICA");
     final IdenticaAPIOperations identicaAPIOperations =
         new IdenticaAPITemplate(
             EnMePlaceHolderConfigurer.getProperty("identica.consumer.key"),
             EnMePlaceHolderConfigurer.getProperty("identica.consumer.secret"),
             socialAccount.getAccessToken(),
             socialAccount.getSecretToken());
     try {
       log.debug("Publish on Identica............>");
       published = identicaAPIOperations.updateStatus(tweetText);
       log.debug("Publish on Identica...... " + published);
     } catch (Exception e) {
       published.setDatePublished(Calendar.getInstance().getTime());
       log.error(e);
       e.printStackTrace();
     }
   } else if (socialAccount.getAccounType().equals(SocialProvider.PLURK)) {
     log.debug("Publish on PLURK");
     final PlurkAPIOperations tumblrAPIOperations =
         new PlurkAPITemplate(
             EnMePlaceHolderConfigurer.getProperty("plurk.consumer.key"),
             EnMePlaceHolderConfigurer.getProperty("plurk.consumer.secret"),
             socialAccount.getAccessToken(),
             socialAccount.getSecretToken());
     try {
       log.debug("Publish on Identica............>");
       published = tumblrAPIOperations.updateStatus(tweetText);
       log.debug("Publish on Identica...... " + published);
     } catch (Exception e) {
       published.setDatePublished(Calendar.getInstance().getTime());
       log.error(e);
       e.printStackTrace();
     }
   } else if (socialAccount.getAccounType().equals(SocialProvider.TUMBLR)) {
     log.debug("Publish on TUMBLR");
     final TumblrAPIOperations tumblrAPIOperations =
         new TumblrAPITemplate(
             EnMePlaceHolderConfigurer.getProperty("tumblr.consumer.key"),
             EnMePlaceHolderConfigurer.getProperty("tumblr.consumer.secret"),
             socialAccount.getAccessToken(),
             socialAccount.getSecretToken());
     try {
       log.debug("Publish on TUMBLR............>");
       published = tumblrAPIOperations.updateStatus(tweetText, socialAccount, hashtags);
       log.debug("Publish on TUMBLR...... " + published);
     } catch (Exception e) {
       published.setDatePublished(Calendar.getInstance().getTime());
       log.error(e);
       e.printStackTrace();
     }
   } else if (socialAccount.getAccounType().equals(SocialProvider.FACEBOOK)) {
     log.debug("Publish on FACEBOOK");
     FacebookAPIOperations facebookAPIOperations =
         new FacebookAPITemplate(socialAccount.getAccessToken());
     try {
       log.debug("Publish on FACEBOOK............>");
       published = facebookAPIOperations.updateStatus(tweetText);
       log.debug("Publish on FACEBOOK...... " + published);
       published.setDatePublished(Calendar.getInstance().getTime());
     } catch (HttpClientErrorException e) {
       log.error("-----------------------FACEBOOK EXPIRED TOKEN----------------------- 1");
       log.error(e.getStatusCode());
       log.error(e.getResponseBodyAsString());
       log.error(e.getStatusText());
       published.setDatePublished(Calendar.getInstance().getTime());
       // refresh token point.
       // offline_access scope permission is enabled by default . In this case
       // https://developers.facebook.com/docs/authentication/permissions/
       log.error("-----------------------FACEBOOK EXPIRED TOKEN----------------------- 2");
       e.printStackTrace();
     } catch (Exception e) {
       published.setDatePublished(Calendar.getInstance().getTime());
       log.error(e);
       e.printStackTrace();
     }
   } else if (socialAccount.getAccounType().equals(SocialProvider.LINKEDIN)) {
     log.debug("Publish on LinkedIn");
     LinkedInAPIOperations linkedInAPIOperations =
         new LinkedInAPITemplate(
             EnMePlaceHolderConfigurer.getProperty("linkedIn.oauth.api.key"),
             EnMePlaceHolderConfigurer.getProperty("linkedIn.oauth.api.secret"),
             socialAccount.getAccessToken(),
             socialAccount.getSecretToken());
     try {
       log.debug("Publish on LinkedIn 1............>");
       published = linkedInAPIOperations.updateStatus(tweetText);
       published.setTextTweeted(tweetText);
       published.setDatePublished(Calendar.getInstance().getTime());
       published.setTweetId(RandomStringUtils.randomAscii(15));
       log.debug("Publish on LinkedIn 2...... " + published);
     } catch (Exception e) {
       published.setDatePublished(Calendar.getInstance().getTime());
       log.error(e);
       e.printStackTrace();
     }
   } else if (socialAccount.getAccounType().equals(SocialProvider.GOOGLE_BUZZ)) {
     BuzzAPIOperations buzzInAPIOperations = new GoogleBuzzAPITemplate(socialAccount);
     try {
       log.debug("Publish on LinkedIn............>");
       published = buzzInAPIOperations.updateStatus(tweetText);
       published.setTextTweeted(tweetText);
       published.setDatePublished(Calendar.getInstance().getTime());
       published.setTweetId(RandomStringUtils.randomAscii(15));
       log.debug("Publish on LinkedIn...... " + published);
     } catch (Exception e) {
       published.setDatePublished(Calendar.getInstance().getTime());
       log.error(e);
       e.printStackTrace();
     }
   }
   if (published != null) {
     log.debug("publicTweetPoll:s " + published.toString());
   }
   return published;
 }
 /**
  * Create new social account.
  *
  * @param socialAccountId
  * @param token
  * @param tokenSecret
  * @param expiresToken
  * @param username
  * @param socialUserProfile
  * @param socialProvider
  * @param userAccount
  * @return
  */
 public SocialAccount createSocialAccount(
     final String socialAccountId,
     final String token,
     final String tokenSecret,
     final String expiresToken,
     final String username,
     final SocialUserProfile socialUserProfile,
     final SocialProvider socialProvider,
     final UserAccount userAccount) {
   final SocialAccount socialAccount = new SocialAccount();
   socialAccount.setAccessToken(token);
   socialAccount.setSecretToken(tokenSecret);
   socialAccount.setAccount(userAccount.getAccount());
   socialAccount.setUserOwner(userAccount);
   socialAccount.setExpires(expiresToken);
   socialAccount.setAccounType(socialProvider);
   socialAccount.setAddedAccount(new Date());
   socialAccount.setVerfied(Boolean.TRUE);
   socialAccount.setSocialAccountName(socialUserProfile.getUsername());
   socialAccount.setType(SocialProvider.getTypeAuth(socialProvider));
   socialAccount.setUpgradedCredentials(new Date());
   socialAccount.setSocialProfileId(socialUserProfile.getId());
   socialAccount.setPublicProfileUrl(socialUserProfile.getProfileUrl());
   socialAccount.setPrictureUrl(socialUserProfile.getProfileImageUrl()); // TODO: repeated
   socialAccount.setProfilePictureUrl(socialUserProfile.getProfileImageUrl());
   socialAccount.setEmail(socialUserProfile.getEmail());
   socialAccount.setProfileThumbnailPictureUrl(socialUserProfile.getProfileImageUrl());
   socialAccount.setRealName(socialUserProfile.getRealName());
   this.saveOrUpdate(socialAccount);
   return socialAccount;
 }