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