fixing MediaAutoInitializer & fixing bug in AbstractBeanInitializer
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / DescriptionPortalController.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.DistributionTree;
37 import eu.etaxonomy.cdm.api.service.IDescriptionService;
38 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
39 import eu.etaxonomy.cdm.api.service.NamedAreaTree;
40 import eu.etaxonomy.cdm.api.service.pager.Pager;
41 import eu.etaxonomy.cdm.model.common.Annotation;
42 import eu.etaxonomy.cdm.model.description.DescriptionBase;
43 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
44 import eu.etaxonomy.cdm.model.description.FeatureTree;
45 import eu.etaxonomy.cdm.model.description.TaxonDescription;
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 = {"/portal/description/{uuid}", "/portal/description/{uuid_list}", "/portal/descriptionElement/{descriptionelement_uuid}", "/portal/featureTree/{featuretree_uuid}"})
62 public class DescriptionPortalController extends BaseController<DescriptionBase, IDescriptionService>
63 {
64 @Autowired
65 private IFeatureTreeService featureTreeService;
66
67 private static final List<String> FEATURETREE_INIT_STRATEGY = Arrays.asList(
68 new String[]{
69 "representations",
70 "root.feature.representations",
71 "root.children.feature.representations",
72 });
73
74 private static final List<String> DESCRIPTIONS_DISTRIBUTION_INIT_STRATEGY = Arrays.asList(new String []{
75 "elements.sources.citation.$",
76 "elements.area.$",
77 });
78
79 protected static final List<String> DESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
80 "$",
81 "elements.$",
82 "elements.sources.citation.authorTeam.$",
83 "elements.sources.nameUsedInSource.originalNameString",
84 "elements.area.level",
85 "elements.modifyingText",
86 "elements.states.*",
87 "elements.media.representations.parts", // TODO "elements.media" should be sufficient since we have the MediaAutoInitializer
88 });
89
90
91 public DescriptionPortalController() {
92 super();
93 setInitializationStrategy(DESCRIPTION_INIT_STRATEGY);
94 }
95
96 @InitBinder
97 @Override
98 public void initBinder(WebDataBinder binder) {
99 super.initBinder(binder);
100 binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
101 binder.registerCustomEditor(NamedAreaLevel.class, new NamedAreaLevelPropertyEditor());
102 }
103
104 /* (non-Javadoc)
105 * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)
106 */
107 @Autowired
108 @Override
109 public void setService(IDescriptionService service) {
110 this.service = service;
111 }
112
113 /**
114 * TODO write controller method documentation
115 *
116 * @param request
117 * @param response
118 * @return
119 * @throws IOException
120 */
121
122 @RequestMapping(value = {"/portal/featureTree/{featuretree_uuid}"}, method = RequestMethod.GET)
123 public FeatureTree doGetFeatureTree(
124 @PathVariable("featuretree_uuid") UUID featureUuid,
125 HttpServletRequest request,
126 HttpServletResponse response)throws IOException {
127 //UUID featureTreeUuid = readValueUuid(request, null);
128 FeatureTree featureTree = featureTreeService.load(featureUuid, FEATURETREE_INIT_STRATEGY);
129 if(featureTree == null){
130 HttpStatusMessage.UUID_NOT_FOUND.send(response);
131 }
132 return featureTree;
133 }
134
135 @RequestMapping(value = "/portal/descriptionElement/{descriptionelement_uuid}/annotation", method = RequestMethod.GET)
136 public Pager<Annotation> getAnnotations(
137 @PathVariable("descriptionelement_uuid") UUID uuid,
138 HttpServletRequest request,
139 HttpServletResponse response) throws IOException {
140 logger.info("getAnnotations() - " + request.getServletPath());
141 DescriptionElementBase annotatableEntity = service.getDescriptionElementByUuid(uuid);
142 Pager<Annotation> annotations = service.getDescriptionElementAnnotations(annotatableEntity, null, null, 0, null, DEFAULT_INIT_STRATEGY);
143 return annotations;
144 }
145
146 @RequestMapping(value = "/portal/description/{uuid_list}/DistributionTree", method = RequestMethod.GET)
147 public DistributionTree doGetOrderedDistributionsB(
148 @PathVariable("uuid_list") UuidList descriptionUuidList,
149 @RequestParam(value = "omitLevels", required = false) Set<NamedAreaLevel> levels,
150 HttpServletRequest request, HttpServletResponse response) {
151 logger.info("getOrderedDistributionsB(" + ObjectUtils.toString(levels) + ") - " + request.getServletPath());
152 Set<TaxonDescription> taxonDescriptions = new HashSet<TaxonDescription>();
153 TaxonDescription description;
154 for (UUID descriptionUuid : descriptionUuidList) {
155 description = (TaxonDescription) service.load(descriptionUuid, DESCRIPTION_INIT_STRATEGY);
156 taxonDescriptions.add(description);
157 }
158 DistributionTree distTree = service.getOrderedDistributions(taxonDescriptions, levels, DESCRIPTION_INIT_STRATEGY);
159 return distTree;
160 }
161
162 }