package kr.wisestone.owl.service.impl;
|
|
import kr.wisestone.owl.constant.MsgConstants;
|
import kr.wisestone.owl.domain.Issue;
|
import kr.wisestone.owl.domain.IssueComment;
|
import kr.wisestone.owl.domain.User;
|
import kr.wisestone.owl.exception.OwlRuntimeException;
|
import kr.wisestone.owl.repository.IssueCommentRepository;
|
import kr.wisestone.owl.service.IssueCommentService;
|
import kr.wisestone.owl.service.IssueService;
|
import kr.wisestone.owl.service.WorkspaceService;
|
import kr.wisestone.owl.util.ConvertUtil;
|
import kr.wisestone.owl.vo.IssueCommentVo;
|
import kr.wisestone.owl.vo.UserVo;
|
import kr.wisestone.owl.web.form.IssueCommentForm;
|
import org.apache.commons.lang3.StringUtils;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import java.util.List;
|
|
@Service
|
public class IssueCommentServiceImpl extends AbstractServiceImpl<IssueComment, Long, JpaRepository<IssueComment, Long>>
|
implements IssueCommentService {
|
|
static final Logger log = LoggerFactory.getLogger(IssueCommentServiceImpl.class);
|
|
@Autowired
|
private IssueCommentRepository issueCommentRepository;
|
|
@Autowired
|
private IssueService issueService;
|
|
@Autowired
|
private WorkspaceService workspaceService;
|
|
@Override
|
protected JpaRepository<IssueComment, Long> getRepository() {
|
return this.issueCommentRepository;
|
}
|
|
// 댓글을 등록한다.
|
@Override
|
@Transactional
|
public IssueComment addIssueComment(IssueCommentForm issueCommentForm) {
|
// 사용하고 있는 업무 공간이 활성 상태인지 확인한다. 사용 공간에서 로그인한 사용자가 비활성인지 확인한다.
|
this.workspaceService.checkUseWorkspace();
|
|
IssueComment issueComment = ConvertUtil.copyProperties(issueCommentForm, IssueComment.class);
|
|
this.verifyComment(issueCommentForm.getDescription());
|
|
Issue issue = this.issueService.getIssue(issueCommentForm.getIssueId());
|
issueComment.setIssue(issue);
|
issueComment.setWorkspace(issue.getProject().getWorkspace());
|
|
this.issueCommentRepository.saveAndFlush(issueComment);
|
|
return issueComment;
|
}
|
|
// 댓글을 등록한다. (api용)
|
@Override
|
@Transactional
|
public IssueComment addIssueComment(IssueCommentForm issueCommentForm, User user) {
|
// 사용하고 있는 업무 공간이 활성 상태인지 확인한다. 사용 공간에서 로그인한 사용자가 비활성인지 확인한다.
|
this.workspaceService.checkUseWorkspace(user);
|
|
IssueComment issueComment = ConvertUtil.copyProperties(issueCommentForm, IssueComment.class);
|
|
this.verifyComment(issueCommentForm.getDescription());
|
|
Issue issue = this.issueService.getIssue(issueCommentForm.getIssueId());
|
issueComment.setIssue(issue);
|
issueComment.setWorkspace(issue.getProject().getWorkspace());
|
|
this.issueCommentRepository.saveAndFlush(issueComment);
|
|
return issueComment;
|
}
|
|
private void verifyComment(String comment) {
|
if (StringUtils.isEmpty(comment)) {
|
throw new OwlRuntimeException(
|
this.messageAccessor.getMessage(MsgConstants.ISSUE_COMMENT_NOT_COMMENT));
|
}
|
|
if (comment.length() > 300) {
|
throw new OwlRuntimeException(
|
this.messageAccessor.getMessage(MsgConstants.ISSUE_COMMENT_MAX_LENGTH_OUT));
|
}
|
}
|
|
// 댓글을 삭제한다.
|
@Override
|
@Transactional
|
public void removeIssueComments(IssueCommentForm issueCommentForm) {
|
// 사용하고 있는 업무 공간이 활성 상태인지 확인한다. 사용 공간에서 로그인한 사용자가 비활성인지 확인한다.
|
this.workspaceService.checkUseWorkspace();
|
|
if (issueCommentForm.getRemoveIds().size() < 1) {
|
throw new OwlRuntimeException(MsgConstants.ISSUE_COMMENT_REMOVE_NOT_SELECT);
|
}
|
|
for (Long issueCommentId : issueCommentForm.getRemoveIds()) {
|
this.removeIssueComments(issueCommentId);
|
}
|
|
this.issueCommentRepository.flush();
|
}
|
|
// 댓글을 삭제한다.
|
private void removeIssueComments(Long issueCommentId) {
|
IssueComment issueComment = this.getIssueComment(issueCommentId);
|
// 댓글 삭제 권한 체크
|
this.checkRemovePermission(issueComment);
|
|
this.issueCommentRepository.delete(issueComment);
|
}
|
|
// 댓글 삭제 권한이 있는지 체크한다.
|
private void checkRemovePermission(IssueComment issueComment) {
|
UserVo userVo = this.webAppUtil.getLoginUser();
|
|
if (!issueComment.getRegisterId().equals(userVo.getId())) {
|
throw new OwlRuntimeException(
|
this.messageAccessor.getMessage(MsgConstants.ISSUE_COMMENT_NOT_REMOVE_PERMISSION));
|
}
|
}
|
|
// 댓글 아이디로 댓글을 조회한다.
|
@Override
|
@Transactional(readOnly = true)
|
public IssueComment getIssueComment(Long id ) {
|
if (id == null) {
|
throw new OwlRuntimeException(
|
this.messageAccessor.getMessage(MsgConstants.ISSUE_COMMENT_NOT_EXIST));
|
}
|
|
IssueComment issueComment = this.findOne(id);
|
|
if (issueComment == null) {
|
throw new OwlRuntimeException(
|
this.messageAccessor.getMessage(MsgConstants.ISSUE_COMMENT_NOT_EXIST));
|
}
|
|
return issueComment;
|
}
|
|
// 이슈에 등록된 댓글 목록을 조회한다.
|
@Override
|
@Transactional(readOnly = true)
|
public List<IssueCommentVo> findIssueComment(Long issueId) {
|
List<IssueCommentVo> issueCommentVos = this.issueCommentRepository.findByIssueId(issueId);
|
Issue issue = this.issueService.getIssue(issueId);
|
if (issueCommentVos != null && issueCommentVos.size() > 0) {
|
for (IssueCommentVo issueCommentVo : issueCommentVos) {
|
issueCommentVo.setTitle(issue.getTitle());
|
}
|
}
|
return issueCommentVos;
|
}
|
|
}
|