OWL ITS + 탐지시스템(인터넷 진흥원)
이민희
2022-01-12 3b019e5599dfb5d368f4e8fd50fb557f4679a645
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
package kr.wisestone.owl.service.impl;
 
import com.google.common.collect.Lists;
import kr.wisestone.owl.common.ExcelConditionCheck;
import kr.wisestone.owl.constant.Constants;
import kr.wisestone.owl.constant.MsgConstants;
import kr.wisestone.owl.domain.CompanyField;
import kr.wisestone.owl.domain.HostingField;
import kr.wisestone.owl.exception.OwlRuntimeException;
import kr.wisestone.owl.mapper.HostingFieldMapper;
import kr.wisestone.owl.repository.HostingFieldRepository;
import kr.wisestone.owl.service.HostingFieldService;
import kr.wisestone.owl.service.WorkspaceService;
import kr.wisestone.owl.util.ConvertUtil;
import kr.wisestone.owl.vo.HostingFieldVo;
import kr.wisestone.owl.vo.ExportExcelAttrVo;
import kr.wisestone.owl.vo.ExportExcelVo;
import kr.wisestone.owl.vo.ResPage;
import kr.wisestone.owl.web.condition.HostingFieldCondition;
import kr.wisestone.owl.web.form.HostingFieldForm;
import kr.wisestone.owl.web.view.ExcelView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;
 
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
public class HostingFieldServiceImpl extends AbstractServiceImpl<HostingField, Long, JpaRepository<HostingField, Long>> implements HostingFieldService {
 
    @Autowired
    private HostingFieldRepository hostingFieldRepository;
 
    @Autowired
    private HostingFieldMapper hostingFieldMapper;
 
    @Autowired
    private WorkspaceService workspaceService;
 
    @Autowired
    private ExcelView excelView;
 
    @Autowired
    private ExcelConditionCheck excelConditionCheck;
 
    @Override
    protected JpaRepository<HostingField, Long> getRepository() {
        return this.hostingFieldRepository;
    }
 
    // Hosting 추가
    @Override
    public HostingField add(HostingFieldForm HostingFieldForm) {
        if (HostingFieldForm.getTelList() != null && HostingFieldForm.getTelList().size() > 0) {
            String[] tels = ConvertUtil.ToArray(HostingFieldForm.getTelList());
            HostingFieldForm.setTel(Arrays.toString(tels));
        }
        if (HostingFieldForm.getEmailList() != null && HostingFieldForm.getEmailList().size() > 0) {
            String[] emails = ConvertUtil.ToArray(HostingFieldForm.getEmailList());
            HostingFieldForm.setEmail(Arrays.toString(emails));
        }
 
        HostingField hostingField = ConvertUtil.copyProperties(HostingFieldForm, HostingField.class);
        if (hostingField.getCode() != null && !hostingField.getCode().equals("")) {
            hostingFieldRepository.saveAndFlush(hostingField);
        } else {
            throw new OwlRuntimeException(this.messageAccessor.getMessage(MsgConstants.HOSTING_CODE_NOT_ENTER));
        }
        return hostingField;
    }
 
    // Hosting 목록을 가져온다.
    @Override
    public List<HostingFieldVo> find(Map<String, Object> resJsonData,
                                     HostingFieldCondition condition, Pageable pageable) {
        condition.setPage(pageable.getPageNumber() * pageable.getPageSize());
        condition.setPageSize(pageable.getPageSize());
 
        List<Map<String, Object>> results = this.hostingFieldMapper.find(condition);
        Long totalHostingCount = this.hostingFieldMapper.count(condition);
 
        return this.convertHostingVoToMap(results, totalHostingCount, pageable, resJsonData);
    }
 
    public Map<String, Object> find(Long id) {
        return this.hostingFieldMapper.findById(id);
    }
 
    // Hosting 상세 조회한다.
    @Override
    public void detail(Map<String, Object> resJsonData, HostingFieldCondition hostingFieldCondition) {
        HostingFieldVo HostingFieldVo = new HostingFieldVo();
 
        Long hostingId = hostingFieldCondition.getId();
        if (hostingId != null) {
            HostingField HostingField = this.getHosting(hostingId);
            HostingFieldVo = ConvertUtil.copyProperties(HostingField, HostingFieldVo.class);
        }
        resJsonData.put(Constants.REQ_KEY_CONTENT, HostingFieldVo);
    }
 
    // Hosting 정로를 수정한다.
    @Override
    public void modify(HostingFieldForm HostingFieldForm) {
        if (HostingFieldForm.getTelList() != null && HostingFieldForm.getTelList().size() > 0) {
            String[] tels = ConvertUtil.ToArray(HostingFieldForm.getTelList());
            HostingFieldForm.setTel(Arrays.toString(tels));
        }
        if (HostingFieldForm.getEmailList() != null && HostingFieldForm.getEmailList().size() > 0) {
            String[] emails = ConvertUtil.ToArray(HostingFieldForm.getEmailList());
            HostingFieldForm.setEmail(Arrays.toString(emails));
        }
        HostingField HostingField = ConvertUtil.copyProperties(HostingFieldForm, HostingField.class);
        hostingFieldRepository.saveAndFlush(HostingField);
    }
 
