/**
  * Update Answer Name by Answer Id.
  *
  * @param answerId answer Id
  * @param nameUpdated new name for answer
  * @throws EnMeExpcetion exception
  */
 public void updateAnswerByAnswerId(final Long answerId, String nameUpdated) throws EnMeExpcetion {
   final QuestionAnswer answer = getQuestionDao().retrieveAnswerById(answerId);
   if (answer == null) {
     throw new EnMeExpcetion("answer not found");
   }
   answer.setAnswer(nameUpdated);
   getQuestionDao().saveOrUpdate(answer);
 }
 /**
  * @param question
  * @return
  */
 public QuestionAnswer createAnswers(final Question question, final String answerText) {
   final QuestionAnswer answer = new QuestionAnswer();
   answer.setQuestions(question);
   answer.setAnswer(answerText);
   answer.setColor(PictureUtils.getRandomHexColor());
   this.getQuestionDao().saveOrUpdate(answer);
   log.debug("createAnswers =>" + answer.getQuestionAnswerId());
   return answer;
 }
 /**
  * Build Url Answer.
  *
  * @param anwer answer
  * @throws IOException exception
  * @throws HttpException exception
  */
 private String buildUrlAnswer(final QuestionAnswer answer, final String domain)
     throws HttpException, IOException {
   StringBuffer stringBuffer = new StringBuffer(domain);
   stringBuffer.append(getTweetPath());
   stringBuffer.append(answer.getUniqueAnserHash());
   // return getTwitterService().getTinyUrl(stringBuffer.toString());
   return null;
 }
 /**
  * Generate TweetPoll Text.
  *
  * @param tweetPoll tweetPoll
  * @param url url
  * @return tweet text
  * @throws EnMeExpcetion exception
  */
 public String generateTweetPollText(final TweetPollBean tweetPoll, final String url)
     throws EnMeExpcetion {
   String tweetQuestionText = "";
   try {
     final TweetPoll tweetPollDomain = getTweetPollDao().getTweetPollById(tweetPoll.getId());
     tweetQuestionText = tweetPollDomain.getQuestion().getQuestion();
     final List<QuestionAnswer> answers =
         getQuestionDao().getAnswersByQuestionId(tweetPollDomain.getQuestion().getQid());
     if (answers.size() == 2) {
       for (final QuestionAnswer questionsAnswers : answers) {
         tweetQuestionText +=
             " " + questionsAnswers.getAnswer() + " " + buildUrlAnswer(questionsAnswers, url);
       }
     }
   } catch (Exception e) {
     throw new EnMeExpcetion(e);
   }
   return tweetQuestionText;
 }
 /**
  * Create vote support for each tweetpoll answer.
  *
  * @param questionId
  * @param tweetPoll
  */
 public void updateTweetPollSwitchSupport(
     final TweetPoll tweetPoll, final HttpServletRequest httpServletRequest) {
   final List<QuestionAnswer> answers =
       this.getQuestionDao().getAnswersByQuestionId(tweetPoll.getQuestion().getQid());
   log.debug("updateTweetPollSwitchSupport answers size:{" + answers.size());
   // iterate answer for one question
   for (QuestionAnswer answer : answers) {
     // try to locate current switch if exist
     TweetPollSwitch tPollSwitch = getTweetPollDao().getAnswerTweetSwitch(tweetPoll, answer);
     if (tPollSwitch == null) {
       log.debug("created tweetpoll switch for tweetpoll:{" + tweetPoll.getTweetPollId());
       tPollSwitch = this.createTweetPollSwitch(tweetPoll, answer, httpServletRequest);
     } else {
       log.debug(
           "updated tweetpoll switch:{"
               + tPollSwitch.getSwitchId()
               + " for tweetpoll :{"
               + tweetPoll.getTweetPollId());
     }
     // update answer url.
     answer.setUrlAnswer(tPollSwitch.getShortUrl()); // store url without short.
     getQuestionDao().saveOrUpdate(answer);
   }
 }
  /**
   * Create {@link TweetPollSwitch}.
   *
   * @return {@link TweetPollSwitch}.
   */
  public TweetPollSwitch createTweetPollSwitch(
      final TweetPoll tweetPoll, final QuestionAnswer answer, final HttpServletRequest request) {
    final TweetPollSwitch tPollSwitch = new TweetPollSwitch();
    tPollSwitch.setAnswers(answer);
    tPollSwitch.setTweetPoll(tweetPoll);
    // FIXME: Verfy if it these code already exist.
    tPollSwitch.setCodeTweet(
        MD5Utils.shortMD5(Calendar.getInstance().getTimeInMillis() + answer.getAnswer()));
    tPollSwitch.setDateUpdated(Calendar.getInstance().getTime());

    // vote without domain
    final StringBuffer voteUrlWithoutDomain = new StringBuffer();
    voteUrlWithoutDomain.append(this.TWEETPOLL_VOTE);
    voteUrlWithoutDomain.append(tPollSwitch.getCodeTweet());
    tPollSwitch.setRelativeUrl(voteUrlWithoutDomain.toString());

    final StringBuffer completeDomain = new StringBuffer();
    if (request != null) {
      final String domain = WidgetUtil.getDomain(request);
      completeDomain.append(domain);
    }
    completeDomain.append(voteUrlWithoutDomain.toString());
    log.debug("tweet poll answer vote :{" + voteUrlWithoutDomain.toString());
    if (InternetUtils.validateUrl(completeDomain.toString())) {
      log.debug("createTweetPollSwitch: URL IS VALID");
      //			log.debug("createTweetPollSwitch: short url provider "+ answer.getProvider());
      tPollSwitch.setShortUrl(
          WidgetUtil.createShortUrl(answer.getProvider(), completeDomain.toString()));
    } else {
      log.debug("createTweetPollSwitch: url IS NOT valid");
      tPollSwitch.setShortUrl(completeDomain.toString());
      log.warn("Invalid format vote url:{" + voteUrlWithoutDomain.toString());
    }
    getTweetPollDao().saveOrUpdate(tPollSwitch);
    return tPollSwitch;
  }
 /**
  * Save Question Answer.
  *
  * @param answerBean answer
  * @throws EnMeExpcetion EnMeExpcetion
  */
 public QuestionAnswer createQuestionAnswer(
     final QuestionAnswerBean answerBean, final Question question) {
   log.debug("action createQuestionAnswer " + answerBean.toString());
   final QuestionAnswer answer = new QuestionAnswer();
   answer.setQuestions(question);
   answer.setAnswer(answerBean.getAnswers());
   answer.setProvider(answerBean.getShortUrlType());
   answer.setColor(PictureUtils.getRandomHexColor());
   answer.setUniqueAnserHash(answerBean.getAnswerHash());
   this.getQuestionDao().saveOrUpdate(answer);
   answerBean.setAnswerId(answer.getQuestionAnswerId());
   log.debug("QuestionAnswer created:{" + answerBean.toString());
   return answer;
 }