8babc465f1fae330f31f20c9e30206117fa3fdbc
[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.Collection;
16 import java.util.EnumSet;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Set;
20 import java.util.UUID;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.stereotype.Controller;
27 import org.springframework.transaction.annotation.Transactional;
28 import org.springframework.web.bind.WebDataBinder;
29 import org.springframework.web.bind.annotation.InitBinder;
30 import org.springframework.web.bind.annotation.PathVariable;
31 import org.springframework.web.bind.annotation.RequestMapping;
32 import org.springframework.web.bind.annotation.RequestMethod;
33 import org.springframework.web.bind.annotation.RequestParam;
34 import org.springframework.web.servlet.ModelAndView;
35
36 import eu.etaxonomy.cdm.api.service.DistributionTree;
37 import eu.etaxonomy.cdm.api.service.IDescriptionService;
38 import eu.etaxonomy.cdm.api.service.ITermService;
39 import eu.etaxonomy.cdm.api.service.dto.DistributionInfoDTO;
40 import eu.etaxonomy.cdm.api.service.dto.DistributionInfoDTO.InfoPart;
41 import eu.etaxonomy.cdm.api.service.pager.Pager;
42 import eu.etaxonomy.cdm.api.utility.DescriptionUtility;
43 import eu.etaxonomy.cdm.ext.geo.IEditGeoService;
44 import eu.etaxonomy.cdm.model.common.Annotation;
45 import eu.etaxonomy.cdm.model.common.Marker;
46 import eu.etaxonomy.cdm.model.common.MarkerType;
47 import eu.etaxonomy.cdm.model.description.DescriptionBase;
48 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
49 import eu.etaxonomy.cdm.model.description.TaxonDescription;
50 import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
51 import eu.etaxonomy.cdm.remote.editor.DefinedTermBaseList;
52 import eu.etaxonomy.cdm.remote.editor.NamedAreaLevelPropertyEditor;
53 import eu.etaxonomy.cdm.remote.editor.TermBaseListPropertyEditor;
54 import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor;
55 import eu.etaxonomy.cdm.remote.editor.UuidList;
56 import eu.etaxonomy.cdm.remote.l10n.LocaleContext;
57
58 /**
59 * IMPORTANT:
60 *
61 * This controller is mostly a 1:1 copy of the DescriptionController
62 * and this provides identical end points which only differ in the depth of the
63 * object graphs returned.
64 *
65 * @author a.kohlbecker
66 * @date Jun 25, 2013
67 *
68 */
69 @Controller
70 @Transactional(readOnly=true)
71 @RequestMapping(value = {
72 "/portal/description/{uuid}",
73 "/portal/description/{uuid_list}",
74 "/portal/descriptionElement/{descriptionelement_uuid}"})
75 public class DescriptionPortalController extends BaseController<DescriptionBase, IDescriptionService>
76 {
77
78 protected static final List<String> DESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
79 "$",
80 "elements.$",
81 "elements.multilanguageText.*",
82 "elements.annotations",
83 "elements.sources.citation.authorTeam.$",
84 "elements.sources.nameUsedInSource",
85 "elements.area.level",
86 "elements.modifyingText",
87 "elements.stateData.*",
88 "elements.media",
89 });
90
91 protected static final List<String> ORDERED_DISTRIBUTION_INIT_STRATEGY = Arrays.asList(new String []{
92 "elements.$",
93 "elements.annotations",
94 "elements.markers",
95 "elements.sources.citation.authorTeam.$",
96 "elements.sources.nameUsedInSource",
97 "elements.area.level",
98 });
99
100 protected static final List<String> DISTRIBUTION_INFO_INIT_STRATEGY = Arrays.asList(new String []{
101 "sources.citation.authorTeam.$",
102 "sources.nameUsedInSource"
103 });
104
105 @Autowired
106 private ITermService termService;
107
108 @Autowired
109 private IEditGeoService geoService;
110
111
112 public DescriptionPortalController() {
113 super();
114 setInitializationStrategy(DESCRIPTION_INIT_STRATEGY);
115 }
116
117 @InitBinder
118 @Override
119 public void initBinder(WebDataBinder binder) {
120 super.initBinder(binder);
121 binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
122 binder.registerCustomEditor(NamedAreaLevel.class, new NamedAreaLevelPropertyEditor());
123 binder.registerCustomEditor(DefinedTermBaseList.class, new TermBaseListPropertyEditor<MarkerType>(termService));
124 }
125
126 /* (non-Javadoc)
127 * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)
128 */
129 @Autowired
130 @Override
131 public void setService(IDescriptionService service) {
132 this.service = service;
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() - " + requestPathAndQuery(request) );
141 DescriptionElementBase annotatableEntity = service.getDescriptionElementByUuid(uuid);
142 Pager<Annotation> annotations = service.getDescriptionElementAnnotations(annotatableEntity, null, null, 0, null, getInitializationStrategy());
143 return annotations;
144 }
145
146 /**
147 * NOTICE: required to have a TreeNodeBeanProcessor configured which suppresses the
148 * redundant output of distribution.area
149 *
150 * @param descriptionUuidList
151 * @param subAreaPreference
152 * enables the <b>Sub area preference rule</b> if set to true,
153 * see {@link DescriptionUtility#filterDistributions(Collection, boolean, boolean}
154 * @param statusOrderPreference
155 * enables the <b>Status order preference rule</b> if set to true,
156 * see {@link DescriptionUtility#filterDistributions(Collection, boolean, boolean}
157 * @param hideMarkedAreas
158 * distributions where the area has a {@link Marker} with one of
159 * the specified {@link MarkerType}s will be skipped, see
160 * {@link DescriptionUtility#filterDistributions(Collection, boolean, boolean, Set)}
161 * @param omitLevels
162 * @param request
163 * @param response
164 * @return
165 */
166 @RequestMapping(value = "/portal/description/{uuid_list}/DistributionTree", method = RequestMethod.GET)
167 public DistributionTree doGetOrderedDistributionsB(
168 @PathVariable("uuid_list") UuidList descriptionUuidList,
169 @RequestParam(value = "subAreaPreference", required = false) boolean subAreaPreference,
170 @RequestParam(value = "statusOrderPreference", required = false) boolean statusOrderPreference,
171 @RequestParam(value = "hideMarkedAreas", required = false) DefinedTermBaseList<MarkerType> hideMarkedAreasList,
172 @RequestParam(value = "omitLevels", required = false) Set<NamedAreaLevel> omitLevels,
173 HttpServletRequest request,
174 HttpServletResponse response) {
175
176 logger.info("getOrderedDistributionsB() - " + requestPathAndQuery(request) );
177
178 Set<TaxonDescription> taxonDescriptions = new HashSet<TaxonDescription>();
179 TaxonDescription description;
180 for (UUID descriptionUuid : descriptionUuidList) {
181 logger.debug(" loading description " + descriptionUuid.toString() );
182 description = (TaxonDescription) service.load(descriptionUuid, null);
183 taxonDescriptions.add(description);
184 }
185
186 Set<MarkerType> hideMarkedAreas = null;
187 if(hideMarkedAreasList != null){
188 hideMarkedAreas = hideMarkedAreasList.asSet();
189 }
190
191 logger.debug(" get ordered distributions ");
192 DistributionTree distTree = service.getOrderedDistributions(taxonDescriptions, subAreaPreference, statusOrderPreference,
193 hideMarkedAreas, omitLevels, ORDERED_DISTRIBUTION_INIT_STRATEGY);
194 if (logger.isDebugEnabled()){ logger.debug("done");}
195 return distTree;
196 }
197
198 /**
199 * @param taxonUuid
200 * @param parts
201 * possible values: condensedStatusString, tree, mapUriParams,
202 * elements,
203 * @param subAreaPreference
204 * @param statusOrderPreference
205 * @param hideMarkedAreasList
206 * @param omitLevels
207 * @param request
208 * @param response
209 * @return
210 */
211 @RequestMapping(value = "/portal/description/distributionInfoFor/{uuid}", method = RequestMethod.GET)
212 public ModelAndView doGetDistributionInfo(
213 @PathVariable("uuid") UUID taxonUuid,
214 @RequestParam("part") Set<InfoPart> partSet,
215 @RequestParam(value = "subAreaPreference", required = false) boolean subAreaPreference,
216 @RequestParam(value = "statusOrderPreference", required = false) boolean statusOrderPreference,
217 @RequestParam(value = "hideMarkedAreas", required = false) DefinedTermBaseList<MarkerType> hideMarkedAreasList,
218 @RequestParam(value = "omitLevels", required = false) Set<NamedAreaLevel> omitLevels,
219 HttpServletRequest request,
220 HttpServletResponse response) {
221
222 logger.debug("doGetDistributionInfo() - " + requestPathAndQuery(request));
223
224 ModelAndView mv = new ModelAndView();
225
226 Set<MarkerType> hideMarkedAreas = null;
227 if(hideMarkedAreasList != null){
228 hideMarkedAreas = hideMarkedAreasList.asSet();
229 }
230
231 EnumSet<InfoPart> parts = EnumSet.copyOf(partSet);
232
233 DistributionInfoDTO dto = geoService.composeDistributionInfoFor(parts, taxonUuid, subAreaPreference, statusOrderPreference,
234 hideMarkedAreas, omitLevels, LocaleContext.getLanguages(), DISTRIBUTION_INFO_INIT_STRATEGY);
235
236 mv.addObject(dto);
237
238 return mv;
239 }
240
241
242 }