merge-update from trunk
[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.List;
16 import java.util.UUID;
17
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpServletResponse;
20
21 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.stereotype.Controller;
23 import org.springframework.web.bind.WebDataBinder;
24 import org.springframework.web.bind.annotation.InitBinder;
25 import org.springframework.web.bind.annotation.PathVariable;
26 import org.springframework.web.bind.annotation.RequestMapping;
27 import org.springframework.web.bind.annotation.RequestMethod;
28 import org.springframework.web.servlet.ModelAndView;
29
30 import eu.etaxonomy.cdm.api.service.IDescriptionService;
31 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
32 import eu.etaxonomy.cdm.api.service.pager.Pager;
33 import eu.etaxonomy.cdm.model.common.Annotation;
34 import eu.etaxonomy.cdm.model.common.Language;
35 import eu.etaxonomy.cdm.model.description.CategoricalData;
36 import eu.etaxonomy.cdm.model.description.DescriptionBase;
37 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
38 import eu.etaxonomy.cdm.model.description.Feature;
39 import eu.etaxonomy.cdm.model.description.FeatureTree;
40 import eu.etaxonomy.cdm.model.description.StateData;
41 import eu.etaxonomy.cdm.model.description.TaxonDescription;
42 import eu.etaxonomy.cdm.model.description.TextData;
43 import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
44 import eu.etaxonomy.cdm.remote.editor.NamedAreaLevelPropertyEditor;
45 import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor;
46 import eu.etaxonomy.cdm.remote.editor.UuidList;
47 import eu.etaxonomy.cdm.remote.l10n.LocaleContext;
48
49 /**
50 * TODO write controller documentation
51 *
52 * @author a.kohlbecker
53 * @date 24.03.2009
54 */
55
56 @Controller
57 @RequestMapping(value = {"/description/{uuid}", "/description/{uuid_list}"})
58 public class DescriptionController extends BaseController<DescriptionBase, IDescriptionService>
59 {
60 @Autowired
61 private IFeatureTreeService featureTreeService;
62
63
64 public DescriptionController(){
65 super();
66 }
67
68 protected static final List<String> TAXONDESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
69 "$",
70 "elements.$",
71 "elements.sources.citation.authorTeam",
72 "elements.sources.nameUsedInSource.originalNameString",
73 "elements.multilanguageText",
74 "elements.media",
75 });
76
77 @InitBinder
78 @Override
79 public void initBinder(WebDataBinder binder) {
80 super.initBinder(binder);
81 binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
82 binder.registerCustomEditor(NamedAreaLevel.class, new NamedAreaLevelPropertyEditor());
83 }
84
85 /* (non-Javadoc)
86 * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)
87 */
88 @Autowired
89 @Override
90 public void setService(IDescriptionService service) {
91 this.service = service;
92 }
93
94 @RequestMapping(value = "hasStructuredData", method = RequestMethod.GET)
95 public ModelAndView doHasStructuredData(
96 @PathVariable("uuid") UUID uuid,
97 HttpServletRequest request,
98 HttpServletResponse response) throws IOException {
99 logger.info("doHasStructuredData() - " + request.getRequestURI());
100
101 ModelAndView mv = new ModelAndView();
102
103 DescriptionBase description = service.load(uuid);
104
105 if(!(description instanceof TaxonDescription)){
106 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
107 // will terminate thread
108 }
109
110 boolean hasStructuredData = service.hasStructuredData(description);
111
112 mv.addObject(hasStructuredData);
113 return mv;
114 }
115
116 @RequestMapping(value = "/descriptionElement/{descriptionelement_uuid}", method = RequestMethod.GET)
117 public ModelAndView doGetDescriptionElement(
118 @PathVariable("descriptionelement_uuid") UUID uuid,
119 HttpServletRequest request,
120 HttpServletResponse response) throws IOException {
121
122 ModelAndView mv = new ModelAndView();
123 logger.info("doGetDescriptionElement() - " + request.getRequestURI());
124 DescriptionElementBase element = service.getDescriptionElementByUuid(uuid);
125 if(element == null) {
126 HttpStatusMessage.UUID_NOT_FOUND.send(response);
127 }
128 mv.addObject(element);
129 return mv;
130 }
131
132 @RequestMapping(value = "/descriptionElement/{descriptionelement_uuid}/annotations", method = RequestMethod.GET)
133 public Pager<Annotation> doGetDescriptionElementAnnotations(
134 @PathVariable("descriptionelement_uuid") UUID uuid,
135 HttpServletRequest request,
136 HttpServletResponse response) throws IOException {
137 logger.info("doGetDescriptionElementAnnotations() - " + request.getRequestURI());
138 DescriptionElementBase annotatableEntity = service.getDescriptionElementByUuid(uuid);
139 if(annotatableEntity == null){
140 HttpStatusMessage.UUID_INVALID.send(response);
141 // method will exit here
142 return null;
143 }
144
145 Pager<Annotation> annotations = service.getDescriptionElementAnnotations(annotatableEntity, null, null, 0, null, getInitializationStrategy());
146 return annotations;
147 }
148
149 @RequestMapping(value = "/descriptionElement/{descriptionelement_uuid}/states", method = RequestMethod.GET)
150 public ModelAndView doGetDescriptionElementStates(
151 @PathVariable("descriptionelement_uuid") UUID uuid,
152 HttpServletRequest request,
153 HttpServletResponse response) throws IOException {
154 logger.info("doGetDescriptionElementStates() - " + request.getRequestURI());
155
156 ModelAndView mv = new ModelAndView();
157
158 DescriptionElementBase descriptionElement = service.loadDescriptionElement(uuid,
159 Arrays.asList( new String[]{
160 "states.state.representations",
161 "modifiers",
162 "modifyingText"
163 } ));
164 if(descriptionElement == null){
165 HttpStatusMessage.UUID_INVALID.send(response);
166 // method will exit here
167 return null;
168 }
169
170 if(descriptionElement instanceof CategoricalData){
171
172 }
173 List<StateData> states = ((CategoricalData)descriptionElement).getStateData();
174 mv.addObject(states);
175 return mv;
176 }
177
178 /*
179 @RequestMapping(value = "{uuid_list}/namedAreaTree", method = RequestMethod.GET)
180 public NamedAreaTree doGetOrderedDistributions(
181 @PathVariable("uuid_list") UuidList descriptionUuidList,
182 @RequestParam(value = "omitLevels", required = false) Set<NamedAreaLevel> levels,
183 //@ModelAttribute("omitLevels") HashSet<NamedAreaLevel> levels,
184 HttpServletRequest request, HttpServletResponse response) {
185 logger.info("getOrderedDistributions(" + ObjectUtils.toString(levels) + ") - " + request.getRequestURI());
186 Set<TaxonDescription> taxonDescriptions = new HashSet<TaxonDescription>();
187 TaxonDescription description;
188 for (UUID descriptionUuid : descriptionUuidList) {
189 description = (TaxonDescription) service.load(descriptionUuid);
190 taxonDescriptions.add(description);
191 }
192 NamedAreaTree areaTree = service.getOrderedDistributions(taxonDescriptions, levels);
193 return areaTree;
194 }
195 */
196
197 @RequestMapping(value = "/description/{uuid}/naturalLanguageDescription/{featuretree_uuid}", method = RequestMethod.GET)
198 public ModelAndView doGenerateNaturalLanguageDescription(
199 @PathVariable("uuid") UUID uuid,
200 @PathVariable("featuretree_uuid") UUID featureTreeUuid,
201 HttpServletRequest request,
202 HttpServletResponse response) throws IOException {
203 logger.info("doGenerateNaturalLanguageDescription() - " + request.getRequestURI());
204
205 DescriptionBase description = service.load(uuid);
206
207 ModelAndView mv = new ModelAndView();
208
209 List<Language> languages = LocaleContext.getLanguages();
210
211 if(!(description instanceof TaxonDescription)){
212 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
213 // will terminate thread
214 }
215
216 FeatureTree featureTree = featureTreeService.load(featureTreeUuid, null);
217 if(featureTree == null){
218 HttpStatusMessage.UUID_NOT_FOUND.send(response);
219 // will terminate thread
220 }
221
222 String naturalLanguageDescription = service.generateNaturalLanguageDescription(
223 featureTree,
224 (TaxonDescription)description,
225 languages,
226 ", ");
227 TextData textData = TextData.NewInstance(Feature.DESCRIPTION());
228 textData.putText(Language.DEFAULT(), naturalLanguageDescription);
229 mv.addObject(textData);
230 return mv;
231 }
232
233 }