Project

General

Profile

Download (9.07 KB) Statistics
| Branch: | Tag: | Revision:
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.Arrays;
14
import java.util.List;
15
import java.util.UUID;
16

    
17
import javax.servlet.http.HttpServletRequest;
18
import javax.servlet.http.HttpServletResponse;
19

    
20
import org.apache.log4j.Logger;
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

    
29
import eu.etaxonomy.cdm.api.service.ITaxonService;
30
import eu.etaxonomy.cdm.api.service.ITaxonTreeService;
31
import eu.etaxonomy.cdm.api.service.ITermService;
32
import eu.etaxonomy.cdm.database.UpdatableRoutingDataSource;
33
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
34
import eu.etaxonomy.cdm.model.name.Rank;
35
import eu.etaxonomy.cdm.model.taxon.Taxon;
36
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
37
import eu.etaxonomy.cdm.model.taxon.TaxonomicTree;
38
import eu.etaxonomy.cdm.remote.editor.RankPropertyEditor;
39
import eu.etaxonomy.cdm.remote.editor.UUIDPropertyEditor;
40

    
41
/**
42
 * The TaxonomicTreeController class is a Spring MVC Controller.
43
 * <p>
44
 * The syntax of the mapped service URIs contains the the {datasource-name} path element.
45
 * The available {datasource-name}s are defined in a configuration file which
46
 * is loaded by the {@link UpdatableRoutingDataSource}. If the
47
 * UpdatableRoutingDataSource is not being used in the actual application
48
 * context any arbitrary {datasource-name} may be used.
49
 * <p>
50
 * @author a.kohlbecker
51
 * @date 20.03.2009
52
 */
