ba8a1236ea1c2e2b7cb27bab4d63f669a1b60998
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / DerivedUnitFacadeController.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;
10
11 import java.io.IOException;
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.UUID;
17 import java.util.stream.Collectors;
18
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
21
22 import org.apache.logging.log4j.LogManager;
23 import org.apache.logging.log4j.Logger;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.stereotype.Controller;
26 import org.springframework.web.bind.WebDataBinder;
27 import org.springframework.web.bind.annotation.InitBinder;
28 import org.springframework.web.bind.annotation.PathVariable;
29 import org.springframework.web.bind.annotation.RequestMapping;
30 import org.springframework.web.bind.annotation.RequestMethod;
31 import org.springframework.web.servlet.ModelAndView;
32
33 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
34 import eu.etaxonomy.cdm.api.service.dto.MediaDTO;
35 import eu.etaxonomy.cdm.facade.DerivedUnitFacade;
36 import eu.etaxonomy.cdm.facade.DerivedUnitFacadeNotSupportedException;
37 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
38 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
39 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
40 import eu.etaxonomy.cdm.remote.editor.UUIDPropertyEditor;
41 import io.swagger.annotations.Api;
42
43 /**
44 * @author a.kohlbecker
45 * @since 28.06.2010
46 */
47 @Controller
48 @Api("derivedUnitFacade")
49 @RequestMapping(value = {"/derivedUnitFacade/{uuid}"})
50 public class DerivedUnitFacadeController extends AbstractController<SpecimenOrObservationBase, IOccurrenceService>{
51
52 private static final Logger logger = LogManager.getLogger();
53
54 private IOccurrenceService service;
55
56 private final List<String> derivedUnitFacadeInitStrategy = Arrays.asList(new String []{
57 "$",
58 "titleCache",
59 "collection"
60 });
61
62 public DerivedUnitFacadeController(){
63 setInitializationStrategy(derivedUnitFacadeInitStrategy);
64 }
65
66 @Override
67 @Autowired
68 public void setService(IOccurrenceService service) {
69 this.service = service;
70 }
71
72 @InitBinder
73 public void initBinder(WebDataBinder binder) {
74 binder.registerCustomEditor(UUID.class, new UUIDPropertyEditor());
75 }
76
77 @RequestMapping(method = RequestMethod.GET)
78 public DerivedUnitFacade doGet(
79 @PathVariable("uuid") UUID occurrenceUuid,
80 HttpServletRequest request,
81 HttpServletResponse response) throws IOException {
82
83 logger.info("doGet() - " + request.getRequestURI());
84 DerivedUnitFacade duf = newFacadeFrom(occurrenceUuid, response, null);
85 return duf;
86 }
87
88 @RequestMapping(value = {"derivedUnitMedia"}, method = RequestMethod.GET)
89 public ModelAndView doGetDerivedUnitMedia(
90 @PathVariable("uuid") UUID occurrenceUuid,
91 HttpServletRequest request,
92 HttpServletResponse response) throws IOException {
93
94 logger.info("doGetDerivedUnitMedia() - " + request.getRequestURI());
95 ModelAndView mv = new ModelAndView();
96 DerivedUnitFacade duf = newFacadeFrom(occurrenceUuid, response, Arrays.asList(new String []{
97 "derivedUnitMedia", "derivedUnitMedia.title"}));
98 if(duf != null){
99 mv.addObject(duf.getDerivedUnitMedia());
100 }
101 return mv;
102 }
103
104 @RequestMapping(value = {"fieldObjectMediaDTO"}, method = RequestMethod.GET)
105 public List<MediaDTO> doGetFieldObjectMediaDTO(
106 @PathVariable("uuid") UUID occurrenceUuid,
107 HttpServletRequest request,
108 HttpServletResponse response) {
109
110 logger.info("doGetFieldObjectMediaDTO() - " + readPathParameter(request, null));
111
112 Collection<FieldUnit> fus = service.findFieldUnits(occurrenceUuid, null);
113 List<MediaDTO> mediaDTOs = fus.stream()
114 .map(fu -> service.getMediaDTOs(fu, null, null))
115 .flatMap(p -> p.getRecords().stream())
116 .collect(Collectors.toList());
117 return mediaDTOs;
118 }
119
120 @RequestMapping(value = {"fieldObjectMedia"}, method = RequestMethod.GET)
121 public ModelAndView doGetFieldObjectMedia(
122 @PathVariable("uuid") UUID occurrenceUuid,
123 HttpServletRequest request,
124 HttpServletResponse response) throws IOException {
125
126 logger.info("doGetFieldObjectMedia() - " + request.getRequestURI());
127 ModelAndView mv = new ModelAndView();
128 DerivedUnitFacade duf = newFacadeFrom(occurrenceUuid, response,Arrays.asList(new String []{
129 "fieldObjectMedia", "fieldObjectMedia.title"}));
130
131 mv.addObject(duf.getFieldObjectMedia());
132
133 return mv;
134 }
135
136
137 // TODO
138 //@RequestMapping(method = RequestMethod.GET, value = "{uuid}/collectingareas")
139 @RequestMapping(
140 value = {"collectingareas"},
141 method = RequestMethod.GET)
142 public Object doGetCollectingAreas(
143 @PathVariable("uuid") UUID occurrenceUuid,
144 HttpServletRequest request,
145 HttpServletResponse response) throws IOException {
146
147 logger.info("doGetCollectingAreas() - " + request.getRequestURI());
148 DerivedUnitFacade duf = newFacadeFrom(occurrenceUuid,
149 response,
150 Arrays.asList(new String []{"collectingAreas"}));
151 return duf.getCollectingAreas();
152 }
153
154 @RequestMapping(method = RequestMethod.GET, value = "collection")
155 public Object doGetCollection(
156 @PathVariable("uuid") UUID uuid,
157 HttpServletRequest request,
158 HttpServletResponse response) throws IOException {
159
160 logger.info("doGetCollection() - " + request.getRequestURI());
161 DerivedUnitFacade duf = newFacadeFrom(uuid,
162 response,
163 Arrays.asList(new String []{"collection"}));
164 return duf.getCollection();
165 }
166
167
168 //TODO:
169 // public Point getExactLocation() => valueProcessor?
170
171
172 // public Collection getCollection() {
173 // public AgentBase getCollector() {
174 // public DerivedUnit getDerivedUnit() {
175 // public Map<Language, LanguageString> getDerivedUnitDefinitions(){
176 // public List<Media> getDerivedUnitMedia() {
177 // public Set<DeterminationEvent> getDeterminations() {
178 // public Set<Specimen> getDuplicates(){
179 // public Map<Language, LanguageString> getEcologyAll(){
180 // public Map<Language, LanguageString> getFieldObjectDefinition() {
181 // public List<Media> getFieldObjectMedia() {
182 // public FieldUnit getFieldUnit(){
183 // public GatheringEvent getGatheringEvent() {
184 // public String getGatheringEventDescription() {
185 // public Map<Language, LanguageString> getPlantDescriptionAll(){ ==> representation !!
186 // public PreservationMethod getPreservationMethod()
187 // public Set<IdentifiableSource> getSources(){
188 // public TaxonName getStoredUnder() {
189
190
191 /**
192 * @param occurrenceUuid
193 * @param response
194 * @param extendedInitStrategy
195 * @return the requested <code>DerivedUnitFacade</code> instance or <code>null</code>
196 * @throws IOException
197 */
198 private DerivedUnitFacade newFacadeFrom(UUID occurrenceUuid, HttpServletResponse response, List<String> extendedInitStrategy)
199 throws IOException {
200 List<String> initStrategy = new ArrayList<String>(getInitializationStrategy());
201 if(extendedInitStrategy != null && extendedInitStrategy.size() > 0){
202 initStrategy.addAll(extendedInitStrategy);
203 }
204 SpecimenOrObservationBase<?> sob = service.load(occurrenceUuid, null);
205 if(sob instanceof DerivedUnit){
206 try {
207 return service.getDerivedUnitFacade(((DerivedUnit)sob), initStrategy);
208 } catch (DerivedUnitFacadeNotSupportedException e) {
209 logger.error(e); //TODO ...
210 }
211 } else {
212
213 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
214 }
215 return null;
216 }
217
218
219
220
221
222
223
224
225
226
227
228 }