Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package service; import datastructure.NoticeDataList; import email.EncryptProtocol; import email.MailSender; import entity.Admin; import entity.Author; import entity.Branch; import entity.Direction; import entity.EmailNotice; import entity.EmailNoticeFile; import entity.Mailbox; import entity.Message; import entity.Notice; import entity.Notice.NoticeType; import entity.Order; import entity.OrderFile; import entity.ReadyOrderFile; import entity.SmsNotice; import entity.SmsNoticeTime; import entity.Template; import entity.TemplateEventLink; import entity.UploadedFile; import entity.User; import entity.eventType.SystemEventType; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Set; import mvc.support.UrlBuilder; import org.apache.commons.io.FileUtils; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import persistence.EmailNoticeDao; import persistence.NoticeDao; import persistence.SmsNoticeDao; import persistence.SmsNoticeTimeDao; import persistence.TemplateEventLinkDao; import service.TemplateEventLinkService.TemplateType; import service.parent.PrimService; import support.ErrorMess; import support.ServiceResult; import support.Sms4bSender; import support.StringAdapter; import support.ValidatorUtils; import support.attachments.Attachment; import support.attachments.FileAttachment; /** * * @author Rice Pavel */ @Service @Transactional public class NoticeService extends PrimService { protected Logger log = Logger.getLogger(this.getClass()); public enum AddressType { CLIENT, AUTHOR; } @Autowired private PermissionSendMessagesService permissionSendService; @Autowired private NoticeDao noticeDao; @Autowired private TemplateEventLinkDao linkDao; @Autowired private SmsNoticeDao smsNoticeDao; @Autowired private EmailNoticeDao emailNoticeDao; @Autowired private SmsNoticeTimeDao smsNoticeTimeDao; @Autowired private UrlBuilder urlBuilder; @Autowired private TemplateEventLinkService templateEventLinkService; private Integer todayNewOrderSentCount = 0; public Notice find(Long id) { return noticeDao.find(id); } public EmailNotice findEmailNotice(Long id) { return emailNoticeDao.find(id); } public SmsNotice findSmsNotice(Long id) { return smsNoticeDao.find(id); } public void delete(Long id) { _delete(id, noticeDao); } /** * ?? ?? ? * * @param type ?? * @param order * @param readyFilesIds , * ??? ?. ? null - ??? ? * */ public void addSystemClientNotices(SystemEventType type, Order order, Set<Long> readyFilesIds) throws IOException { _addSystemClientNotices(type, order, null, readyFilesIds); } private String getAuthorEmail(Author author) { if (author.getEmail() != null && !author.getEmail().isEmpty()) { return author.getEmail(); } else { return author.getLogin(); } } /** * ?? ?? ? * * @param type ?? * @param order * @param author */ public void addSystemAuthorNotices(SystemEventType type, Order order, Author author) throws IOException { _addSystemAuthorNotices(type, author, order, null); } /** * ?? ? ?, ? * * @param type ?? * @param message ? */ public void addSystemClientNoticesByMessage(SystemEventType type, Message message) throws IOException { _addSystemClientNotices(type, message.getOrder(), message, null); } private void _addSystemClientNotices(SystemEventType type, Order order, Message message, Set<Long> readyFileIds) throws IOException { Long branchId = null; if (order != null && order.getBranch() != null) { branchId = order.getBranch().getBranchId(); } Template smsTemplate = templateEventLinkService.getTemplate(type, TemplateType.ClientSms, branchId); Template emailTemplate = templateEventLinkService.getTemplate(type, TemplateType.ClientEmail, branchId); if (smsTemplate != null) { addSystemSmsNotice(smsTemplate, order, message, order.getClientPhone(), type, null); } if (emailTemplate != null) { addSystemEmailNotice(emailTemplate, order, message, order.getClientEmail(), type, null, readyFileIds); } } private void _addSystemAuthorNotices(SystemEventType type, Author author, Order order, Message message) throws IOException { if (author != null) { Long branchId = order.getBranch().getBranchId(); Template smsTemplate = templateEventLinkService.getTemplate(type, TemplateType.AuthorSms, branchId); Template emailTemplate = templateEventLinkService.getTemplate(type, TemplateType.AuthorEmail, branchId); if (smsTemplate != null) { addSystemSmsNotice(smsTemplate, order, message, author.getPhone(), type, author); } if (emailTemplate != null) { addSystemEmailNotice(emailTemplate, order, message, getAuthorEmail(author), type, author, null); } } } public void addSystemNoticesForOtherEmails(SystemEventType eventType, Order order) throws IOException { addSystemNoticesForOtherEmails(eventType, order, null); } public void addSystemNoticesForOtherEmails(SystemEventType eventType, Message message) throws IOException { addSystemNoticesForOtherEmails(eventType, message.getOrder(), message); } private void addSystemNoticesForOtherEmails(SystemEventType eventType, Order order, Message message) throws IOException { // template emails TemplateEventLink link = linkDao.getByTypeAndBranch(eventType, order.getBranch().getBranchId()); TemplateEventLink link2 = linkDao.getCommonByType(eventType); if (link == null) { log.error(order.getNumber() + "linkBranch null"); link = link2; } else if (link.getStopOtherEmail() == true) { log.error("stob by branch"); } else { if (StringAdapter.isNull(link.getOtherEmails())) { log.error(order.getNumber() + "no email by branch"); if (link2 == null) { log.error(order.getNumber() + "linkCommon null"); } else if (link2.getStopOtherEmail() == true) { log.error(order.getNumber() + "stob by common"); } else { link = link2; log.error(order.getNumber() + "create by common"); } } } if (link != null) { Set<String> emails = link.getOtherEmailList(); log.error(order.getNumber() + link.getOtherEmails()); Template template = templateEventLinkService.getTemplate(eventType, TemplateType.OtherEmail, order.getBranch().getBranchId()); if (template != null && emails != null) { for (String email : emails) { addSystemEmailNotice(template, order, message, email, eventType, null, null); } } else { if (template == null) { log.error("Template not found"); } else { log.error("emailList not found"); } } } else { log.error("link null"); } } /** * ?? ? ?, ? * * @param type ?? * @param message ? */ public void addSystemAuthorNoticesByMessage(SystemEventType type, Message message) throws IOException { Order order = message.getOrder(); Author author = null; if (order != null) { author = order.getAuthor(); } if (author != null) { _addSystemAuthorNotices(type, author, order, message); } } public List<Notice> getByOrder(Order order) { return noticeDao.getByOrder(order); } public List<SmsNotice> getSmsByOrder(Order order) { return smsNoticeDao.getByOrder(order); } public List<SmsNotice> getNotSystemSmsByOrder(Order order) { return smsNoticeDao.getNotSystemByOrder(order); } public List<EmailNotice> getEmailByOrder(Order order) { return emailNoticeDao.getByOrder(order); } public List<EmailNotice> getNotSystemEmailByOrder(Order order) { return emailNoticeDao.getNotSystemByOrder(order); } public List<EmailNotice> getNotSystemEmails() { return emailNoticeDao.getNotSystem(); } /** * ?? ?? ? ? * * @return */ public List<Object[]> getNotSystemEmailsWithOrders() { return emailNoticeDao.getNotSystemWithOrders(); } public NoticeDataList getNotSystemAsDataList() { return emailNoticeDao.getNotSystemAsDataList(); } public NoticeDataList getNotSystemAsDataList(String emailFrom, String searchString) { return emailNoticeDao.getNotSystemAsDataList(emailFrom, searchString); } /** * ? ? * * @return */ public List<Object[]> getDraftsWithOrders(String searchString) { return emailNoticeDao.getDraftsWithOrders(searchString); } public ServiceResult addUserEmailNotice(EmailNotice notice) throws IOException { return addUserEmailNotice(notice, null); } /** * email-? * * @param notice ? * @param files * @return * @throws IOException */ public ServiceResult addUserEmailNotice(EmailNotice notice, MultipartFile[] files) throws IOException { notice.setNoticeType(Notice.NoticeType.USER); notice.setNoticeDate(new Date()); ServiceResult result = new ServiceResult(); validateEmailNotice(notice, result); if (!result.hasErrors()) { saveUserNotice(notice, result); if (!result.hasErrors()) { if (files != null) { for (MultipartFile file : files) { if (file != null && !file.isEmpty()) { EmailNoticeFile fileEntity = new EmailNoticeFile(); fileEntity.setEmailNotice(notice); saveFile(fileEntity, file); } } } } } return result; } /** * * * @param branch * @param email * @param subject * @param message * @return */ public ServiceResult createDraftEmailNotice(Branch branch, String email, String subject, String message) { ServiceResult result = new ServiceResult(); // ? // EmailNotice notice = new EmailNotice(); notice.setDraft(true); notice.setNoticeType(Notice.NoticeType.USER); notice.setBranch(branch); if (ValidatorUtils.isEmail(email)) { notice.setEmail(email); } notice.setSubject(subject); notice.setMessage(message); if (!result.hasErrors()) { setStandartUserParameters(notice); validateEmailNotice(notice, result); if (!result.hasErrors()) { Long id = emailNoticeDao.save(notice); result.setValue(id); } } return result; } /** * * * @param draftId * @param branch null * @param email null * @param subject * @param message */ public ServiceResult updateDraftEmailNotice(Long draftId, Branch branch, String email, String subject, String message) { // ServiceResult result = new ServiceResult(); EmailNotice updateNotice = emailNoticeDao.find(draftId); // ? ? if (updateNotice != null && updateNotice.getDraft() != null && updateNotice.getDraft() == true) { // if (branch != null) { updateNotice.setBranch(branch); } if (email != null && ValidatorUtils.isEmail(email)) { updateNotice.setEmail(email); } updateNotice.setMessage(message); updateNotice.setSubject(subject); validateEmailNotice(updateNotice, result); if (!result.hasErrors()) { emailNoticeDao.update(updateNotice); } } return result; } /** * ? */ public List<EmailNotice> getDrafts() { return emailNoticeDao.getDraft(null); } /** * ? ? * * @param draftId , ??? ?? * @param branch * @param email * @param subject * @param message ? * @param files * @return */ public ServiceResult addUserEmailNoticeByDraft(Long draftId, Branch branch, String email, String subject, String message, MultipartFile[] files) throws IOException { ServiceResult result = new ServiceResult(); EmailNotice draftNotice = emailNoticeDao.find(draftId); draftNotice.setDraft(false); draftNotice.setBranch(branch); draftNotice.setSubject(subject); draftNotice.setMessage(message); draftNotice.setEmail(email); validateEmailNotice(draftNotice, result); if (!result.hasErrors()) { emailNoticeDao.update(draftNotice); if (files != null) { for (MultipartFile file : files) { if (!file.isEmpty()) { EmailNoticeFile fileEnt = new EmailNoticeFile(); fileEnt.setEmailNotice(draftNotice); saveFile(fileEnt, file); } } } } return result; } /** * ??-? * * @param notice ? * @return */ public ServiceResult addUserSmsNotice(SmsNotice notice) { ServiceResult result = new ServiceResult(); notice.setNoticeType(Notice.NoticeType.USER); notice.setNoticeDate(new Date()); saveUserNotice(notice, result); return result; } private void saveUserNotice(Notice notice, ServiceResult result) { setStandartUserParameters(notice); validateSave(notice, noticeDao, result); } private void setStandartUserParameters(Notice notice) { notice.setNoticeDate(new Date()); notice.setUser(authManager.getCurrentUser()); } /** * emailNotice * * @param notice * @param result */ private void validateEmailNotice(EmailNotice notice, ServiceResult result) { if (notice.getDraft() == null || !notice.getDraft()) { if (notice.getEmail() == null || notice.getEmail().isEmpty()) { result.addError("Email: " + ErrorMess.NOT_EMPTY); } } validate(result, notice); } public List<Notice> getAll(Notice.NoticeType noticeType) { return getAll(noticeType, null, null); } public List<Notice> getUsers(Date dateFrom, Date dateTo) { return noticeDao.getAll(Notice.NoticeType.USER, dateFrom, dateTo, null); } public List<Notice> getSystemNotAuthors(Date dateFrom, Date dateTo) { return noticeDao.getAll(Notice.NoticeType.SYSTEM, dateFrom, dateTo, NoticeDao.AuthorType.NOT_AUTHOR); } public List<Notice> getAllNotAuthors(Date dateFrom, Date dateTo) { return noticeDao.getAll(null, dateFrom, dateTo, NoticeDao.AuthorType.NOT_AUTHOR); } public List<Notice> getSystemOnlyAuthors(Date dateFrom, Date dateTo) { return noticeDao.getAll(Notice.NoticeType.SYSTEM, dateFrom, dateTo, NoticeDao.AuthorType.ONLY_AUTHOR); } public List<Notice> getAll(Notice.NoticeType noticeType, Date dateFrom, Date dateTo) { return noticeDao.getAll(noticeType, dateFrom, dateTo, null); } /** * ?? ?? - ?? email, ? ? * * @param eventType ?? * @param order * @param message ? * @param phone * @param email ? email * @param addressType ? - * @throws IOException */ private void addSystemNotices(SystemEventType eventType, Order order, Message message, String phone, String email, AddressType addressType, User recipientUser) throws IOException { // ? ? ? ? Long branchId = order.getBranch().getBranchId(); TemplateEventLinkService.TemplateType smsTemplateType; if (addressType.equals(AddressType.AUTHOR)) { smsTemplateType = TemplateType.AuthorSms; } else { smsTemplateType = TemplateType.ClientSms; } Template smsTemplate = templateEventLinkService.getTemplate(eventType, smsTemplateType, branchId); if (smsTemplate != null) { addSystemSmsNotice(smsTemplate, order, message, phone, eventType, recipientUser); } if (addressType.equals(AddressType.AUTHOR)) { smsTemplateType = TemplateType.AuthorEmail; } else { smsTemplateType = TemplateType.ClientEmail; } Template emailTemplate = templateEventLinkService.getTemplate(eventType, smsTemplateType, branchId); if (emailTemplate != null) { addSystemEmailNotice(emailTemplate, order, message, email, eventType, recipientUser, null); } } private void addSystemSmsNotice(Template smsTemplate, Order order, Message message, String phone, SystemEventType systemEventType, User recipientUser) { if (smsTemplate != null && phone != null && !phone.isEmpty()) { String messageText = getText(smsTemplate.getMessage(), order, message, null); String subjectText = getText(smsTemplate.getSubject(), order, message, null); SmsNotice notice = new SmsNotice(); notice.setMessage(messageText); notice.setSubject(subjectText); notice.setPhone(phone); notice.setNoticeType(Notice.NoticeType.SYSTEM); notice.setNoticeDate(new Date()); notice.setOrder(order); notice.setSystemEventType(systemEventType); notice.setRecipientUser(recipientUser); if (validate(notice)) { smsNoticeDao.save(notice); } } } /** * ?? ? email * * @param emailTemplate template * @param order . null * @param message ?. null. * @param email. ??, ? ? * @param recipientUser. , ? ? * @throws IOException */ private void addSystemEmailNotice(Template emailTemplate, Order order, Message message, String email, SystemEventType systemEventType, User recipientUser, Set<Long> readyFilesIds) throws IOException { if (emailTemplate != null && email != null && !email.isEmpty()) { String messageText = getMessageText(emailTemplate, order, message, readyFilesIds); String subjectText = getText(emailTemplate.getSubject(), order, message, null); EmailNotice notice = new EmailNotice(); notice.setMessage(messageText); notice.setSubject(subjectText); notice.setEmail(email); notice.setNoticeType(Notice.NoticeType.SYSTEM); notice.setBranch(order.getBranch()); notice.setNoticeDate(new Date()); notice.setOrder(order); notice.setSystemEventType(systemEventType); notice.setRecipientUser(recipientUser); if (recipientUser instanceof Author && systemEventType != null && systemEventType.equals(SystemEventType.NEW_ORDER)) { notice.setAuthorDelivery(true); } if (validate(notice)) { emailNoticeDao.save(notice); } else { log.error("dont validate"); } } } private String getMessageText(Template template, Order order, Message message, Set<Long> readyFilesIds) { String messageText = getText(template.getMessage(), order, message, readyFilesIds); return messageText; } private String getFileLinks(Order order) { String str = ""; if (order.getFiles() != null) { for (OrderFile file : order.getFiles()) { str += urlBuilder.orderFileForClient(file) + "<br/>"; } } return str; } private String getReadyFileLinks(Order order, Set<Long> readyFilesIds) { List<ReadyOrderFile> readyFiles = new ArrayList(); if (order.getReadyFiles() != null) { if (readyFilesIds == null) { readyFiles = order.getReadyFiles(); } else { for (ReadyOrderFile file : order.getReadyFiles()) { if (readyFilesIds.contains(file.getFileId())) { readyFiles.add(file); } } } } String str = ""; for (ReadyOrderFile file : readyFiles) { str += urlBuilder.readyOrderFileByClient(file) + "<br/>"; } return str; } private void copyFileByNotice(UploadedFile uploadFile, EmailNotice notice) throws IOException { Long fileId = uploadFile.getFileId(); File file = fileStorage.getFile(fileId); EmailNoticeFile noticeFile = new EmailNoticeFile(); noticeFile.setEmailNotice(notice); noticeFile.setRusname(uploadFile.getRusname()); saveFile(noticeFile, FileUtils.readFileToByteArray(file)); } private Template getSmsTemplate(TemplateEventLink link, AddressType addressType) { if (addressType.equals(AddressType.CLIENT)) { return link.getClientSmsTemplate(); } else { return link.getAuthorSmsTemplate(); } } private Template getEmailTemplate(TemplateEventLink link, AddressType addressType) { if (addressType.equals(AddressType.CLIENT)) { return link.getClientEmailTemplate(); } else { return link.getAuthorEmailTemplate(); } } private String getText(String templateText, Order order, Message message, Set<Long> readyFilesIds) { if (templateText != null) { String orderId = (order.getNumber() != null ? order.getNumber().toString() : ""); String city = (order.getCity() != null ? order.getCity() : ""); String orderTypeName = ""; if (order.getOrderType() != null && order.getOrderType().getName() != null) { orderTypeName = order.getOrderType().getName(); } String subject = (order.getSubject() != null ? order.getSubject() : ""); String comment = (order.getComment() != null ? order.getComment() : ""); String authorComment = (order.getAuthorComment() != null ? order.getAuthorComment() : ""); templateText = templateText.replace("{{order.id}}", orderId); templateText = templateText.replace("{{order.university}}", city); templateText = templateText.replace("{{order.type}}", orderTypeName); templateText = templateText.replace("{{order.spheres}}", getSpheres(order)); templateText = templateText.replace("{{order.theme}}", subject); templateText = templateText.replace("{{order.redline}}", formatDate(order.getDeadlineDate())); templateText = templateText.replace("{{order.description}}", comment); templateText = templateText.replace("{{order.authorComment}}", authorComment); templateText = templateText.replace("{{order.createDate}}", formatDate(order.getOrderDate())); templateText = templateText.replace("{{order.files}}", getFileLinks(order)); templateText = templateText.replace("{{order.readyFiles}}", getReadyFileLinks(order, readyFilesIds)); if (message != null) { templateText = templateText.replace("{{message}}", message.getText()); } if (order.getAuthor() != null) { String fullname = StringAdapter.getString(order.getAuthor().getSurname()) + " " + StringAdapter.getString(order.getAuthor().getName()) + " " + StringAdapter.getString(order.getAuthor().getMiddlename()); templateText = templateText.replace("{{author.fullname}}", fullname); } return templateText; } return ""; } private String formatDate(Date date) { if (date != null) { SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); return sdf.format(date); } return ""; } private String getSpheres(Order order) { String str = ""; if (order.getDirections() != null) { for (Direction dir : order.getDirections()) { str += dir.getName() + ", "; } } return str; } /** * ? ??, ? ? ? * * @throws UnsupportedEncodingException */ @Scheduled(fixedDelay = 60000) public void sendSmsOnTime() throws UnsupportedEncodingException { if (isTimeForSend()) { ServiceResult res = sendSystemSms(); if (res.hasErrors()) { log.warn("errors :" + res.getErrors()); } } sendUserSms(); } private boolean isTimeForSend() { int currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); int currentMinute = Calendar.getInstance().get(Calendar.MINUTE); List<SmsNoticeTime> times = smsNoticeTimeDao.getAll(); for (SmsNoticeTime time : times) { if (time.getHour().equals(currentHour) && time.getMinute().equals(currentMinute)) { return true; } } return false; } /** * ? ? ?? ?? * * @return * @throws UnsupportedEncodingException */ public ServiceResult sendSystemSms() throws UnsupportedEncodingException { List<SmsNotice> notices = smsNoticeDao.getNotSentSystem(); return sendSms(notices); } /** * ? ? ? ?? * * @return * @throws UnsupportedEncodingException */ public ServiceResult sendUserSms() throws UnsupportedEncodingException { List<SmsNotice> notices = smsNoticeDao.getNotSentUser(); return sendSms(notices); } /** * ? ? ?? * * @return * @throws UnsupportedEncodingException */ private ServiceResult sendSms(List<SmsNotice> notices) throws UnsupportedEncodingException { Sms4bSender sender = new Sms4bSender(); ServiceResult finalResult = new ServiceResult(); String source = "vuz"; for (SmsNotice notice : notices) { NoticeType type = notice.getNoticeType(); try { if (type.equals(NoticeType.USER)) { sendOneSms(sender, finalResult, source, notice); } else { User recipient = notice.getRecipientUser(); if (recipient instanceof Admin) { sendOneSms(sender, finalResult, source, notice); } else { if (permissionSendService.nowEnable()) { sendOneSms(sender, finalResult, source, notice); } else { markAsSent(notice); } } } } catch (Exception e) { finalResult.addError(e.getMessage()); } } return finalResult; } private void markAsSent(Notice notice) { notice.setMarkAtSent(true); noticeDao.update(notice); } private void sendOneSms(Sms4bSender sender, ServiceResult finalResult, String source, SmsNotice notice) throws UnsupportedEncodingException { ServiceResult noticeRes = sender.sendSms(source, notice.getPhone(), notice.getMessage()); finalResult.addErrors(noticeRes.getErrors()); if (!noticeRes.hasErrors()) { notice.setSent(true); smsNoticeDao.update(notice); } } @Scheduled(fixedDelay = 60000) public void sendPost() { try { // email-?? List<EmailNotice> notices = emailNoticeDao.getNotSent(); int NOSCount = noticeDao.getTodayNewOrderNoticesSentCount(); // ? ?? for (EmailNotice notice : notices) { NoticeType type = notice.getNoticeType(); User recipient = notice.getRecipientUser(); if (type.equals(NoticeType.USER)) { NOSCount = checkNOSCountAndSend(notice, NOSCount); } else { if (recipient instanceof Admin) { NOSCount = checkNOSCountAndSend(notice, NOSCount); } else { if (permissionSendService.nowEnable()) { NOSCount = checkNOSCountAndSend(notice, NOSCount); } else { markAsSent(notice); } } } } } catch (Exception e) { log.error("SENDPOSTERROR: " + e.getMessage() + ";| " + StringAdapter.getStackExeption(e)); } } private int checkNOSCountAndSend(EmailNotice notice, int NOSCount) { //int NOSCount = getTodayNewOSentCount(); SystemEventType seType = notice.getSystemEventType(); if (SystemEventType.NEW_ORDER == seType) { if (NOSCount < 1500) { Boolean result = sendOneEmail(notice); if (result != null && result) { //todayNewOrderSentCount++; NOSCount++; } } else { markAsSent(notice); log.error("Notice_id=" + StringAdapter.getString(notice.getNoticeId()) + "; lastOrderSentCount=" + NOSCount + ";"); } } else { sendOneEmail(notice); } return NOSCount; //log.error("Notice_id="+StringAdapter.getString(notice.getNoticeId())+"; NoticeType:"+StringAdapter.getString(seType)+"; lastOrderSentCount="+todayNewOrderSentCount+";"); } /*private int getTodayNewOSentCount() { if (todayNewOrderSentCount == 0) { todayNewOrderSentCount = noticeDao.getTodayNewOrderNoticesSentCount(); log.error("BeginOrderSentCount="+todayNewOrderSentCount+";"); } return todayNewOrderSentCount; }*/ /*@Scheduled(cron = "0 0 0 * * ?") public void resetNewOrderNoticeSentCount() { log.error("FinalOrderSentCount="+todayNewOrderSentCount+";"); todayNewOrderSentCount = 0; }*/ private Boolean sendOneEmail(EmailNotice notice) { try { // ?? Notice.NoticeType type = notice.getNoticeType(); Branch branch = notice.getBranch(); // email ? ? ?? Mailbox mailbox = null; if (type.equals(Notice.NoticeType.SYSTEM)) { mailbox = branch.getSystemMailbox(); } else if (type.equals(Notice.NoticeType.USER)) { mailbox = branch.getUserMailbox(); } // ? email ?? if (mailbox != null) { String login = mailbox.getLogin(); String password = mailbox.getPassword(); String outHost = mailbox.getOutHost(); Integer outPort = mailbox.getOutPort(); if (StringAdapter.NotNull(login, password, outHost) && outPort != null) { // EncryptProtocol protocol = (mailbox.getOutProtocol() != null ? mailbox.getOutProtocol() : EncryptProtocol.SSL); MailSender sender = new MailSender(login, password, outHost, outPort, protocol); List<Attachment> attList = new ArrayList(); List<EmailNoticeFile> files = notice.getFiles(); for (EmailNoticeFile file : files) { String path = fileStorage.getFile(file.getFileId()).getPath(); String name = StringAdapter.transliterate(file.getRusname()); Attachment att = new FileAttachment(path, name); attList.add(att); } sender.sendMail(notice.getMessage(), notice.getSubject(), notice.getEmail(), attList); Boolean status = sender.getStatus(); if (status) { notice.setSent(true); notice.setMailboxFrom(mailbox); //notice.setSendError(null); noticeDao.update(notice); } else { notice.setSendError(login + ":" + sender.getErrors().toString()); noticeDao.update(notice); //log.error(sender.getErrors().toString()); } return status; } } } catch (Throwable e) { log.error(e.getMessage()); } return false; } }