public QuestionUI create(QuestionUI uiBean, Principal principal) {
    Question newQ = qMapper.toPersistenceBean(uiBean);

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

    newQ = qaRepo.save(newQ);
    addTags(newQ);

    logger.debug(newQ);
    return qMapper.toUIBean(newQ);
  }
  public QuestionUI addQuestionComment(CommentUI comment, Principal principal) {
    QuestionUI result = new QuestionUI();
    Comment newComment = cMapper.toPersistenceBean(comment);
    newComment.setId(UUID.randomUUID().toString());
    newComment.setCreatedDate(Calendar.getInstance().getTime());
    User user = userRepo.findByUserName(principal.getName());
    if (user != null) {
      newComment.setCreatedBy(user);
    }

    Query q = new Query(Criteria.where("id").is(comment.getQuestionId()));
    Update updateQ = new Update().set("comments." + newComment.getId(), newComment);
    template.updateFirst(q, updateQ, Question.class);

    result = qMapper.toUIBean(template.findOne(q, Question.class));
    return result;
  }
  public QuestionUI addVote(String questionId, Principal principal) {
    QuestionUI result = new QuestionUI();
    User user = userRepo.findByUserName(principal.getName());

    Query q = new Query(Criteria.where("id").is(questionId));
    if (user != null) {
      Vote vote = new Vote();
      vote.setId(user.getId());
      vote.setUser(user.getUserName());
      vote.setCreatedDate(Calendar.getInstance().getTime());

      Update updateQ = new Update().set("votes." + user.getId(), vote);
      template.updateFirst(q, updateQ, Question.class);
    }

    result = qMapper.toUIBean(template.findOne(q, Question.class));
    return result;
  }
  public QuestionUI addBookmark(String questionId, Principal principal) {
    QuestionUI result = new QuestionUI();
    User user = userRepo.findByUserName(principal.getName());

    Query q = new Query(Criteria.where("id").is(questionId));
    if (user != null) {
      Bookmark bookmark = new Bookmark();
      bookmark.setId(user.getId());
      bookmark.setUser(user.getUserName());
      bookmark.setQuestionId(questionId);
      bookmark.setCreatedDate(Calendar.getInstance().getTime());

      template.updateFirst(
          new Query(Criteria.where("id").is(user.getId())),
          new Update().set("bookmarks." + questionId, bookmark),
          User.class);

      Update updateQ = new Update().set("bookmarks." + user.getId(), bookmark);
      template.updateFirst(q, updateQ, Question.class);
    }

    result = qMapper.toUIBean(template.findOne(q, Question.class));
    return result;
  }
  public QuestionUI update(QuestionUI question) {
    Question existing = qaRepo.findBySubject(question.getSubject());

    return qMapper.toUIBean(qaRepo.save(existing));
  }
  public QuestionUI findById(String id, Principal principal) {
    template.updateFirst(
        new Query(Criteria.where("id").is(id)), new Update().inc("viewCount", 1), Question.class);

    return qMapper.toUIBean(qaRepo.findById(id));
  }
  public List<QuestionUI> findById(List<String> id) {
    List<Question> questions = new ArrayList<Question>();
    questions = template.find(new Query(Criteria.where("id").in(id)), Question.class);

    return qMapper.toUIBean(questions);
  }
 public QuestionUI findById(String id) {
   return qMapper.toUIBean(qaRepo.findById(id));
 }
 public QuestionUI findBySubject(String subject) {
   return qMapper.toUIBean(qaRepo.findBySubject(subject));
 }
 public Page<QuestionUI> findAll(Pageable pageable) {
   return qMapper.toUIBean(qaRepo.findAll(pageable), pageable);
 }
 public List<QuestionUI> findAll() {
   return qMapper.toUIBean(qaRepo.findAll());
 }