OWL ITS + 탐지시스템(인터넷 진흥원)
박지현
2022-02-22 1dad4d94cc415d2f8092aff0368744289337f504
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
package kr.wisestone.owl.service.impl;
 
import kr.wisestone.owl.constant.MsgConstants;
import kr.wisestone.owl.domain.*;
import kr.wisestone.owl.domain.enumType.IssueHistoryType;
import kr.wisestone.owl.exception.OwlRuntimeException;
import kr.wisestone.owl.mapper.HostingFieldMapper;
import kr.wisestone.owl.mapper.IssueHostingMapper;
import kr.wisestone.owl.repository.IssueHostingRepository;
import kr.wisestone.owl.service.*;
import kr.wisestone.owl.util.ConvertUtil;
import kr.wisestone.owl.util.MapUtil;
import kr.wisestone.owl.vo.IssueHostingVo;
import kr.wisestone.owl.web.condition.IssueCondition;
import kr.wisestone.owl.web.form.HostingFieldForm;
import kr.wisestone.owl.web.form.IspFieldForm;
import kr.wisestone.owl.web.form.IssueForm;
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 IssueHostingServiceImpl extends AbstractServiceImpl<IssueHosting, Long, JpaRepository<IssueHosting, Long>> implements IssueHostingService {
 
    private static final Logger log = LoggerFactory.getLogger(IssueHostingServiceImpl.class);
 
    @Autowired
    private IssueHostingRepository issueHostingRepository;
 
    @Autowired
    private HostingFieldService hostingFieldService;
 
    @Autowired
    private IssueService issueService;
 
    @Autowired
    private IssueHistoryService issueHistoryService;
 
    @Autowired
    private IssueHostingMapper issueHostingMapper;
 
    @Override
    protected JpaRepository<IssueHosting, Long> getRepository() {
        return this.issueHostingRepository;
    }
 
    /**
     * 새로운 호스팅 직접 추가
     * @param issueForm IssueForm
     * @param issue Issue
     */
    private void CreateHostingField(IssueForm issueForm, Issue issue, StringBuilder sb) {
        //코드 유효성 체크
        this.verifyCode(issueForm);
 
        // issueHosting 필드 폼 만들기
        HostingFieldForm hostingFieldForm = new HostingFieldForm();
        hostingFieldForm.setName(issueForm.getHostingName());
        hostingFieldForm.setCode(issueForm.getHostingCode());
        hostingFieldForm.setEmail(issueForm.getHostingEmail());
        hostingFieldForm.setUrl(issueForm.getHostingUrl());
        hostingFieldForm.setManager(issueForm.getHostingManager());
        hostingFieldForm.setTel(issueForm.getHostingTel());
        hostingFieldForm.setMemo(issueForm.getHostingMemo());
 
        IssueHosting newIssueHosting = CreateIssueHosting(hostingFieldForm, issue);
        HostingField hostingField = new HostingField();
 
        // 사용자가 직접 입력시에 호스팅 목록에 추가
        if (newIssueHosting.getHostingField() == null) {
            IssueHosting oldIssueHosting = this.issueHostingRepository.findByIssueId(issue.getId());
            hostingField = this.hostingFieldService.add(hostingFieldForm);
            hostingFieldForm.setId(hostingField.getId());
            if (oldIssueHosting != null) {
                this.issueHostingRepository.deleteById(oldIssueHosting.getId());
                issueHistoryService.detectIssueHosting(IssueHistoryType.MODIFY, null, hostingFieldForm, oldIssueHosting, sb);
            } else {
                newIssueHosting.setHostingField(hostingField);
                issueHistoryService.detectIssueHosting(IssueHistoryType.ADD, null, hostingFieldForm, newIssueHosting, sb);
            }
        }
        this.issueHostingRepository.saveAndFlush(newIssueHosting);
    }
 
    /**
     * 이슈 호스팅 만들기
     * @param hostingFieldForm HostingFieldForm
     * @param issue 이슈
     * @return IssueHosting
     */
    private IssueHosting CreateIssueHosting(HostingFieldForm hostingFieldForm, Issue issue) {
        IssueHosting issueHosting = ConvertUtil.copyProperties(hostingFieldForm, IssueHosting.class, "id");
        issueHosting.setIssue(issue);
        if (hostingFieldForm.getId() != null && hostingFieldForm.getId() != -1) {
            HostingField hostingField = this.hostingFieldService.getHosting(hostingFieldForm.getId());
            issueHosting.setHostingField(hostingField);
        }
        return issueHosting;
    }
 
 
    /**
     *  이슈 호스팅 만들기
     * @param hostingFieldMap Map<String, Object> hostingFieldMap
     * @param issue 이슈
     * @return IssueHosting
     */
    private IssueHosting CreateIssueHosting(Map<String, Object> hostingFieldMap, Issue issue) {
        HostingFieldForm hostingFieldForm = ConvertUtil.convertMapToClass(hostingFieldMap, HostingFieldForm.class);
        hostingFieldForm.setId(MapUtil.getLong(hostingFieldMap, "hostingId"));
        return CreateIssueHosting(hostingFieldForm, issue);
    }
 
    /**
     * 코드 유효성 검사
     * @param param Map<String, Object>
     */
    private void verifyCode(Map<String, Object> param) {
        if (param != null) {
            if (MapUtil.getString(param, "code") == null || Objects.equals(MapUtil.getString(param, "code"), "")) {
                throw new OwlRuntimeException(
                        this.messageAccessor.getMessage(MsgConstants.HOSTING_CODE_NOT_ENTER));
            }
        }
    }
 
    /**
     * 코드 유효성 검사
     * @param issueForm IssueForm
     */
    private void verifyCode(IssueForm issueForm) {
        if (issueForm != null) {
            if (issueForm.getHostingCode() == null || issueForm.getHostingCode().equals("")) {
                throw new OwlRuntimeException(
                        this.messageAccessor.getMessage(MsgConstants.HOSTING_CODE_NOT_ENTER));
            }
        }
    }
 
    //  이슈에서 사용되는 업체 값을 업데이트한다.
    @Override
    @Transactional
    public void modifyIssueHostingField(Issue issue, IssueForm issueForm, StringBuilder sb) {
        if (issue != null) {
            IssueHosting issueHosting = this.issueHostingRepository.findByIssueId(issue.getId());
            List<Map<String, Object>> issueHostingFields = issueForm.getIssueHostingFields();
            if (issueHostingFields != null && issueHostingFields.size() > 0) {
                Map<String, Object> param = issueHostingFields.get(0);
 
                this.verifyCode(param); //코드 유효성 체크
 
                // 변경 이력 남기고 issueHosting에 set해주기
                if (issueHosting != null) { //수정 시
                    issueHistoryService.detectIssueHosting(IssueHistoryType.MODIFY, param, null, issueHosting, sb);
 
                    issueHosting.setName(MapUtil.getString(param, "name"));
                    issueHosting.setEmail(MapUtil.getString(param, "email"));
                    issueHosting.setCode(MapUtil.getString(param, "code"));
                    issueHosting.setUrl(MapUtil.getString(param, "url"));
                    issueHosting.setManager(MapUtil.getString(param, "manager"));
                    issueHosting.setTel(MapUtil.getString(param, "tel"));
                    issueHosting.setMemo(MapUtil.getString(param, "memo"));
 
                    this.issueHostingRepository.saveAndFlush(issueHosting);
 
                } else { //추가 시
                    IssueHosting newIssueHosting = CreateIssueHosting(param, issue);
                    // 추가 이력
                    issueHistoryService.detectIssueHosting(IssueHistoryType.ADD, param, null, newIssueHosting, sb);
                    this.issueHostingRepository.saveAndFlush(newIssueHosting);
                }
 
            }else if (issueForm.getHostingName() != null && !issueForm.getHostingName().equals("")) {
                // 호스팅 정보 직접 추가
                CreateHostingField(issueForm, issue, sb);
            } else {
                this.issueHostingRepository.deleteByIssueId(issue.getId());
                this.issueHostingRepository.flush();
 
                if (issueHosting != null) {
                    issueHistoryService.detectIssueHosting(IssueHistoryType.DELETE, null, null, issueHosting, sb);
                }
            }
            //issueHistoryService.addIssueHistory(issue, IssueHistoryType.MODIFY, sb.toString());
        }
    }
 
    @Override
    @Transactional(readOnly = true)
    public List<Map<String, Object>> findInIssueIds(IssueCondition issueCondition) {
        return issueHostingMapper.findInIssueIds(issueCondition);
    }
}