adding missing editor
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / DescriptionController.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.cdm.remote.controller;
12
13 import java.io.IOException;
14 import java.util.Arrays;
15 import java.util.EnumSet;
16 import java.util.List;
17 import java.util.Set;
18 import java.util.UUID;
19
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
22
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.stereotype.Controller;
25 import org.springframework.web.bind.WebDataBinder;
26 import org.springframework.web.bind.annotation.InitBinder;
27 import org.springframework.web.bind.annotation.PathVariable;
28 import org.springframework.web.bind.annotation.RequestMapping;
29 import org.springframework.web.bind.annotation.RequestMethod;
30 import org.springframework.web.bind.annotation.RequestParam;
31 import org.springframework.web.servlet.ModelAndView;
32
33 import eu.etaxonomy.cdm.api.service.IDescriptionService;
34 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
35 import eu.etaxonomy.cdm.api.service.ITermService;
36 import eu.etaxonomy.cdm.api.service.dto.DistributionInfoDTO;
37 import eu.etaxonomy.cdm.api.service.dto.DistributionInfoDTO.InfoPart;
38 import eu.etaxonomy.cdm.api.service.pager.Pager;
39 import eu.etaxonomy.cdm.ext.geo.IEditGeoService;
40 import eu.etaxonomy.cdm.model.common.Annotation;
41 import eu.etaxonomy.cdm.model.common.Language;
42 import eu.etaxonomy.cdm.model.common.MarkerType;
43 import eu.etaxonomy.cdm.model.description.CategoricalData;
44 import eu.etaxonomy.cdm.model.description.DescriptionBase;
45 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
46 import eu.etaxonomy.cdm.model.description.Feature;
47 import eu.etaxonomy.cdm.model.description.FeatureTree;
48 import eu.etaxonomy.cdm.model.description.StateData;
49 import eu.etaxonomy.cdm.model.description.TaxonDescription;
50 import eu.etaxonomy.cdm.model.description.TextData;
51 import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
52 import eu.etaxonomy.cdm.remote.editor.DefinedTermBaseList;
53 import eu.etaxonomy.cdm.remote.editor.NamedAreaLevelPropertyEditor;
54 import eu.etaxonomy.cdm.remote.editor.TermBaseListPropertyEditor;
55 import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor;
56 import eu.etaxonomy.cdm.remote.editor.UuidList;
57 import eu.etaxonomy.cdm.remote.l10n.LocaleContext;
58
59 /**
60 * TODO write controller documentation
61 *
62 * @author a.kohlbecker
63 * @date 24.03.2009
64 */
65
66 @Controller
67 @RequestMapping(value = {"/description/{uuid}", "/description/{uuid_list}"})
68 public class DescriptionController extends BaseController<DescriptionBase, IDescriptionService>
69 {
70 @Autowired
71 private IFeatureTreeService featureTreeService;
72
73 @Autowired
74 private ITermService termService;
75
76
77 @Autowired
78 private IEditGeoService geoService;
79
80 protected static final List<String> TAXONDESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
81 "$",
82 "elements.$",
83 "elements.sources.citation.authorTeam",
84 "elements.sources.nameUsedInSource",
85 "elements.multilanguageText",
86 "elements.media",
87 });
88
89 @InitBinder
90 @Override
91 public void initBinder(WebDataBinder binder) {
92 super.initBinder(binder);
93 binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
94 binder.registerCustomEditor(NamedAreaLevel.class, new NamedAreaLevelPropertyEditor());
95 binder.registerCustomEditor(DefinedTermBaseList.class, new TermBaseListPropertyEditor<MarkerType>(termService));
96 }
97
98 /* (non-Javadoc)
99 * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)
100 */
101 @Autowired
102 @Override
103 public void setService(IDescriptionService service) {
104 this.service = service;
105 }
106
107 @RequestMapping(value = "hasStructuredData", method = RequestMethod.GET)
108 public ModelAndView doHasStructuredData(
109 @PathVariable("uuid") UUID uuid,
110 HttpServletRequest request,
111 HttpServletResponse response) throws IOException {
112 logger.info("doHasStructuredData() - " + request.getRequestURI());
113
114 ModelAndView mv = new ModelAndView();
115
116 DescriptionBase description = service.load(uuid);
117
118 if(!(description instanceof TaxonDescription)){
119 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
120 // will terminate thread
121 }
122
123 boolean hasStructuredData = service.hasStructuredData(description);
124
125 mv.addObject(hasStructuredData);
126 return mv;
127 }
128
129 @RequestMapping(value = "/descriptionElement/{descriptionelement_uuid}", method = RequestMethod.GET)
130 public ModelAndView doGetDescriptionElement(
131 @PathVariable("descriptionelement_uuid") UUID uuid,
132 HttpServletRequest request,
133 HttpServletResponse response) throws IOException {
134
135 ModelAndView mv = new ModelAndView();
136 logger.info("doGetDescriptionElement() - " + request.getRequestURI());
137 DescriptionElementBase element = service.getDescriptionElementByUuid(uuid);
138 if(element == null) {
139 HttpStatusMessage.UUID_NOT_FOUND.send(response);
140 }
141 mv.addObject(element);
142 return mv;
143 }
144
145 @RequestMapping(value = "/descriptionElement/{descriptionelement_uuid}/annotations", method = RequestMethod.GET)
146 public Pager<Annotation> doGetDescriptionElementAnnotations(
147 @PathVariable("descriptionelement_uuid") UUID uuid,
148 HttpServletRequest request,
149 HttpServletResponse response) throws IOException {
150 logger.info("doGetDescriptionElementAnnotations() - " + request.getRequestURI());
151 DescriptionElementBase annotatableEntity = service.getDescriptionElementByUuid(uuid);
152 if(annotatableEntity == null){
153 HttpStatusMessage.UUID_INVALID.send(response);
154 // method will exit here
155 return null;
156 }
157
158 Pager<Annotation> annotations = service.getDescriptionElementAnnotations(annotatableEntity, null, null, 0, null, getInitializationStrategy());
159 return annotations;
160 }
161
162 @RequestMapping(value = "/descriptionElement/{descriptionelement_uuid}/states", method = RequestMethod.GET)
163 public ModelAndView doGetDescriptionElementStates(
164 @PathVariable("descriptionelement_uuid") UUID uuid,
165 HttpServletRequest request,
166 HttpServletResponse response) throws IOException {
167 logger.info("doGetDescriptionElementStates() - " + request.getRequestURI());
168
169 ModelAndView mv = new ModelAndView();
170
171 DescriptionElementBase descriptionElement = service.loadDescriptionElement(uuid,
172 Arrays.asList( new String[]{
173 "states.state.representations",
174 "modifiers",
175 "modifyingText"
176 } ));
177 if(descriptionElement == null){
178 HttpStatusMessage.UUID_INVALID.send(response);
179 // method will exit here
180 return null;
181 }
182
183 if(descriptionElement instanceof CategoricalData){
184
185 }
186 List<StateData> states = ((CategoricalData)descriptionElement).getStateData();
187 mv.addObject(states);
188 return mv;
189 }
190
191 /*
192 @RequestMapping(value = "{uuid_list}/namedAreaTree", method = RequestMethod.GET)
193 public NamedAreaTree doGetOrderedDistributions(
194 @PathVariable("uuid_list") UuidList descriptionUuidList,
195 @RequestParam(value = "omitLevels", required = false) Set<NamedAreaLevel> levels,
196 //@ModelAttribute("omitLevels") HashSet<NamedAreaLevel> levels,
197 HttpServletRequest request, HttpServletResponse response) {
198 logger.info("getOrderedDistributions(" + ObjectUtils.toString(levels) + ") - " + request.getRequestURI());
199 Set<TaxonDescription> taxonDescriptions = new HashSet<TaxonDescription>();
200 TaxonDescription description;
201 for (UUID descriptionUuid : descriptionUuidList) {
202 description = (TaxonDescription) service.load(descriptionUuid);
203 taxonDescriptions.add(description);
204 }
205 NamedAreaTree areaTree = service.getOrderedDistributions(taxonDescriptions, levels);
206 return areaTree;
207 }
208 */
209
210 @RequestMapping(value = "/description/{uuid}/naturalLanguageDescription/{featuretree_uuid}", method = RequestMethod.GET)
211 public ModelAndView doGenerateNaturalLanguageDescription(
212 @PathVariable("uuid") UUID uuid,
213 @PathVariable("featuretree_uuid") UUID featureTreeUuid,
214 HttpServletRequest request,
215 HttpServletResponse response) throws IOException {
216 logger.info("doGenerateNaturalLanguageDescription() - " + request.getRequestURI());
217
218 DescriptionBase description = service.load(uuid);
219
220 ModelAndView mv = new ModelAndView();
221
222 List<Language> languages = LocaleContext.getLanguages();
223
224 if(!(description instanceof TaxonDescription)){
225 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
226 // will terminate thread
227 }
228
229 FeatureTree featureTree = featureTreeService.load(featureTreeUuid, null);
230 if(featureTree == null){
231 HttpStatusMessage.UUID_NOT_FOUND.send(response);
232 // will terminate thread
233 }
234
235 String naturalLanguageDescription = service.generateNaturalLanguageDescription(
236 featureTree,
237 (TaxonDescription)description,
238 languages,
239 ", ");
240 TextData textData = TextData.NewInstance(Feature.DESCRIPTION());
241 textData.putText(Language.DEFAULT(), naturalLanguageDescription);
242 mv.addObject(textData);
243 return mv;
244 }
245
246 /**
247 * @param taxonUuid
248 * @param parts
249 * possible values: condensedStatusString, tree, mapUriParams,
250 * elements,
251 * @param subAreaPreference
252 * @param statusOrderPreference
253 * @param hideMarkedAreasList
254 * @param omitLevels
255 * @param request
256 * @param response
257 * @return
258 */
259 @RequestMapping(value = "/description/distributionInfoFor/{uuid}", method = RequestMethod.GET)
260 public ModelAndView doGetDistributionInfo(
261 @PathVariable("uuid") UUID taxonUuid,
262 @RequestParam("part") Set<InfoPart> partSet,
263 @RequestParam(value = "subAreaPreference", required = false) boolean subAreaPreference,
264 @RequestParam(value = "statusOrderPreference", required = false) boolean statusOrderPreference,
265 @RequestParam(value = "hideMarkedAreas", required = false) DefinedTermBaseList<MarkerType> hideMarkedAreasList,
266 @RequestParam(value = "omitLevels", required = false) Set<NamedAreaLevel> omitLevels,
267 HttpServletRequest request,
268 HttpServletResponse response) {
269
270 logger.debug("doGetDistributionInfo() - " + requestPathAndQuery(request));
271
272 ModelAndView mv = new ModelAndView();
273
274 Set<MarkerType> hideMarkedAreas = null;
275 if(hideMarkedAreasList != null){
276 hideMarkedAreas = hideMarkedAreasList.asSet();
277 }
278
279 EnumSet<InfoPart> parts = EnumSet.copyOf(partSet);
280
281 DistributionInfoDTO dto = geoService.composeDistributionInfoFor(parts, taxonUuid, subAreaPreference, statusOrderPreference,
282 hideMarkedAreas, omitLevels, LocaleContext.getLanguages());
283
284 mv.addObject(dto);
285
286 return mv;
287 }
288
289 }