OWL ITS + 탐지시스템(인터넷 진흥원)
이민희
2022-03-17 916a3cbabe4e50062fce61ff6f2f5d46c05dfbd1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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<WorkflowDepartment, Long, JpaRepository<WorkflowDepartment, Long>> 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<WorkflowDepartment, Long> 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<WorkflowDepartmentVo> find(Long workflowId, Long issueStatusId) {
        WorkflowDepartmentCondition condition = new WorkflowDepartmentCondition();
        condition.setWorkflowId(workflowId);
        condition.setIssueStatusId(issueStatusId);
 
        return find(condition);
    }
 
    public List<WorkflowDepartment> find(Long workflowId) {
        return this.workflowDepartmentRepository.findByWorkflowId(workflowId);
    }
 
 
    @Override
    // 워크플로우의 해당 이슈상태를 가진 워크플로우 부서 가져오기(Ready)
    public List<Long> findFirstDepartmentIds(Workflow workflow) {
        List<Long> departmentIds = Lists.newArrayList();
        IssueStatus issueStatus = issueStatusService.findByIssueStatusTypeIsReady(workflow);
 
        // 워크플로우 상태에 따른 담당부서 가져오기
        if (issueStatus != null) {
            WorkflowDepartmentCondition workflowDepartmentCondition = new WorkflowDepartmentCondition();
            workflowDepartmentCondition.setIssueStatusId(issueStatus.getId());
            workflowDepartmentCondition.setWorkflowId(workflow.getId());
            List<WorkflowDepartmentVo> workflowDepartmentVos = this.find(workflowDepartmentCondition);
 
            for (WorkflowDepartmentVo workflowDepartmentVo : workflowDepartmentVos) {
                departmentIds.add(workflowDepartmentVo.getDepartmentVo().getId());
            }
        }
        return departmentIds;
    }
 
    // 담당 부서정보를 조회한다.
    @Transactional(readOnly = true)
    @Override
    public List<WorkflowDepartmentVo> find(WorkflowDepartmentCondition condition) {
 
        List<WorkflowDepartment> workflowDepartmentList = null;
        List<WorkflowDepartmentVo> 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<Map<String, Object>> results = this.workflowDepartmentMapper.find(condition);
            List<WorkflowDepartmentCondition> 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<IssueStatusVo> issueStatusVos) {
        this.remove(workflow.getId());
        List<WorkflowDepartment> 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<IssueType> 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);
    }
 
    /**
     * 특정 부서를 워크플로우에서 사용중인지 체크
     * @param id Long
     * @return useYn
     */
    @Override
    public boolean usingDepartment(Long id) {
        boolean useYn = false;
        List<WorkflowDepartment> workflowDepartments = this.workflowDepartmentRepository.findByDepartmentId(id);
        if (workflowDepartments != null && workflowDepartments.size() > 0) {
            useYn = true;
        }
        return useYn;
    }
}