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); }
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); } } }