solving probelms with the HttpStatusMessage class and potentially fixing a problem...
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / OccurrenceListController.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT 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
7 * 1.1 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 java.io.IOException;
13 import java.util.List;
14 import java.util.Set;
15 import java.util.UUID;
16
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19
20 import org.apache.lucene.queryParser.ParseException;
21 import org.hibernate.search.spatial.impl.Rectangle;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.stereotype.Controller;
24 import org.springframework.web.bind.WebDataBinder;
25 import org.springframework.web.bind.annotation.InitBinder;
26 import org.springframework.web.bind.annotation.RequestMapping;
27 import org.springframework.web.bind.annotation.RequestMethod;
28 import org.springframework.web.bind.annotation.RequestParam;
29
30 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
31 import eu.etaxonomy.cdm.api.service.ITaxonService;
32 import eu.etaxonomy.cdm.api.service.ITermService;
33 import eu.etaxonomy.cdm.api.service.pager.Pager;
34 import eu.etaxonomy.cdm.api.service.search.SearchResult;
35 import eu.etaxonomy.cdm.api.service.util.TaxonRelationshipEdge;
36 import eu.etaxonomy.cdm.model.common.Language;
37 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
38 import eu.etaxonomy.cdm.model.taxon.Taxon;
39 import eu.etaxonomy.cdm.persistence.query.OrderHint;
40 import eu.etaxonomy.cdm.remote.controller.util.ControllerUtils;
41 import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
42 import eu.etaxonomy.cdm.remote.editor.RectanglePropertyEditor;
43 import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor;
44 import eu.etaxonomy.cdm.remote.editor.UuidList;
45
46 /**
47 * TODO write controller documentation
48 *
49 * @author a.kohlbecker
50 * @date 24.03.2009
51 */
52 @Controller
53 @RequestMapping(value = {"/occurrence"})
54 public class OccurrenceListController extends IdentifiableListController<SpecimenOrObservationBase, IOccurrenceService> {
55
56
57 @Autowired
58 private ITaxonService taxonService;
59
60 @Autowired
61 private ITermService termService;
62
63 /* (non-Javadoc)
64 * @see eu.etaxonomy.cdm.remote.controller.BaseListController#setService(eu.etaxonomy.cdm.api.service.IService)
65 */
66 @Override
67 @Autowired
68 public void setService(IOccurrenceService service) {
69 this.service = service;
70 }
71
72 @InitBinder
73 @Override
74 public void initBinder(WebDataBinder binder) {
75 super.initBinder(binder);
76 binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
77 binder.registerCustomEditor(Rectangle.class, new RectanglePropertyEditor());
78 }
79
80 /**
81 * @param taxonUuid
82 * @param relationshipUuids a comma separated list of uuids e.g. CongruentTo; "60974c98-64ab-4574-bb5c-c110f6db634d"
83 * @param relationshipInversUuids a comma separated list of uuids
84 * @param maxDepth null for unlimited
85 * @param pageNumber
86 * @param pageSize
87 * @param request
88 * @param response
89 * @return
90 * @throws IOException
91 */
92 @RequestMapping(
93 value = {"byAssociatedTaxon"},
94 method = RequestMethod.GET)
95 public Pager<SpecimenOrObservationBase> doListByAssociatedTaxon(
96 @RequestParam(value = "taxonUuid", required = true) UUID taxonUuid,
97 @RequestParam(value = "relationships", required = false) UuidList relationshipUuids,
98 @RequestParam(value = "relationshipsInvers", required = false) UuidList relationshipInversUuids,
99 @RequestParam(value = "maxDepth", required = false) Integer maxDepth,
100 @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
101 @RequestParam(value = "pageSize", required = false) Integer pageSize,
102 HttpServletRequest request,
103 HttpServletResponse response) throws IOException {
104
105 logger.info("doListByAssociatedTaxon()" + request.getRequestURI() + "?" + request.getQueryString());
106
107 Set<TaxonRelationshipEdge> includeRelationships = ControllerUtils.loadIncludeRelationships(relationshipUuids, relationshipInversUuids, termService);
108
109 Taxon associatedTaxon = (Taxon) taxonService.find(taxonUuid);
110 PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);
111 pagerParams.normalizeAndValidate(response);
112
113 List<OrderHint> orderHints = null;
114
115 return service.pageByAssociatedTaxon(null, includeRelationships, associatedTaxon,
116 maxDepth, pagerParams.getPageSize(), pagerParams.getPageIndex(),
117 orderHints, getInitializationStrategy());
118
119 }
120
121 /**
122 *
123 * @param clazz
124 * @param queryString
125 * @param boundingBox
126 * as
127 * minx(minlongitute),miny(minlatitute),maxx(maxlongitute),max(
128 * maxlatitute), e.g. 13.112,52.374,13.681,52.641 for the Berlin
129 * area
130 * @param languages
131 * @param highlighting
132 * @param pageNumber
133 * @param pageSize
134 * @param request
135 * @param response
136 * @return
137 * @throws IOException
138 * @throws ParseException
139 */
140 @RequestMapping(method = RequestMethod.GET, value={"findByFullText"})
141 public Pager<SearchResult<SpecimenOrObservationBase>> dofindByFullText(
142 @RequestParam(value = "clazz", required = false) Class<? extends SpecimenOrObservationBase<?>> clazz,
143 @RequestParam(value = "query", required = false) String queryString,
144 @RequestParam(value = "bbox", required = false) Rectangle boundingBox,
145 @RequestParam(value = "languages", required = false) List<Language> languages,
146 @RequestParam(value = "hl", required = false) Boolean highlighting,
147 @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
148 @RequestParam(value = "pageSize", required = false) Integer pageSize,
149 HttpServletRequest request,
150 HttpServletResponse response
151 )
152 throws IOException, ParseException {
153
154 logger.info("findByFullText : " + request.getRequestURI() + "?" + request.getQueryString() );
155
156 PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);
157 pagerParams.normalizeAndValidate(response);
158
159 if(highlighting == null){
160 highlighting = false;
161 }
162
163 if(queryString == null && boundingBox == null) {
164 HttpStatusMessage.create("Either query or bbox must be given", 400).send(response);
165 return null;
166 }
167
168 Pager<SearchResult<SpecimenOrObservationBase>> pager = service.findByFullText(clazz, queryString, boundingBox, languages,
169 highlighting, pagerParams.getPageSize(), pagerParams.getPageIndex(), ((List<OrderHint>)null),
170 initializationStrategy);
171 return pager;
172 }
173
174 }