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