fixing #2544 (No mapping found for /descriptionElement/{uuid}/annotations)
[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.common.Representation;
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.TaxonDescription;
41 import eu.etaxonomy.cdm.model.description.TextData;
42 import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
43 import eu.etaxonomy.cdm.remote.editor.NamedAreaLevelPropertyEditor;
44 import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor;
45 import eu.etaxonomy.cdm.remote.editor.UUIDPropertyEditor;
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 AnnotatableController<DescriptionBase, IDescriptionService>
59 {
60 @Autowired
61 private IFeatureTreeService featureTreeService;
62
63
64 public DescriptionController(){
65 super();
66 }
67
68 private static final List<String> FEATURETREE_INIT_STRATEGY = Arrays.asList(
69 new String[]{
70 "representations",
71 "root.feature.representations",
72 "root.children.feature.representations",
73 "root.children.children.feature.representations",
74 });
75
76 @InitBinder
77 @Override
78 public void initBinder(WebDataBinder binder) {
79 super.initBinder(binder);
80 binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
81 binder.registerCustomEditor(NamedAreaLevel.class, new NamedAreaLevelPropertyEditor());
82 }
83
84 /* (non-Javadoc)
85 * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)
86 */
87 @Autowired
88 @Override
89 public void setService(IDescriptionService service) {
90 this.service = service;
91 }
92
93 /**
94 * @param request
95 * @param response
96 * @return
97 * @throws IOException
98 */
99 @RequestMapping(value = {"/featureTree/{uuid}"}, method = RequestMethod.GET)
100 public FeatureTree doGetFeatureTree(
101 @PathVariable("uuid") UUID uuid,
102 HttpServletRequest request,
103 HttpServletResponse response)throws IOException {
104 FeatureTree featureTree = getCdmBaseInstance(FeatureTree.class, featureTreeService, uuid, response, FEATURETREE_INIT_STRATEGY);
105 return featureTree;
106 }
107
108
109 @RequestMapping(value = "/descriptionElement/{descriptionelement_uuid}/annotations", method = RequestMethod.GET)
110 public Pager<Annotation> getAnnotations(
111 @PathVariable("descriptionelement_uuid") UUID uuid,
112 HttpServletRequest request,
113 HttpServletResponse response) throws IOException {
114 logger.info("getAnnotations() - " + request.getServletPath());
115 DescriptionElementBase annotatableEntity = service.getDescriptionElementByUuid(uuid);
116 if(annotatableEntity == null){
117 HttpStatusMessage.UUID_INVALID.send(response);
118 // method will exit here
119 return null;
120 }
121
122 Pager<Annotation> annotations = service.getDescriptionElementAnnotations(annotatableEntity, null, null, 0, null, ANNOTATION_INIT_STRATEGY);
123 return annotations;
124 }
125
126 /*
127 @RequestMapping(value = "{uuid_list}/namedAreaTree", method = RequestMethod.GET)
128 public NamedAreaTree doGetOrderedDistributions(
129 @PathVariable("uuid_list") UuidList descriptionUuidList,
130 @RequestParam(value = "omitLevels", required = false) Set<NamedAreaLevel> levels,
131 //@ModelAttribute("omitLevels") HashSet<NamedAreaLevel> levels,
132 HttpServletRequest request, HttpServletResponse response) {
133 logger.info("getOrderedDistributions(" + ObjectUtils.toString(levels) + ") - " + request.getServletPath());
134 Set<TaxonDescription> taxonDescriptions = new HashSet<TaxonDescription>();
135 TaxonDescription description;
136 for (UUID descriptionUuid : descriptionUuidList) {
137 description = (TaxonDescription) service.load(descriptionUuid);
138 taxonDescriptions.add(description);
139 }
140 NamedAreaTree areaTree = service.getOrderedDistributions(taxonDescriptions, levels);
141 return areaTree;
142 }
143 */
144
145 @RequestMapping(value = "/description/{uuid}/naturalLanguageDescription/{featuretree_uuid}", method = RequestMethod.GET)
146 public ModelAndView doGenerateNaturalLanguageDescription(
147 @PathVariable("uuid") UUID uuid,
148 @PathVariable("featuretree_uuid") UUID featureTreeUuid,
149 HttpServletRequest request,
150 HttpServletResponse response) throws IOException {
151 logger.info("doGenerateNaturalLanguageDescription() - " + request.getServletPath());
152
153 DescriptionBase description = service.load(uuid);
154
155 ModelAndView mv = new ModelAndView();
156
157 List<Language> languages = LocaleContext.getLanguages();
158
159 if(!(description instanceof TaxonDescription)){
160 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
161 // will terminate thread
162 }
163
164 FeatureTree featureTree = featureTreeService.load(featureTreeUuid, null);
165 if(featureTree == null){
166 HttpStatusMessage.UUID_NOT_FOUND.send(response);
167 // will terminate thread
168 }
169
170 String naturalLanguageDescription = service.generateNaturalLanguageDescription(
171 featureTree,
172 (TaxonDescription)description,
173 languages,
174 ", ");
175
176 TextData textData = TextData.NewInstance(Feature.DESCRIPTION());
177 textData.putText(Language.DEFAULT(), naturalLanguageDescription);
178
179 mv.addObject(textData);
180
181 return mv;
182 }
183
184
185 @RequestMapping(value = "/description/{uuid}/hasStructuredData", method = RequestMethod.GET)
186 public ModelAndView doHasStructuredData(
187 @PathVariable("uuid") UUID uuid,
188 HttpServletRequest request,
189 HttpServletResponse response) throws IOException {
190 logger.info("doHasStructuredData() - " + request.getServletPath());
191
192 ModelAndView mv = new ModelAndView();
193
194 DescriptionBase description = service.load(uuid);
195
196 if(!(description instanceof TaxonDescription)){
197 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
198 // will terminate thread
199 }
200
201 boolean hasStructuredData = service.hasStructuredData(description);
202
203 mv.addObject(hasStructuredData);
204
205 return mv;
206 }
207
208 }