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