OWL ITS + 탐지시스템(인터넷 진흥원)
이민희
2022-03-02 20d2fc7868921587e7a0aafd0dc00690507bb6e9
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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.MngPermission;
import kr.wisestone.owl.constant.MsgConstants;
import kr.wisestone.owl.domain.Guide;
import kr.wisestone.owl.domain.User;
import kr.wisestone.owl.domain.UserLevel;
import kr.wisestone.owl.exception.OwlRuntimeException;
import kr.wisestone.owl.mapper.GuideMapper;
import kr.wisestone.owl.repository.GuideRepository;
import kr.wisestone.owl.service.GuideService;
import kr.wisestone.owl.service.UserLevelService;
import kr.wisestone.owl.service.UserWorkspaceService;
import kr.wisestone.owl.service.WorkspaceService;
import kr.wisestone.owl.util.CommonUtil;
import kr.wisestone.owl.util.ConvertUtil;
import kr.wisestone.owl.vo.*;
import kr.wisestone.owl.web.condition.GuideCondition;
import kr.wisestone.owl.web.condition.NoticeCondition;
import kr.wisestone.owl.web.form.GuideForm;
import kr.wisestone.owl.web.form.NoticeForm;
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.transaction.annotation.Transactional;
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 GuideServiceImpl extends AbstractServiceImpl<Guide, Long, JpaRepository<Guide, Long>> implements GuideService {
 
    @Autowired
    private GuideRepository guideRepository;
 
    @Autowired
    private GuideMapper guideMapper;
 
    @Autowired
    private WorkspaceService workspaceService;
 
    @Autowired
    private ExcelView excelView;
 
    @Autowired
    private ExcelConditionCheck excelConditionCheck;
 
    @Autowired
    private UserLevelService userLevelService;
 
    @Autowired
    private UserWorkspaceService userWorkspaceService;
 
    @Override
    protected JpaRepository<Guide, Long> getRepository() {
        return this.guideRepository;
    }
 
    //  guide 등록
    @Override
    @Transactional
    public Guide addGuide(GuideForm guideForm) {
        //  guide 제목 및 내용 공백 체크
        this.verifyTitleAndDescription(guideForm.getTitle(), guideForm.getDescription());
 
        guideForm.setStatus(Guide.INACTIVATION);
        Guide guide = ConvertUtil.copyProperties(guideForm, Guide.class);
 
        return this.guideRepository.saveAndFlush(guide);
    }
 
    //  guide 제목 및 내용 공백 체크
    private void verifyTitleAndDescription(String title, String description) {
        if (StringUtils.isEmpty(title) || StringUtils.isEmpty(description)) {
            throw new OwlRuntimeException(this.messageAccessor.getMessage(MsgConstants.GUIDE_EMPTY_CONTENT));
        }
    }
 
    //  guide 조회
    @Override
    @Transactional(readOnly = true)
    public List<GuideVo> findGuide(Map<String, Object> resJsonData,
                                   GuideCondition guideCondition, Pageable pageable) {
 
        guideCondition.setPage(pageable.getPageNumber() * pageable.getPageSize());
        guideCondition.setPageSize(pageable.getPageSize());
        guideCondition.setTitle(guideCondition.getTitle());
 
        User user = this.webAppUtil.getLoginUserObject();
        UserLevel userLevel = this.userLevelService.getUserLevel(user.getUserLevel().getId());
 
        List<Map<String, Object>> results = Lists.newArrayList();
        Long totalCount = 0L;
        if (this.userWorkspaceService.checkWorkspaceManager(user)
                || MngPermission.checkMngPermission(userLevel.getPermission(), MngPermission.USER_PERMISSION_MNG_GUIDE)) {
            results = this.guideMapper.find(guideCondition);
            totalCount = this.guideMapper.count(guideCondition);
        } else {
            results = this.guideMapper.findNotActivation(guideCondition);
            totalCount = this.guideMapper.countNotActivation(guideCondition);
        }
        int totalPage = (int) Math.ceil((totalCount - 1) / pageable.getPageSize()) + 1;
        List<GuideVo> guideVos = ConvertUtil.convertListToListClass(results, GuideVo.class);
 
        for (GuideVo guideVo : guideVos) {
            Boolean bActivation = false;
 
            if(guideVo.getStatus().equals(Guide.ACTIVATION)) {
                bActivation = true;
            }
            guideVo.activation =  bActivation;
        }
 
        resJsonData.put(Constants.RES_KEY_CONTENTS, guideVos);
        resJsonData.put(Constants.REQ_KEY_PAGE_VO, new ResPage(pageable.getPageNumber(), pageable.getPageSize(),
                totalPage, totalCount));
 
        return guideVos;
    }
 
    //  guide 수정
    @Override
    @Transactional
    public Guide modifyGuide(GuideForm guideForm) {
        //  guide 제목 및 내용 공백 체크
        this.verifyTitleAndDescription(guideForm.getTitle(), guideForm.getDescription());
 
        Guide guide = this.getGuide(guideForm.getId());
        ConvertUtil.copyProperties(guideForm, guide, "id");
 
        return this.guideRepository.saveAndFlush(guide);
    }
 
    //  guide 수정
    @Override
    @Transactional
    public Guide activeGuide(GuideForm guideForm) {
 
        boolean bActivation = guideForm.getActivation();
 
        if(bActivation) {
            guideForm.setStatus(Guide.ACTIVATION);
        } else {
            guideForm.setStatus(Guide.INACTIVATION);
        }
 
        Guide guide = this.getGuide(guideForm.getId());
        ConvertUtil.copyProperties(guideForm, guide, "id");
 
        if(bActivation) {
            this.guideRepository.updateInActivation(guideForm.getId());
        }
 
        return this.guideRepository.saveAndFlush(guide);
    }
 
    //  guide을 id 로 조회한다.
    @Override
    @Transactional(readOnly = true)
    public Guide getGuide(Long id) {
        if (id == null) {
            throw new OwlRuntimeException(
                    this.messageAccessor.getMessage(MsgConstants.GUIDE_NOT_EXIST));
        }
 
        Guide guide = this.findOne(id);
 
        if (guide == null) {
            throw new OwlRuntimeException(
                    this.messageAccessor.getMessage(MsgConstants.GUIDE_NOT_EXIST));
        }
 
        return guide;
    }
 
    //  guide 상세 정보를 조회한다.
    @Override
    @Transactional(readOnly = true)
    public void detailGuide(Map<String, Object> resJsonData, GuideCondition guideCondition) {
        GuideVo guideVo = new GuideVo();
 
        if (guideCondition.getId() != null) {
            Guide guide = this.getGuide(guideCondition.getId());
            guideVo = ConvertUtil.copyProperties(guide, GuideVo.class);
        }
 
        resJsonData.put(Constants.RES_KEY_CONTENTS, guideVo);
    }
 
    @Override
    public void remove(GuideForm guideForm) {
        if (guideForm.getRemoveIds().size() < 1) {
            throw new OwlRuntimeException(
                    this.messageAccessor.getMessage(MsgConstants.COMPANY_REMOVE_NOT_SELECT));
        }
        for (Long id : guideForm.getRemoveIds()) {
            this.guideRepository.deleteById(id);
            this.guideRepository.flush();
        }
    }
 
    @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;
        }
 
        GuideCondition guideCondition = GuideCondition.make(conditions);
        List<Map<String, Object>> results = this.guideMapper.find(guideCondition);
        List<GuideVo> guideVos = ConvertUtil.convertListToListClass(results, GuideVo.class);
 
        // code_ko_KR 에 code명 설정
        ExportExcelVo excelInfo = new ExportExcelVo();
        excelInfo.setFileName(this.messageAccessor.message("가이드 목록"));
        excelInfo.addAttrInfos(new ExportExcelAttrVo("title", this.messageAccessor.message("guide.title"), 6, ExportExcelAttrVo.ALIGN_CENTER));
        excelInfo.addAttrInfos(new ExportExcelAttrVo("registerDate", this.messageAccessor.message("guide.registerDate"), 10, ExportExcelAttrVo.ALIGN_CENTER));
        excelInfo.addAttrInfos(new ExportExcelAttrVo("writer", this.messageAccessor.message("guide.registerId"), 10, ExportExcelAttrVo.ALIGN_CENTER));
 
        excelInfo.setDatas(guideVos);
 
        model.addAttribute(Constants.EXCEL, excelInfo);
        return new ModelAndView(this.excelView);
    }
}