Example #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);
 }
Example #2
0
    public void execute(Event<UIVoteQuestion> event) throws Exception {
      UIVoteQuestion voteQuestion = event.getSource();
      int number = Integer.parseInt(event.getRequestContext().getRequestParameter(OBJECTID));
      String currentUser = FAQUtils.getCurrentUser();
      List<String> listUsers = new ArrayList<String>();

      if (voteQuestion.question_.getUsersVote() != null) {
        listUsers.addAll(Arrays.asList(voteQuestion.question_.getUsersVote()));
      }
      long totalVote = listUsers.size();
      double markVote =
          (voteQuestion.question_.getMarkVote() * totalVote + number) / (totalVote + 1);

      listUsers.add(currentUser + "/" + number);
      voteQuestion.question_.setMarkVote(markVote);
      voteQuestion.question_.setUsersVote(listUsers.toArray(new String[] {}));
      FAQService faqService_ =
          (FAQService) PortalContainer.getInstance().getComponentInstanceOfType(FAQService.class);
      FAQUtils.getEmailSetting(voteQuestion.faqSetting_, false, false);
      faqService_.saveQuestion(voteQuestion.question_, false, voteQuestion.faqSetting_);
      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);
    }
Example #3
0
 public static String getUserAvatar(String userName) throws Exception {
   String url = "";
   try {
     FAQService service = getFAQService();
     FileAttachment avatar = service.getUserAvatar(userName);
     if (avatar != null) {
       url = CommonUtils.getImageUrl(avatar.getPath()) + "?size=" + avatar.getSize();
     }
   } catch (Exception e) {
     log.debug("Failed to get user avatar of user: " + userName, e);
   }
   return (isFieldEmpty(url)) ? org.exoplatform.faq.service.Utils.DEFAULT_AVATAR_URL : url;
 }
 private void refreshActivity(String questionId, String questionActivityId) {
   try {
     ExoContainer exoContainer = ExoContainerContext.getCurrentContainer();
     FAQService faqS = (FAQService) exoContainer.getComponentInstanceOfType(FAQService.class);
     Question question = faqS.getQuestionById(questionId);
     ActivityManager activityM =
         (ActivityManager) exoContainer.getComponentInstanceOfType(ActivityManager.class);
     ExoSocialActivity activity = activityM.getActivity(questionActivityId);
     updateActivity(activity, question);
     activityM.updateActivity(activity);
   } catch (Exception e) {
     LOG.debug("Fail to refresh activity " + e.getMessage());
   }
 }
  public void unVoteQuestion(String questionId) {
    try {
      ExoContainer exoContainer = ExoContainerContext.getCurrentContainer();
      FAQService faqS = (FAQService) exoContainer.getComponentInstanceOfType(FAQService.class);
      Question question = faqS.getQuestionById(questionId);

      // No event is created because old and new values are equal but we must update the activity's
      // content
      question.setEditedQuestionRating(question.getMarkVote());
      saveQuestion(question, false);
    } catch (Exception e) {
      LOG.debug("Fail to unvote question " + e.getMessage());
    }
  }
