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