Browse code

reorganise and add tsv

devnewton authored on 16/12/2015 at 18:31:51
Showing 16 changed files
1 1
deleted file mode 100644
... ...
@@ -1,62 +0,0 @@
1
-package im.bci.passgrid.controllers;
2
-
3
-import im.bci.passgrid.data.Passgrid;
4
-import im.bci.passgrid.data.PassgridRepository;
5
-import im.bci.passgrid.frontend.CreatePassgridRQ;
6
-import java.util.Random;
7
-import org.springframework.beans.factory.annotation.Autowired;
8
-import org.springframework.stereotype.Controller;
9
-import org.springframework.ui.Model;
10
-import org.springframework.web.bind.annotation.PathVariable;
11
-import org.springframework.web.bind.annotation.RequestMapping;
12
-import org.springframework.web.bind.annotation.RequestMethod;
13
-import org.springframework.web.servlet.mvc.support.RedirectAttributes;
14
-
15
-@Controller
16
-@RequestMapping("/")
17
-public class FrontendController {
18
-    
19
-    @Autowired
20
-    private PassgridRepository passgridRepository;
21
-    
22
-    @RequestMapping("")
23
-    public String index(Model model) {
24
-        return "index";
25
-    }
26
-    
27
-    @RequestMapping("search")
28
-    public String search(String name, Model model) {
29
-        model.addAttribute("passgrids", passgridRepository.searchByName(name));
30
-        return "search";
31
-    }
32
-    
33
-    @RequestMapping("view/{id}")
34
-    public String passgrid(@PathVariable("id") String id, Model model) {
35
-        model.addAttribute("passgrid", passgridRepository.findOne(id));
36
-        return "view";
37
-    }
38
-    
39
-    @RequestMapping(value = "create", method = RequestMethod.POST)
40
-    public String create(CreatePassgridRQ rq, Model model, RedirectAttributes attributes) {
41
-        Passgrid passgrid = new Passgrid();
42
-        passgrid.setName(rq.getName());
43
-        passgrid.setGrid(generateGrid(rq));
44
-        passgridRepository.save(passgrid);
45
-        attributes.addAttribute("id", passgrid.getId());
46
-        return "redirect:/view/{id}";
47
-    }
48
-    
49
-    private Character[][] generateGrid(CreatePassgridRQ rq) {
50
-        final int lines = rq.getLines();
51
-        final int columns = rq.getColumns();
52
-        final Character[][] grid = new Character[lines][columns];
53
-        Random random = new Random();
54
-        for (int y = 0; y < lines; ++y) {
55
-            for (int x = 0; x < columns; ++x) {
56
-                final String allowedCharacters = rq.getAllowedCharacters();
57
-                grid[y][x] = allowedCharacters.charAt(random.nextInt(allowedCharacters.length()));
58
-            }
59
-        }
60
-        return grid;
61
-    }
62
-}
63 1
new file mode 100644
... ...
@@ -0,0 +1,97 @@
0
+package im.bci.passgrid.controllers;
1
+
2
+import im.bci.passgrid.data.Passgrid;
3
+import im.bci.passgrid.data.PassgridRepository;
4
+import im.bci.passgrid.frontend.CreatePassgridRQ;
5
+import java.io.IOException;
6
+import java.io.PrintWriter;
7
+import java.util.Random;
8
+import javax.servlet.http.HttpServletResponse;
9
+import org.springframework.beans.factory.annotation.Autowired;
10
+import org.springframework.stereotype.Controller;
11
+import org.springframework.ui.Model;
12
+import org.springframework.web.bind.annotation.PathVariable;
13
+import org.springframework.web.bind.annotation.RequestMapping;
14
+import org.springframework.web.bind.annotation.RequestMethod;
15
+import org.springframework.web.servlet.mvc.support.RedirectAttributes;
16
+
17
+@Controller
18
+@RequestMapping("/")
19
+public class PassgridController {
20
+
21
+    @Autowired
22
+    private PassgridRepository passgridRepository;
23
+
24
+    @RequestMapping("")
25
+    public String index(Model model) {
26
+        return "index";
27
+    }
28
+
29
+    @RequestMapping("search")
30
+    public String search(String name, Model model) {
31
+        model.addAttribute("passgrids", passgridRepository.searchByName(name));
32
+        return "search";
33
+    }
34
+
35
+    @RequestMapping("view/{id}")
36
+    public String passgrid(@PathVariable("id") String id, Model model) {
37
+        model.addAttribute("passgrid", passgridRepository.findOne(id));
38
+        return "view";
39
+    }
40
+
41
+    @RequestMapping(value = "tsv/{id}")
42
+    public void tsv(@PathVariable("id") String id, HttpServletResponse response) throws IOException {
43
+        Passgrid passgrid = passgridRepository.findOne(id);
44
+        if (null != passgrid) {
45
+            response.setContentType("text/tsv");
46
+            response.addHeader("Content-Disposition", "attachment; filename=" + passgrid.getName() + ".tsv");
47
+            PrintWriter writer = response.getWriter();
48
+            try {
49
+                String rowSeparator = "";
50
+                if (null != passgrid.getGrid()) {
51
+                    for (Character[] row : passgrid.getGrid()) {
52
+                        writer.append(rowSeparator);
53
+                        if (null != row) {
54
+                            String columnSeparator = "";
55
+                            for (Character c : row) {
56
+                                writer.append(columnSeparator);
57
+                                if (null != c) {
58
+                                    writer.append(c);
59
+                                }
60
+                                columnSeparator = "\t";
61
+                            }
62
+                            rowSeparator = "\n";
63
+                        }
64
+                    }
65
+                }
66
+            } finally {
67
+                writer.close();
68
+            }
69
+        }
70
+
71
+    }
72
+
73
+    @RequestMapping(value = "create", method = RequestMethod.POST)
74
+    public String create(CreatePassgridRQ rq, Model model, RedirectAttributes attributes) {
75
+        Passgrid passgrid = new Passgrid();
76
+        passgrid.setName(rq.getName());
77
+        passgrid.setGrid(generateGrid(rq));
78
+        passgridRepository.save(passgrid);
79
+        attributes.addAttribute("id", passgrid.getId());
80
+        return "redirect:/view/{id}";
81
+    }
82
+
83
+    private Character[][] generateGrid(CreatePassgridRQ rq) {
84
+        final int lines = rq.getLines();
85
+        final int columns = rq.getColumns();
86
+        final Character[][] grid = new Character[lines][columns];
87
+        Random random = new Random();
88
+        for (int y = 0; y < lines; ++y) {
89
+            for (int x = 0; x < columns; ++x) {
90
+                final String allowedCharacters = rq.getAllowedCharacters();
91
+                grid[y][x] = allowedCharacters.charAt(random.nextInt(allowedCharacters.length()));
92
+            }
93
+        }
94
+        return grid;
95
+    }
96
+}
... ...
@@ -24,4 +24,16 @@
24 24
 }
