1
|
package eu.etaxonomy.cdm.remote.controller;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.ArrayList;
|
5
|
import java.util.List;
|
6
|
|
7
|
import javax.servlet.http.HttpServletRequest;
|
8
|
import javax.servlet.http.HttpServletResponse;
|
9
|
|
10
|
import org.apache.log4j.Logger;
|
11
|
import org.springframework.beans.factory.annotation.Autowired;
|
12
|
import org.springframework.stereotype.Controller;
|
13
|
import org.springframework.web.bind.annotation.RequestMapping;
|
14
|
import org.springframework.web.bind.annotation.RequestMethod;
|
15
|
import org.springframework.web.bind.annotation.RequestParam;
|
16
|
import org.springframework.web.servlet.ModelAndView;
|
17
|
|
18
|
import eu.etaxonomy.cdm.api.service.IClassificationService;
|
19
|
import eu.etaxonomy.cdm.api.service.IStatisticsService;
|
20
|
import eu.etaxonomy.cdm.api.service.statistics.Statistics;
|
21
|
import eu.etaxonomy.cdm.api.service.statistics.StatisticsConfigurator;
|
22
|
import eu.etaxonomy.cdm.api.service.statistics.StatisticsPartEnum;
|
23
|
import eu.etaxonomy.cdm.api.service.statistics.StatisticsTypeEnum;
|
24
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
25
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
26
|
|
27
|
/**
|
28
|
* this controller provides a method to count different entities in the entire
|
29
|
* database as well as from a particular classification items of different types
|
30
|
* can be choosen have a look at doStatistics method
|
31
|
*
|
32
|
* @author s.buers
|
33
|
* @created 07.11.2012
|
34
|
*
|
35
|
*/
|
36
|
@Controller
|
37
|
@RequestMapping(value = { "/statistics" })
|
38
|
public class StatisticsController {
|
39
|
|
40
|
private static final Logger logger = Logger
|
41
|
.getLogger(StatisticsController.class);
|
42
|
|
43
|
private static final IdentifiableEntity ALL_DB = null;
|
44
|
private static final IdentifiableEntity DEFAULT_TYPES = null;
|
45
|
|
46
|
@Autowired
|
47
|
private IClassificationService classificationService;
|
48
|
|
49
|
private IStatisticsService service;
|
50
|
|
51
|
@Autowired
|
52
|
public void setService(IStatisticsService service) {
|
53
|
this.service = service;
|
54
|
}
|
55
|
|
56
|
/**
|
57
|
* example query:
|
58
|
*
|
59
|
* <pre>
|
60
|
* part=ALL&part=CLASSIFICATION&type=DESCRIPTIVE_SOURCE_REFERENCES&type=ALL_TAXA&type=ACCEPTED_TAXA&type=SYNONYMS&type=TAXON_NAMES&type=ALL_REFERENCES&type=NOMECLATURAL_REFERENCES
|
61
|
* </pre>
|
62
|
*
|
63
|
* @param part
|
64
|
* @param type
|
65
|
* @param request
|
66
|
* @param response
|
67
|
* @return
|
68
|
* @throws IOException
|
69
|
*/
|
70
|
@RequestMapping(method = RequestMethod.GET)
|
71
|
public ModelAndView doStatistics(
|
72
|
@RequestParam(value = "part", required = false) String[] part,
|
73
|
@RequestParam(value = "type", required = false) String[] type,
|
74
|
HttpServletRequest request, HttpServletResponse response)
|
75
|
throws IOException {
|
76
|
|
77
|
ModelAndView mv = new ModelAndView();
|
78
|
|
79
|
List<StatisticsConfigurator> configuratorList = createConfiguratorList(
|
80
|
part, type);
|
81
|
List<Statistics> statistics = service
|
82
|
.getCountStatistics(configuratorList);
|
83
|
logger.info("doStatistics() - " + request.getRequestURI());
|
84
|
|
85
|
mv.addObject(statistics);
|
86
|
return mv;
|
87
|
}
|
88
|
|
89
|
private List<StatisticsConfigurator> createConfiguratorList(String[] part,
|
90
|
String[] type) {
|
91
|
|
92
|
ArrayList<StatisticsConfigurator> configuratorList = new ArrayList<StatisticsConfigurator>();
|
93
|
|
94
|
// 1. get types for configurators:
|
95
|
// in our case all the configurators will have the same types
|
96
|
// so we calculate the types once and save them in a helperConfigurator
|
97
|
StatisticsConfigurator helperConfigurator = new StatisticsConfigurator();
|
98
|
|
99
|
if (type != null) {
|
100
|
for (String string : type) {
|
101
|
helperConfigurator.addType(StatisticsTypeEnum.valueOf(string));
|
102
|
}
|
103
|
} else { // if nothing is chosen, count all:
|
104
|
for (StatisticsTypeEnum enumValue : StatisticsTypeEnum.values()) {
|
105
|
helperConfigurator.addType(enumValue);
|
106
|
}
|
107
|
}
|
108
|
|
109
|
// 2. determine the search areas (entire db or classifications) and put
|
110
|
// each of them in a configurator:
|
111
|
|
112
|
// if no part was given:
|
113
|
if (part == null) {
|
114
|
helperConfigurator.addFilter(DEFAULT_TYPES);
|
115
|
configuratorList.add(helperConfigurator);
|
116
|
}
|
117
|
// else parse list of parts and create configurator for each:
|
118
|
else {
|
119
|
helperConfigurator.addFilter(DEFAULT_TYPES);
|
120
|
for (String string : part) {
|
121
|
// System.out.println(StatisticsPartEnum.ALL.toString());
|
122
|
if (string.equals(StatisticsPartEnum.ALL.toString())) {
|
123
|
configuratorList.add(helperConfigurator);
|
124
|
}
|
125
|
else if (string.equals(StatisticsPartEnum.CLASSIFICATION
|
126
|
.toString())) {
|
127
|
List<Classification> classificationsList = classificationService
|
128
|
.listClassifications(null, 0, null, null);
|
129
|
for (Classification classification : classificationsList) {
|
130
|
|
131
|
StatisticsConfigurator newConfigurator = new StatisticsConfigurator();
|
132
|
newConfigurator.setType(helperConfigurator.getType());
|
133
|
newConfigurator.getFilter().addAll(
|
134
|
helperConfigurator.getFilter());
|
135
|
newConfigurator.addFilter(classification);
|
136
|
configuratorList.add(newConfigurator);
|
137
|
}
|
138
|
}
|
139
|
}
|
140
|
|
141
|
}
|
142
|
|
143
|
return configuratorList;
|
144
|
}
|
145
|
|
146
|
}
|