package kr.wisestone.owl.service.impl; import com.google.common.collect.Lists; import kr.wisestone.owl.constant.MsgConstants; import kr.wisestone.owl.domain.*; import kr.wisestone.owl.domain.enumType.IssueStatusType; import kr.wisestone.owl.domain.enumType.ProjectType; import kr.wisestone.owl.exception.OwlRuntimeException; import kr.wisestone.owl.mapper.WorkflowDepartmentMapper; import kr.wisestone.owl.repository.WorkflowDepartmentRepository; import kr.wisestone.owl.repository.WorkflowTransitionRepository; import kr.wisestone.owl.service.*; import kr.wisestone.owl.util.ConvertUtil; import kr.wisestone.owl.util.MapUtil; import kr.wisestone.owl.vo.*; import kr.wisestone.owl.web.condition.WorkflowCondition; import kr.wisestone.owl.web.condition.WorkflowDepartmentCondition; 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.*; @Service public class WorkflowDepartmentServiceImpl extends AbstractServiceImpl> implements WorkflowDepartmentService { private static final Logger log = LoggerFactory.getLogger(WorkflowDepartmentServiceImpl.class); @Autowired private WorkflowDepartmentMapper workflowDepartmentMapper; @Autowired private WorkflowDepartmentRepository workflowDepartmentRepository; @Autowired private IssueStatusService issueStatusService; @Autowired private DepartmentService departmentService; @Autowired private IssueTypeService issueTypeService; @Autowired private ProjectRoleService projectRoleService; @Override protected JpaRepository getRepository() { return workflowDepartmentRepository; } @Override public WorkflowDepartment getWorkflowDepartment(Long id) { if (id == null) { throw new OwlRuntimeException( this.messageAccessor.getMessage(MsgConstants.WORKFLOW_DEPARTMENT_NOT_EXIST)); } WorkflowDepartment workflowDepartment = this.findOne(id); if (workflowDepartment == null) { throw new OwlRuntimeException( this.messageAccessor.getMessage(MsgConstants.WORKFLOW_DEPARTMENT_NOT_EXIST)); } return workflowDepartment; } // 담당 부서정보를 조회한다. public List find(Long workflowId, Long issueStatusId) { WorkflowDepartmentCondition condition = new WorkflowDepartmentCondition(); condition.setWorkflowId(workflowId); condition.setIssueStatusId(issueStatusId); return find(condition); } public List find(Long workflowId) { return this.workflowDepartmentRepository.findByWorkflowId(workflowId); } @Override // 워크플로우의 해당 이슈상태를 가진 워크플로우 부서 가져오기(Ready) public List findFirstDepartmentIds(Workflow workflow) { List departmentIds = Lists.newArrayList(); IssueStatus issueStatus = issueStatusService.findByIssueStatusTypeIsReady(workflow); // 워크플로우 상태에 따른 담당부서 가져오기 if (issueStatus != null) { WorkflowDepartmentCondition workflowDepartmentCondition = new WorkflowDepartmentCondition(); workflowDepartmentCondition.setIssueStatusId(issueStatus.getId()); workflowDepartmentCondition.setWorkflowId(workflow.getId()); List workflowDepartmentVos = this.find(workflowDepartmentCondition); for (WorkflowDepartmentVo workflowDepartmentVo : workflowDepartmentVos) { departmentIds.add(workflowDepartmentVo.getDepartmentVo().getId()); } } return departmentIds; } // 담당 부서정보를 조회한다. @Transactional(readOnly = true) @Override public List find(WorkflowDepartmentCondition condition) { List workflowDepartmentList = null; List workflowDepartmentVos = Lists.newArrayList(); //워크플로우, 이슈 상태별 담당부서 조회 if (condition.getWorkflowId() != null && condition.getIssueStatusId() != null) { workflowDepartmentList = this.workflowDepartmentRepository.findByWorkflowIdAndIssueStatusId(condition.getWorkflowId(), condition.getIssueStatusId()); for(int i = 0; i < workflowDepartmentList.size(); i++){ WorkflowDepartment workflowDepartment = workflowDepartmentList.get(i); WorkflowDepartmentVo workflowDepartmentVo = ConvertUtil.copyProperties(workflowDepartment, WorkflowDepartmentVo.class); DepartmentVo departmentVo = ConvertUtil.copyProperties(workflowDepartment.getDepartment(), DepartmentVo.class); departmentVo.setByName(departmentVo.getDepartmentName()); workflowDepartmentVo.setDepartmentVo(departmentVo); workflowDepartmentVos.add(workflowDepartmentVo); } } else { // 워크플로우 모든 담당 부서 조회 List> results = this.workflowDepartmentMapper.find(condition); List conditions = ConvertUtil.convertListToListClass(results, WorkflowDepartmentCondition.class); for (WorkflowDepartmentCondition cond : conditions) { WorkflowDepartmentVo workflowDepartmentVo = new WorkflowDepartmentVo(); Department department = this.departmentService.getDepartment(cond.getDepartmentId()); DepartmentVo departmentVo = ConvertUtil.copyProperties(department, DepartmentVo.class); departmentVo.setByName(department.getDepartmentName()); workflowDepartmentVo.setDepartmentVo(departmentVo); workflowDepartmentVos.add(workflowDepartmentVo); } } return workflowDepartmentVos; } // 워크플로우의 상태에 지정된 담당부서를 변경한다 @Override @Transactional public void modify(Workflow workflow, List issueStatusVos) { this.remove(workflow.getId()); List workflowDepartmentList = Lists.newArrayList(); for (IssueStatusVo issueStatusVo : issueStatusVos) { for (WorkflowDepartmentVo workflowDepartmentVo : issueStatusVo.getWorkflowDepartmentVos()) { WorkflowDepartment workflowDepartment = ConvertUtil.copyProperties(workflowDepartmentVo, WorkflowDepartment.class); workflowDepartment.setIssueStatus(this.issueStatusService.getIssueStatus(issueStatusVo.getId())); workflowDepartment.setWorkflow(workflow); Department department = this.departmentService.getDepartment(workflowDepartmentVo.getDepartmentVo().getId()); workflowDepartment.setDepartment(department); workflowDepartmentList.add(workflowDepartment); // 변경한 워크플로우의 담당부서를 이슈유형과 연결된 프로젝트의 담당부서에도 세팅 this.setDepartmentOfProject(workflow, department); } } this.workflowDepartmentRepository.saveAll(workflowDepartmentList); } /** * 워크플로우의 담당부서를 이슈유형과 연결된 프로젝트의 담당부서에도 세팅 * @param workflow Workflow * @param department Department */ private void setDepartmentOfProject(Workflow workflow, Department department) { List issueTypeList = this.issueTypeService.findByWorkflowId(workflow.getId()); if (issueTypeList != null && issueTypeList.size() > 0) { for (IssueType issueType : issueTypeList) { if (issueType.getProject() != null) { ProjectRole projectRole = this.projectRoleService.findByProjectIdAndRoleType(issueType.getProject().getId(), ProjectRole.TYPE_DEFAULT); // 프로젝트 담당부서로 추가 projectRole.addDepartment(department); } } } } @Override @Transactional public void remove(Long workflowId) { WorkflowDepartmentCondition condition = new WorkflowDepartmentCondition(); condition.setWorkflowId(workflowId); this.workflowDepartmentMapper.deleteAll(condition); } }