Guest User

Untitled

a guest
Oct 29th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. #database
  2. spring.jpa.hibernate.ddl-auto=validate
  3. spring.datasource.url=jdbc:mysql://localhost:3306/notification?true&useSSL=false
  4. spring.datasource.username=root
  5. spring.datasource.password=root
  6.  
  7. package notification.controllers;
  8.  
  9. import notification.entities.Command;
  10. import notification.repo.CommandRepository;
  11. import notification.services.NotificationService;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.data.jpa.repository.Modifying;
  14. import org.springframework.stereotype.Controller;
  15. import org.springframework.transaction.annotation.Transactional;
  16. import org.springframework.web.bind.annotation.GetMapping;
  17. import org.springframework.web.bind.annotation.PostMapping;
  18. import org.springframework.web.bind.annotation.RequestParam;
  19.  
  20. import java.text.ParseException;
  21. import java.util.Map;
  22.  
  23. @Controller
  24. public class NotificationController
  25. {
  26. @Autowired
  27. private CommandRepository commandRepository;
  28.  
  29. @Autowired
  30. private NotificationService notificationService;
  31.  
  32. @GetMapping("/")
  33. public String command(Map<String,Object> model)
  34. {
  35. Iterable<Command> commands = commandRepository.findAll();
  36.  
  37. model.put("commands", commands);
  38. return "command";
  39. }
  40.  
  41. @Transactional
  42. @Modifying
  43. @PostMapping
  44. public String add(@RequestParam String name,
  45. @RequestParam String message,
  46. @RequestParam String time,
  47. @RequestParam String notificationType,
  48. @RequestParam String extra_params,
  49. Map<String, Object> model) throws ParseException {
  50. Command command = new Command(name, message, time, notificationType, extra_params);
  51.  
  52. commandRepository.save(command);
  53.  
  54. Iterable<Command> commands = commandRepository.findAll();
  55.  
  56. model.put("commands", commands);
  57. notificationService.sendNotification(command);
  58.  
  59. return "command";
  60. }
  61. }
  62.  
  63. package notification.entities;
  64.  
  65. import javax.persistence.*;
  66. import java.text.DateFormat;
  67. import java.text.ParseException;
  68. import java.text.SimpleDateFormat;
  69. import java.util.Date;
  70.  
  71. @Entity
  72. @Table(name = "command")
  73. public class Command {
  74. @Id
  75. @GeneratedValue(strategy = GenerationType.AUTO)
  76. private Long id;
  77.  
  78. @Column(name = "name")
  79. private String name;
  80.  
  81. @Column(name = "message")
  82. private String message;
  83.  
  84. @Column(name = "time")
  85. private Date time;
  86.  
  87. @Column(name = "notificationType")
  88. @Enumerated(EnumType.STRING)
  89. private NotificationType notificationType;
  90.  
  91. @Column(name = "extra_params")
  92. private String extra_params;
  93.  
  94. public Command() {
  95. }
  96.  
  97. public Command(String name, String message, Date time, NotificationType notificationType, String extra_params) {
  98. this.name = name;
  99. this.message = message;
  100. this.time = time;
  101. this.notificationType = notificationType;
  102.  
  103. try {
  104. if (notificationType == NotificationType.email)
  105. this.extra_params = extra_params.substring(6);
  106. else
  107. this.extra_params = extra_params.substring(4);
  108. } catch (StringIndexOutOfBoundsException e) {
  109. //The field has to be email= or url=
  110. }
  111.  
  112. try {
  113. if (!Command.checkParams(notificationType, extra_params)) {
  114. this.extra_params = "";
  115. throw new IllegalArgumentException();
  116. }
  117. } catch (IllegalArgumentException e) {
  118. System.err.println("notification type and extra param don't coincide");
  119. }
  120.  
  121.  
  122. }
  123.  
  124. public Command(String name, String message, String time, String notificationType, String extra_params) throws ParseException {
  125. this(name, message, Command.getCorrect(time), NotificationType.fromString(notificationType), extra_params);
  126. }
  127.  
  128. public static Date getCorrect(String time) throws ParseException {
  129. DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  130. Date date;
  131. date = format.parse(time);
  132. return date;
  133. }
  134.  
  135. public static String getCorrect(Date time) {
  136. String date = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(time);
  137.  
  138. return date;
  139. }
  140.  
  141. public static boolean checkParams(NotificationType nt, String extraParam) {
  142. boolean right = true;
  143. try {
  144. if (nt == NotificationType.http) {
  145. right = extraParam.indexOf("url=http://") == 0;
  146. } else if (nt == NotificationType.email) {
  147. right = extraParam.indexOf("email=") == 0;
  148. right &= extraParam.indexOf('@') == extraParam.lastIndexOf('@');
  149. right = right & ((extraParam.indexOf(".ru") == extraParam.length() - 3) || (extraParam.indexOf(".com") == extraParam.length() - 4));
  150. }
  151. } catch (NullPointerException e) {
  152. //The field can't be empty
  153. }
  154. return right;
  155. }
  156.  
  157. public String getExtra_params() {
  158. return extra_params;
  159. }
  160.  
  161. public void setExtra_params(String extra_params) {
  162. this.extra_params = extra_params;
  163. }
  164.  
  165. public NotificationType getNotificationType() {
  166. return notificationType;
  167. }
  168.  
  169. public void setNotificationType(NotificationType notificationType) {
  170. this.notificationType = notificationType;
  171. }
  172.  
  173. public String getName() {
  174. return name;
  175. }
  176.  
  177. public void setName(String name) {
  178. this.name = name;
  179. }
  180.  
  181. public Long getId() {
  182. return id;
  183. }
  184.  
  185. public void setId(Long id) {
  186. this.id = id;
  187. }
  188.  
  189. public String getMessage() {
  190. return message;
  191. }
  192.  
  193. public void setMessage(String message) {
  194. this.message = message;
  195. }
  196.  
  197. public Date getTime() {
  198. return time;
  199. }
  200.  
  201. public void setTime(Date time) {
  202. this.time = time;
  203. }
  204.  
  205. @Override
  206. public String toString() {
  207. return "message == " + message +
  208. "; time == " + time +
  209. "; type == " + notificationType +
  210. "; params == " + extra_params;
  211. }
  212. }
  213.  
  214. import notification.entities.Command;
  215. import org.springframework.data.repository.CrudRepository;
  216.  
  217. public interface CommandRepository extends CrudRepository<Command, Long>
  218. {
  219. }
Add Comment
Please, Sign In to add comment