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