fixing MediaAutoInitializer & fixing bug in AbstractBeanInitializer
[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.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;
52
53 /**
54 * TODO write controller documentation
55 *
56 * @author a.kohlbecker
57 * @date 24.03.2009
58 */
59
60 @Controller
61 @RequestMapping(value = {"/description/{uuid}", "/description/{uuid_list}"})
62 public class DescriptionController extends BaseController<DescriptionBase, IDescriptionService>
63 {
64 @Autowired
65 private IFeatureTreeService featureTreeService;
66
67
68 public DescriptionController(){
69 super();
70 }
71
72 private static final List<String> FEATURETREE_INIT_STRATEGY = Arrays.asList(
73 new String[]{
74 "representations",
75 "root.feature.representations",
76 "root.children.feature.representations",
77 "root.children.children.feature.representations",
78 });
79
80
81
82 protected static final List<String> TAXONDESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
83 "$",
84 "elements.$",
85 "elements.sources.citation.authorTeam",
86 "elements.sources.nameUsedInSource.originalNameString",
87 "elements.multilanguageText",
88 "elements.media.representations.parts",
89 "elements.media.title",
90 });
91
92 @InitBinder
93 @Override
94 public void initBinder(WebDataBinder binder) {
95 super.initBinder(binder);
96 binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
97 binder.registerCustomEditor(NamedAreaLevel.class, new NamedAreaLevelPropertyEditor());
98 }
99
100 /* (non-Javadoc)
101 * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)
102 */
103 @Autowired
104 @Override
105 public void setService(IDescriptionService service) {
106 this.service = service;
107 }
108
109 /**
110 * @param request
111 * @param response
112 * @return
113 * @throws IOException
114 */
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);
121 return featureTree;
122 }
123
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 {
129
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);
135 }
136 mv.addObject(element);
137 return mv;
138 }
139
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
150 return null;
151 }
152
153 Pager<Annotation> annotations = service.getDescriptionElementAnnotations(annotatableEntity, null, null, 0, null, DEFAULT_INIT_STRATEGY);
154 return annotations;
155 }
156
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());
163
164 ModelAndView mv = new ModelAndView();
165
166 DescriptionElementBase descriptionElement = service.loadDescriptionElement(uuid,
167 Arrays.asList( new String[]{
168 "states.state.representations",
169 "modifiers",
170 "modifyingText"
171 } ));
172 if(descriptionElement == null){
173 HttpStatusMessage.UUID_INVALID.send(response);
174 // method will exit here
175 return null;
176 }
177
178 if(descriptionElement instanceof CategoricalData){
179
180 }
181 List<StateData> states = ((CategoricalData)descriptionElement).getStates();
182 mv.addObject(states);
183 return mv;
184 }
185
186
187
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
197 )
198 throws IOException {
199
200 logger.info("doFindDescriptionElements : " + request.getRequestURI() + "?" + request.getQueryString() );
201
202 PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);
203 pagerParams.normalizeAndValidate(response);
204
205 Pager<DescriptionElementBase> pager = service.searchElements(type, queryString, pageSize, pageNumber, null, DEFAULT_INIT_STRATEGY);
206
207 return pager;
208 }
209
210 /*
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);
223 }
224 NamedAreaTree areaTree = service.getOrderedDistributions(taxonDescriptions, levels);
225 return areaTree;
226 }
227 */
228
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());
236
237 DescriptionBase description = service.load(uuid);
238
239 ModelAndView mv = new ModelAndView();
240
241 List<Language> languages = LocaleContext.getLanguages();
242
243 if(!(description instanceof TaxonDescription)){
244 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
245 // will terminate thread
246 }
247
248 FeatureTree featureTree = featureTreeService.load(featureTreeUuid, null);
249 if(featureTree == null){
250 HttpStatusMessage.UUID_NOT_FOUND.send(response);
251 // will terminate thread
252 }
253
254 String naturalLanguageDescription = service.generateNaturalLanguageDescription(
255 featureTree,
256 (TaxonDescription)description,
257 languages,
258 ", ");
259 TextData textData = TextData.NewInstance(Feature.DESCRIPTION());
260 textData.putText(Language.DEFAULT(), naturalLanguageDescription);
261 mv.addObject(textData);
262 return mv;
263 }
264
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());
271
272 ModelAndView mv = new ModelAndView();
273
274 DescriptionBase description = service.load(uuid);
275
276 if(!(description instanceof TaxonDescription)){
277 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
278 // will terminate thread
279 }
280
281 boolean hasStructuredData = service.hasStructuredData(description);
282
283 mv.addObject(hasStructuredData);
284
285 return mv;
286 }
287
288 }