25 25
 .passgrid tbody tr:nth-child(even) td:nth-child(even) {
26 26
     background-color: #CCCCCC;
27
+}
28
+
29
+nav ul {
30
+    list-style-type: none;;
31
+    display: flex;
32
+    padding: 0;
33
+    margin: 8px;
34
+}
35
+
36
+header {
37
+    display: flex;
38
+    align-items: center;
27 39
 }
28 40
\ No newline at end of file
29 41
deleted file mode 100644
... ...
@@ -1,5 +0,0 @@
1
-<meta charset="UTF-8">
2
-<meta name="viewport" content="width=device-width, initial-scale=1.0">
3
-<meta name="robots" content="noindex,nofollow">
4
-<link rel="stylesheet" type="text/css" href="/passgrid.css">
5
-<link rel="icon" type="image/png" href="/favicon.png">
6 1
\ No newline at end of file
7 2
deleted file mode 100644
... ...
@@ -1,13 +0,0 @@
1
-<h2>Create grid</h2>
2
-<form action="/create" method="POST">
3
-    <label for="create-passgrid-name" >Name: </label>
4
-    <input id="create-passgrid-name" type="text" name="name">
5
-    <label for="create-passgrid-allowed-characters" >Lines: </label>
6
-    <input id="create-passgrid-allowed-characters" type="text" name="allowedCharacters" value="0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz">
7
-    <label for="create-passgrid-lines" >Lines: </label>
8
-    <input id="create-passgrid-lines" type="number" name="lines" value="26">
9
-    <label for="create-passgrid-columns" >Columns: </label>
10
-    <input id="create-passgrid-columns" type="number" name="columns" value="10">
11
-    <input type="hidden" name="{{_csrf.parameterName}}" value="{{_csrf.token}}"/>
12
-    <input type="submit" >
13
-</form>
14 1
\ No newline at end of file
... ...
@@ -1,8 +1,9 @@
1 1
 <!DOCTYPE html>
2 2
 <html>
3 3
     <head>
4
-        <title>passgrid error</title>
5
-        {{> common-head}}
4
+        <title>error</title>
5
+        <meta charset="UTF-8">
6
+        <meta name="robots" content="noindex,nofollow">
6 7
     </head>
7 8
     <body>
8 9
         <table>
... ...
@@ -1,11 +1,8 @@
1 1
 <!DOCTYPE html>
2 2
 <html>
3
-    <head>
4
-        <title>passgrid</title>
5
-        {{> common-head}}
6
-    </head>
3
+    {{> partials/head title="home"}}
7 4
     <body>
8
-        {{> search-form}}
9
-        {{> create-form}}
5
+        {{> partials/header}}
6
+        {{> partials/forms/create}}
10 7
     </body>
11 8
 </html>
