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