    // Hosting를 삭제한다.
    @Override
    public void remove(HostingFieldForm HostingFieldForm) {
        if (HostingFieldForm.getRemoveIds().size() < 1) {
            throw new OwlRuntimeException(
                    this.messageAccessor.getMessage(MsgConstants.COMPANY_REMOVE_NOT_SELECT));
        }
        for (Long id : HostingFieldForm.getRemoveIds()) {
                this.hostingFieldRepository.deleteById(id);
 
        }
        this.hostingFieldRepository.flush();
    }
 
    // Hosting 목록을 엑셀로 다운로드 한다.
    @Override
    public ModelAndView downloadExcel(HttpServletRequest request, Model model) {
        ModelAndView modelAndView = this.workspaceService.checkUseExcelDownload(model);
        if (modelAndView != null) {
            return modelAndView;
        }
 
        Map<String, Object> conditions = new HashMap<>();
        //  엑셀 다운로드에 필요한 검색 조건 정보를 추출하고 검색 조건 추출에 오류가 발생하면 경고를 표시해준다.
        modelAndView = this.excelConditionCheck.checkCondition(conditions, request, model);
        if (modelAndView != null) {
            return modelAndView;
        }
 
        HostingFieldCondition hostingFieldCondition = HostingFieldCondition.make(conditions);
 
 
        List<Map<String, Object>> results = this.hostingFieldMapper.find(hostingFieldCondition);
        List<HostingFieldVo> hostingFieldVos = ConvertUtil.convertListToListClass(results, HostingFieldVo.class);
        // code_ko_KR 에 code명 설정
        ExportExcelVo excelInfo = new ExportExcelVo();
        excelInfo.setFileName(this.messageAccessor.message("Hosting 목록"));
        excelInfo.addAttrInfos(new ExportExcelAttrVo("name", this.messageAccessor.message("Hosting.HostingName"), 6, ExportExcelAttrVo.ALIGN_CENTER));
        excelInfo.addAttrInfos(new ExportExcelAttrVo("code", this.messageAccessor.message("Hosting.HostingCode"), 6, ExportExcelAttrVo.ALIGN_CENTER));
        excelInfo.addAttrInfos(new ExportExcelAttrVo("manager", this.messageAccessor.message("Hosting.HostingManager"), 10, ExportExcelAttrVo.ALIGN_CENTER));
        excelInfo.addAttrInfos(new ExportExcelAttrVo("tel", this.messageAccessor.message("Hosting.HostingTel"), 10, ExportExcelAttrVo.ALIGN_CENTER));
        excelInfo.addAttrInfos(new ExportExcelAttrVo("email", this.messageAccessor.message("Hosting.HostingEmail"), 10, ExportExcelAttrVo.ALIGN_CENTER));
        excelInfo.addAttrInfos(new ExportExcelAttrVo("url", this.messageAccessor.message("Hosting.HostingUrl"), 10, ExportExcelAttrVo.ALIGN_CENTER));
        excelInfo.addAttrInfos(new ExportExcelAttrVo("memo", this.messageAccessor.message("Hosting.HostingMemo"), 10, ExportExcelAttrVo.ALIGN_CENTER));
 
        excelInfo.setDatas(hostingFieldVos);
 
        model.addAttribute(Constants.EXCEL, excelInfo);
        return new ModelAndView(this.excelView);
    }
 
    //  검색 결과를 HostingVo 로 변환한다.
    private List<HostingFieldVo> convertHostingVoToMap(List<Map<String, Object>> results, Long totalHostingCount, Pageable pageable, Map<String, Object> resJsonData) {
        List<HostingFieldVo> hostingFieldVos = Lists.newArrayList();
 
        for (Map<String, Object> result : results) {
            HostingFieldVo HostingFieldVo = ConvertUtil.convertMapToClass(result, HostingFieldVo.class);
            hostingFieldVos.add(HostingFieldVo);
        }
 
        int totalPage = (int) Math.ceil((totalHostingCount - 1) / pageable.getPageSize()) + 1;
 
        resJsonData.put(Constants.RES_KEY_CONTENTS, hostingFieldVos);
        resJsonData.put(Constants.REQ_KEY_PAGE_VO, new ResPage(pageable.getPageNumber(), pageable.getPageSize(),
                totalPage, totalHostingCount));
 
        return hostingFieldVos;
    }
 
    // Hosting ID 로 조회한다
    @Override
    public HostingField getHosting(Long id) {
        if (id == null) {
            throw new OwlRuntimeException(
                    this.messageAccessor.getMessage(MsgConstants.HOSTING_NOT_EXIST));
        }
 
        HostingField HostingField = this.findOne(id);
 
        if (HostingField == null) {
            throw new OwlRuntimeException(
                    this.messageAccessor.getMessage(MsgConstants.HOSTING_NOT_EXIST));
        }
        return HostingField;
    }
}