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