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",
93 public void initBinder(WebDataBinder binder
) {
94 super.initBinder(binder
);
95 binder
.registerCustomEditor(UuidList
.class, new UUIDListPropertyEditor());
96 binder
.registerCustomEditor(NamedAreaLevel
.class, new NamedAreaLevelPropertyEditor());
100 * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)
104 public void setService(IDescriptionService service
) {
105 this.service
= service
;
112 * @throws IOException
114 @RequestMapping(value
= {"/featureTree/{uuid}"}, method
= RequestMethod
.GET
)
115 public FeatureTree
doGetFeatureTree(
116 @PathVariable("uuid") UUID uuid
,
117 HttpServletRequest request
,
118 HttpServletResponse response
)throws IOException
{
119 FeatureTree featureTree
= getCdmBaseInstance(FeatureTree
.class, featureTreeService
, uuid
, response
, FEATURETREE_INIT_STRATEGY
);
123 @RequestMapping(value
= "/descriptionElement/{descriptionelement_uuid}", method
= RequestMethod
.GET
)
124 public ModelAndView
doGetDescriptionElement(
125 @PathVariable("descriptionelement_uuid") UUID uuid
,
126 HttpServletRequest request
,
127 HttpServletResponse response
) throws IOException
{
129 ModelAndView mv
= new ModelAndView();
130 logger
.info("doGetDescriptionElement() - " + request
.getRequestURI());
131 DescriptionElementBase element
= service
.getDescriptionElementByUuid(uuid
);
132 if(element
== null) {
133 HttpStatusMessage
.UUID_NOT_FOUND
.send(response
);
135 mv
.addObject(element
);
139 @RequestMapping(value
= "/descriptionElement/{descriptionelement_uuid}/annotations", method
= RequestMethod
.GET
)
140 public Pager
<Annotation
> doGetDescriptionElementAnnotations(
141 @PathVariable("descriptionelement_uuid") UUID uuid
,
142 HttpServletRequest request
,
143 HttpServletResponse response
) throws IOException
{
144 logger
.info("doGetDescriptionElementAnnotations() - " + request
.getRequestURI());
145 DescriptionElementBase annotatableEntity
= service
.getDescriptionElementByUuid(uuid
);
146 if(annotatableEntity
== null){
147 HttpStatusMessage
.UUID_INVALID
.send(response
);
148 // method will exit here
152 Pager
<Annotation
> annotations
= service
.getDescriptionElementAnnotations(annotatableEntity
, null, null, 0, null, DEFAULT_INIT_STRATEGY
);
156 @RequestMapping(value
= "/descriptionElement/{descriptionelement_uuid}/states", method
= RequestMethod
.GET
)
157 public ModelAndView
doGetDescriptionElementStates(
158 @PathVariable("descriptionelement_uuid") UUID uuid
,
159 HttpServletRequest request
,
160 HttpServletResponse response
) throws IOException
{
161 logger
.info("doGetDescriptionElementStates() - " + request
.getRequestURI());
163 ModelAndView mv
= new ModelAndView();
165 DescriptionElementBase descriptionElement
= service
.loadDescriptionElement(uuid
,
166 Arrays
.asList( new String
[]{
167 "states.state.representations",
171 if(descriptionElement
== null){
172 HttpStatusMessage
.UUID_INVALID
.send(response
);
173 // method will exit here
177 if(descriptionElement
instanceof CategoricalData
){
180 List
<StateData
> states
= ((CategoricalData
)descriptionElement
).getStates();
181 mv
.addObject(states
);
187 @RequestMapping(value
= "/descriptionElement/find", method
= RequestMethod
.GET
)
188 public Pager
<DescriptionElementBase
> doFindDescriptionElements(
189 @RequestParam(value
= "query", required
= true) String queryString
,
190 @RequestParam(value
= "type", required
= false) Class type
,
191 @RequestParam(value
= "pageSize", required
= false) Integer pageSize
,
192 @RequestParam(value
= "pageNumber", required
= false) Integer pageNumber
,
193 @RequestParam(value
= "matchMode", required
= false) MatchMode matchMode
,
194 HttpServletRequest request
,
195 HttpServletResponse response
199 logger
.info("doFindDescriptionElements : " + request
.getRequestURI() + "?" + request
.getQueryString() );
201 PagerParameters pagerParams
= new PagerParameters(pageSize
, pageNumber
);
202 pagerParams
.normalizeAndValidate(response
);
204 Pager
<DescriptionElementBase
> pager
= service
.searchElements(type
, queryString
, pageSize
, pageNumber
, null, DEFAULT_INIT_STRATEGY
);
210 @RequestMapping(value = "{uuid_list}/namedAreaTree", method = RequestMethod.GET)
211 public NamedAreaTree doGetOrderedDistributions(
212 @PathVariable("uuid_list") UuidList descriptionUuidList,
213 @RequestParam(value = "omitLevels", required = false) Set<NamedAreaLevel> levels,
214 //@ModelAttribute("omitLevels") HashSet<NamedAreaLevel> levels,
215 HttpServletRequest request, HttpServletResponse response) {
216 logger.info("getOrderedDistributions(" + ObjectUtils.toString(levels) + ") - " + request.getRequestURI());
217 Set<TaxonDescription> taxonDescriptions = new HashSet<TaxonDescription>();
218 TaxonDescription description;
219 for (UUID descriptionUuid : descriptionUuidList) {
220 description = (TaxonDescription) service.load(descriptionUuid);
221 taxonDescriptions.add(description);
223 NamedAreaTree areaTree = service.getOrderedDistributions(taxonDescriptions, levels);
228 @RequestMapping(value
= "/description/{uuid}/naturalLanguageDescription/{featuretree_uuid}", method
= RequestMethod
.GET
)
229 public ModelAndView
doGenerateNaturalLanguageDescription(
230 @PathVariable("uuid") UUID uuid
,
231 @PathVariable("featuretree_uuid") UUID featureTreeUuid
,
232 HttpServletRequest request
,
233 HttpServletResponse response
) throws IOException
{
234 logger
.info("doGenerateNaturalLanguageDescription() - " + request
.getRequestURI());
236 DescriptionBase description
= service
.load(uuid
);
238 ModelAndView mv
= new ModelAndView();
240 List
<Language
> languages
= LocaleContext
.getLanguages();
242 if(!(description
instanceof TaxonDescription
)){
243 HttpStatusMessage
.UUID_REFERENCES_WRONG_TYPE
.send(response
);
244 // will terminate thread
247 FeatureTree featureTree
= featureTreeService
.load(featureTreeUuid
, null);
248 if(featureTree
== null){
249 HttpStatusMessage
.UUID_NOT_FOUND
.send(response
);
250 // will terminate thread
253 String naturalLanguageDescription
= service
.generateNaturalLanguageDescription(
255 (TaxonDescription
)description
,
258 TextData textData
= TextData
.NewInstance(Feature
.DESCRIPTION());
259 textData
.putText(Language
.DEFAULT(), naturalLanguageDescription
);
260 mv
.addObject(textData
);
264 @RequestMapping(value
= "/description/{uuid}/hasStructuredData", method
= RequestMethod
.GET
)
265 public ModelAndView
doHasStructuredData(
266 @PathVariable("uuid") UUID uuid
,
267 HttpServletRequest request
,
268 HttpServletResponse response
) throws IOException
{
269 logger
.info("doHasStructuredData() - " + request
.getRequestURI());
271 ModelAndView mv
= new ModelAndView();
273 DescriptionBase description
= service
.load(uuid
);
275 if(!(description
instanceof TaxonDescription
)){
276 HttpStatusMessage
.UUID_REFERENCES_WRONG_TYPE
.send(response
);
277 // will terminate thread
280 boolean hasStructuredData
= service
.hasStructuredData(description
);
282 mv
.addObject(hasStructuredData
);