12 9
\ No newline at end of file
13 10
new file mode 100644
... ...
@@ -0,0 +1,13 @@
0
+<h2>Create grid</h2>
1
+<form action="/create" method="POST">
2
+    <label for="create-passgrid-name" >Name: </label>
3
+    <input id="create-passgrid-name" type="text" name="name">
4
+    <label for="create-passgrid-allowed-characters" >Lines: </label>
5
+    <input id="create-passgrid-allowed-characters" type="text" name="allowedCharacters" value="0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz">
6
+    <label for="create-passgrid-lines" >Lines: </label>
7
+    <input id="create-passgrid-lines" type="number" name="lines" value="26">
8
+    <label for="create-passgrid-columns" >Columns: </label>
9
+    <input id="create-passgrid-columns" type="number" name="columns" value="10">
10
+    <input type="hidden" name="{{_csrf.parameterName}}" value="{{_csrf.token}}"/>
11
+    <input type="submit" >
12
+</form>
0 13
\ No newline at end of file
1 14
new file mode 100644
... ...
@@ -0,0 +1,4 @@
0
+<form action="/search">
1
+    <input id="search-passgrid-name" type="text" name="name">
2
+    <input type="submit" value="Search" >
3
+</form>
0 4
\ No newline at end of file
1 5
new file mode 100644
... ...
@@ -0,0 +1,10 @@
0
+<head>
1
+    <title>passgrid {{title}}</title>
2
+    <meta charset="UTF-8">
3
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
4
+    <meta name="robots" content="noindex,nofollow">
5
+    <link rel="stylesheet" type="text/css" href="/passgrid.css">
6
+    <link rel="icon" type="image/png" href="/favicon.png">
7
+    {{#> @partial-block }}
8
+    {{/@partial-block}}
9
+</head>
0 10
\ No newline at end of file
1 11
new file mode 100644
... ...
@@ -0,0 +1,8 @@
0
+<header>
1
+    <nav>
2
+        <ul>
3
+            <li><a href="/">Home</a></li>
4
+        </ul>
5
+    </nav>
6
+    {{> partials/forms/search}}
7
+</header>
0 8
\ No newline at end of file
1 9
new file mode 100644
... ...
@@ -0,0 +1,25 @@
0
+<table class="passgrid">
1
+    <thead>
2
+        <tr>
3
+            <td colspan="{{passgridColspan}}"><a href="/view/{{id}}">{{name}}</a></td>
4
+        </tr>
5
+    </thead>
6
+    <tbody>
7
+        {{#each grid}}
8
+        {{#if @first}}
9
+        <tr>
10
+            <th></th>
11
+            {{#each this}}
12
+            <th>{{@index_1}}</th>
13
+            {{/each}}            
14
+        </tr>
15
+        {{/if}}
16
+        <tr>
17
+            <th>{{@index_1}}</th>
18
+            {{#each this}}
19
+            <td>{{this}}</td>
20
+            {{/each}}
21
+        </tr>
22
+        {{/each}}
23
+    </tbody>
24
+</table>
0 25
\ No newline at end of file
1 26
deleted file mode 100644
... ...
@@ -1,25 +0,0 @@
1
-<table class="passgrid">
2
-    <thead>
3
-        <tr>
4
-            <td colspan="{{passgridColspan}}">{{name}}</td>
5
-        </tr>
6
-    </thead>
7
-    <tbody>
8
-        {{#each grid}}
9
-        {{#if @first}}
10
-        <tr>
11
-            <th></th>
12
-            {{#each this}}
13
-            <th>{{@index_1}}</th>
14
-            {{/each}}            
15
-        </tr>
16
-        {{/if}}
17
-        <tr>
18
-            <th>{{@index_1}}</th>
19
-            {{#each this}}
20
-            <td>{{this}}</td>
21
-            {{/each}}
22
-        </tr>
23
-        {{/each}}
24
-    </tbody>
25
-</table>
26 1
\ No newline at end of file
27 2
deleted file mode 100644
... ...
@@ -1,5 +0,0 @@
1
-<form action="/search">
2
-    <label for="search-passgrid-name" >Search grid: </label>
3
-    <input id="search-passgrid-name" type="text" name="name">
4
-    <input type="submit" >
5
-</form>
6 1
\ No newline at end of file
... ...
@@ -1,14 +1,11 @@
1 1
 <!DOCTYPE html>
2 2
 <html>
3
-    <head>
4
-        <title>passgrid search</title>
5
-        {{> common-head}}
6
-    </head>
3
+    {{> partials/head title="search"}}
7 4
     <body>
8
-        {{> search-form}}
5
+        {{> partials/header}}
9 6
         {{#each passgrids}}
10 7
         <div>
11
-            <a href="/view/{{this.id}}">{{this.id}}</a>
8
+            {{> partials/passgrid this}}
12 9
         </div>
13 10
         {{/each}}
14 11
     </body>
... ...
@@ -1,12 +1,9 @@
1 1
 <!DOCTYPE html>
2 2
 <html>
3
-    <head>
4
-        <title>passgrid</title>
5
-        {{> common-head}}
6
-    </head>
3
+    {{> partials/head title="search"}}
7 4
     <body>
8
-        {{> search-form}}
9
-        {{> passgrid passgrid}}
10
-        {{> create-form}}
5
+        {{> partials/header}}
6
+        <a href="/tsv/{{passgrid.id}}">Download TSV</a>
7
+        {{> partials/passgrid passgrid}}
11 8
     </body>
12
-</html>
13 9
\ No newline at end of file
10
+</html>