merged changes from trunk for later reintegration
[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.bind.annotation.RequestParam;
29 import org.springframework.web.servlet.ModelAndView;
30
31 import eu.etaxonomy.cdm.api.service.IDescriptionService;
32 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
33 import eu.etaxonomy.cdm.api.service.pager.Pager;
34
35 import eu.etaxonomy.cdm.model.common.Annotation;
36 import eu.etaxonomy.cdm.model.common.Language;
37 import eu.etaxonomy.cdm.model.description.DescriptionBase;
38 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
39 import eu.etaxonomy.cdm.model.description.Feature;
40 import eu.etaxonomy.cdm.model.description.FeatureTree;
41 import eu.etaxonomy.cdm.model.description.TaxonDescription;
42 import eu.etaxonomy.cdm.model.description.TextData;
43 import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
44 import eu.etaxonomy.cdm.persistence.query.MatchMode;
45 import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
46 import eu.etaxonomy.cdm.remote.editor.NamedAreaLevelPropertyEditor;
47 import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor;
48 import eu.etaxonomy.cdm.remote.editor.UuidList;
49 import eu.etaxonomy.cdm.remote.l10n.LocaleContext;
50
51 /**
52 * TODO write controller documentation
53 *
54 * @author a.kohlbecker
55 * @date 24.03.2009
56 */
57
58 @Controller
59 @RequestMapping(value = {"/description/{uuid}", "/description/{uuid_list}"})
60 public class DescriptionController extends BaseController<DescriptionBase, IDescriptionService>
61 {
62 @Autowired
63 private IFeatureTreeService featureTreeService;
64
65
66 public DescriptionController(){
67 super();
68 }
69
70 private static final List<String> FEATURETREE_INIT_STRATEGY = Arrays.asList(
71 new String[]{
72 "representations",
73 "root.feature.representations",
74 "root.children.feature.representations",
75 "root.children.children.feature.representations",
76 });
77
78
79 protected static final List<String> TAXONDESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
80 "$",
81 "elements.$",
82 "elements.sources.citation.authorTeam",
83 "elements.sources.nameUsedInSource.originalNameString",
84 "elements.multilanguageText",
85 "elements.media.representations.parts",
86 "elements.media.title",
87 });
88
89 @InitBinder
90 @Override
91 public void initBinder(WebDataBinder binder) {
92 super.initBinder(binder);
93 binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
94 binder.registerCustomEditor(NamedAreaLevel.class, new NamedAreaLevelPropertyEditor());
95 }
96
97 /* (non-Javadoc)
98 * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)
99 */
100 @Autowired
101 @Override
102 public void setService(IDescriptionService service) {
103 this.service = service;
104 }
105
106 /**
107 * @param request
108 * @param response
109 * @return
110 * @throws IOException
111 */
112 @RequestMapping(value = {"/featureTree/{uuid}"}, method = RequestMethod.GET)
113 public FeatureTree doGetFeatureTree(
114 @PathVariable("uuid") UUID uuid,
115 HttpServletRequest request,
116 HttpServletResponse response)throws IOException {
117 FeatureTree featureTree = getCdmBaseInstance(FeatureTree.class, featureTreeService, uuid, response, FEATURETREE_INIT_STRATEGY);
118 return featureTree;
119 }
120
121
122 @RequestMapping(value = "/descriptionElement/{descriptionelement_uuid}/annotations", method = RequestMethod.GET)
123 public Pager<Annotation> getDescriptionElementAnnotations(
124 @PathVariable("descriptionelement_uuid") UUID uuid,
125 HttpServletRequest request,
126 HttpServletResponse response) throws IOException {
127 logger.info("getDescriptionElementAnnotations() - " + request.getServletPath());
128 DescriptionElementBase annotatableEntity = service.getDescriptionElementByUuid(uuid);
129 if(annotatableEntity == null){
130 HttpStatusMessage.UUID_INVALID.send(response);
131 // method will exit here
132 return null;
133 }
134
135 Pager<Annotation> annotations = service.getDescriptionElementAnnotations(annotatableEntity, null, null, 0, null, DEFAULT_INIT_STRATEGY);
136 return annotations;
137 }
138
139
140
141 @RequestMapping(value = "/descriptionElement/find", method = RequestMethod.GET)
142 public Pager<DescriptionElementBase> doFindDescriptionElements(
143 @RequestParam(value = "query", required = true) String queryString,
144 @RequestParam(value = "type", required = false) Class type,
145 @RequestParam(value = "pageSize", required = false) Integer pageSize,
146 @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
147 @RequestParam(value = "matchMode", required = false) MatchMode matchMode,
148 HttpServletRequest request,
149 HttpServletResponse response
150 )
151 throws IOException {
152
153 logger.info("doFindDescriptionElements : " + request.getRequestURI() + "?" + request.getQueryString() );
154
155 PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);
156 pagerParams.normalizeAndValidate(response);
157
158 Pager<DescriptionElementBase> pager = service.searchElements(type, queryString, pageSize, pageNumber, null, DEFAULT_INIT_STRATEGY);
159
160 return pager;
161 }
162
163 /*
164 @RequestMapping(value = "{uuid_list}/namedAreaTree", method = RequestMethod.GET)
165 public NamedAreaTree doGetOrderedDistributions(
166 @PathVariable("uuid_list") UuidList descriptionUuidList,
167 @RequestParam(value = "omitLevels", required = false) Set<NamedAreaLevel> levels,
168 //@ModelAttribute("omitLevels") HashSet<NamedAreaLevel> levels,
169 HttpServletRequest request, HttpServletResponse response) {
170 logger.info("getOrderedDistributions(" + ObjectUtils.toString(levels) + ") - " + request.getServletPath());
171 Set<TaxonDescription> taxonDescriptions = new HashSet<TaxonDescription>();
172 TaxonDescription description;
173 for (UUID descriptionUuid : descriptionUuidList) {
174 description = (TaxonDescription) service.load(descriptionUuid);
175 taxonDescriptions.add(description);
176 }
177 NamedAreaTree areaTree = service.getOrderedDistributions(taxonDescriptions, levels);
178 return areaTree;
179 }
180 */
181
182 @RequestMapping(value = "/description/{uuid}/naturalLanguageDescription/{featuretree_uuid}", method = RequestMethod.GET)
183 public ModelAndView doGenerateNaturalLanguageDescription(
184 @PathVariable("uuid") UUID uuid,
185 @PathVariable("featuretree_uuid") UUID featureTreeUuid,
186 HttpServletRequest request,
187 HttpServletResponse response) throws IOException {
188 logger.info("doGenerateNaturalLanguageDescription() - " + request.getServletPath());
189
190 DescriptionBase description = service.load(uuid);
191
192 ModelAndView mv = new ModelAndView();
193
194 List<Language> languages = LocaleContext.getLanguages();
195
196 if(!(description instanceof TaxonDescription)){
197 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
198 // will terminate thread
199 }
200
201 FeatureTree featureTree = featureTreeService.load(featureTreeUuid, null);
202 if(featureTree == null){
203 HttpStatusMessage.UUID_NOT_FOUND.send(response);
204 // will terminate thread
205 }
206
207 String naturalLanguageDescription = service.generateNaturalLanguageDescription(
208 featureTree,
209 (TaxonDescription)description,
210 languages,
211 ", ");
212 TextData textData = TextData.NewInstance(Feature.DESCRIPTION());
213 textData.putText(Language.DEFAULT(), naturalLanguageDescription);
214 mv.addObject(textData);
215 return mv;
216 }
217
218 @RequestMapping(value = "/description/{uuid}/hasStructuredData", method = RequestMethod.GET)
219 public ModelAndView doHasStructuredData(
220 @PathVariable("uuid") UUID uuid,
221 HttpServletRequest request,
222 HttpServletResponse response) throws IOException {
223 logger.info("doHasStructuredData() - " + request.getServletPath());
224
225 ModelAndView mv = new ModelAndView();
226
227 DescriptionBase description = service.load(uuid);
228
229 if(!(description instanceof TaxonDescription)){
230 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
231 // will terminate thread
232 }
233
234 boolean hasStructuredData = service.hasStructuredData(description);
235
236 mv.addObject(hasStructuredData);
237
238 return mv;
239 }
240
241 }