resolving conflicts after merge of ticket_4716 into develop
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / DescriptionPortalController.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.cdm.remote.controller;
11
12 import io.swagger.annotations.Api;
13
14 import java.awt.Color;
15 import java.io.IOException;
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.EnumSet;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23 import java.util.UUID;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.stereotype.Controller;
30 import org.springframework.transaction.annotation.Transactional;
31 import org.springframework.web.bind.WebDataBinder;
32 import org.springframework.web.bind.annotation.InitBinder;
33 import org.springframework.web.bind.annotation.PathVariable;
34 import org.springframework.web.bind.annotation.RequestMapping;
35 import org.springframework.web.bind.annotation.RequestMethod;
36 import org.springframework.web.bind.annotation.RequestParam;
37 import org.springframework.web.servlet.ModelAndView;
38
39 import com.fasterxml.jackson.core.JsonParseException;
40 import com.fasterxml.jackson.databind.JsonMappingException;
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 *
69 * @author a.kohlbecker
70 * @date Jun 25, 2013
71 *
72 */
73 @Controller
74 @Api("portal_description")
75 @Transactional(readOnly=true)
76 @RequestMapping(value = {
77 "/portal/description/{uuid}",
78 "/portal/description/{uuid_list}"})
79 public class DescriptionPortalController extends BaseController<DescriptionBase, IDescriptionService>
80 {
81
82 protected static final List<String> DESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
83 "$",
84 "elements.$",
85 "elements.multilanguageText.*",
86 "elements.annotations",
87 "elements.sources.citation.authorship.$",
88 "elements.sources.nameUsedInSource",
89 "elements.area.level",
90 "elements.modifyingText",
91 "elements.stateData.*",
92 "elements.statisticalValues.*",
93 "elements.unit",
94 "elements.media",
95 "elements.kindOfUnit"
96
97 });
98
99 protected static final List<String> ORDERED_DISTRIBUTION_INIT_STRATEGY = Arrays.asList(new String []{
100 "elements.$",
101 "elements.annotations",
102 "elements.markers",
103 "elements.sources.citation.authorship.$",
104 "elements.sources.nameUsedInSource",
105 "elements.area.level",
106 });
107
108 protected static final List<String> DISTRIBUTION_INFO_INIT_STRATEGY = Arrays.asList(new String []{
109 "sources.citation.authorship.$",
110 "sources.nameUsedInSource",
111 "annotations"
112 });
113
114 @Autowired
115 private ITermService termService;
116
117 @Autowired
118 private IEditGeoService geoService;
119
120
121 public DescriptionPortalController() {
122 super();
123 setInitializationStrategy(DESCRIPTION_INIT_STRATEGY);
124 }
125
126 @InitBinder
127 @Override
128 public void initBinder(WebDataBinder binder) {
129 super.initBinder(binder);
130 binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
131 binder.registerCustomEditor(NamedAreaLevel.class, new NamedAreaLevelPropertyEditor());
132 binder.registerCustomEditor(DefinedTermBaseList.class, new TermBaseListPropertyEditor<MarkerType>(termService));
133 }
134
135 /* (non-Javadoc)
136 * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)
137 */
138 @Autowired
139 @Override
140 public void setService(IDescriptionService service) {
141 this.service = service;
142 }
143
144 @RequestMapping(value = "//portal/descriptionElement/{descriptionelement_uuid}/annotation", method = RequestMethod.GET) // mapped as absolute path, see CdmAntPathMatcher
145 public Pager<Annotation> getAnnotations(
146 @PathVariable("descriptionelement_uuid") UUID uuid,
147 HttpServletRequest request,
148 HttpServletResponse response) throws IOException {
149 logger.info("getAnnotations() - " + requestPathAndQuery(request) );
150 DescriptionElementBase annotatableEntity = service.getDescriptionElementByUuid(uuid);
151 Pager<Annotation> annotations = service.getDescriptionElementAnnotations(annotatableEntity, null, null, 0, null, getInitializationStrategy());
152 return annotations;
153 }
154
155 /**
156 * NOTICE: required to have a TreeNodeBeanProcessor configured which suppresses the
157 * redundant output of distribution.area
158 *
159 * @param descriptionUuidList
160 * @param subAreaPreference
161 * enables the <b>Sub area preference rule</b> if set to true,
162 * see {@link DescriptionUtility#filterDistributions(Collection, boolean, boolean}
163 * @param statusOrderPreference
164 * enables the <b>Status order preference rule</b> if set to true,
165 * see {@link DescriptionUtility#filterDistributions(Collection, boolean, boolean}
166 * @param hideMarkedAreas
167 * distributions where the area has a {@link Marker} with one of
168 * the specified {@link MarkerType}s will be skipped, see
169 * {@link DescriptionUtility#filterDistributions(Collection, boolean, boolean, Set)}
170 * @param omitLevels
171 * @param request
172 * @param response
173 * @return
174 */
175 @RequestMapping(value = "//portal/description/{uuid_list}/DistributionTree", method = RequestMethod.GET) // mapped as absolute path, see CdmAntPathMatcher
176 public DistributionTree doGetOrderedDistributionsB(
177 @PathVariable("uuid_list") UuidList descriptionUuidList,
178 @RequestParam(value = "subAreaPreference", required = false) boolean subAreaPreference,
179 @RequestParam(value = "statusOrderPreference", required = false) boolean statusOrderPreference,
180 @RequestParam(value = "hideMarkedAreas", required = false) DefinedTermBaseList<MarkerType> hideMarkedAreasList,
181 @RequestParam(value = "omitLevels", required = false) Set<NamedAreaLevel> omitLevels,
182 HttpServletRequest request,
183 HttpServletResponse response) {
184
185 logger.info("getOrderedDistributionsB() - " + requestPathAndQuery(request) );
186
187 Set<TaxonDescription> taxonDescriptions = new HashSet<TaxonDescription>();
188 TaxonDescription description;
189 for (UUID descriptionUuid : descriptionUuidList) {
190 logger.debug(" loading description " + descriptionUuid.toString() );
191 description = (TaxonDescription) service.load(descriptionUuid, null);
192 taxonDescriptions.add(description);
193 }
194
195 Set<MarkerType> hideMarkedAreas = null;
196 if(hideMarkedAreasList != null){
197 hideMarkedAreas = hideMarkedAreasList.asSet();
198 }
199
200 logger.debug(" get ordered distributions ");
201 DistributionTree distTree = service.getOrderedDistributions(taxonDescriptions, subAreaPreference, statusOrderPreference,
202 hideMarkedAreas, omitLevels, ORDERED_DISTRIBUTION_INIT_STRATEGY);
203 if (logger.isDebugEnabled()){ logger.debug("done");}
204 return distTree;
205 }
206
207 /**
208 * @param taxonUuid
209 * @param parts
210 * possible values: condensedStatus, tree, mapUriParams,
211 * elements,
212 * @param subAreaPreference
213 * @param statusOrderPreference
214 * @param hideMarkedAreasList
215 * @param recipe
216 * The recipe for creating the condensed distribution status
217 * @param omitLevels
218 * @param request
219 * @param response
220 * @return
221 * @throws IOException
222 * @throws JsonMappingException
223 * @throws JsonParseException
224 */
225 @RequestMapping(value = "//portal/description/distributionInfoFor/{uuid}", method = RequestMethod.GET) // mapped as absolute path, see CdmAntPathMatcher
226 public ModelAndView doGetDistributionInfo(
227 @PathVariable("uuid") UUID taxonUuid,
228 @RequestParam("part") Set<InfoPart> partSet,
229 @RequestParam(value = "subAreaPreference", required = false) boolean subAreaPreference,
230 @RequestParam(value = "statusOrderPreference", required = false) boolean statusOrderPreference,
231 @RequestParam(value = "hiddenAreaMarkerType", required = false) DefinedTermBaseList<MarkerType> hideMarkedAreasList,
232 @RequestParam(value = "omitLevels", required = false) Set<NamedAreaLevel> omitLevels,
233 @RequestParam(value = "statusColors", required = false) String statusColorsString,
234 @RequestParam(value = "recipe", required = false, defaultValue="EuroPlusMed") CondensedDistributionRecipe recipe,
235
236 HttpServletRequest request,
237 HttpServletResponse response) throws JsonParseException, JsonMappingException, IOException {
238
239 logger.info("doGetDistributionInfo() - " + requestPathAndQuery(request));
240
241 ModelAndView mv = new ModelAndView();
242
243 Set<MarkerType> hideMarkedAreas = null;
244 if(hideMarkedAreasList != null){
245 hideMarkedAreas = hideMarkedAreasList.asSet();
246 }
247
248 EnumSet<InfoPart> parts = EnumSet.copyOf(partSet);
249
250 Map<PresenceAbsenceTerm, Color> presenceAbsenceTermColors = EditGeoServiceUtilities.buildStatusColorMap(statusColorsString, termService);
251
252 DistributionInfoDTO dto = geoService.composeDistributionInfoFor(parts, taxonUuid, subAreaPreference, statusOrderPreference,
253 hideMarkedAreas, omitLevels, presenceAbsenceTermColors, LocaleContext.getLanguages(), DISTRIBUTION_INFO_INIT_STRATEGY, recipe);
254
255 mv.addObject(dto);
256
257 return mv;
258 }
259
260
261 }