| | |
| | | import com.google.common.collect.Lists; |
| | | import com.sun.mail.smtp.SMTPAddressFailedException; |
| | | import kr.wisestone.owl.config.CommonConfiguration; |
| | | import kr.wisestone.owl.constant.ElasticSearchConstants; |
| | | import kr.wisestone.owl.constant.MailConstants; |
| | | import kr.wisestone.owl.constant.MsgConstants; |
| | | import kr.wisestone.owl.domain.Department; |
| | | import kr.wisestone.owl.domain.SystemEmail; |
| | | import kr.wisestone.owl.domain.User; |
| | | import kr.wisestone.owl.domain.UserDepartment; |
| | | import kr.wisestone.owl.domain.enumType.EmailType; |
| | | import kr.wisestone.owl.exception.OwlRuntimeException; |
| | | import kr.wisestone.owl.repository.SystemEmailRepository; |
| | | import kr.wisestone.owl.service.SystemEmailService; |
| | | import kr.wisestone.owl.service.UserDepartmentService; |
| | | import kr.wisestone.owl.service.UserService; |
| | | import kr.wisestone.owl.util.CommonUtil; |
| | | import kr.wisestone.owl.util.DateUtil; |
| | | import kr.wisestone.owl.util.MapUtil; |
| | | import kr.wisestone.owl.util.StringTemplateUtil; |
| | | import kr.wisestone.owl.util.*; |
| | | import org.apache.commons.io.FilenameUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | |
| | | @Autowired |
| | | private SpringTemplateEngine springTemplateEngine; |
| | | |
| | | @Autowired |
| | | private UserDepartmentService userDepartmentService; |
| | | |
| | | @Override |
| | | protected JpaRepository<SystemEmail, Long> getRepository() { |
| | | return this.systemEmailRepository; |
| | |
| | | locale = CommonUtil.getUserLanguage(loginUser.getLanguage()); |
| | | } |
| | | } |
| | | |
| | | this.makeDirectContextEmail(emailType, params, locale, new String[]{CommonUtil.decryptAES128(sendUser)}); |
| | | } |
| | | } |
| | |
| | | content = this.springTemplateEngine.process(mailConstants.getMailTemplate(), context); |
| | | break; |
| | | |
| | | case ISSUE_SEND_1: // 이슈 이메일 템플릿1로 대상자에게 발송 |
| | | mailConstants = MailConstants.ISSUE_SEND_1; |
| | | context = StringTemplateUtil.makeContext(params, locale); |
| | | content = this.springTemplateEngine.process(mailConstants.getMailTemplate(), context); |
| | | break; |
| | | |
| | | case ISSUE_SEND_2: // 이슈 이메일 템플릿2로 대상자에게 발송 |
| | | mailConstants = MailConstants.ISSUE_SEND_2; |
| | | context = StringTemplateUtil.makeContext(params, locale); |
| | | content = this.springTemplateEngine.process(mailConstants.getMailTemplate(), context); |
| | | break; |
| | | |
| | | case ISSUE_SEND_3: // 이슈 이메일 템플릿3로 대상자에게 발송 |
| | | mailConstants = MailConstants.ISSUE_SEND_3; |
| | | context = StringTemplateUtil.makeContext(params, locale); |
| | | content = this.springTemplateEngine.process(mailConstants.getMailTemplate(), context); |
| | | break; |
| | | |
| | | case USER_JOIN_STATISTICS: // 일일 사용자 가입 정보 발송 |
| | | mailConstants = MailConstants.USER_JOIN_STATISTICS; |
| | | context = StringTemplateUtil.makeContext(params, locale); |
| | |
| | | this.sendEmail(this.messageAccessor.message(mailConstants.getTitle(), locale), content, sendUsers, filePaths); |
| | | } |
| | | |
| | | // 이메일 템플릿 찾기 |
| | | public String makeEmailContent(EmailType emailType, Map<String, Object> params) { |
| | | MailConstants mailConstants = null; |
| | | Context context; |
| | | String content = null; |
| | | //params.put("url", this.commonConfiguration.getEmailSendUrl()); |
| | | |
| | | Locale locale = Locale.getDefault(); |
| | | |
| | | switch (emailType) { |
| | | case ISSUE_SEND_1: // 이슈 이메일 템플릿1로 대상자에게 발송 |
| | | mailConstants = MailConstants.ISSUE_SEND_1; |
| | | context = StringTemplateUtil.makeContext(params, locale); |
| | | content = this.springTemplateEngine.process(mailConstants.getMailTemplate(), context); |
| | | break; |
| | | |
| | | case ISSUE_SEND_2: // 이슈 이메일 템플릿2로 대상자에게 발송 |
| | | mailConstants = MailConstants.ISSUE_SEND_2; |
| | | context = StringTemplateUtil.makeContext(params, locale); |
| | | content = this.springTemplateEngine.process(mailConstants.getMailTemplate(), context); |
| | | break; |
| | | |
| | | case ISSUE_SEND_3: // 이슈 이메일 템플릿3로 대상자에게 발송 |
| | | mailConstants = MailConstants.ISSUE_SEND_3; |
| | | context = StringTemplateUtil.makeContext(params, locale); |
| | | content = this.springTemplateEngine.process(mailConstants.getMailTemplate(), context); |
| | | break; |
| | | } |
| | | return content; |
| | | } |
| | | |
| | | |
| | | // 이메일을 발송한다. |
| | | @Override |
| | | @Transactional(readOnly = true) |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | this.javaMailSender.send(message); |
| | | |
| | | } catch (MailSendException e) { |
| | | log.error(e.getMessage()); |
| | | Exception[] exceptions = e.getMessageExceptions(); |
| | |
| | | return results; |
| | | } |
| | | |
| | | // 프로젝트에서 변경된 담당부서를 찾기 위해 사용 |
| | | @Override |
| | | @Transactional(readOnly = true) |
| | | public List<String> notificationDepartmentChange(List<Department> totalDepartments, List<Department> targetDepartments) { |
| | | List<String> results = Lists.newArrayList(); |
| | | |
| | | // 제외 대상자 찾기 |
| | | for (Department department : totalDepartments) { |
| | | boolean excludeCheck = true; |
| | | |
| | | for (Department newDepartment : targetDepartments) { |
| | | if (department.getId().equals(newDepartment.getId())) { |
| | | excludeCheck = false; |
| | | break; |
| | | } |
| | | } |
| | | |
| | | if (excludeCheck) { |
| | | /*List<UserDepartment> userDepartments = this.userDepartmentService.getUserDepartments(department.getId()); |
| | | if(userDepartments != null){ |
| | | for (UserDepartment userDepartment : userDepartments){ |
| | | User user = this.userService.getUser(userDepartment.getUserId()); |
| | | results.add(user.getAccount()); |
| | | } |
| | | }*/ |
| | | results.add(department.getDepartmentName()); |
| | | } |
| | | } |
| | | return results; |
| | | } |
| | | |
| | | // 아직 발송되지 않은 이메일을 조회한다. |
| | | @Override |
| | | @Transactional(readOnly = true) |
| | |
| | | } |
| | | } |
| | | |
| | | // 실시간 이메일 발송 |
| | | @Override |
| | | @Transactional |
| | | public void realTimeSendEmail() { |
| | | |
| | | List<String> sendUsers = this.userService.findByRealTimeNotifyTime(); |
| | | |
| | | for (String sendUser : sendUsers) { |
| | | // 사용자 별로 아직 발송되지 않은 이메일을 조회한다. |
| | | List<SystemEmail> systemEmails = this.findBySendAddressAndSendYn(sendUser); |
| | | StringBuilder emailBuilder = new StringBuilder(); |
| | | User user = this.userService.findByAccount(sendUser); |
| | | |
| | | if (user != null) { |
| | | Locale locale = CommonUtil.getUserLanguage(user.getLanguage()); // 메일을 받는 사용자가 사용하고 있는 언어 정보를 가져온다. |
| | | |
| | | for (SystemEmail systemEmail : systemEmails) { |
| | | // 받는 사람의 언어로 변경하여 예약된 이메일을 발송한다. |
| | | this.makeReservationContextEmail(emailBuilder, systemEmail, locale); |
| | | systemEmail.setSendYn(true); |
| | | } |
| | | // 내용이 있으면 발송 |
| | | if (!StringUtils.isEmpty(emailBuilder.toString())) { |
| | | // 이메일을 발송한다. |
| | | this.sendEmail(this.messageAccessor.message(MsgConstants.REALTIME_EMAIL_TITLE, locale), emailBuilder.toString(), new String[]{CommonUtil.decryptAES128(sendUser)}, null); |
| | | this.systemEmailRepository.saveAll(systemEmails); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 받는 사람의 언어로 변경하여 예약된 이메일을 발송한다. |
| | | private void makeReservationContextEmail(StringBuilder emailBuilder, SystemEmail systemEmail, Locale locale) { |
| | | MailConstants mailConstants; |