]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/result/ResultViewController.java
Refactor package names
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / result / ResultViewController.java
1 package org.argeo.slc.web.mvc.result;
2
3 import java.util.SortedMap;
4 import java.util.TreeMap;
5
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8
9 import org.springframework.web.servlet.ModelAndView;
10 import org.springframework.web.servlet.mvc.ParameterizableViewController;
11
12 import org.argeo.slc.core.structure.tree.TreeSPath;
13 import org.argeo.slc.core.test.tree.PartSubList;
14 import org.argeo.slc.core.test.tree.TreeTestResult;
15 import org.argeo.slc.dao.test.tree.TreeTestResultDao;
16 import org.argeo.slc.structure.StructureElement;
17 import org.argeo.slc.test.TestResultPart;
18 import org.argeo.slc.test.TestStatus;
19
20 public class ResultViewController extends ParameterizableViewController {
21 public final static String MODELKEY_RESULT = "result";
22
23 private final TreeTestResultDao testResultDao;
24
25 public ResultViewController(TreeTestResultDao testResultDao) {
26 this.testResultDao = testResultDao;
27 }
28
29 @Override
30 protected ModelAndView handleRequestInternal(HttpServletRequest request,
31 HttpServletResponse response) throws Exception {
32
33 String uuid = request.getParameter("uuid");
34 TreeTestResult result = testResultDao.getTestResult(uuid);
35
36 SortedMap<TreeSPath, String> toc = generateToc(result);
37
38 SortedMap<TreeSPath, String> describedPaths = new TreeMap<TreeSPath, String>();
39 for (TreeSPath path : toc.keySet()) {
40 describedPaths.put(path, describedPath(path, result));
41 }
42
43 SortedMap<TreeSPath, String> anchors = new TreeMap<TreeSPath, String>();
44 for (TreeSPath path : toc.keySet()) {
45 anchors.put(path, anchor(path));
46 }
47
48 ModelAndView modelAndView = new ModelAndView();
49
50 modelAndView.addObject(MODELKEY_RESULT, result);
51 modelAndView.addObject("toc", toc);
52 modelAndView.addObject("describedPaths", describedPaths);
53 modelAndView.addObject("anchors", anchors);
54 modelAndView.setViewName(getViewName());
55 return modelAndView;
56 }
57
58 private SortedMap<TreeSPath, String> generateToc(TreeTestResult result) {
59 SortedMap<TreeSPath, String> toc = new TreeMap<TreeSPath, String>();
60 for (TreeSPath path : result.getResultParts().keySet()) {
61 PartSubList subList = (PartSubList) result.getResultParts().get(
62 path);
63 boolean isFailed = false;
64 for (TestResultPart part : subList.getParts()) {
65 if (!part.getStatus().equals(TestStatus.PASSED)) {
66 isFailed = true;
67 break;
68 }
69 }
70 fillToc(toc, path, isFailed);
71 }
72 return toc;
73 }
74
75 private void fillToc(SortedMap<TreeSPath, String> toc, TreeSPath path,
76 boolean isFailed) {
77 if (isFailed) {
78 toc.put(path, "failed");
79 } else {
80 if (!toc.containsKey(path)) {
81 toc.put(path, "passed");
82 }
83 }
84
85 if (path.getParent() != null) {
86 fillToc(toc, path.getParent(), isFailed);
87 }
88 }
89
90 private static String anchor(TreeSPath path) {
91 return path.getAsUniqueString().replace(path.getSeparator(), '_');
92 }
93
94 private static String describedPath(TreeSPath path, TreeTestResult ttr) {
95 StringBuffer buf = new StringBuffer("");
96 // TODO :optimize with hierarchy
97 describedPath(path, buf, ttr);
98 return buf.toString();
99 }
100
101 private static void describedPath(TreeSPath path, StringBuffer buf,
102 TreeTestResult ttr) {
103 if (path.getParent() != null) {
104 describedPath(path.getParent(), buf, ttr);
105 }
106 String description = path.getName();
107 StructureElement element = ttr.getElements().get(path);
108 if (element != null) {
109 description = element.getLabel();
110 }
111 buf.append('/').append(description);
112 }
113
114 }