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
.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
;
52 * TODO write controller documentation
54 * @author a.kohlbecker
59 @RequestMapping(value
= {"/description/{uuid}", "/description/{uuid_list}"})
60 public class DescriptionController
extends BaseController
<DescriptionBase
, IDescriptionService
>
63 private IFeatureTreeService featureTreeService
;
66 public DescriptionController(){
70 private static final List
<String
> FEATURETREE_INIT_STRATEGY
= Arrays
.asList(
73 "root.feature.representations",
74 "root.children.feature.representations",
75 "root.children.children.feature.representations",
79 protected static final List
<String
> TAXONDESCRIPTION_INIT_STRATEGY
= Arrays
.asList(new String
[]{
82 "elements.sources.citation.authorTeam",
83 "elements.sources.nameUsedInSource.originalNameString",
84 "elements.multilanguageText",
85 "elements.media.representations.parts",
86 "elements.media.title",
91 public void initBinder(WebDataBinder binder
) {
92 super.initBinder(binder
);
93 binder
.registerCustomEditor(UuidList
.class, new UUIDListPropertyEditor());
94 binder
.registerCustomEditor(NamedAreaLevel
.class, new NamedAreaLevelPropertyEditor());
98 * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)
102 public void setService(IDescriptionService service
) {
103 this.service
= service
;
110 * @throws IOException
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
);
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
135 Pager
<Annotation
> annotations
= service
.getDescriptionElementAnnotations(annotatableEntity
, null, null, 0, null, DEFAULT_INIT_STRATEGY
);
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
153 logger
.info("doFindDescriptionElements : " + request
.getRequestURI() + "?" + request
.getQueryString() );
155 PagerParameters pagerParams
= new PagerParameters(pageSize
, pageNumber
);
156 pagerParams
.normalizeAndValidate(response
);
158 Pager
<DescriptionElementBase
> pager
= service
.searchElements(type
, queryString
, pageSize
, pageNumber
, null, DEFAULT_INIT_STRATEGY
);
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);
177 NamedAreaTree areaTree = service.getOrderedDistributions(taxonDescriptions, levels);
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());
190 DescriptionBase description
= service
.load(uuid
);
192 ModelAndView mv
= new ModelAndView();
194 List
<Language
> languages
= LocaleContext
.getLanguages();
196 if(!(description
instanceof TaxonDescription
)){
197 HttpStatusMessage
.UUID_REFERENCES_WRONG_TYPE
.send(response
);
198 // will terminate thread
201 FeatureTree featureTree
= featureTreeService
.load(featureTreeUuid
, null);
202 if(featureTree
== null){
203 HttpStatusMessage
.UUID_NOT_FOUND
.send(response
);
204 // will terminate thread
207 String naturalLanguageDescription
= service
.generateNaturalLanguageDescription(
209 (TaxonDescription
)description
,
212 TextData textData
= TextData
.NewInstance(Feature
.DESCRIPTION());
213 textData
.putText(Language
.DEFAULT(), naturalLanguageDescription
);
214 mv
.addObject(textData
);
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());
225 ModelAndView mv
= new ModelAndView();
227 DescriptionBase description
= service
.load(uuid
);
229 if(!(description
instanceof TaxonDescription
)){
230 HttpStatusMessage
.UUID_REFERENCES_WRONG_TYPE
.send(response
);
231 // will terminate thread
234 boolean hasStructuredData
= service
.hasStructuredData(description
);
236 mv
.addObject(hasStructuredData
);