Example #1
0
  public TagUI create(TagUI uiBean, Principal principal) {
    Tag newTag = tMapper.toPersistenceBean(uiBean);

    User user = userRepo.findByUserName(principal.getName());
    if (user != null) {
      newTag.setCreatedBy(user);
    }
    newTag.setCreatedDate(Calendar.getInstance().getTime());

    newTag = tagRepo.save(newTag);

    logger.debug(newTag);
    return tMapper.toUIBean(newTag);
  }
  private void addTags(Question question) {
    for (String tagName : question.getTags()) {
      Tag existingTag = tagRepo.findByName(tagName);
      if (existingTag != null) {
        Query q = new Query(Criteria.where("id").is(existingTag.getId()));
        template.updateFirst(
            q, new Update().push("questions", question.getId()).inc("questionCount", 1), Tag.class);
      } else {
        Tag newTag = new Tag();
        newTag.setName(tagName);
        newTag.setCreatedBy(question.getCreatedBy());
        newTag.setCreatedDate(Calendar.getInstance().getTime());
        newTag.setQuestionCount(1);

        List<String> questions = new ArrayList<String>();
        questions.add(question.getId());
        newTag.setQuestions(questions);

        template.insert(newTag);
      }
    }
  }