bf109f874b4f60cdaa07884360b234407ecc56b3
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / ext / ExternalGeoController.java
1 /**
2 * Copyright (C) 2009 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 package eu.etaxonomy.cdm.remote.controller.ext;
10
11 import java.awt.Color;
12 import java.io.FileReader;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.EnumSet;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
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.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.Logger;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.stereotype.Controller;
29 import org.springframework.web.bind.WebDataBinder;
30 import org.springframework.web.bind.annotation.InitBinder;
31 import org.springframework.web.bind.annotation.PathVariable;
32 import org.springframework.web.bind.annotation.RequestMapping;
33 import org.springframework.web.bind.annotation.RequestMethod;
34 import org.springframework.web.bind.annotation.RequestParam;
35 import org.springframework.web.servlet.ModelAndView;
36
37 import eu.etaxonomy.cdm.api.filter.TaxonOccurrenceRelationType;
38 import eu.etaxonomy.cdm.api.service.IDescriptionService;
39 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
40 import eu.etaxonomy.cdm.api.service.ITaxonService;
41 import eu.etaxonomy.cdm.api.service.ITermService;
42 import eu.etaxonomy.cdm.api.service.geo.IDistributionService;
43 import eu.etaxonomy.cdm.api.util.TaxonRelationshipEdge;
44 import eu.etaxonomy.cdm.database.UpdatableRoutingDataSource;
45 import eu.etaxonomy.cdm.ext.geo.EditGeoServiceUtilities;
46 import eu.etaxonomy.cdm.ext.geo.IEditGeoService;
47 import eu.etaxonomy.cdm.ext.geo.OccurrenceServiceRequestParameterDto;
48 import eu.etaxonomy.cdm.model.common.MarkerType;
49 import eu.etaxonomy.cdm.model.location.NamedArea;
50 import eu.etaxonomy.cdm.model.location.Point;
51 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
52 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
53 import eu.etaxonomy.cdm.model.taxon.Taxon;
54 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
55 import eu.etaxonomy.cdm.persistence.query.OrderHint;
56 import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
57 import eu.etaxonomy.cdm.remote.controller.BaseController;
58 import eu.etaxonomy.cdm.remote.controller.TaxonController;
59 import eu.etaxonomy.cdm.remote.controller.util.ControllerUtils;
60 import eu.etaxonomy.cdm.remote.editor.DefinedTermBaseList;
61 import eu.etaxonomy.cdm.remote.editor.TermBaseListPropertyEditor;
62 import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor;
63 import eu.etaxonomy.cdm.remote.editor.UuidList;
64 import io.swagger.annotations.Api;
65
66 /**
67 * The ExternalGeoController class is a Spring MVC Controller.
68 * <p>
69 * The syntax of the mapped service URIs contains the the {datasource-name} path element.
70 * The available {datasource-name}s are defined in a configuration file which
71 * is loaded by the {@link UpdatableRoutingDataSource}. If the
72 * UpdatableRoutingDataSource is not being used in the actual application
73 * context any arbitrary {datasource-name} may be used.
74 * <p>
75 * @author a.kohlbecker
76 * @since 18.06.2009
77 *
78 */
79
80 @Controller
81 @Api(value="mapServiceParameters")
82 @RequestMapping(value = { "ext/edit/mapServiceParameters/" })
83 public class ExternalGeoController extends BaseController<TaxonBase, ITaxonService> {
84
85 private static final Logger logger = LogManager.getLogger();
86
87 @Autowired
88 private IEditGeoService geoService;
89
90 @Autowired
91 private IDistributionService distributionService;
92
93 @Autowired
94 private IDescriptionService descriptionService;
95
96 @Autowired
97 private IOccurrenceService occurrenceService;
98
99 @Autowired
100 private ITermService termService;
101
102 @InitBinder
103 @Override
104 public void initBinder(WebDataBinder binder) {
105 super.initBinder(binder);
106 binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
107 binder.registerCustomEditor(DefinedTermBaseList.class, new TermBaseListPropertyEditor<MarkerType>(termService));
108 }
109
110 @Autowired
111 @Override
112 public void setService(ITaxonService service) {
113 this.service = service;
114 }
115
116 /**
117 * Assembles and returns URI parameter Strings for the EDIT Map Service. The distribution areas for the
118 * {@link Taxon} instance identified by the <code>{taxon-uuid}</code> are found and are translated into
119 * an valid URI parameter String. Higher level distribution areas are expanded in order to include all
120 * nested sub-areas.
121 * <p>
122 * URI: <b>&#x002F;{datasource-name}&#x002F;geo&#x002F;map&#x002F;distribution&#x002F;{taxon-uuid}</b>
123 *
124 * @param request
125 * @param response
126 * @return URI parameter Strings for the EDIT Map Service
127 * @throws IOException TODO write controller method documentation
128 */
129 @RequestMapping(value = { "taxonOccurrencesFor/{uuid}" }, method = RequestMethod.GET)
130 public OccurrenceServiceRequestParameterDto doGetOccurrenceMapUriParams(
131 @PathVariable("uuid") UUID taxonUuid,
132 @RequestParam(value = "relationships", required = false) UuidList relationshipUuids,
133 @RequestParam(value = "relationshipsInvers", required = false) UuidList relationshipInversUuids,
134 @RequestParam(value = "maxDepth", required = false) Integer maxDepth,
135 @RequestParam(value = "taxOccRelFilter", required = false) String taxOccRelFilter,
136 HttpServletRequest request,
137 HttpServletResponse response)
138 throws IOException {
139
140 logger.info("doGetOccurrenceMapUriParams() " + requestPathAndQuery(request));
141
142 boolean includeUnpublished = NO_UNPUBLISHED;
143 EnumSet<TaxonOccurrenceRelationType> taxonOccurrenceRelTypes = TaxonController.bindAssociationFilter(taxOccRelFilter);
144
145 Map<SpecimenOrObservationType, Color> specimenOrObservationTypeColors = null;
146
147 List<SpecimenOrObservationBase> specimensOrObersvations = occurencesForTaxon(taxonUuid, relationshipUuids,
148 relationshipInversUuids, includeUnpublished,
149 taxonOccurrenceRelTypes,
150 maxDepth, response);
151
152 OccurrenceServiceRequestParameterDto dto = geoService.getOccurrenceServiceRequestParameters(specimensOrObersvations,
153 specimenOrObservationTypeColors );
154
155 return dto;
156 }
157
158 private List<SpecimenOrObservationBase> occurencesForTaxon(UUID taxonUuid, UuidList relationshipUuids,
159 UuidList relationshipInversUuids, boolean includeUnpublished,
160 EnumSet<TaxonOccurrenceRelationType> taxonOccurrenceRelTypes,
161 Integer maxDepth,
162 HttpServletResponse response) throws IOException {
163
164 Set<TaxonRelationshipEdge> includeRelationships = ControllerUtils.loadIncludeRelationships(
165 relationshipUuids, relationshipInversUuids, termService);
166
167 Taxon taxon = getCdmBaseInstance(Taxon.class, taxonUuid, response, (List<String>)null);
168
169 List<OrderHint> orderHints = new ArrayList<>();
170 orderHints.add(new OrderHint("titleCache", SortOrder.DESCENDING));
171
172 List<SpecimenOrObservationBase> specimensOrObersvations = occurrenceService.listByAssociatedTaxon(
173 null, includeRelationships, taxon, includeUnpublished,
174 taxonOccurrenceRelTypes,
175 maxDepth, null, null, orderHints, null);
176 return specimensOrObersvations;
177 }
178
179 /**
180 * Assembles and returns URI parameter Strings for the EDIT Map Service. The distribution areas for the
181 * {@link Taxon} instance identified by the <code>{taxon-uuid}</code> are found and are translated into
182 * an valid URI parameter String. Higher level distribution areas are expanded in order to include all
183 * nested sub-areas.
184 * <p>
185 * URI: <b>&#x002F;{datasource-name}&#x002F;geo&#x002F;map&#x002F;distribution&#x002F;{taxon-uuid}</b>
186 *
187 * @param request
188 * @param response
189 * @return URI parameter Strings for the EDIT Map Service
190 * @throws IOException TODO write controller method documentation
191 */
192 //TODO needed? Seems to be not used currently. Difference to
193 // doGetOccurrenceMapUriParams() seems to be that there is taxon relation here
194 @RequestMapping(value = { "taxonOccurrencesForX" }, method = RequestMethod.GET)
195 public ModelAndView doGetOccurrenceXMapUriParams(
196 @RequestParam(value = "fieldUnitUuidList", required = false) UuidList fieldUnitUuids,
197 HttpServletRequest request,
198 HttpServletResponse response)
199 throws IOException {
200
201 Map<SpecimenOrObservationType, Color> specimenOrObservationTypeColors = null;
202
203 logger.info("doGetOccurrenceMapUriParams() " + requestPathAndQuery(request));
204 ModelAndView mv = new ModelAndView();
205
206 List<Point> fieldUnitPoints = occurrenceService.findPointsForFieldUnitList(fieldUnitUuids);
207
208 OccurrenceServiceRequestParameterDto dto = EditGeoServiceUtilities.getOccurrenceServiceRequestParameterString(fieldUnitPoints,
209 null, specimenOrObservationTypeColors);
210 mv.addObject(dto);
211 return mv;
212 }
213
214 /**
215 * EXPERIMENTAL !!!!!
216 * DO NOT USE !!!!!
217 *
218 * @param vocabUuid
219 * @param request
220 * @param response
221 * @return
222 * @throws IOException
223 *
224 * @author a.kohlbecker
225 */
226 @RequestMapping(value = { "mapShapeFileToNamedAreas" }, method = RequestMethod.GET)
227 public ModelAndView doMapShapeFileToNamedAreas(
228 @RequestParam(required=false, value="vocabularyUuid") UUID vocabUuid,
229 @RequestParam(required=false, value="namedAreaUuids") UuidList namedAreaUuids,
230 @RequestParam(required=true, value="localFile") String localFile,
231 @RequestParam(required=true, value="idSearchField") List<String> idSearchFields,
232 @RequestParam(required=true, value="wmsLayerName") String wmsLayerName,
233 HttpServletRequest request,
234 HttpServletResponse response)
235 throws IOException {
236
237 logger.info("doMapShapeFileToNamedAreas() " + requestPathAndQuery(request));
238 ModelAndView mv = new ModelAndView();
239
240 FileReader reader = new FileReader(localFile);
241
242 Set<UUID> areaUuidSet = null;
243 if(namedAreaUuids != null) {
244 areaUuidSet = namedAreaUuids.asSet();
245 }
246 Map<NamedArea, String> resultMap = distributionService.mapShapeFileToNamedAreas(
247 reader, idSearchFields , wmsLayerName , vocabUuid, areaUuidSet);
248 Map<String, String> flatResultMap = new HashMap<String, String>(resultMap.size());
249 for(NamedArea area : resultMap.keySet()){
250 flatResultMap.put(area.getTitleCache() + " [" + area.getUuid() + "]", resultMap.get(area));
251 }
252 mv.addObject(flatResultMap);
253 return mv;
254
255 }
256
257
258 }