3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.cdm
.remote
.controller
;
13 import java
.io
.IOException
;
14 import java
.util
.Arrays
;
15 import java
.util
.List
;
16 import java
.util
.UUID
;
18 import javax
.servlet
.http
.HttpServletRequest
;
19 import javax
.servlet
.http
.HttpServletResponse
;
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
;
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
;
35 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
36 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
37 import eu
.etaxonomy
.cdm
.model
.description
.CategoricalData
;
38 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
39 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
40 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
41 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
42 import eu
.etaxonomy
.cdm
.model
.description
.StateData
;
43 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
44 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
45 import eu
.etaxonomy
.cdm
.model
.location
.NamedAreaLevel
;
46 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
47 import eu
.etaxonomy
.cdm
.remote
.controller
.util
.PagerParameters
;
48 import eu
.etaxonomy
.cdm
.remote
.editor
.NamedAreaLevelPropertyEditor
;
49 import eu
.etaxonomy
.cdm
.remote
.editor
.UUIDListPropertyEditor
;
50 import eu
.etaxonomy
.cdm
.remote
.editor
.UuidList
;
51 import eu
.etaxonomy
.cdm
.remote
.l10n
.LocaleContext
;
54 * TODO write controller documentation
56 * @author a.kohlbecker
61 @RequestMapping(value
= {"/description/{uuid}", "/description/{uuid_list}"})
62 public class DescriptionController
extends BaseController
<DescriptionBase
, IDescriptionService
>
65 private IFeatureTreeService featureTreeService
;
68 public DescriptionController(){
72 private static final List
<String
> FEATURETREE_INIT_STRATEGY
= Arrays
.asList(
75 "root.feature.representations",
76 "root.children.feature.representations",
77 "root.children.children.feature.representations",
82 protected static final List
<String
> TAXONDESCRIPTION_INIT_STRATEGY
= Arrays
.asList(new String
[]{
85 "elements.sources.citation.authorTeam",
86 "elements.sources.nameUsedInSource.originalNameString",
87 "elements.multilanguageText",
88 "elements.media.representations.parts",
89 "elements.media.title",
94 public void initBinder(WebDataBinder binder
) {
95 super.initBinder(binder
);
96 binder
.registerCustomEditor(UuidList
.class, new UUIDListPropertyEditor());
97 binder
.registerCustomEditor(NamedAreaLevel
.class, new NamedAreaLevelPropertyEditor());
101 * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)
105 public void setService(IDescriptionService service
) {
106 this.service
= service
;
113 * @throws IOException
115 @RequestMapping(value
= {"/featureTree/{uuid}"}, method
= RequestMethod
.GET
)
116 public FeatureTree
doGetFeatureTree(
117 @PathVariable("uuid") UUID uuid
,
118 HttpServletRequest request
,
119 HttpServletResponse response
)throws IOException
{
120 FeatureTree featureTree
= getCdmBaseInstance(FeatureTree
.class, featureTreeService
, uuid
, response
, FEATURETREE_INIT_STRATEGY
);
124 @RequestMapping(value
= "/descriptionElement/{descriptionelement_uuid}", method
= RequestMethod
.GET
)
125 public ModelAndView
doGetDescriptionElement(
126 @PathVariable("descriptionelement_uuid") UUID uuid
,
127 HttpServletRequest request
,
128 HttpServletResponse response
) throws IOException
{
130 ModelAndView mv
= new ModelAndView();
131 logger
.info("doGetDescriptionElement() - " + request
.getServletPath());
132 DescriptionElementBase element
= service
.getDescriptionElementByUuid(uuid
);
133 if(element
== null) {
134 HttpStatusMessage
.UUID_NOT_FOUND
.send(response
);
136 mv
.addObject(element
);
140 @RequestMapping(value
= "/descriptionElement/{descriptionelement_uuid}/annotations", method
= RequestMethod
.GET
)
141 public Pager
<Annotation
> doGetDescriptionElementAnnotations(
142 @PathVariable("descriptionelement_uuid") UUID uuid
,
143 HttpServletRequest request
,
144 HttpServletResponse response
) throws IOException
{
145 logger
.info("doGetDescriptionElementAnnotations() - " + request
.getServletPath());
146 DescriptionElementBase annotatableEntity
= service
.getDescriptionElementByUuid(uuid
);
147 if(annotatableEntity
== null){
148 HttpStatusMessage
.UUID_INVALID
.send(response
);
149 // method will exit here
153 Pager
<Annotation
> annotations
= service
.getDescriptionElementAnnotations(annotatableEntity
, null, null, 0, null, DEFAULT_INIT_STRATEGY
);
157 @RequestMapping(value
= "/descriptionElement/{descriptionelement_uuid}/states", method
= RequestMethod
.GET
)
158 public ModelAndView
doGetDescriptionElementStates(
159 @PathVariable("descriptionelement_uuid") UUID uuid
,
160 HttpServletRequest request
,
161 HttpServletResponse response
) throws IOException
{
162 logger
.info("doGetDescriptionElementStates() - " + request
.getServletPath());
164 ModelAndView mv
= new ModelAndView();
166 DescriptionElementBase descriptionElement
= service
.loadDescriptionElement(uuid
,
167 Arrays
.asList( new String
[]{
168 "states.state.representations",
172 if(descriptionElement
== null){
173 HttpStatusMessage
.UUID_INVALID
.send(response
);
174 // method will exit here
178 if(descriptionElement
instanceof CategoricalData
){
181 List
<StateData
> states
= ((CategoricalData
)descriptionElement
).getStates();
182 mv
.addObject(states
);
188 @RequestMapping(value
= "/descriptionElement/find", method
= RequestMethod
.GET
)
189 public Pager
<DescriptionElementBase
> doFindDescriptionElements(
190 @RequestParam(value
= "query", required
= true) String queryString
,
191 @RequestParam(value
= "type", required
= false) Class type
,
192 @RequestParam(value
= "pageSize", required
= false) Integer pageSize
,
193 @RequestParam(value
= "pageNumber", required
= false) Integer pageNumber
,
194 @RequestParam(value
= "matchMode", required
= false) MatchMode matchMode
,
195 HttpServletRequest request
,
196 HttpServletResponse response
200 logger
.info("doFindDescriptionElements : " + request
.getRequestURI() + "?" + request
.getQueryString() );
202 PagerParameters pagerParams
= new PagerParameters(pageSize
, pageNumber
);
203 pagerParams
.normalizeAndValidate(response
);
205 Pager
<DescriptionElementBase
> pager
= service
.searchElements(type
, queryString
, pageSize
, pageNumber
, null, DEFAULT_INIT_STRATEGY
);
211 @RequestMapping(value = "{uuid_list}/namedAreaTree", method = RequestMethod.GET)
212 public NamedAreaTree doGetOrderedDistributions(
213 @PathVariable("uuid_list") UuidList descriptionUuidList,
214 @RequestParam(value = "omitLevels", required = false) Set<NamedAreaLevel> levels,
215 //@ModelAttribute("omitLevels") HashSet<NamedAreaLevel> levels,
216 HttpServletRequest request, HttpServletResponse response) {
217 logger.info("getOrderedDistributions(" + ObjectUtils.toString(levels) + ") - " + request.getServletPath());
218 Set<TaxonDescription> taxonDescriptions = new HashSet<TaxonDescription>();
219 TaxonDescription description;
220 for (UUID descriptionUuid : descriptionUuidList) {
221 description = (TaxonDescription) service.load(descriptionUuid);
222 taxonDescriptions.add(description);
224 NamedAreaTree areaTree = service.getOrderedDistributions(taxonDescriptions, levels);
229 @RequestMapping(value
= "/description/{uuid}/naturalLanguageDescription/{featuretree_uuid}", method
= RequestMethod
.GET
)
230 public ModelAndView
doGenerateNaturalLanguageDescription(
231 @PathVariable("uuid") UUID uuid
,
232 @PathVariable("featuretree_uuid") UUID featureTreeUuid
,
233 HttpServletRequest request
,
234 HttpServletResponse response
) throws IOException
{
235 logger
.info("doGenerateNaturalLanguageDescription() - " + request
.getServletPath());
237 DescriptionBase description
= service
.load(uuid
);
239 ModelAndView mv
= new ModelAndView();
241 List
<Language
> languages
= LocaleContext
.getLanguages();
243 if(!(description
instanceof TaxonDescription
)){
244 HttpStatusMessage
.UUID_REFERENCES_WRONG_TYPE
.send(response
);
245 // will terminate thread
248 FeatureTree featureTree
= featureTreeService
.load(featureTreeUuid
, null);
249 if(featureTree
== null){
250 HttpStatusMessage
.UUID_NOT_FOUND
.send(response
);
251 // will terminate thread
254 String naturalLanguageDescription
= service
.generateNaturalLanguageDescription(
256 (TaxonDescription
)description
,
259 TextData textData
= TextData
.NewInstance(Feature
.DESCRIPTION());
260 textData
.putText(Language
.DEFAULT(), naturalLanguageDescription
);
261 mv
.addObject(textData
);
265 @RequestMapping(value
= "/description/{uuid}/hasStructuredData", method
= RequestMethod
.GET
)
266 public ModelAndView
doHasStructuredData(
267 @PathVariable("uuid") UUID uuid
,
268 HttpServletRequest request
,
269 HttpServletResponse response
) throws IOException
{
270 logger
.info("doHasStructuredData() - " + request
.getServletPath());
272 ModelAndView mv
= new ModelAndView();
274 DescriptionBase description
= service
.load(uuid
);
276 if(!(description
instanceof TaxonDescription
)){
277 HttpStatusMessage
.UUID_REFERENCES_WRONG_TYPE
.send(response
);
278 // will terminate thread
281 boolean hasStructuredData
= service
.hasStructuredData(description
);
283 mv
.addObject(hasStructuredData
);