public String intercept(ActionInvocation invocation) throws Exception { User user = null; HttpServletRequest request = ServletActionContext.getRequest(); int messageCount; userService.setSessionFactory(sessionFactory); user = (User) request.getSession().getAttribute("user"); if (user == null) { System.out.println("redirecting to login"); return MUST_AUTHENTICATE; } else { invocation.getStack().setValue("user", user); List<Message> messages = messageService.findCountByType(user.getCompanyId()); long totalCount = 0; for (int i = 0; i < messages.size(); i++) { if (messages.get(i).getType().compareTo("RFQ") == 0) { request.getSession().putValue("rfqMessageCount", messages.get(i).getCount()); } else if (messages.get(i).getType().compareTo("USER") == 0) { request.getSession().putValue("userMessageCount", messages.get(i).getCount()); } totalCount += messages.get(i).getCount(); } request.getSession().putValue("totalMessageCount", totalCount); return invocation.invoke(); } }
public void sendEmail() { System.out.println("Sending RFQ Response emails"); List<Email> emails = new ArrayList<Email>(); emails = emailService.findByType("RFQ_RESPONDED"); for (int i = 0; i < emails.size(); i++) { Email email = emails.get(i); try { RFQ rfq = rFQService.getById(email.getTransactionId()); List<User> users = userService.findByCompanyId(rfq.getReceiverId()); User from = users.get(0); Session session = Session.getInstance( Emailer.properties, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("*****@*****.**", "codehoppers"); } }); Message message = new MimeMessage(session); message.setFrom(new InternetAddress("*****@*****.**")); message.setRecipients( Message.RecipientType.TO, InternetAddress.parse(rfq.getSender().getEmail())); message.setSubject("RFQ Responded"); List<Template> templates = templateService.findByType("RFQ_RESPONDED"); String body = templates.get(0).getBody(); body = body.replace( "##Name##", rfq.getSender().getFirstName() + " " + rfq.getSender().getLastName()); body = body.replace("##CompanyName##", from.getCompany().getName()); message.setContent(body, "text/html; charset=utf-8"); Transport.send(message); email.setIsSent(1); emailService.addOrUpdate(email); } catch (Exception e) { email.setNumberOfTries(email.getNumberOfTries() + 1); emailService.addOrUpdate(email); } } }