private ExoSocialActivity createCommentWhenUpdateQuestion(
     Identity userIdentity, Question question) {
   ExoSocialActivityImpl comment = new ExoSocialActivityImpl();
   comment.setType(ANSWER_APP_ID);
   comment.setUserId(userIdentity.getId());
   StringBuilder commentTitle = new StringBuilder();
   String prefix = "";
   for (PropertyChangeEvent pce : question.getChangeEvent()) {
     commentTitle.append(prefix);
     prefix = "\n";
     commentTitle.append(getQuestionMessage(pce, question, comment));
   }
   comment.setTitle(commentTitle.toString());
   return comment;
 }
  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;
  }
 private ExoSocialActivity createComment(
     String title,
     String titleId,
     String spacePrettyName,
     String type,
     Identity identity,
     Map<String, String> templateParams) {
   ExoSocialActivityImpl comment = new ExoSocialActivityImpl();
   comment.setTitle(title);
   comment.setTitleId(titleId);
   comment.setUserId(identity.getId());
   comment.setType(type);
   if (spacePrettyName != null) {
     templateParams.put(SPACE_DISPLAY_NAME_PARAM, spacePrettyName);
     templateParams.put(
         BaseActivityProcessorPlugin.TEMPLATE_PARAM_TO_PROCESS, SPACE_DISPLAY_NAME_PARAM);
   }
   comment.setTemplateParams(templateParams);
   return comment;
 }
 @Override
 public void saveComment(String questionId, Comment cm, String language) {
   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);
     String message = ActivityUtils.processContent(cm.getComments());
     Identity userIdentity =
         identityM.getOrCreateIdentity(
             OrganizationIdentityProvider.NAME, cm.getCommentBy(), false);
     String activityId = faqS.getActivityIdForQuestion(questionId);
     if (activityId != null) {
       try {
         ExoSocialActivity activity = activityM.getActivity(activityId);
         ExoSocialActivityImpl comment = new ExoSocialActivityImpl();
         String commentActivityId = faqS.getActivityIdForComment(questionId, cm.getId(), language);
         Map<String, String> commentTemplateParams = new HashMap<String, String>();
         commentTemplateParams.put(LINK_KEY, cm.getId());
         if (commentActivityId != null) { // try to update activity's comment
           ExoSocialActivityImpl oldComment =
               (ExoSocialActivityImpl) activityM.getActivity(commentActivityId);
           if (oldComment != null) {
             comment = oldComment;
             comment.setTitle(message);
             comment.setTitleId("update-comment");
             activityM.updateActivity(comment);
           } else {
             commentActivityId = null;
           }
         }
         if (commentActivityId == null) { // create new activity's comment
           comment.setTemplateParams(commentTemplateParams);
           comment.setTitle(message);
           comment.setTitleId("add-comment");
           comment.setUserId(userIdentity.getId());
           updateActivity(activity, question);
           activityM.updateActivity(activity);
           activityM.saveComment(activity, comment);
           faqS.saveActivityIdForComment(questionId, cm.getId(), language, comment.getId());
         }
       } catch (Exception e) {
         LOG.debug("Run in case of activity deleted and reupdate");
         activityId = null;
       }
     }
     if (activityId == null) { // Create new activity for the question and add new comment
       saveQuestion(question, false);
       String newActivityId = faqS.getActivityIdForQuestion(questionId);
       ExoSocialActivity activity = activityM.getActivity(newActivityId);
       ExoSocialActivity comment = new ExoSocialActivityImpl();
       comment.setUserId(userIdentity.getId());
       Map<String, String> commentTemplateParams = new HashMap<String, String>();
       commentTemplateParams.put(LINK_KEY, cm.getId());
       comment.setTitle(message);
       comment.setTemplateParams(commentTemplateParams);
       activityM.saveComment(activity, comment);
     }
   } catch (Exception e) { // FQAService
     LOG.error("Can not record Activity for space when post comment ", 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());
     }
   }
 }