1
|
/**
|
2
|
* Copyright (C) 2007 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
|
|
10
|
package eu.etaxonomy.cdm.remote.controller;
|
11
|
|
12
|
import java.io.IOException;
|
13
|
import java.util.Arrays;
|
14
|
import java.util.Map;
|
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.annotation.PathVariable;
|
24
|
import org.springframework.web.bind.annotation.RequestMapping;
|
25
|
import org.springframework.web.bind.annotation.RequestMethod;
|
26
|
import org.springframework.web.bind.annotation.RequestParam;
|
27
|
import org.springframework.web.servlet.ModelAndView;
|
28
|
|
29
|
import eu.etaxonomy.cdm.api.service.IRegistrationService;
|
30
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
31
|
import eu.etaxonomy.cdm.model.name.Registration;
|
32
|
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
|
33
|
import io.swagger.annotations.Api;
|
34
|
import io.swagger.annotations.ApiImplicitParam;
|
35
|
import io.swagger.annotations.ApiImplicitParams;
|
36
|
import io.swagger.annotations.ApiOperation;
|
37
|
|
38
|
/**
|
39
|
* TODO write controller documentation
|
40
|
*
|
41
|
* @author a.kohlbecker
|
42
|
* @since 24.03.2009
|
43
|
*/
|
44
|
@Controller
|
45
|
@Api("registration")
|
46
|
@RequestMapping(value = {"/registration"})
|
47
|
public class RegistrationController extends BaseController<Registration, IRegistrationService> {
|
48
|
|
49
|
public static final Logger logger = Logger.getLogger(RegistrationController.class);
|
50
|
|
51
|
public RegistrationController(){
|
52
|
setInitializationStrategy(Arrays.asList(new String[]{
|
53
|
"$",
|
54
|
"name.$",
|
55
|
"typeDesignations.$"
|
56
|
}));
|
57
|
}
|
58
|
|
59
|
@Autowired
|
60
|
@Override
|
61
|
public void setService(IRegistrationService service) {
|
62
|
this.service = service;
|
63
|
}
|
64
|
|
65
|
@Override
|
66
|
@RequestMapping(value="{uuid}", method = RequestMethod.GET)
|
67
|
public Registration doGet(
|
68
|
@PathVariable("uuid") UUID uuid,
|
69
|
HttpServletRequest request,
|
70
|
HttpServletResponse response) throws IOException {
|
71
|
|
72
|
logger.info("doGet() " + requestPathAndQuery(request));
|
73
|
|
74
|
Registration reg = super.doGet(uuid, request, response);
|
75
|
if(reg != null){
|
76
|
if((!userHelper.userIsAutheticated() || userHelper.userIsAnnonymous()) && !reg.getStatus().equals(RegistrationStatus.PUBLISHED)) {
|
77
|
// completely hide the fact that there is a registration
|
78
|
HttpStatusMessage.create("No such Registration", HttpServletResponse.SC_NO_CONTENT).send(response);
|
79
|
}
|
80
|
}
|
81
|
return reg;
|
82
|
}
|
83
|
|
84
|
@ApiImplicitParams({
|
85
|
@ApiImplicitParam(name = "identifier", value = "The persitent identifier of the Registration.", required = true, dataType = "string", paramType = "path"),
|
86
|
})
|
87
|
@ApiOperation(value = "Finds Registration by persitent identifier.",
|
88
|
notes = "The identifier passed as paramter must be unique in the database otherwise the server will responde with the HTTP error code: " + HttpServletResponse.SC_PRECONDITION_FAILED
|
89
|
)
|
90
|
@RequestMapping(method = RequestMethod.GET)
|
91
|
public Registration doGetByIdentifier(
|
92
|
HttpServletRequest request,
|
93
|
HttpServletResponse response) throws IOException {
|
94
|
|
95
|
logger.info("doGetByIdentifier() " + requestPathAndQuery(request));
|
96
|
|
97
|
String identifier = readPathParameter(request, "/registration/identifier/");
|
98
|
|
99
|
Pager<Registration> regPager = service.pageByIdentifier(identifier, 0, 2, getInitializationStrategy());
|
100
|
|
101
|
if(regPager.getCount() == 1){
|
102
|
return regPager.getRecords().get(0);
|
103
|
} else if(regPager.getCount() > 1){
|
104
|
HttpStatusMessage.create("The identifier " + identifier + " refrences multiple registrations", HttpServletResponse.SC_PRECONDITION_FAILED).send(response);
|
105
|
return null; // never reached, due to previous send()
|
106
|
} else {
|
107
|
return null;
|
108
|
}
|
109
|
}
|
110
|
|
111
|
@ApiImplicitParams({
|
112
|
@ApiImplicitParam(name = "identifier", value = "The persitent identifier of the Registration.", required = true, dataType = "string", paramType = "path"),
|
113
|
})
|
114
|
@ApiOperation(value = "Finds status of a Registration by persitent identifier.",
|
115
|
notes = "The identifier passed as paramter must be unique in the database otherwise the server will responde with the HTTP error code: " + HttpServletResponse.SC_PRECONDITION_FAILED
|
116
|
)
|
117
|
@RequestMapping(value="status", method = RequestMethod.GET, params={"identifier"})
|
118
|
public ModelAndView doStatusByIdentifier(
|
119
|
@RequestParam(value = "identifier", required = true) String identifier,
|
120
|
HttpServletRequest request,
|
121
|
HttpServletResponse response) throws IOException {
|
122
|
|
123
|
logger.info("doStatusByIdentifier() " + requestPathAndQuery(request));
|
124
|
|
125
|
|
126
|
Map<UUID, RegistrationStatus> map = service.statusByIdentifier(identifier);
|
127
|
|
128
|
ModelAndView mv = new ModelAndView();
|
129
|
|
130
|
if(map.size() == 1){
|
131
|
String status = map.values().iterator().next().name();
|
132
|
mv.addObject(status);
|
133
|
return mv;
|
134
|
} else if(map.size() > 1){
|
135
|
HttpStatusMessage.create("The identifier " + identifier + " refrences multiple registrations", HttpServletResponse.SC_PRECONDITION_FAILED).send(response);
|
136
|
return mv; // never reached, due to previous send()
|
137
|
} else {
|
138
|
return mv;
|
139
|
}
|
140
|
}
|
141
|
|
142
|
@ApiImplicitParams({
|
143
|
@ApiImplicitParam(name = "identifier", value = "The persitent identifier of the Registration", required = true, dataType = "string", paramType = "path"),
|
144
|
})
|
145
|
@RequestMapping(method = RequestMethod.GET, params={"identifier", "validateUniqueness"})
|
146
|
public Pager<Registration> doPageByIdentifier(
|
147
|
@RequestParam(value = "identifier", required = true) String identifier,
|
148
|
@RequestParam(value = "validateUniqueness") boolean validateUniqueness,
|
149
|
@RequestParam(value = "pageIndex", required = true) Integer pageIndex,
|
150
|
@RequestParam(value = "pageSize", required = false) Integer pageSize,
|
151
|
HttpServletRequest request,
|
152
|
HttpServletResponse response) throws IOException {
|
153
|
|
154
|
logger.info("doPageByIdentifier() " + requestPathAndQuery(request));
|
155
|
|
156
|
return pageRegistrations(identifier, validateUniqueness, pageIndex, pageSize, response);
|
157
|
}
|
158
|
|
159
|
protected Pager<Registration> pageRegistrations(String identifier, boolean validateUniqueness, Integer pageIndex, Integer pageSize, HttpServletResponse response) throws IOException {
|
160
|
|
161
|
Pager<Registration> regPager = service.pageByIdentifier(identifier, pageIndex, pageSize, getInitializationStrategy());
|
162
|
|
163
|
if(regPager.getCount() == 1){
|
164
|
return regPager;
|
165
|
} else if(regPager.getCount() > 1){
|
166
|
if(validateUniqueness) {
|
167
|
HttpStatusMessage.create("The identifier " + identifier + " refrences multiple registrations", HttpServletResponse.SC_PRECONDITION_FAILED).send(response);
|
168
|
return null; // never reached, due to previous send()
|
169
|
} else {
|
170
|
return regPager;
|
171
|
}
|
172
|
} else {
|
173
|
return null;
|
174
|
}
|
175
|
}
|
176
|
}
|