OWL ITS + 탐지시스템(인터넷 진흥원)
이민희
2021-11-05 b401c04a16e870af6789a7cc89e530873653037c
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
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.DepartmentManage;
import kr.wisestone.owl.exception.OwlRuntimeException;
import kr.wisestone.owl.mapper.DepartmentManageMapper;
import kr.wisestone.owl.repository.DepartmentManageRepository;
import kr.wisestone.owl.service.DepartmentManageService;
import kr.wisestone.owl.service.WorkspaceService;
import kr.wisestone.owl.util.ConvertUtil;
import kr.wisestone.owl.vo.*;
import kr.wisestone.owl.web.condition.DepartmentManageCondition;
import kr.wisestone.owl.web.form.DepartmentManageForm;
import kr.wisestone.owl.web.view.ExcelView;
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.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
public class DepartmentManageServiceImpl extends AbstractServiceImpl<DepartmentManage, Long, JpaRepository<DepartmentManage, Long>> implements DepartmentManageService {
 
    @Autowired
    private DepartmentManageRepository departmentManageRepository;
 
    @Autowired
    private DepartmentManageMapper departmentManageMapper;
 
    @Autowired
    private WorkspaceService workspaceService;
 
    @Autowired
    private ExcelView excelView;
 
    @Autowired
    private ExcelConditionCheck excelConditionCheck;
 
    @Override
    protected JpaRepository<DepartmentManage, Long> getRepository() {
        return this.departmentManageRepository;
    }
 
    @Override
    public DepartmentManage addDepartmentManage(DepartmentManageForm departmentManageForm) {
        DepartmentManage departmentManage = ConvertUtil.copyProperties(departmentManageForm, DepartmentManage.class);
        departmentManageRepository.saveAndFlush(departmentManage);
        return departmentManage;
    }
 
    @Override
    public DepartmentManage getDepartment(Long id) {
        if (id == null) {
            throw new OwlRuntimeException(
                    this.messageAccessor.getMessage(MsgConstants.USER_LEVEL_NOT_EXIST));
        }
 
        DepartmentManage departmentManage = this.findOne(id);
 
        if (departmentManage == null) {
            throw new OwlRuntimeException(
                    this.messageAccessor.getMessage(MsgConstants.USER_LEVEL_NOT_EXIST));
        }
 
        return departmentManage;
    }
 
    @Override
    public List<DepartmentManageVo> findDepartment(Map<String, Object> resJsonData,
                                                   DepartmentManageCondition condition, Pageable pageable) {
 
        condition.setPage(pageable.getPageNumber() * pageable.getPageSize());
        condition.setPageSize(pageable.getPageSize());
 
        List<Map<String, Object>> results = this.departmentManageMapper.find(condition);
        Long totalDepartmentCount = this.departmentManageMapper.count(condition);
 
        return this.convertDepartmentManageVoToMap(results, totalDepartmentCount, pageable, resJsonData);
    }
 
    //  검색 결과를 DepartmentManageVo 로 변환한다.
    private List<DepartmentManageVo> convertDepartmentManageVoToMap(List<Map<String, Object>> results, Long totalDepartmentCount, Pageable pageable, Map<String, Object> resJsonData) {
        List<DepartmentManageVo> departmentManageVos = Lists.newArrayList();
 
        for (Map<String, Object> result : results) {
            DepartmentManageVo departmentManageVo = ConvertUtil.convertMapToClass(result, DepartmentManageVo.class);
            // 부서명만 변환 하면 되는건가 ?
            departmentManageVos.add(departmentManageVo);
        }
 
        int totalPage = (int) Math.ceil((totalDepartmentCount - 1) / pageable.getPageSize()) + 1;
 
        resJsonData.put(Constants.RES_KEY_CONTENTS, departmentManageVos);
        resJsonData.put(Constants.REQ_KEY_PAGE_VO, new ResPage(pageable.getPageNumber(), pageable.getPageSize(),
                totalPage, totalDepartmentCount));
 
        return departmentManageVos;
    }
 
    //  부서 목록을 엑셀로 다운로드 한다.
    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;
        }
 
        DepartmentManageCondition departmentManageCondition = DepartmentManageCondition.make(conditions);
 
 
        List<Map<String, Object>> results = this.departmentManageMapper.find(departmentManageCondition);
        List<DepartmentManageVo> departmentManageVos = ConvertUtil.convertListToListClass(results, DepartmentManageVo.class);
 
        // code_ko_KR 에 code명 설정
        ExportExcelVo excelInfo = new ExportExcelVo();
        excelInfo.setFileName(this.messageAccessor.message("부서 목록"));
        excelInfo.addAttrInfos(new ExportExcelAttrVo("departmentName", this.messageAccessor.message("departmentManage.departmentName"), 6, ExportExcelAttrVo.ALIGN_CENTER));
        excelInfo.addAttrInfos(new ExportExcelAttrVo("departmentDescription", this.messageAccessor.message("departmentManage.departmentDescription"), 20, ExportExcelAttrVo.ALIGN_CENTER));
 
        excelInfo.setDatas(departmentManageVos);
 
        model.addAttribute(Constants.EXCEL, excelInfo);
        return new ModelAndView(this.excelView);
    }
 
}