Edit improvements

This commit is contained in:
Katerina Iatropoulou 2020-10-13 18:23:40 +00:00
parent d60cecdc5d
commit 4b0a3a8c01
1 changed files with 27 additions and 7 deletions

View File

@ -1,5 +1,6 @@
package eu.dnetlib.openaire.usermanagement;
import eu.dnetlib.openaire.user.pojos.RegisteredService;
import eu.dnetlib.openaire.usermanagement.utils.RegisteredServicesUtils;
import eu.dnetlib.openaire.usermanagement.utils.TokenUtils;
import org.apache.http.HttpResponse;
@ -42,8 +43,10 @@ public class EditRegisteredService extends HttpServlet {
String accessToken = authentication.getAccessTokenValue();
String serviceId = request.getParameter("id");
int serviceIdInt = Integer.parseInt(serviceId);
try {
if (!registeredServicesUtils.isAuthorized(authentication.getSub(), Integer.parseInt(serviceId))) {
if (!registeredServicesUtils.isAuthorized(authentication.getSub(), serviceIdInt)) {
request.getSession().setAttribute("message", "You have no permission to edit the service.");
response.sendRedirect("./registeredServices");
}
@ -62,13 +65,30 @@ public class EditRegisteredService extends HttpServlet {
request.getSession().setAttribute("message", "Service with id " + serviceId + " does not exist.");
}
ServiceResponse serviceResponse = TokenUtils.getRegisteredService(serviceId, accessToken);
request.getSession().setAttribute("serviceId", serviceResponse.getId());
request.getSession().setAttribute("first_name", serviceResponse.getClientName());
request.getSession().setAttribute("description", serviceResponse.getClientDescription());
RegisteredService registeredService = null;
try {
registeredService = registeredServicesUtils.getRegisteredServiceDao().fetchRegisteredServiceById(Integer.parseInt(serviceId));
} catch (SQLException sqle) {
logger.error("Unable to access service with id " + serviceId, sqle);
request.getSession().setAttribute("message", "There was an error accessing your service.");
response.sendRedirect("./registeredServices");
}
if (registeredService != null && registeredService.getAai_id() != null) {
ServiceResponse serviceResponse = TokenUtils.getRegisteredService(registeredService.getAai_id(), accessToken);
request.getSession().setAttribute("serviceId", serviceResponse.getId());
System.out.println("service client name " + serviceResponse.getClientName());
request.getSession().setAttribute("first_name", serviceResponse.getClientName());
System.out.println("service client description " + serviceResponse.getClientDescription());
request.getSession().setAttribute("description", serviceResponse.getClientDescription());
} else {
request.getSession().setAttribute("message", "Service with id " + serviceId + " does not exist.");
}
response.setContentType("text/html");
request.getRequestDispatcher("./editRegisteredService.jsp").include(request, response);
}
@Override
@ -84,10 +104,10 @@ public class EditRegisteredService extends HttpServlet {
HttpResponse resp = TokenUtils.updateService(serviceId, authentication.getSub(), name, description, authentication.getUserInfo().getEmail(), accessToken);
if (resp.getStatusLine().getStatusCode()==200) {
request.getSession().setAttribute("message", "Your service with name '" + name + "' was successfully updated");
request.getSession().setAttribute("success", "Your service with name '" + name + "' was successfully updated");
}
response.sendRedirect("/registeredServices");
response.sendRedirect("./registeredServices");
}
}