Beispiel #1
0
 public void execute(Event<UIVoteQuestion> event) throws Exception {
   UIVoteQuestion voteQuestion = event.getSource();
   double markVote = Double.parseDouble(event.getRequestContext().getRequestParameter(OBJECTID));
   FAQService faqService_ =
       (FAQService) PortalContainer.getInstance().getComponentInstanceOfType(FAQService.class);
   Answer answer = null;
   if (voteQuestion.language_ != null
       && voteQuestion.language_.trim().length() > 0
       && !voteQuestion.language_.equals(voteQuestion.question_.getLanguage())) {
     answer =
         faqService_.getAnswerById(
             voteQuestion.question_.getPath(), voteQuestion.answerId_, voteQuestion.language_);
     answer.setMarksVoteAnswer(markVote);
     faqService_.saveAnswer(voteQuestion.question_.getPath(), answer, voteQuestion.language_);
   } else {
     answer =
         faqService_.getAnswerById(voteQuestion.question_.getPath(), voteQuestion.answerId_);
     answer.setMarksVoteAnswer(markVote);
     faqService_.saveAnswer(voteQuestion.question_.getPath(), answer, false);
   }
   UIAnswersPortlet portlet = voteQuestion.getAncestorOfType(UIAnswersPortlet.class);
   UIPopupAction popupAction = portlet.getChild(UIPopupAction.class);
   UIQuestions questions =
       portlet.getChild(UIAnswersContainer.class).getChild(UIQuestions.class);
   // questions.setIsNotChangeLanguage() ;
   event.getRequestContext().addUIComponentToUpdateByAjax(questions);
   popupAction.deActivate();
   event.getRequestContext().addUIComponentToUpdateByAjax(popupAction);
 }
 private String getAnswerMessage(PropertyChangeEvent e, Answer answer, ExoSocialActivity comment) {
   String answerContent = ActivityUtils.processContent(answer.getResponses());
   if ("answerEdit".equals(e.getPropertyName())) {
     I18NActivityUtils.addResourceKey(comment, "answer-update-content", answerContent);
     return "Answer has been edited to: " + answerContent;
   } else if ("answerPromoted".equals(e.getPropertyName())) {
     I18NActivityUtils.addResourceKey(comment, "answer-promoted", answerContent);
     return "Comment " + answerContent + " has been promoted as an answer";
   } else if ("answerActivated".equals(e.getPropertyName())) {
     if (answer.getActivateAnswers()) {
       I18NActivityUtils.addResourceKey(comment, "answer-activated", answerContent);
       return "Answer has been activated: " + answerContent + ".";
     } else {
       I18NActivityUtils.addResourceKey(comment, "answer-unactivated", answerContent);
       return "Answer has been unactivated: " + answerContent + ".";
     }
   } else {
     if (answer.getApprovedAnswers()) {
       I18NActivityUtils.addResourceKey(comment, "answer-approved", answerContent);
       return "Answer has been approved: " + answerContent + ".";
     } else {
       I18NActivityUtils.addResourceKey(comment, "answer-disapproved", answerContent);
       return "Answer has been disapproved: " + answerContent + ".";
     }
   }
 }
  private ExoSocialActivity createCommentForAnswer(Identity userIdentity, Answer answer) {
    ExoSocialActivityImpl comment = new ExoSocialActivityImpl();
    StringBuilder commentTitle = new StringBuilder();
    comment.setUserId(userIdentity.getId());
    comment.setType(ANSWER_APP_ID);

    // Build the message corresponding with an event
    String prefix = "";
    for (PropertyChangeEvent pce : answer.getChangeEvent()) {
      commentTitle.append(prefix);
      prefix = "\n";
      commentTitle.append(getAnswerMessage(pce, answer, comment));
    }
    comment.setTitle(commentTitle.toString());
    return comment;
  }
  @Override
  public void saveAnswer(String questionId, Answer answer, boolean isNew) {
    try {
      ExoContainer exoContainer = ExoContainerContext.getCurrentContainer();
      IdentityManager identityM =
          (IdentityManager) exoContainer.getComponentInstanceOfType(IdentityManager.class);
      ActivityManager activityM =
          (ActivityManager) exoContainer.getComponentInstanceOfType(ActivityManager.class);
      FAQService faqS = (FAQService) exoContainer.getComponentInstanceOfType(FAQService.class);
      Question question = faqS.getQuestionById(questionId);
      Identity userIdentity =
          identityM.getOrCreateIdentity(
              OrganizationIdentityProvider.NAME, answer.getResponseBy(), false);
      String activityId = faqS.getActivityIdForQuestion(questionId);

      //
      String answerContent = ActivityUtils.processContent(answer.getResponses());

      if (activityId != null && answer.getApprovedAnswers()) {
        try {
          ExoSocialActivity activity = activityM.getActivity(activityId);
          ExoSocialActivity comment = createCommentForAnswer(userIdentity, answer);

          if (!comment.getTitle().equals("")) { // Case update answer or promote comment to answer
            String promotedAnswer = "Comment " + answerContent + " has been promoted as an answer";
            if (promotedAnswer.equals(comment.getTitle())) {
              // promote a comment to an answer
              updateCommentTemplateParms(comment, answer.getId());
              activityM.saveComment(activity, comment);
              faqS.saveActivityIdForAnswer(questionId, answer, comment.getId());

              // update question activity content
              updateActivity(activity, question);
              activityM.updateActivity(activity);
            } else {
              // update answer
              activityM.saveComment(activity, comment);
              String answerActivityId = faqS.getActivityIdForAnswer(questionId, answer);
              faqS.saveActivityIdForAnswer(
                  questionId, answer, answerActivityId + "," + comment.getId());
            }
          } else {
            // Case submit new answer
            comment.setTitle("Answer has been submitted: " + answerContent);
            I18NActivityUtils.addResourceKey(comment, "answer-add", answerContent);

            updateActivity(activity, question);
            activityM.updateActivity(activity);

            updateCommentTemplateParms(comment, answer.getId());
            activityM.saveComment(activity, comment);

            faqS.saveActivityIdForAnswer(questionId, answer, comment.getId());
          }

        } catch (Exception e) {
          LOG.debug("Run in case of activity deleted and reupdate");
          activityId = null;
        }
      }
      if (activityId == null) {
        saveQuestion(question, false);
        String newActivityId = faqS.getActivityIdForQuestion(question.getId());
        ExoSocialActivity activity = activityM.getActivity(newActivityId);
        ExoSocialActivity comment = createCommentForAnswer(userIdentity, answer);
        if (comment.getTitle().equals("")) {
          comment.setTitle("Answer has been submitted: " + answerContent);
          I18NActivityUtils.addResourceKey(comment, "answer-add", answerContent);
          updateCommentTemplateParms(comment, answer.getId());
        }
        activityM.saveComment(activity, comment);
      }
    } catch (Exception e) { // FQAService
      LOG.error("Can not record Activity for space when post answer ", e);
    }
  }
 @Override
 public void moveQuestions(List<String> questions, String catId) {
   ActivityManager activityM = CommonsUtils.getService(ActivityManager.class);
   FAQService faqS = CommonsUtils.getService(FAQService.class);
   IdentityManager identityM = CommonsUtils.getService(IdentityManager.class);
   for (String questionId : questions) {
     try {
       Question question = faqS.getQuestionById(questionId);
       String activityId = faqS.getActivityIdForQuestion(question.getPath());
       Identity streamOwner = null;
       Map<String, String> templateParams =
           updateTemplateParams(
               new HashMap<String, String>(),
               question.getId(),
               ActivityUtils.getQuestionRate(question),
               ActivityUtils.getNbOfAnswers(question),
               ActivityUtils.getNbOfComments(question),
               question.getLanguage(),
               question.getLink(),
               Utils.getQuestionPoint(question));
       String questionDetail = ActivityUtils.processContent(question.getDetail());
       Identity spaceIdentity = getSpaceIdentity(catId);
       if (spaceIdentity != null) {
         streamOwner = spaceIdentity;
         templateParams.put(SPACE_GROUP_ID, ActivityUtils.getSpaceGroupId(catId));
       }
       if (activityId != null) {
         ExoSocialActivity oldActivity = activityM.getActivity(activityId);
         activityM.deleteActivity(oldActivity);
         Identity userIdentity =
             identityM.getOrCreateIdentity(
                 OrganizationIdentityProvider.NAME, question.getAuthor(), false);
         ExoSocialActivity activity =
             newActivity(userIdentity, question.getQuestion(), questionDetail, templateParams);
         streamOwner = streamOwner != null ? streamOwner : userIdentity;
         activityM.saveActivityNoReturn(streamOwner, activity);
         faqS.saveActivityIdForQuestion(questionId, activity.getId());
         for (Answer answer : question.getAnswers()) {
           ExoSocialActivity comment = createCommentForAnswer(userIdentity, answer);
           String answerContent = ActivityUtils.processContent(answer.getResponses());
           comment.setTitle("Answer has been submitted: " + answerContent);
           I18NActivityUtils.addResourceKey(comment, "answer-add", answerContent);
           updateActivity(activity, question);
           activityM.updateActivity(activity);
           updateCommentTemplateParms(comment, answer.getId());
           activityM.saveComment(activity, comment);
           faqS.saveActivityIdForAnswer(questionId, answer, comment.getId());
         }
         for (Comment cm : question.getComments()) {
           String message = ActivityUtils.processContent(cm.getComments());
           ExoSocialActivityImpl comment = new ExoSocialActivityImpl();
           Map<String, String> commentTemplateParams = new HashMap<String, String>();
           commentTemplateParams.put(LINK_KEY, cm.getId());
           comment.setTemplateParams(commentTemplateParams);
           comment.setTitle(message);
           comment.setUserId(userIdentity.getId());
           updateActivity(activity, question);
           activityM.updateActivity(activity);
           activityM.saveComment(activity, comment);
           faqS.saveActivityIdForComment(
               questionId, cm.getId(), question.getLanguage(), comment.getId());
         }
       }
     } catch (Exception e) {
       LOG.error("Failed to move questions " + e.getMessage());
     }
   }
 }