Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #database
- spring.jpa.hibernate.ddl-auto=validate
- spring.datasource.url=jdbc:mysql://localhost:3306/notification?true&useSSL=false
- spring.datasource.username=root
- spring.datasource.password=root
- package notification.controllers;
- import notification.entities.Command;
- import notification.repo.CommandRepository;
- import notification.services.NotificationService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.jpa.repository.Modifying;
- import org.springframework.stereotype.Controller;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import java.text.ParseException;
- import java.util.Map;
- @Controller
- public class NotificationController
- {
- @Autowired
- private CommandRepository commandRepository;
- @Autowired
- private NotificationService notificationService;
- @GetMapping("/")
- public String command(Map<String,Object> model)
- {
- Iterable<Command> commands = commandRepository.findAll();
- model.put("commands", commands);
- return "command";
- }
- @Transactional
- @Modifying
- @PostMapping
- public String add(@RequestParam String name,
- @RequestParam String message,
- @RequestParam String time,
- @RequestParam String notificationType,
- @RequestParam String extra_params,
- Map<String, Object> model) throws ParseException {
- Command command = new Command(name, message, time, notificationType, extra_params);
- commandRepository.save(command);
- Iterable<Command> commands = commandRepository.findAll();
- model.put("commands", commands);
- notificationService.sendNotification(command);
- return "command";
- }
- }
- package notification.entities;
- import javax.persistence.*;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- @Entity
- @Table(name = "command")
- public class Command {
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private Long id;
- @Column(name = "name")
- private String name;
- @Column(name = "message")
- private String message;
- @Column(name = "time")
- private Date time;
- @Column(name = "notificationType")
- @Enumerated(EnumType.STRING)
- private NotificationType notificationType;
- @Column(name = "extra_params")
- private String extra_params;
- public Command() {
- }
- public Command(String name, String message, Date time, NotificationType notificationType, String extra_params) {
- this.name = name;
- this.message = message;
- this.time = time;
- this.notificationType = notificationType;
- try {
- if (notificationType == NotificationType.email)
- this.extra_params = extra_params.substring(6);
- else
- this.extra_params = extra_params.substring(4);
- } catch (StringIndexOutOfBoundsException e) {
- //The field has to be email= or url=
- }
- try {
- if (!Command.checkParams(notificationType, extra_params)) {
- this.extra_params = "";
- throw new IllegalArgumentException();
- }
- } catch (IllegalArgumentException e) {
- System.err.println("notification type and extra param don't coincide");
- }
- }
- public Command(String name, String message, String time, String notificationType, String extra_params) throws ParseException {
- this(name, message, Command.getCorrect(time), NotificationType.fromString(notificationType), extra_params);
- }
- public static Date getCorrect(String time) throws ParseException {
- DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
- Date date;
- date = format.parse(time);
- return date;
- }
- public static String getCorrect(Date time) {
- String date = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(time);
- return date;
- }
- public static boolean checkParams(NotificationType nt, String extraParam) {
- boolean right = true;
- try {
- if (nt == NotificationType.http) {
- right = extraParam.indexOf("url=http://") == 0;
- } else if (nt == NotificationType.email) {
- right = extraParam.indexOf("email=") == 0;
- right &= extraParam.indexOf('@') == extraParam.lastIndexOf('@');
- right = right & ((extraParam.indexOf(".ru") == extraParam.length() - 3) || (extraParam.indexOf(".com") == extraParam.length() - 4));
- }
- } catch (NullPointerException e) {
- //The field can't be empty
- }
- return right;
- }
- public String getExtra_params() {
- return extra_params;
- }
- public void setExtra_params(String extra_params) {
- this.extra_params = extra_params;
- }
- public NotificationType getNotificationType() {
- return notificationType;
- }
- public void setNotificationType(NotificationType notificationType) {
- this.notificationType = notificationType;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getMessage() {
- return message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- public Date getTime() {
- return time;
- }
- public void setTime(Date time) {
- this.time = time;
- }
- @Override
- public String toString() {
- return "message == " + message +
- "; time == " + time +
- "; type == " + notificationType +
- "; params == " + extra_params;
- }
- }
- import notification.entities.Command;
- import org.springframework.data.repository.CrudRepository;
- public interface CommandRepository extends CrudRepository<Command, Long>
- {
- }
- package notification.services;
- import notification.entities.Command;
- import notification.entities.UrlContent;
- import notification.repo.CommandRepository;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.mail.MailException;
- import org.springframework.mail.MailMessage;
- import org.springframework.mail.SimpleMailMessage;
- import org.springframework.mail.javamail.JavaMailSender;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Service;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.List;
- @Service
- public class NotificationService
- {
- private static final String CRON = "*/5 * * * * *";
- private UrlContent urlContent = new UrlContent();
- private JavaMailSender javaMailSender;
- private SimpleMailMessage mailMes;
- private String httpContent;
- private String email = "email";
- private String http = "http";
- private String time;
- private List<String> timeMailList = new ArrayList<>();
- private List<String> timeHttpList = new ArrayList<>();
- private List<MailMessage> mailList = new ArrayList<>();
- private List<String> httpList = new ArrayList<>();
- @Autowired
- public CommandRepository commandRepository;
- @Autowired
- public NotificationService(JavaMailSender javaMailSender)
- {
- this.javaMailSender = javaMailSender;
- }
- public void sendNotification(Command command)
- {
- SimpleMailMessage mailMessage = new SimpleMailMessage();
- mailMessage.setTo(command.getExtra_params());
- mailMessage.setFrom("NoitificationServiceTest99@gmail.com");
- mailMessage.setSubject("Напоминалка");
- mailMessage.setText(command.getTime() + " - id: " + command.getId() + " message: " + command.getMessage());
- time = Command.getCorrect(command.getTime());
- mailMes = mailMessage;
- if (command.getNotificationType().name().equals(email) && command.getTime() != null && !command.getExtra_params().equals(""))
- {
- mailList.add(mailMes);
- timeMailList.add(time);
- }
- else if(command.getNotificationType().name().equals(http) && command.getTime() != null && !command.getExtra_params().equals(""))
- {
- httpContent = urlContent.staticUrlResult("", command.getExtra_params());
- timeHttpList.add(time);
- httpList.add(httpContent);
- }
- }
- @Scheduled(cron = CRON)
- public void notifyMailOrHttp()
- {
- long l = System.currentTimeMillis();
- String curStringDate = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(l);
- if(mailList != null && !timeMailList.isEmpty())
- {
- for (int i = 0; i < timeMailList.size(); i++)
- {
- if (curStringDate.equals(timeMailList.get(i)))
- {
- try
- {
- javaMailSender.send((SimpleMailMessage) mailList.get(i));
- timeMailList.remove(i);
- mailList.remove(i);
- // System.out.println("Mail is sent");
- }
- catch (MailException e)
- {
- System.err.println("Mail isn't sent -> " + mailList.get(i) + " == Could not parse mail, " +
- "local address contains illegal character in string or doesn't correspond to a template");
- timeMailList.remove(i);
- mailList.remove(i);
- }
- }
- }
- }
- else if(httpList != null && !timeHttpList.isEmpty())
- {
- for(int i = 0; i < timeHttpList.size(); i++)
- {
- if(curStringDate.equals(timeHttpList.get(i)))
- {
- System.out.println(timeHttpList.get(i) + "n" + httpList.get(i));
- httpList.remove(i);
- timeHttpList.remove(i);
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment