merging /branches/cdmlib/SPRINT-Chichorieae1/ to trunk
[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.HashSet;
16 import java.util.List;
17 import java.util.Set;
18 import java.util.UUID;
19
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
22
23 import org.apache.commons.lang.ObjectUtils;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.stereotype.Controller;
26 import org.springframework.web.bind.WebDataBinder;
27 import org.springframework.web.bind.annotation.InitBinder;
28 import org.springframework.web.bind.annotation.ModelAttribute;
29 import org.springframework.web.bind.annotation.PathVariable;
30 import org.springframework.web.bind.annotation.RequestMapping;
31 import org.springframework.web.bind.annotation.RequestMethod;
32 import org.springframework.web.bind.annotation.RequestParam;
33
34 import eu.etaxonomy.cdm.api.service.AnnotatableServiceBase;
35 import eu.etaxonomy.cdm.api.service.DescriptionServiceImpl;
36 import eu.etaxonomy.cdm.api.service.IDescriptionService;
37 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
38 import eu.etaxonomy.cdm.api.service.NamedAreaTree;
39 import eu.etaxonomy.cdm.api.service.pager.Pager;
40 import eu.etaxonomy.cdm.model.common.Annotation;
41 import eu.etaxonomy.cdm.model.description.DescriptionBase;
42 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
43 import eu.etaxonomy.cdm.model.description.FeatureTree;
44 import eu.etaxonomy.cdm.model.description.TaxonDescription;
45 import eu.etaxonomy.cdm.model.location.NamedArea;
46 import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
47 import eu.etaxonomy.cdm.remote.editor.NamedAreaLevelPropertyEditor;
48
49 import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor;
50 import eu.etaxonomy.cdm.remote.editor.UUIDPropertyEditor;
51 import eu.etaxonomy.cdm.remote.editor.UuidList;
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/*","/description/{uuid}", "/description/{uuid_list}", "/descriptionelement/*", "/featuretree/*"})
62 public class DescriptionController extends AnnotatableController<DescriptionBase, IDescriptionService>
63 {
64 @Autowired
65 private IFeatureTreeService featureTreeService;
66
67 public DescriptionController(){
68 super();
69 setUuidParameterPattern("^/(?:[^/]+)/([^/?#&\\.]+).*");
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 });
78
79 @InitBinder
80 public void initBinder(WebDataBinder binder) {
81 binder.registerCustomEditor(UUID.class, new UUIDPropertyEditor());
82 binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
83 binder.registerCustomEditor(NamedAreaLevel.class, new NamedAreaLevelPropertyEditor());
84 }
85
86 /* (non-Javadoc)
87 * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)
88 */
89 @Autowired
90 @Override
91 public void setService(IDescriptionService service) {
92 this.service = service;
93 }
94
95 /**
96 * TODO write controller method documentation
97 *
98 * @param request
99 * @param response
100 * @return
101 * @throws IOException
102 */
103 @RequestMapping(value = {"/featuretree/{featuretree_uuid}"}, method = RequestMethod.GET)
104 public FeatureTree doGetFeatureTree(
105 @PathVariable("featuretree_uuid") UUID uuid,
106 HttpServletRequest request,
107 HttpServletResponse response)throws IOException {
108 UUID featureTreeUuid = readValueUuid(request, null);
109 FeatureTree featureTree = featureTreeService.load(featureTreeUuid, FEATURETREE_INIT_STRATEGY);
110 if(featureTree == null){
111 HttpStatusMessage.UUID_NOT_FOUND.send(response);
112 }
113 return featureTree;
114 }
115
116 @RequestMapping(value = "/{descriptionelement_uuid}/annotation", method = RequestMethod.GET)
117 public Pager<Annotation> getAnnotations(
118 @PathVariable("descriptionelement_uuid") UUID uuid,
119 HttpServletRequest request,
120 HttpServletResponse response) throws IOException {
121 logger.info("getAnnotations() - " + request.getServletPath());
122 DescriptionElementBase annotatableEntity = service.getDescriptionElementByUuid(uuid);
123 Pager<Annotation> annotations = service.getDescriptionElementAnnotations(annotatableEntity, null, null, 0, null, ANNOTATION_INIT_STRATEGY);
124 return annotations;
125 }
126
127 @RequestMapping(value = "{uuid_list}/namedAreaTree", method = RequestMethod.GET)
128 public NamedAreaTree doGetOrderedDistributions(
129 @PathVariable("uuid_list") UuidList descriptionUuidList,
130 @RequestParam(value = "omitLevels", required = false) Set<NamedAreaLevel> levels,
131 //@ModelAttribute("omitLevels") HashSet<NamedAreaLevel> levels,
132 HttpServletRequest request, HttpServletResponse response) {
133 logger.info("getOrderedDistributions(" + ObjectUtils.toString(levels) + ") - " + request.getServletPath());
134 Set<TaxonDescription> taxonDescriptions = new HashSet<TaxonDescription>();
135 TaxonDescription description;
136 for (UUID descriptionUuid : descriptionUuidList) {
137 description = (TaxonDescription) service.load(descriptionUuid);
138 taxonDescriptions.add(description);
139 }
140 NamedAreaTree areaTree = service.getOrderedDistributions(taxonDescriptions, levels);
141 return areaTree;
142 }
143
144
145 }