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