View Javadoc
1   /*
2    * Copyright 2018-2022 Medical Information Systems Research Group (https://medical.zcu.cz),
3    * Department of Computer Science and Engineering, University of West Bohemia.
4    * Address: Univerzitni 8, 306 14 Plzen, Czech Republic.
5    *
6    * Author Petr Vcelak (vcelak@kiv.zcu.cz).
7    *
8    * This file is part of MRECore project.
9    *
10   * MRECore is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU General Public License as published by
12   * the Free Software Foundation, either version 3 of the License.
13   *
14   * MRECore is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17   * GNU General Public License for more details.
18   *
19   * You should have received a copy of the GNU General Public License
20   * along with MRECore. If not, see <http://www.gnu.org/licenses/>.
21   */
22  package cz.zcu.mre.data.support;
23  
24  import static cz.zcu.mre.data.support.Message.MESSAGE_ATTRIBUTE;
25  import org.springframework.ui.Model;
26  import org.springframework.web.servlet.mvc.support.RedirectAttributes;
27  
28  /**
29   * 
30   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
31   */
32  public final class MessageHelper {
33  
34      private MessageHelper() {
35  
36      }
37  
38      public static void addSuccessAttribute(RedirectAttributes ra, String message, Object... args) {
39          addAttribute(ra, message, Message.Type.SUCCESS, args);
40      }
41  
42      public static void addErrorAttribute(RedirectAttributes ra, String message, Object... args) {
43          addAttribute(ra, message, Message.Type.DANGER, args);
44      }
45  
46      public static void addInfoAttribute(RedirectAttributes ra, String message, Object... args) {
47          addAttribute(ra, message, Message.Type.INFO, args);
48      }
49  
50      public static void addWarningAttribute(RedirectAttributes ra, String message, Object... args) {
51          addAttribute(ra, message, Message.Type.WARNING, args);
52      }
53  
54      private static void addAttribute(RedirectAttributes ra, String message, Message.Type type, Object... args) {
55          ra.addFlashAttribute(MESSAGE_ATTRIBUTE, new Message(message, type, args));
56      }
57  
58      public static void addSuccessAttribute(Model model, String message, Object... args) {
59          addAttribute(model, message, Message.Type.SUCCESS, args);
60      }
61  
62      public static void addErrorAttribute(Model model, String message, Object... args) {
63          addAttribute(model, message, Message.Type.DANGER, args);
64      }
65  
66      public static void addInfoAttribute(Model model, String message, Object... args) {
67          addAttribute(model, message, Message.Type.INFO, args);
68      }
69  
70      public static void addWarningAttribute(Model model, String message, Object... args) {
71          addAttribute(model, message, Message.Type.WARNING, args);
72      }
73  
74      private static void addAttribute(Model model, String message, Message.Type type, Object... args) {
75          model.addAttribute(MESSAGE_ATTRIBUTE, new Message(message, type, args));
76      }
77  }