da09321494571d143f8b2e279dd883f229883ad9
[demos/spring-boot] / src / main / java / de / juplo / demo / DemoController.java
1 package de.juplo.demo;
2
3
4 import java.util.HashMap;
5 import java.util.LinkedHashMap;
6 import java.util.Map;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9 import org.springframework.stereotype.Controller;
10 import org.springframework.web.bind.annotation.ModelAttribute;
11 import org.springframework.web.bind.annotation.RequestMapping;
12 import org.springframework.web.bind.annotation.RequestParam;
13
14
15 /**
16  *
17  * @author Kai Moritz
18  */
19 @Controller
20 public class DemoController
21 {
22   private static final Logger LOG =
23       LoggerFactory.getLogger(DemoController.class);
24
25
26   @RequestMapping(path = "/", params = { "!card", "!add", "!remove" })
27   public String display(@ModelAttribute Form form)
28   {
29     for (Integer id : form.cards.keySet())
30       for (String entry : form.cards.get(id).keySet())
31         LOG.info("{} - {}: {}", id, entry, form.cards.get(id).get(entry));
32
33     return "form";
34   }
35
36   @RequestMapping(path = "/", params = { "card=add", "!add", "!remove" })
37   public String addCard(@ModelAttribute Form form)
38   {
39     Integer next =
40         form.cards
41             .keySet()
42             .stream()
43             .reduce(0, (a, b) -> a > b ? a : b) + 1;
44
45     Map<String, Boolean> content = new LinkedHashMap<>();
46     content.put(form.names.get(0), Boolean.FALSE);
47     LOG.info("Adding new card #{} with content: {}", next, content);
48     form.cards.put(next, content);
49
50     for (Integer id : form.cards.keySet())
51       for (String entry : form.cards.get(id).keySet())
52         LOG.info("{} - {}: {}", id, entry, form.cards.get(id).get(entry));
53
54     return "form";
55   }
56
57   @RequestMapping(path = "/", params = { "card!=add", "!add", "!remove" })
58   public String removeCard(@ModelAttribute Form form, @RequestParam Integer card)
59   {
60     Map<String, Boolean> content = form.cards.remove(card);
61     LOG.info("Removed card #{} with content: {}", card, content);
62
63     for (Integer id : form.cards.keySet())
64       for (String entry : form.cards.get(id).keySet())
65         LOG.info("{} - {}: {}", id, entry, form.cards.get(id).get(entry));
66
67     return "form";
68   }
69
70   @RequestMapping(path = "/", params = { "!card", "add", "!remove" })
71   public String addRow(@ModelAttribute Form form, @RequestParam Integer add)
72   {
73     LOG.info("Adding row {} to card #{}", form.names.get(add), add);
74     form.cards.get(add).put(form.names.get(add), Boolean.FALSE);
75
76     for (Integer id : form.cards.keySet())
77       for (String entry : form.cards.get(id).keySet())
78         LOG.info("{} - {}: {}", id, entry, form.cards.get(id).get(entry));
79
80     return "form";
81   }
82
83   @RequestMapping(path = "/", params = { "!card", "!add", "remove" })
84   public String removeRow(@ModelAttribute Form form, @RequestParam String remove)
85   {
86     String[] parts = remove.split(":", 2);
87     Integer card = Integer.valueOf(parts[0]);
88     String row = parts[1];
89     Boolean value = form.cards.get(card).remove(row);
90     LOG.info("Removed row {} with value {} from card #{}", row, value, card);
91
92     if (form.cards.get(card).isEmpty())
93     {
94       LOG.info("Card #{} is empty: Removing card!", card);
95       form.cards.remove(card);
96     }
97
98     for (Integer id : form.cards.keySet())
99       for (String entry : form.cards.get(id).keySet())
100         LOG.info("{} - {}: {}", id, entry, form.cards.get(id).get(entry));
101
102     return "form";
103   }
104 }