removing init strategies which are replaces by MediaAutoInitializer
[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",
89 });
90
91 @InitBinder
92 @Override
93 public void initBinder(WebDataBinder binder) {
94 super.initBinder(binder);
95 binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
96 binder.registerCustomEditor(NamedAreaLevel.class, new NamedAreaLevelPropertyEditor());
97 }
98
99 /* (non-Javadoc)
100 * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)
101 */
102 @Autowired
103 @Override
104 public void setService(IDescriptionService service) {
105 this.service = service;
106 }
107
108 /**
109 * @param request
110 * @param response
111 * @return
112 * @throws IOException
113 */
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);
120 return featureTree;
121 }
122
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 {
128
129 ModelAndView mv = new ModelAndView();
130 logger.info("doGetDescriptionElement() - " + request.getServletPath());
131 DescriptionElementBase element = service.getDescriptionElementByUuid(uuid);
132 if(element == null) {
133 HttpStatusMessage.UUID_NOT_FOUND.send(response);
134 }
135 mv.addObject(element);
136 return mv;
137 }
138
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.getServletPath());
145 DescriptionElementBase annotatableEntity = service.getDescriptionElementByUuid(uuid);
146 if(annotatableEntity == null){
147 HttpStatusMessage.UUID_INVALID.send(response);
148 // method will exit here
149 return null;
150 }
151
152 Pager<Annotation> annotations = service.getDescriptionElementAnnotations(annotatableEntity, null, null, 0, null, DEFAULT_INIT_STRATEGY);
153 return annotations;
154 }
155
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.getServletPath());
162
163 ModelAndView mv = new ModelAndView();
164
165 DescriptionElementBase descriptionElement = service.loadDescriptionElement(uuid,
166 Arrays.asList( new String[]{
167 "states.state.representations",
168 "modifiers",
169 "modifyingText"
170 } ));
171 if(descriptionElement == null){
172 HttpStatusMessage.UUID_INVALID.send(response);
173 // method will exit here
174 return null;
175 }
176
177 if(descriptionElement instanceof CategoricalData){
178
179 }
180 List<StateData> states = ((CategoricalData)descriptionElement).getStates();
181 mv.addObject(states);
182 return mv;
183 }
184
185
186
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
196 )
197 throws IOException {
198
199 logger.info("doFindDescriptionElements : " + request.getRequestURI() + "?" + request.getQueryString() );
200
201 PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);
202 pagerParams.normalizeAndValidate(response);
203
204 Pager<DescriptionElementBase> pager = service.searchElements(type, queryString, pageSize, pageNumber, null, DEFAULT_INIT_STRATEGY);
205
206 return pager;
207 }
208
209 /*
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.getServletPath());
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);
222 }
223 NamedAreaTree areaTree = service.getOrderedDistributions(taxonDescriptions, levels);
224 return areaTree;
225 }
226 */
227
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.getServletPath());
235
236 DescriptionBase description = service.load(uuid);
237
238 ModelAndView mv = new ModelAndView();
239
240 List<Language> languages = LocaleContext.getLanguages();
241
242 if(!(description instanceof TaxonDescription)){
243 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
244 // will terminate thread
245 }
246
247 FeatureTree featureTree = featureTreeService.load(featureTreeUuid, null);
248 if(featureTree == null){
249 HttpStatusMessage.UUID_NOT_FOUND.send(response);
250 // will terminate thread
251 }
252
253 String naturalLanguageDescription = service.generateNaturalLanguageDescription(
254 featureTree,
255 (TaxonDescription)description,
256 languages,
257 ", ");
258 TextData textData = TextData.NewInstance(Feature.DESCRIPTION());
259 textData.putText(Language.DEFAULT(), naturalLanguageDescription);
260 mv.addObject(textData);
261 return mv;
262 }
263
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.getServletPath());
270
271 ModelAndView mv = new ModelAndView();
272
273 DescriptionBase description = service.load(uuid);
274
275 if(!(description instanceof TaxonDescription)){
276 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
277 // will terminate thread
278 }
279
280 boolean hasStructuredData = service.hasStructuredData(description);
281
282 mv.addObject(hasStructuredData);
283
284 return mv;
285 }
286
287 }