controllers binder update, related with drupal error "Please choose a valid classific...
[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}/annotation", 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 }
120 Pager<Annotation> annotations = service.getDescriptionElementAnnotations(annotatableEntity, null, null, 0, null, ANNOTATION_INIT_STRATEGY);
121 return annotations;
122 }
123
124 /*
125 @RequestMapping(value = "{uuid_list}/namedAreaTree", method = RequestMethod.GET)
126 public NamedAreaTree doGetOrderedDistributions(
127 @PathVariable("uuid_list") UuidList descriptionUuidList,
128 @RequestParam(value = "omitLevels", required = false) Set<NamedAreaLevel> levels,
129 //@ModelAttribute("omitLevels") HashSet<NamedAreaLevel> levels,
130 HttpServletRequest request, HttpServletResponse response) {
131 logger.info("getOrderedDistributions(" + ObjectUtils.toString(levels) + ") - " + request.getServletPath());
132 Set<TaxonDescription> taxonDescriptions = new HashSet<TaxonDescription>();
133 TaxonDescription description;
134 for (UUID descriptionUuid : descriptionUuidList) {
135 description = (TaxonDescription) service.load(descriptionUuid);
136 taxonDescriptions.add(description);
137 }
138 NamedAreaTree areaTree = service.getOrderedDistributions(taxonDescriptions, levels);
139 return areaTree;
140 }
141 */
142
143 @RequestMapping(value = "/description/{uuid}/naturalLanguageDescription/{featuretree_uuid}", method = RequestMethod.GET)
144 public ModelAndView doGenerateNaturalLanguageDescription(
145 @PathVariable("uuid") UUID uuid,
146 @PathVariable("featuretree_uuid") UUID featureTreeUuid,
147 HttpServletRequest request,
148 HttpServletResponse response) throws IOException {
149 logger.info("doGenerateNaturalLanguageDescription() - " + request.getServletPath());
150
151 DescriptionBase description = service.load(uuid);
152
153 ModelAndView mv = new ModelAndView();
154
155 List<Language> languages = LocaleContext.getLanguages();
156
157 if(!(description instanceof TaxonDescription)){
158 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
159 // will terminate thread
160 }
161
162 FeatureTree featureTree = featureTreeService.load(featureTreeUuid, null);
163 if(featureTree == null){
164 HttpStatusMessage.UUID_NOT_FOUND.send(response);
165 // will terminate thread
166 }
167
168 String naturalLanguageDescription = service.generateNaturalLanguageDescription(
169 featureTree,
170 (TaxonDescription)description,
171 languages,
172 ", ");
173
174 TextData textData = TextData.NewInstance(Feature.DESCRIPTION());
175 textData.putText(naturalLanguageDescription, Language.DEFAULT());
176
177 mv.addObject(textData);
178
179 return mv;
180 }
181
182
183 @RequestMapping(value = "/description/{uuid}/hasStructuredData", method = RequestMethod.GET)
184 public ModelAndView doHasStructuredData(
185 @PathVariable("uuid") UUID uuid,
186 HttpServletRequest request,
187 HttpServletResponse response) throws IOException {
188 logger.info("doHasStructuredData() - " + request.getServletPath());
189
190 ModelAndView mv = new ModelAndView();
191
192 DescriptionBase description = service.load(uuid);
193
194 if(!(description instanceof TaxonDescription)){
195 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
196 // will terminate thread
197 }
198
199 boolean hasStructuredData = service.hasStructuredData(description);
200
201 mv.addObject(hasStructuredData);
202
203 return mv;
204 }
205
206 }