OWL ITS + 탐지시스템(인터넷 진흥원)
이민희
2022-03-03 2b0d6901597206d1c24abad0ff3ac0889f9194dd
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package kr.wisestone.owl.service.impl;
 
import kr.wisestone.owl.constant.Constants;
import kr.wisestone.owl.constant.MsgConstants;
import kr.wisestone.owl.domain.*;
import kr.wisestone.owl.exception.OwlRuntimeException;
import kr.wisestone.owl.mapper.UserWorkspaceMapper;
import kr.wisestone.owl.repository.UserWorkspaceRepository;
import kr.wisestone.owl.service.*;
import kr.wisestone.owl.util.CommonUtil;
import kr.wisestone.owl.util.ConvertUtil;
import kr.wisestone.owl.vo.DepartmentVo;
import kr.wisestone.owl.vo.ResPage;
import kr.wisestone.owl.vo.UserWorkspaceVo;
import kr.wisestone.owl.web.condition.UserCondition;
import kr.wisestone.owl.web.condition.UserWorkspaceCondition;
import kr.wisestone.owl.web.form.UserWorkspaceForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
 
@Service
public class UserWorkspaceServiceImpl extends AbstractServiceImpl<UserWorkspace, Long, JpaRepository<UserWorkspace, Long>>
        implements UserWorkspaceService {
 
    private static final Logger log = LoggerFactory.getLogger(UserWorkspaceServiceImpl.class);
 
    @Autowired
    private UserWorkspaceRepository userWorkspaceRepository;
 
    @Autowired
    private UserService userService;
 
    @Autowired
    private UserLevelService userLevelService;
 
    @Autowired
    private DepartmentService departmentService;
 
    @Autowired
    private UserWorkspaceMapper userWorkspaceMapper;
 
    @Autowired
    private WorkspaceService workspaceService;
 
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;
 
    @Override
    protected JpaRepository<UserWorkspace, Long> getRepository() {
        return this.userWorkspaceRepository;
    }
 
    //  업무 공간에 사용자가 가입된다
    @Override
    @Transactional
    public UserWorkspace addUserWorkspace(User user, Workspace workspace, Boolean managerYn, Boolean useYn) {
        Long disableCount = this.userWorkspaceRepository.maxDisablePosition(workspace.getId());
 
        if (disableCount == null) {
            disableCount = 1L;
        }
 
        UserWorkspace userWorkspace = new UserWorkspace(user, workspace, managerYn, useYn, disableCount);
 
        return this.userWorkspaceRepository.saveAndFlush(userWorkspace);
    }
 
    //  업무 공간에 참여/참여대기 하는 전체 사용자 목록을 조회한다.
    @Override
    @Transactional(readOnly = true)
    public List<UserWorkspaceVo> findUserWorkspace(Map<String, Object> resJsonData,
                                                   UserWorkspaceCondition condition, Pageable pageable) {
 
        UserWorkspace userWorkspace = this.findWorkspaceManager(this.webAppUtil.getLoginId());
        Workspace myWorkspace = userWorkspace.getWorkspace();
        condition.setPage(pageable.getPageNumber() * pageable.getPageSize());
        condition.setPageSize(pageable.getPageSize());
        condition.setWorkspaceId(myWorkspace.getId());
        condition.setAccount(CommonUtil.encryptAES128(condition.getAccount())); //계정 암호화(필수 동작)
 
        List<Map<String, Object>> results = this.userWorkspaceMapper.find(condition);
        Long totalCount = this.userWorkspaceMapper.count(condition);
        int totalPage = (int) Math.ceil((totalCount - 1) / pageable.getPageSize()) + 1;
        List<UserWorkspaceVo> userWorkspaceVos = ConvertUtil.convertListToListClass(results, UserWorkspaceVo.class); //UserWorkspace의 데이터를 리스트 형식으로 복사해서 UserWorkspaceVo에 담기
 
        for (UserWorkspaceVo userWorkspaceVo : userWorkspaceVos) {
            userWorkspaceVo.setAccount(CommonUtil.decryptAES128(userWorkspaceVo.getAccount()));
 
            // UserCondition 는 원래 userId 가 없었다.
            UserCondition con = new UserCondition();
            // 그래서 Condition에 추가 해주고 set 하는데 그건 userWorkspaceVo 에서 Id 를 가져온다
            con.setId(userWorkspaceVo.getUserId());
            // findByDepartmentIds 라는걸 mapper로 만들어서 쿼리 for문을 돌리고 us
            List<Map<String, Object>> re = this.departmentService.findByDepartmentIds(con);
            List<DepartmentVo> vos = ConvertUtil.convertListToListClass(re, DepartmentVo.class);
 
            userWorkspaceVo.setDepartmentVos(vos);
        }
 
        resJsonData.put(Constants.REQ_KEY_PAGE_VO, new ResPage(pageable.getPageNumber(), pageable.getPageSize(),
                totalPage, totalCount));
        resJsonData.put(Constants.RES_KEY_CONTENTS, userWorkspaceVos);
 
        return userWorkspaceVos;
    }
 
    //  업무 공간에 연결된 사용자의 참여 상태를 변경한다.
    @Override
    @Transactional
    public void modifyUserWorkspace(UserWorkspaceForm userWorkspaceForm) {
        UserWorkspace userWorkspace = this.getUserWorkspace(userWorkspaceForm.getId());
 
        User user = userWorkspace.getUser();
 
        // 사용자 등급 변경
        UserLevel currentUserlevel = user.getUserLevel();
        if (currentUserlevel.getId() != userWorkspaceForm.getLevelId()) {
            UserLevel userLevel = this.userLevelService.getUserLevel(userWorkspaceForm.getLevelId());
            user.setUserLevel(userLevel);
            userWorkspace.setUser(user);
 
            //  등급 변경 된 유저 로그아웃 시키기
            this.simpMessagingTemplate.convertAndSendToUser(user.getAccount(), "/notification/changeUserLevel", this.messageAccessor.getMessage(MsgConstants.USER_LEVEL_CHANGE));
 
            //  세션 업데이트
            //SecurityUtils.setUserToSession(user);
        }
 
        // 부서 변경
//        DepartmentManage currentDepartment = user.getDepartmentManage();
//        if (currentDepartment == null || (userWorkspaceForm.getDepartmentId() != null && currentDepartment.getId() != userWorkspaceForm.getDepartmentId())) {
//            // 부서명 변경시
//            DepartmentManage departmentManage = this.departmentService.getDepartment(userWorkspaceForm.getDepartmentId());
//            user.setDepartmentManage(departmentManage);
//            userWorkspace.setUser(user);
//        }
 
        //  참여로 상태를 변경하려고 할때
        if (userWorkspace.getUseYn() != userWorkspaceForm.getUseYn()) {
            if (!userWorkspace.getUseYn()) {
                Integer maxUserCount = userWorkspace.getWorkspace().getMaxUser();  //  최대 사용자
                Integer activeUserCount = this.countByWorkspaceIdAndUseYn(userWorkspace.getWorkspace().getId(), true);
 
                //  최대 사용자 - 현재 참여 사용자가 0보다 크다면 참여 상태로 변경
                if ((maxUserCount - activeUserCount) < 1) {
                    throw new OwlRuntimeException(
                            this.messageAccessor.getMessage(MsgConstants.WORKSPACE_MAX_USER_EXCESS_NOT_INCLUDE));
                }
            } else {
                user = userWorkspace.getUser();
                //  참여 대기 사용자가 현재 해당 업무 공간을 사용하고 있을 경우 즉시 해당 업무 공간에서 튕기게 한다.
                if (user.getLastWorkspaceId().equals(userWorkspace.getWorkspace().getId())) {
                    //  업무 공간에 참여하던 사용자에게 제외 알림 및 화면 새로고침
                    this.simpMessagingTemplate.convertAndSendToUser(user.getAccount(), "/notification/workspace-disabled", this.messageAccessor.getMessage(MsgConstants.WORKSPACE_OUT, userWorkspace.getWorkspace().getName()));
                }
 
                //  참여 대기 상태로 변경하면 해당 사용자의 마지막 접근 업무 공간는 자신이 관리하는 업무 공간로 변경한다.
                this.userService.updateLastMyWorkspace(user);
            }
            userWorkspace.setUseYn(!userWorkspace.getUseYn());
        }
 
        this.userWorkspaceRepository.saveAndFlush(userWorkspace);
    }
 
 
    //  해당 사용자를 업무 공간에서 비활성화 한다.
    @Override
    @Transactional
    public void disabledUserWorkspace(User user, Workspace workspace) {
        UserWorkspace userWorkspace = this.userWorkspaceRepository.findByUserIdAndWorkspaceId(user.getId(), workspace.getId());
 
        /*if (userWorkspace.getManagerYn()) {
            throw new OwlRuntimeException(
                    this.messageAccessor.getMessage(MsgConstants.WORKSPACE_MANAGER_NOT_CHANGE_USE_YN));
        }*/
 
        userWorkspace.setUseYn(false);
        this.userWorkspaceRepository.saveAndFlush(userWorkspace);
    }
    //  해당 사용자가 업무 공간에 참여 정보가 있는지 확인한다.
    @Override
    @Transactional(readOnly = true)
    public UserWorkspace findByUserIdAndWorkspaceId(Long userId, Long workspaceId) {
        return this.userWorkspaceRepository.findByUserIdAndWorkspaceId(userId, workspaceId);
    }
 
    //  업무 공간에 참여/참여대기 사용자 수를 선택적으로 조회한다.
    @Override
    @Transactional(readOnly = true)
    public Integer countByWorkspaceIdAndUseYn(Long workspaceId, Boolean useYn) {
        List<UserWorkspace> userWorkspaces = this.findByWorkspaceIdAndUseYn(workspaceId, useYn);
        return userWorkspaces == null ? 0 : userWorkspaces.size();
    }
 
    //  업무 공간에 참여/참여대기 사용자 정보를 선택적으로 조회한다.
    @Override
    @Transactional(readOnly = true)
    public List<UserWorkspace> findByWorkspaceIdAndUseYn(Long workspaceId, Boolean useYn) {
        return this.userWorkspaceRepository.findByWorkspaceIdAndUseYn(workspaceId, useYn);
    }
 
    //  업무 공간의 관리자/일반 사용자 정보를 조회한다.
    @Override
    @Transactional(readOnly = true)
    public List<UserWorkspace> findByWorkspaceIdAndManagerYn(Long workspaceId, Boolean managerYn) {
        return this.userWorkspaceRepository.findByWorkspaceIdAndManagerYn(workspaceId, managerYn);
    }
 
    //  로그인한 사용자가 관리하는 업무 공간 정보를 조회한다.
    @Override
    @Transactional(readOnly = true)
    public UserWorkspace findMyWorkspace(Long userId) {
        return this.userWorkspaceRepository.findByUserIdAndManagerYn(userId, true);
    }
 
    //  워크스페이스 관리자 조회
    @Override
    @Transactional(readOnly = true)
    public UserWorkspace findWorkspaceManager(Long userId) {
        return this.userWorkspaceRepository.findByUserId(userId);
    }
 
    //  업무 공간 사용자 연결 아이디로 업무 공간 사용자 연결 정보를 조회한다.
    @Override
    @Transactional(readOnly = true)
    public UserWorkspace getUserWorkspace(Long id) {
        if (id == null) {
            throw new OwlRuntimeException(
                    this.messageAccessor.getMessage(MsgConstants.USER_WORKSPACE_NOT_EXIST));
        }
 
        UserWorkspace userWorkspace = this.findOne(id);
 
        if (userWorkspace == null) {
            throw new OwlRuntimeException(
                    this.messageAccessor.getMessage(MsgConstants.USER_WORKSPACE_NOT_EXIST));
        }
 
        return userWorkspace;
    }
 
    //  업무 공간 담당자 여부를 확인한다.
    @Override
    @Transactional(readOnly = true)
    public boolean checkWorkspaceManager(User user) {
        boolean bIsManager = false;
 
        try
        {
            Workspace workspace = this.workspaceService.getWorkspace(user.getLastWorkspaceId());   //  현재 접속한 업무 공간
            //  로그인한 사용자가 관리하는 업무 공간을 찾는다.
            UserWorkspace userWorkspace = this.findMyWorkspace(user.getId());
 
            bIsManager = workspace.getId().equals(userWorkspace.getWorkspace().getId());
        }
        catch (Exception e)
        {
            // exception.
        }
 
        //  관리하는 업무 공간이 다를 경우
        return bIsManager;
    }
 
    //  업무 공간에 모든 사용자 정보를 조회한다.
    @Override
    @Transactional(readOnly = true)
    public List<UserWorkspace> findByWorkspaceId(Long workspaceId) {
        return this.userWorkspaceRepository.findByWorkspaceId(workspaceId);
    }
 
    //  사용기간이 만료된 업무 공간의 관리자가 아닌 사용자를 추출하여 마지막 접근한 업무 공간 정보를 변경한다.
    @Override
    @Transactional
    public void limitExpireUserWorkspace(Workspace workspace) {
        //  관리자가 아닌 사용자 추출
        List<UserWorkspace> userWorkspaces = this.findByWorkspaceIdAndManagerYn(workspace.getId(), false);
        //  사용 금지 설정 & 마지막 접근 업무 공간 정보 변경
        for (UserWorkspace userWorkspace : userWorkspaces) {
            User user = userWorkspace.getUser();
            this.userService.updateLastDefaultWorkspace(workspace, user);
            userWorkspace.setUseYn(false);
        }
 
        if (userWorkspaces.size() > 0) {
            this.userWorkspaceRepository.saveAll(userWorkspaces);
        }
    }
 
}