Example #6
0
 /**
  * Find category which is already exist.<br>
  * for example: when you are standing in category D in path: Root\A\B\C\D, you do some action (add
  * new category, add question, go out to category C or B) but another moderator delete category C
  * (or B, A). Then this function will be use to find the nearest category with category D (which
  * is exist) and move you into this category.<br>
  * <u>Detail:</u><br>
  * the first, system get category C, if C is exist, you will be moved into C else jump to B and
  * test again.<br>
  * This processing is done until find a category already exist.
  *
  * @param faqService_ FAQ Service
  * @param fAQContainer UIAnswersContainer this component is used to updated data
  * @param sessionProvider SessionProvider
  * @throws Exception
  */
 public static void findCateExist(FAQService faqService_, UIAnswersContainer fAQContainer)
     throws Exception {
   UIBreadcumbs breadcumbs = fAQContainer.findFirstComponentOfType(UIBreadcumbs.class);
   String pathCate = "";
   for (String path : breadcumbs.pathList_.get(breadcumbs.pathList_.size() - 1).split("/")) {
     if (path.equals("FAQService")) {
       pathCate = path;
       continue;
     }
     try {
       faqService_.getCategoryById(path);
       if (pathCate.trim().length() > 0) pathCate += "/";
       pathCate += path;
     } catch (Exception pathExc) {
       UIQuestions questions = fAQContainer.findFirstComponentOfType(UIQuestions.class);
       try {
         breadcumbs.setUpdataPath(pathCate);
       } catch (Exception exc) {
         log.debug("Setting update path fail: " + exc.getMessage(), exc);
       }
       if (pathCate.indexOf("/") > 0) {
         questions.setCategoryId(pathCate.substring(pathCate.lastIndexOf("/") + 1));
       } else {
         questions.categoryId_ = null;
         questions.setListObject();
         // questions.setIsNotChangeLanguage() ;
       }
       fAQContainer.findFirstComponentOfType(UICategories.class).setPathCategory(pathCate);
       break;
     }
   }
 }
 public void removeAnswer(String questionPath, String answerActivityId) {
   try {
     ExoContainer exoContainer = ExoContainerContext.getCurrentContainer();
     FAQService faqS = (FAQService) exoContainer.getComponentInstanceOfType(FAQService.class);
     String questionActivityId = faqS.getActivityIdForQuestion(questionPath);
     ActivityManager activityM =
         (ActivityManager) exoContainer.getComponentInstanceOfType(ActivityManager.class);
     for (String id : answerActivityId.split(",")) {
       ExoSocialActivity activity = activityM.getActivity(id);
       if (activity != null) {
         activityM.deleteComment(questionActivityId, id);
       }
     }
     refreshActivity(questionPath, questionActivityId);
   } catch (Exception e) {
     LOG.debug("Fail to remove comment when remove question's answer " + e.getMessage());
   }
 }
  @Override
  public void saveQuestion(Question question, 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);
      Identity userIdentity =
          identityM.getOrCreateIdentity(
              OrganizationIdentityProvider.NAME, question.getAuthor(), false);
      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 activityId = faqS.getActivityIdForQuestion(question.getId());

      String questionDetail = ActivityUtils.processContent(question.getDetail());
      // in case deleted activity, if isUpdate, we will re-create new activity and add a comment
      // associated
      boolean isUpdate = false;
      // UserHelper.checkValueUser(values)
      if (activityId != null) {
        isUpdate = true;
        try {
          ExoSocialActivity activity = activityM.getActivity(activityId);
          if (UserHelper.getUserByUserId(question.getAuthor()) == null) {
            userIdentity = identityM.getIdentity(activity.getPosterId(), false);
          }
          activity.setTitle(CommonUtils.decodeSpecialCharToHTMLnumber(question.getQuestion()));
          activity.setBody(questionDetail);
          activity.setTemplateParams(templateParams);
          activityM.updateActivity(activity);

          ExoSocialActivity comment = createCommentWhenUpdateQuestion(userIdentity, question);
          if (!"".equals(comment.getTitle())) {
            activityM.saveComment(activity, comment);
          }
        } catch (Exception e) {
          LOG.debug("Run in case of activity deleted and reupdate");
          activityId = null;
        }
      }
      if (activityId == null) {
        Identity streamOwner = null;
        String catId =
            (String)
                faqS.readQuestionProperty(
                    question.getId(), FAQNodeTypes.EXO_CATEGORY_ID, String.class);
        Identity spaceIdentity = getSpaceIdentity(catId);
        if (spaceIdentity != null) {
          // publish the activity in the space stream.
          streamOwner = spaceIdentity;
          templateParams.put(SPACE_GROUP_ID, ActivityUtils.getSpaceGroupId(catId));
        }
        List<String> categoryIds = faqS.getCategoryPath(catId);
        Collections.reverse(categoryIds);
        if (streamOwner == null) {
          streamOwner = userIdentity;
        }
        ExoSocialActivity activity =
            newActivity(userIdentity, question.getQuestion(), questionDetail, templateParams);
        activityM.saveActivityNoReturn(streamOwner, activity);
        faqS.saveActivityIdForQuestion(question.getId(), activity.getId());

        if (isUpdate) {
          ExoSocialActivity comment = createCommentWhenUpdateQuestion(userIdentity, question);
          if (!"".equals(comment.getTitle())) {
            activityM.saveComment(activity, comment);
          }
        }
      }
    } catch (Exception e) { // FQAService
      LOG.error("Can not record Activity for space when add new question ", e);
    }
  }
 @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 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());
     }
   }
 }