1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2009 EDIT 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
|
7
|
* 1.1 See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.remote.controller;
|
11
|
|
12
|
import io.swagger.annotations.Api;
|
13
|
|
14
|
import java.awt.Color;
|
15
|
import java.io.IOException;
|
16
|
import java.util.Arrays;
|
17
|
import java.util.EnumSet;
|
18
|
import java.util.List;
|
19
|
import java.util.Map;
|
20
|
import java.util.Set;
|
21
|
import java.util.UUID;
|
22
|
|
23
|
import javax.servlet.http.HttpServletRequest;
|
24
|
import javax.servlet.http.HttpServletResponse;
|
25
|
|
26
|
import org.springframework.beans.factory.annotation.Autowired;
|
27
|
import org.springframework.stereotype.Controller;
|
28
|
import org.springframework.web.bind.WebDataBinder;
|
29
|
import org.springframework.web.bind.annotation.InitBinder;
|
30
|
import org.springframework.web.bind.annotation.PathVariable;
|
31
|
import org.springframework.web.bind.annotation.RequestMapping;
|
32
|
import org.springframework.web.bind.annotation.RequestMethod;
|
33
|
import org.springframework.web.bind.annotation.RequestParam;
|
34
|
import org.springframework.web.servlet.ModelAndView;
|
35
|
|
36
|
import com.fasterxml.jackson.core.JsonParseException;
|
37
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
38
|
|
39
|
import eu.etaxonomy.cdm.api.service.IDescriptionService;
|
40
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
41
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
42
|
import eu.etaxonomy.cdm.api.service.IVocabularyService;
|
43
|
import eu.etaxonomy.cdm.api.service.description.TransmissionEngineDistribution;
|
44
|
import eu.etaxonomy.cdm.api.service.description.TransmissionEngineDistribution.AggregationMode;
|
45
|
import eu.etaxonomy.cdm.api.service.dto.DistributionInfoDTO;
|
46
|
import eu.etaxonomy.cdm.api.service.dto.DistributionInfoDTO.InfoPart;
|
47
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
48
|
import eu.etaxonomy.cdm.api.utility.DistributionOrder;
|
49
|
import eu.etaxonomy.cdm.common.JvmLimitsException;
|
50
|
import eu.etaxonomy.cdm.common.monitor.IRestServiceProgressMonitor;
|
51
|
import eu.etaxonomy.cdm.ext.geo.CondensedDistributionRecipe;
|
52
|
import eu.etaxonomy.cdm.ext.geo.EditGeoServiceUtilities;
|
53
|
import eu.etaxonomy.cdm.ext.geo.IEditGeoService;
|
54
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
55
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
56
|
import eu.etaxonomy.cdm.model.description.Feature;
|
57
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
58
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
59
|
import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
|
60
|
import eu.etaxonomy.cdm.model.location.NamedAreaType;
|
61
|
import eu.etaxonomy.cdm.model.name.Rank;
|
62
|
import eu.etaxonomy.cdm.persistence.dto.TermDto;
|
63
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
64
|
import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
|
65
|
import eu.etaxonomy.cdm.remote.controller.util.ProgressMonitorUtil;
|
66
|
import eu.etaxonomy.cdm.remote.editor.DefinedTermBaseList;
|
67
|
import eu.etaxonomy.cdm.remote.editor.TermBaseListPropertyEditor;
|
68
|
import eu.etaxonomy.cdm.remote.editor.TermBasePropertyEditor;
|
69
|
import eu.etaxonomy.cdm.remote.l10n.LocaleContext;
|
70
|
|
71
|
/**
|
72
|
* TODO write controller documentation
|
73
|
*
|
74
|
* @author a.kohlbecker
|
75
|
* @date 24.03.2009
|
76
|
*/
|
77
|
@Controller
|
78
|
@Api("description")
|
79
|
@RequestMapping(value = {"/description"})
|
80
|
public class DescriptionListController extends IdentifiableListController<DescriptionBase, IDescriptionService> {
|
81
|
|
82
|
|
83
|
@Autowired
|
84
|
private ITermService termService;
|
85
|
|
86
|
@Autowired
|
87
|
private IVocabularyService vocabularyService ;
|
88
|
|
89
|
@Autowired
|
90
|
private ITaxonService taxonService;
|
91
|
|
92
|
@Autowired
|
93
|
private IEditGeoService geoService;
|
94
|
|
95
|
@Autowired
|
96
|
public TransmissionEngineDistribution transmissionEngineDistribution;
|
97
|
|
98
|
@Autowired
|
99
|
public ProgressMonitorController progressMonitorController;
|
100
|
|
101
|
protected static final List<String> DESCRIPTION_ELEMENT_INIT_STRATEGY = Arrays.asList(new String []{
|
102
|
"$",
|
103
|
"multilanguageText",
|
104
|
});
|
105
|
|
106
|
/**
|
107
|
* There should only be one longtime processes
|
108
|
* therefore the according progress monitor uuid is stored in
|
109
|
* this static field.
|
110
|
*/
|
111
|
private static UUID transmissionEngineMonitorUuid = null;
|
112
|
|
113
|
|
114
|
@Override
|
115
|
@Autowired
|
116
|
public void setService(IDescriptionService service) {
|
117
|
this.service = service;
|
118
|
}
|
119
|
|
120
|
@InitBinder
|
121
|
@Override
|
122
|
public void initBinder(WebDataBinder binder) {
|
123
|
super.initBinder(binder);
|
124
|
binder.registerCustomEditor(DefinedTermBaseList.class, new TermBaseListPropertyEditor<Feature>(termService));
|
125
|
binder.registerCustomEditor(NamedAreaLevel.class, new TermBasePropertyEditor<NamedAreaLevel>(termService));
|
126
|
binder.registerCustomEditor(Rank.class, new TermBasePropertyEditor<Rank>(termService));
|
127
|
}
|
128
|
|
129
|
protected List<String> getDescriptionInfoInitStrategy(){
|
130
|
return getInitializationStrategy();
|
131
|
}
|
132
|
|
133
|
|
134
|
/**
|
135
|
* Runs the {@link TransmissionEngineDistribution} in a separate Thread and
|
136
|
* responds with a redirect to a progress monitor REST service end point.
|
137
|
* <p>
|
138
|
*
|
139
|
* @param mode
|
140
|
* one of <code>byAreas</code>, <code>byRanks</code>,
|
141
|
* <code>byAreasAndRanks</code>
|
142
|
* @param frontendBaseUrl
|
143
|
* the cdm server instance base URL, this is needed for the a
|
144
|
* proper redirect URL when the service is running behind a
|
145
|
* reverse HTTP proxy
|
146
|
* @param priority
|
147
|
* the priority for the Thread to spawn, see
|
148
|
* {@link Thread#setPriority(int)}, defaults to 3
|
149
|
* @param targetAreaLevel
|
150
|
* The level of target areas to project the distributions to.
|
151
|
* @param lowerRank
|
152
|
* @param upperRank
|
153
|
*
|
154
|
* @param request
|
155
|
* @param response
|
156
|
* @return
|
157
|
* @throws IOException
|
158
|
*/
|
159
|
@RequestMapping(value = { "accumulateDistributions" }, method = RequestMethod.GET)
|
160
|
public ModelAndView doAccumulateDistributions(
|
161
|
@RequestParam(value= "mode", required = true) final AggregationMode mode,
|
162
|
@RequestParam(value = "frontendBaseUrl", required = false) String frontendBaseUrl,
|
163
|
@RequestParam(value = "priority", required = false) Integer priority,
|
164
|
@RequestParam(value = "targetAreaLevel", required = true) final NamedAreaLevel targetAreaLevel,
|
165
|
@RequestParam(value = "lowerRank", required = false) Rank lowerRank,
|
166
|
@RequestParam(value = "upperRank", required = false) Rank upperRank,
|
167
|
HttpServletRequest request,
|
168
|
HttpServletResponse response) throws IOException {
|
169
|
|
170
|
logger.info("doAccumulateDistributions()" + request.getRequestURI());
|
171
|
|
172
|
// transmissionEngineDistribution.updatePriorities();
|
173
|
|
174
|
String processLabel = "accumulating distributions";
|
175
|
|
176
|
final Rank _lowerRank = lowerRank != null ? lowerRank : Rank.UNKNOWN_RANK(); // this is the lowest rank
|
177
|
final Rank _upperRank = upperRank != null ? upperRank : Rank.GENUS();
|
178
|
|
179
|
ProgressMonitorUtil progressUtil = new ProgressMonitorUtil(progressMonitorController);
|
180
|
|
181
|
final List<String> term_init_strategy = Arrays.asList(new String []{
|
182
|
"representations"
|
183
|
});
|
184
|
|
185
|
if (!progressMonitorController.isMonitorRunning(transmissionEngineMonitorUuid)) {
|
186
|
transmissionEngineMonitorUuid = progressUtil.registerNewMonitor();
|
187
|
Thread subThread = new Thread() {
|
188
|
@Override
|
189
|
public void run() {
|
190
|
Pager<NamedArea> areaPager = termService.list(targetAreaLevel, (NamedAreaType) null,
|
191
|
null, null, (List<OrderHint>) null, term_init_strategy);
|
192
|
try {
|
193
|
transmissionEngineDistribution.accumulate(mode, areaPager.getRecords(), _lowerRank, _upperRank,
|
194
|
null, progressMonitorController.getMonitor(transmissionEngineMonitorUuid));
|
195
|
} catch (JvmLimitsException e) {
|
196
|
IRestServiceProgressMonitor monitor = progressMonitorController.getMonitor(transmissionEngineMonitorUuid);
|
197
|
monitor.setIsFailed(true);
|
198
|
monitor.setFeedback(e);
|
199
|
}
|
200
|
}
|
201
|
};
|
202
|
if(priority == null) {
|
203
|
priority = AbstractController.DEFAULT_BATCH_THREAD_PRIORITY;
|
204
|
}
|
205
|
subThread.setPriority(priority);
|
206
|
subThread.start();
|
207
|
}
|
208
|
|
209
|
// send redirect "see other"
|
210
|
return progressUtil.respondWithMonitor(frontendBaseUrl, request, response, processLabel, transmissionEngineMonitorUuid);
|
211
|
}
|
212
|
|
213
|
@RequestMapping(value = "namedAreasInUse", method = RequestMethod.GET)
|
214
|
public Pager<TermDto> doPageNamedAreasInUse(
|
215
|
@RequestParam(value = "includeAllParents", required = false) boolean includeAllParents,
|
216
|
@RequestParam(value = "pageSize", required = false) Integer pageSize,
|
217
|
@RequestParam(value = "pageNumber", required = false) Integer pageNumber, HttpServletRequest request,
|
218
|
HttpServletResponse response) throws IOException {
|
219
|
|
220
|
logger.info("doPageNamedAreasInUse : " + requestPathAndQuery(request));
|
221
|
|
222
|
PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);
|
223
|
pagerParams.normalizeAndValidate(response);
|
224
|
|
225
|
Pager<TermDto> pager = service.pageNamedAreasInUse(includeAllParents, pagerParams.getPageSize(), pagerParams.getPageIndex());
|
226
|
|
227
|
localizeTerms(pager);
|
228
|
|
229
|
return pager;
|
230
|
}
|
231
|
|
232
|
/**
|
233
|
* @param taxonUuid
|
234
|
* @param parts
|
235
|
* possible values: condensedStatus, tree, mapUriParams,
|
236
|
* elements,
|
237
|
* @param subAreaPreference
|
238
|
* @param statusOrderPreference
|
239
|
* @param hideMarkedAreasList
|
240
|
* @param omitLevels
|
241
|
* @param request
|
242
|
* @param response
|
243
|
* @param distributionOrder
|
244
|
* Default is LABEL
|
245
|
* @param recipe
|
246
|
* The recipe for creating the condensed distribution status
|
247
|
* @return
|
248
|
* @throws IOException
|
249
|
* @throws JsonMappingException
|
250
|
* @throws JsonParseException
|
251
|
*/
|
252
|
@RequestMapping(value = "distributionInfoFor/{uuid}", method = RequestMethod.GET)
|
253
|
public ModelAndView doGetDistributionInfo(
|
254
|
@PathVariable("uuid") UUID taxonUuid,
|
255
|
@RequestParam("part") Set<InfoPart> partSet,
|
256
|
@RequestParam(value = "subAreaPreference", required = false) boolean subAreaPreference,
|
257
|
@RequestParam(value = "statusOrderPreference", required = false) boolean statusOrderPreference,
|
258
|
@RequestParam(value = "hiddenAreaMarkerType", required = false) DefinedTermBaseList<MarkerType> hideMarkedAreasList,
|
259
|
@RequestParam(value = "omitLevels", required = false) Set<NamedAreaLevel> omitLevels,
|
260
|
@RequestParam(value = "statusColors", required = false) String statusColorsString,
|
261
|
@RequestParam(value = "distributionOrder", required = false, defaultValue="LABEL") DistributionOrder distributionOrder,
|
262
|
@RequestParam(value = "recipe", required = false, defaultValue="EuroPlusMed") CondensedDistributionRecipe recipe,
|
263
|
HttpServletRequest request,
|
264
|
HttpServletResponse response) throws JsonParseException, JsonMappingException, IOException {
|
265
|
|
266
|
logger.info("doGetDistributionInfo() - " + requestPathAndQuery(request));
|
267
|
|
268
|
ModelAndView mv = new ModelAndView();
|
269
|
|
270
|
Set<MarkerType> hideMarkedAreas = null;
|
271
|
if(hideMarkedAreasList != null){
|
272
|
hideMarkedAreas = hideMarkedAreasList.asSet();
|
273
|
}
|
274
|
|
275
|
EnumSet<InfoPart> parts = EnumSet.copyOf(partSet);
|
276
|
|
277
|
Map<PresenceAbsenceTerm, Color> presenceAbsenceTermColors = EditGeoServiceUtilities.buildStatusColorMap(statusColorsString, termService, vocabularyService);
|
278
|
|
279
|
DistributionInfoDTO dto = geoService.composeDistributionInfoFor(parts, taxonUuid,
|
280
|
subAreaPreference, statusOrderPreference, hideMarkedAreas, omitLevels,
|
281
|
presenceAbsenceTermColors, LocaleContext.getLanguages(),
|
282
|
getDescriptionInfoInitStrategy(), recipe, distributionOrder);
|
283
|
|
284
|
mv.addObject(dto);
|
285
|
|
286
|
return mv;
|
287
|
}
|
288
|
|
289
|
}
|