Project

General

Profile

Download (5.39 KB) Statistics
| Branch: | Tag: | Revision:
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.io.IOException;
14
import java.util.ArrayList;
15
import java.util.Arrays;
16
import java.util.HashSet;
17
import java.util.List;
18
import java.util.Set;
19
import java.util.UUID;
20

    
21
import javax.servlet.http.HttpServletRequest;
22
import javax.servlet.http.HttpServletResponse;
23

    
24
import org.apache.log4j.Logger;
25
import org.springframework.beans.factory.annotation.Autowired;
26
import org.springframework.stereotype.Controller;
27
import org.springframework.web.bind.annotation.PathVariable;
28
import org.springframework.web.bind.annotation.RequestMapping;
29
import org.springframework.web.bind.annotation.RequestMethod;
30
import org.springframework.web.servlet.ModelAndView;
31

    
32
import eu.etaxonomy.cdm.api.service.INameService;
33
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
34
import eu.etaxonomy.cdm.api.service.ITaxonService;
35
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
36
import eu.etaxonomy.cdm.model.taxon.Synonym;
37
import eu.etaxonomy.cdm.model.taxon.Taxon;
38
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
39
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
40
import eu.etaxonomy.cdm.persistence.query.OrderHint;
41
import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
42
import eu.etaxonomy.cdm.strategy.TaggedTextGenerator;
43

    
44
/**
45
 * TODO write controller documentation
46
 * 
47
 * @author a.kohlbecker
48
 * @date 20.07.2009
49
 *
50
 */
51
@Controller
52
@RequestMapping(value = {"/taxon/{uuid}"})
53
public class TaxonController extends AnnotatableController<TaxonBase, ITaxonService>
54
{
55
	public static final Logger logger = Logger.getLogger(TaxonController.class);
56
	
57
	@Autowired
58
	private IOccurrenceService occurrenceService;
59
	@Autowired
60
	private INameService nameService;
61
	
62
	protected static final List<String> TAXONNODE_INIT_STRATEGY = Arrays.asList(new String []{
63
			"taxonNodes"
64
	});
65
	
66
	public TaxonController(){
67
		super();
68
		setInitializationStrategy(Arrays.asList(new String[]{"$","name.nomenclaturalReference"}));
69
	}
70
	
71

    
72
	@Autowired
73
	public void setService(ITaxonService service) {
74
		this.service = service;
75
	}
76
	
77

    
78
	/**
79
	 * Get the set of accepted {@link Taxon} entities for a given
80
	 * {@link TaxonBase} entity identified by the <code>{taxon-uuid}</code>.
81
	 * <p>
82
	 * URI: <b>&#x002F;{datasource-name}&#x002F;taxon&#x002F;{taxon-uuid}&#x002F;accepted</b>
83
	 * 
84
	 * @param request
85
	 * @param response
86
	 * @return a set on a list of {@link Taxon} entities which are initialized
87
	 *         using the following initialization strategy:
88
	 *         {@link #DEFAULT_INIT_STRATEGY}
89
	 * @throws IOException
90
	 */
91
	@RequestMapping(value = "accepted", method = RequestMethod.GET)
92
	public Set<TaxonBase> doGetAccepted(
93
			@PathVariable("uuid") UUID uuid,
94
			HttpServletRequest request, 
95
			HttpServletResponse response) throws IOException {
96
		logger.info("getAccepted() " + request.getServletPath());
97
		TaxonBase tb = service.load(uuid);
98
		HashSet<TaxonBase> resultset = new HashSet<TaxonBase>();
99
		if(tb instanceof Taxon){
100
			//the taxon already is accepted
101
			//FIXME take the current view into account once views are implemented!!!
102
			resultset.add((Taxon)tb);
103
		} else {
104
			Synonym syn = (Synonym)tb;
105
			resultset.addAll(syn.getAcceptedTaxa());
106
			//TODO init Synonyms
107
		}
108
		return resultset;
109
	}
110
	
111
	@RequestMapping(value = "taxonNodes", method = RequestMethod.GET)
112
	public Set<TaxonNode>  doGetTaxonNodes(
113
			@PathVariable("uuid") UUID uuid,
114
			HttpServletRequest request, 
115
			HttpServletResponse response) throws IOException {
116
		TaxonBase tb = service.load(uuid, TAXONNODE_INIT_STRATEGY);
117
		if(tb instanceof Taxon){
118
			return ((Taxon)tb).getTaxonNodes();
119
		} else {
120
			HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
121
			return null;
122
		}
123
	}
124
	
125
	@RequestMapping(value = "specimensOrObersvations", method = RequestMethod.GET)
126
	public ModelAndView doListSpecimensOrObersvations(
127
			@PathVariable("uuid") UUID uuid,
128
			HttpServletRequest request, 
129
			HttpServletResponse response) throws IOException {
130
		logger.info("doListSpecimensOrObersvations() - " + request.getServletPath());
131
		
132
		ModelAndView mv = new ModelAndView();
133

    
134
		TaxonBase tb = service.load(uuid);
135
		
136
		List<OrderHint> orderHints = new ArrayList<OrderHint>();
137
		orderHints.add(new OrderHint("titleCache", SortOrder.DESCENDING));
138
		
139
		if(tb instanceof Taxon){
140
			List<SpecimenOrObservationBase> specimensOrObersvations = occurrenceService.listByAnyAssociation(
141
					null, (Taxon)tb, null, 0, orderHints, null);
142
			mv.addObject(specimensOrObersvations);
143
		} else {
144
			HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
145
			return null;
146
		}
147
		
148
		return mv;
149
	}
150
	
151
	@RequestMapping(value = "taggedName", method = RequestMethod.GET)
152
	public ModelAndView doGetTaggedName(
153
			@PathVariable("uuid") UUID uuid,
154
			HttpServletRequest request, 
155
			HttpServletResponse response) throws IOException {
156
		logger.info("doGetDescriptionElementsByType() - " + request.getServletPath());
157
		
158
		ModelAndView mv = new ModelAndView();
159
		
160
		TaxonBase tb = service.load(uuid, Arrays.asList(new String[] {"name"}));
161
		mv.addObject(nameService.getTaggedName(tb.getName().getUuid()));
162
		return mv;
163
	}
164
		
165
}
(40-40/48)