53
@Controller
54
public class TaxonomicTreePortalListController extends BaseListController<TaxonomicTree,ITaxonTreeService> {
55
	
56
	
57
	private static final List<String> TAXONTREE_INIT_STRATEGY = Arrays.asList(new String[]{
58
			"reference.authorTeam.titleCache"
59
	});
60
	
61
	private static final List<String> NODE_INIT_STRATEGY = Arrays.asList(new String[]{
62
			"taxon.sec", 
63
			"taxon.name.taggedName",
64
			"taxon.name.titleCache"
65
			});
66
	
67

    
68
	public static final Logger logger = Logger.getLogger(TaxonomicTreePortalListController.class);
69

    
70
	private ITaxonService taxonService;
71
	
72
	private ITaxonTreeService service;
73
	
74
	private ITermService termService;
75
	
76
	@Autowired
77
	public void setService(ITaxonTreeService service) {
78
		this.service = service; 
79
	}
80
	
81
	@Autowired
82
	public void setTermService(ITermService termService) {
83
		this.termService = termService;
84
	}
85
	
86
	@Autowired
87
	public void setTaxonService(ITaxonService taxonService) {
88
		this.taxonService = taxonService;
89
	}
90

    
91
	
92
	@InitBinder
93
    public void initBinder(WebDataBinder binder) {
94
		binder.registerCustomEditor(UUID.class, new UUIDPropertyEditor());
95
		binder.registerCustomEditor(Rank.class, new RankPropertyEditor());
96
	}
97
	
98
	
99
	/**
100
	 * Lists all available {@link TaxonomicTree}s.
101
	 * <p>
102
	 * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;taxontree</b>
103
	 * 
104
	 * @param request
105
	 * @param response
106
	 * @return a list of {@link TaxonomicTree}s initialized by
107
	 *         the {@link #TAXONTREE_INIT_STRATEGY}
108
	 * @throws IOException
109
	 */
110
	@RequestMapping(value = { "/portal/taxontree/" }, method = RequestMethod.GET)
111
	public List<TaxonomicTree> getTaxonomicTrees(HttpServletRequest request, HttpServletResponse response)
112
			throws IOException {
113
		logger.info("getTaxonomicTrees()");
114
		return service.list(null, null, null,null, TAXONTREE_INIT_STRATEGY);
115
	}
116
	
117
	
118
	/**
119
	 * @param treeUuid
120
	 * @param response
121
	 * @return
122
	 * @throws IOException
123
	 */
124
	@RequestMapping(
125
			value = {"/portal/taxontree/{treeUuid}/childNodes/"},
126
			method = RequestMethod.GET)
127
	public List<TaxonNode> getChildNodes(
128
			@PathVariable("treeUuid") UUID treeUuid,
129
			HttpServletResponse response
130
			) throws IOException {
131
		
132
		return getChildNodesAtRank(treeUuid, null, response);
133
	}
134
	
135
	
136
	@RequestMapping(
137
			value = {"/portal/taxontree/{treeUuid}/childNodesAt/{rankUuid}/"},
138
			method = RequestMethod.GET)
139
	public List<TaxonNode> getChildNodesAtRank(
140
			@PathVariable("treeUuid") UUID treeUuid,
141
			@PathVariable("rankUuid") UUID rankUuid,
142
			HttpServletResponse response
143
			) throws IOException {
144
		
145
		logger.info("getChildNodesAtRank()");
146
		TaxonomicTree tree = null;
147
		Rank rank = null;
148
		if(treeUuid != null){
149
			// get view and rank
150
			tree = service.find(treeUuid);
151
			
152
			if(tree == null) {
153
				response.sendError(404 , "TaxonomicTree not found using " + treeUuid );
154
				return null;
155
			}
156
		}
157
		rank = findRank(rankUuid);
158
		
159
		return service.loadRankSpecificRootNodes(tree, rank, NODE_INIT_STRATEGY);
160
	}
161

    
162
	
163

    
164

    
165
	/**
166
	 * Lists all child-{@link TaxonNode}s of the specified {@link Taxon} in the {@link TaxonomicTree}. The
167
	 * a given {@link Rank} is ignored in this method but for consistency reasons it has been allowed to included it into the URI. 
168
	 * <p>
169
	 * URI: <b>&#x002F;portal&#x002F;taxontree&#x002F;{treeUuid}&#x002F;childNodesOf&#x002F;{taxonUuid}</b>
170
	 * <p>
171
     * <b>URI elements:</b>
172
     * <ul>
173
     * <li><b>{tree-uuid}</b> identifies the {@link TaxonomicTree} by its UUID - <i>required</i>.
174
     * <li><b>{taxon-uuid}</b> identifies the {@link Taxon} by its UUID. - <i>required</i>.
175
     * </ul>
176
	 * 
177
	 * @param response
178
	 * @param request
179
	 * @return a List of {@link TaxonNode} entities initialized by
180
	 *         the {@link #NODE_INIT_STRATEGY}
181
	 */
182
	@RequestMapping(
183
			value = {"/portal/taxontree/{treeUuid}/childNodesOf/{taxonUuid}"}, 
184
			method = RequestMethod.GET)
185
	public List<TaxonNode> getChildNodesOfTaxon(
186
			@PathVariable("treeUuid") UUID treeUuid,
187
			@PathVariable("taxonUuid") UUID taxonUuid,
188
			HttpServletResponse response) throws IOException {
189
		logger.info("getChildNodesOfTaxon()");
190
		
191
		TaxonomicTree tree = service.find(treeUuid);
192
		Taxon taxon = (Taxon) taxonService.load(taxonUuid);
193
		List<TaxonNode> childs = service.loadChildNodesOfTaxon(taxon, tree, NODE_INIT_STRATEGY);
194
		return childs;
195
		
196
	}
197
	
198
	/**
199
	 * Provides path of {@link TaxonNode}s from the base node to the node of the specified taxon.
200
	 * <p>
201
	 * URI:<b>&#x002F;portal&#x002F;taxontree&#x002F;{treeUuid}&#x002F;pathFrom&#x002F;{taxonUuid}&#x002F;toRank&#x002F;{rankUuid}</b>
202
	 * <p>
203
     * <b>URI elements:</b>
204
     * <ul>
205
     * <li><b>{treeUuid}</b> identifies the {@link TaxonomicTree} by its UUID - <i>required</i>.
206
     * <li><b>{taxonUuid}</b> identifies the {@link Rank}
207
     * <li><b>{rankUuid}</b> identifies the {@link Taxon} by its UUID. - <i>required</i>.
208
     * </ul>
209
	 * 
210
	 * @param response
211
	 * @param request
212
	 * @return a List of {@link TaxonNode} entities initialized by
213
	 *         the {@link #NODE_INIT_STRATEGY}
214
	 */
215
	@RequestMapping(
216
			value = {"/portal/taxontree/{treeUuid}/pathFrom/{taxonUuid}/toRank/{rankUuid}"}, 
217
			method = RequestMethod.GET)
218
	public List<TaxonNode> getPathFromTaxonToRank(
219
			@PathVariable("treeUuid") UUID treeUuid,
220
			@PathVariable("taxonUuid") UUID taxonUuid,
221
			@PathVariable("rankUuid") UUID rankUuid,
222
			HttpServletRequest request,
223
			HttpServletResponse response) throws IOException {
224
		logger.info("getPathFromTaxonToRank(): " + request.getServletPath());
225
		
226
		TaxonomicTree tree = service.find(treeUuid);
227
		Rank rank = findRank(rankUuid);
228
		Taxon taxon = (Taxon) taxonService.load(taxonUuid);
229
		
230
		return service.loadTreeBranchToTaxon(taxon, tree, rank, NODE_INIT_STRATEGY);
231
	}
232
	
233
	/**
234
	 * Provides path of {@link TaxonNode}s from the base node to the node of the specified taxon.
235
	 * <p>
236
	 * URI:<b>&#x002F;portal&#x002F;taxontree&#x002F;{treeUuid}&#x002F;pathFrom&#x002F;{taxonUuid}</b>
237
	 * <p>
238
     * <b>URI elements:</b>
239
     * <ul>
240
     * <li><b>{treeUuid}</b> identifies the {@link TaxonomicTree} by its UUID - <i>required</i>.
241
     * <li><b>{rankUuid}</b> identifies the {@link Taxon} by its UUID. - <i>required</i>.
242
     * </ul>
243
	 * 
244
	 * @param response
245
	 * @param request
246
	 * @return a List of {@link TaxonNode} entities initialized by
247
	 *         the {@link #NODE_INIT_STRATEGY}
248
	 */
249
	@RequestMapping(
250
			value = {"/portal/taxontree/{treeUuid}/pathFrom/{taxonUuid}"}, 
251
			method = RequestMethod.GET)
252
	public List<TaxonNode> getPathFromTaxon(
253
			@PathVariable("treeUuid") UUID treeUuid,
254
			@PathVariable("taxonUuid") UUID taxonUuid,
255
			HttpServletRequest request,
256
			HttpServletResponse response) throws IOException {
257
		
258
		return getPathFromTaxonToRank(treeUuid, taxonUuid, null, request, response);
259
	}
260

    
261
	
262
	private Rank findRank(UUID rankUuid) {
263
		Rank rank = null;
264
		if(rankUuid != null){
265
			DefinedTermBase dt =  termService.find(rankUuid);
266
			if(dt instanceof Rank){
267
				rank = (Rank)dt;
268
			} else {
269
			   new IllegalArgumentException("DefinedTermBase is not a Rank");
270
			}
271
		}
272
		return rank;
273
	}
274

    
275
	
276
}
(35-35/36)