Can edit animal's name.

This commit is contained in:
Geoffrey POUZET 2020-10-25 15:08:53 +01:00
parent f1b94f48b8
commit fcbe488095
10 changed files with 145 additions and 22 deletions

View File

@ -82,6 +82,28 @@ public class AnimalController {
return ret;
}
@RequestMapping(value = IndexController.URL_ANIMAL_EDIT, method = RequestMethod.POST)
public String doPostEditAnimal(final ModelMap pModel, HttpSession httpSession,
@PathVariable(value = "animal_id") final int animalId,
@RequestParam(name = "name", required = true) String name) {
String ret;
if (LoginController.isUserAlreadyAuth(httpSession)) {
Authentication authentication = (Authentication) httpSession.getAttribute(IndexController.SESSION_ATTRIBUTE_AUTHENTICATION);
Object oUserDetails = authentication.getPrincipal();
if (oUserDetails instanceof UserDetails) {
UserDetails userDetails = (UserDetails) oUserDetails;
serviceAnimal.changeAnimalName(animalId, userDetails.getUsername(), name);
}
ret = IndexController.URL_REDIRECT + IndexController.URL_ANIMAL_LIST;
} else {
ret = IndexController.URL_REDIRECT + IndexController.URL_LOGIN;
}
return ret;
}
@RequestMapping(value = IndexController.URL_ANIMAL_DELETE, method = RequestMethod.POST)
public String doGetDeleteAnimal(final ModelMap pModel, HttpSession httpSession, @PathVariable(value = "animal_id") final int animalId) {
String ret;

View File

@ -59,6 +59,8 @@ public class IndexController {
public static final String URL_ANIMAL_CARES_DELETE = "/animal/{animal_id}/delete/care/{care_id}";
public static final String URL_ANIMAL_CONSENT_BEHAVIOR_DELETE = "/animal/{animal_id}/delete/consent_behavior/{consent_behavior_id}";
public static final String URL_ANIMAL_PROGRESSION_DELETE = "/animal/{animal_id}/delete/progression/{progression_id}";
// Edit URL
public static final String URL_ANIMAL_EDIT = "/animal/edit/{animal_id}";
public static final String URL_REDIRECT = "redirect:";
//</editor-fold>

View File

@ -14,4 +14,6 @@ public interface IAnimalDao extends CommonDao {
public void addAnimal(String name, String owner);
public void deleteAnimal(int animalId, String owner);
public void changeName(int animalId, String owner, String name);
}

View File

@ -201,4 +201,38 @@ public class AnimalDao extends PostgresSqlDao implements IAnimalDao {
}
}
}
@Override
public void changeName(int animalId, String owner, String name) {
Connection conn = null;
PreparedStatement statement = null;
try {
conn = mDataSource.getConnection();
statement = conn.prepareStatement("UPDATE "+TABLE_NAME+" SET name = ? WHERE id = ? AND owner = ?;");
statement.setString(1, name);
statement.setInt(2, animalId);
statement.setString(3, owner);
System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString());
statement.executeUpdate();
} catch (SQLException ex) {
System.err.println(IndexController.LOG_TAG + " SQLException -> changeName()");
System.err.println(ex.getMessage());
ex.printStackTrace();
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException ex) {
System.err.println(IndexController.LOG_TAG + " Failed close statement -> changeName()");
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
System.err.println(IndexController.LOG_TAG + " Failed close connection -> changeName()");
}
}
}
}
}

View File

@ -27,4 +27,6 @@ public interface IServiceAnimal {
public void deleteAnimalConsentBehavior(int animalId, String owner, int consentBehaviorId);
public void deleteAnimalProgression(int animalId, String owner, int progressionId);
public void changeAnimalName(int animalId, String owner, String name);
}

View File

@ -150,4 +150,9 @@ public class ServiceAnimal implements IServiceAnimal {
public void addProgression(int animalId, int careId, int consentBehaviorId) {
progressionDao.addProgression(animalId, careId, consentBehaviorId);
}
@Override
public void changeAnimalName(int animalId, String owner, String name) {
animalDao.changeName(animalId, owner, name);
}
}

View File

@ -89,3 +89,7 @@ animal_behavior.button.delete.tooltip=Delete
animal_behavior.button.add.tooltip=Add
animal_behavior.add_care=Add care to this consent behavior
animal_behavior.add_care.empty_list=No more cares to add
animal_list.animal.edit.tooltip=Edit
animal_list.edit_animal.modal.label=Animal's name:
animal_list.edit_animal.modal.placeholder=New name
modal_button.edit=Edit

View File

@ -85,3 +85,7 @@ animal_behavior.button.delete.tooltip=Delete
animal_behavior.button.add.tooltip=Add
animal_behavior.add_care=Add care to this consent behavior
animal_behavior.add_care.empty_list=No more cares to add
animal_list.animal.edit.tooltip=Edit
animal_list.edit_animal.modal.label=Animal's name:
animal_list.edit_animal.modal.placeholder=New name
modal_button.edit=Edit

View File

@ -85,3 +85,7 @@ animal_behavior.button.delete.tooltip=Supprimer
animal_behavior.button.add.tooltip=Ajouter
animal_behavior.add_care=Ajouter un soin \u00e0 ce comportement de consentement
animal_behavior.add_care.empty_list=Aucun soins \u00e0 ajouter
animal_list.animal.edit.tooltip=Modifier
animal_list.edit_animal.modal.label=Nom de l'animal :
animal_list.edit_animal.modal.placeholder=Nouveau nom
modal_button.edit=Modifier

View File

@ -4,6 +4,7 @@
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ page isELIgnored="false" %>
<spring:url value="/animal/delete" var="animalDeleteURL" />
<spring:url value="/animal/edit" var="animalEditURL" />
<spring:url value="/change_password" var="changePasswordURL" />
<%@ include file="base/language.jsp" %>
@ -33,19 +34,24 @@
<div class="container">
<%@ include file="base/navbar.jsp" %>
<center><h1><fmt:message key="animal_list.title"></fmt:message></h1></center>
<div class="row">
<div class="col-lg-5">
<h4><fmt:message key="animal_list.animal_list.title"></fmt:message></h4>
<div class="list-group">
<div class="row">
<div class="col-lg-5">
<h4><fmt:message key="animal_list.animal_list.title"></fmt:message></h4>
<div class="list-group">
<c:forEach items="${MODEL_MAP_ANIMAL_LIST}" var="animal">
<a class="list-group-item" href="${animalURL}/${animal.getId()}/cares_and_behaviors">
${animal.getName()}
<form class="pull-right" action="${animalDeleteURL}/${animal.getId()}" method="POST">
<input value="${animal.getName()}" hidden />
<button class="btn btn-xs btn-danger" type="submit" data-toggle="tooltip" data-placement="bottom" title="<fmt:message key="animal_list.animal.delete.tooltip"></fmt:message>"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button>
</form>
</a>
<!-- <div class="list-group-item">
</form>
<form class="pull-right" action="${animalEditURL}/${animal.getId()}" method="GET">
<input value="${animal.getName()}" hidden />
<button class="btn btn-xs btn-default" type="submit" data-toggle="tooltip" data-placement="bottom" title="<fmt:message key="animal_list.animal.edit.tooltip"></fmt:message>"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span></button>
</form>
</a>
<!-- <div class="list-group-item">
${animal.getName()}
<form class="pull-right" action="${animalDeleteURL}/${animal.getId()}" method="POST">
<button class="btn btn-sm btn-danger" type="submit" data-toggle="tooltip" data-placement="bottom" title="Suppression"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button>
@ -61,14 +67,14 @@
<div class="form-group">
<label for="form_animal_name"><fmt:message key="animal_list.animal.add.label"></fmt:message></label>
<input type="text" id="form_animal_name" name="name" class="form-control" placeholder="<fmt:message key="animal_list.animal.add.placeholder"></fmt:message>" />
</div>
<button class="btn btn btn-success" type="submit" data-toggle="tooltip" data-placement="bottom" title="<fmt:message key="animal_list.animal.add.tooltip"></fmt:message>"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span></button>
</form>
</div>
<div class="col-lg-2">
</div>
<div class="col-lg-5">
<h4><fmt:message key="animal_list.change_password.title"></fmt:message></h4>
</div>
<button class="btn btn btn-success" type="submit" data-toggle="tooltip" data-placement="bottom" title="<fmt:message key="animal_list.animal.add.tooltip"></fmt:message>"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span></button>
</form>
</div>
<div class="col-lg-2">
</div>
<div class="col-lg-5">
<h4><fmt:message key="animal_list.change_password.title"></fmt:message></h4>
<form id="changePasswordForm" action="${changePasswordURL}" method="POST">
<div class="form-group">
<label for="old_password"><fmt:message key="animal_list.change_password.old_password.label"></fmt:message></label>
@ -77,9 +83,9 @@
<input type="password" id="password" name="password" class="form-control" placeholder="<fmt:message key="animal_list.change_password.new_password.placeholder"></fmt:message>" required/>
<label for="password_check"><fmt:message key="animal_list.change_password.new_password_check.label"></fmt:message></label>
<input type="password" id="password_check" name="password_check" class="form-control" placeholder="<fmt:message key="animal_list.change_password.new_password_check.placeholder"></fmt:message>" required/>
</div>
<button class="btn btn btn-primary" type="submit"><fmt:message key="animal_list.change_password.submit"></fmt:message></button>
</form>
</div>
<button class="btn btn btn-primary" type="submit"><fmt:message key="animal_list.change_password.submit"></fmt:message></button>
</form>
<c:if test="${MODEL_MAP_CHANGE_PASSWORD_ERROR_MESSAGE == 'true'}">
<div class="list-group">
<h5 class="list-group-item list-group-item-danger" style="border-radius: 5px;">Erreur lors de la mise à jour du mot de passe</h5>
@ -112,13 +118,38 @@
<div class="modal-footer">
<form id="modal_delete_animal_validate_delete" class="pull-right" action="" method="POST">
<button type="button" class="btn btn-default" data-dismiss="modal"><fmt:message key="modal_button.cancel"></fmt:message></button>
<button type="submit" class="btn btn-danger"><fmt:message key="modal_button.delete"></fmt:message></button>
<button type="submit" class="btn btn-danger"><fmt:message key="modal_button.delete"></fmt:message></button>
</form>
</div>
</div>
</div>
</div>
<div id="modal_edit_animal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 id="modal_edit_animal_title" class="modal-title">Animal name</h4>
</div>
<div class="modal-body">
<p id="modal_delete_animal_body"><fmt:message key="animal_list.delete.modal_body"></fmt:message></p>
<form id="modal_edit_animal_form" action="" method="POST">
<label for="modal_edit_animal_form_name"><fmt:message key="animal_list.edit_animal.modal.label"></fmt:message></label>
<input type="text" id="modal_edit_animal_form_name" name="name" class="form-control" placeholder="<fmt:message key="animal_list.edit_animal.modal.placeholder"></fmt:message>" required/>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal"><fmt:message key="modal_button.cancel"></fmt:message></button>
<button form="modal_edit_animal_form" type="submit" class="btn btn-primary"><fmt:message key="modal_button.edit"></fmt:message></button>
</div>
</div>
</div>
</div>
</body>
<script type="text/javascript">
@ -126,9 +157,9 @@
$('[data-toggle="tooltip"]').tooltip();
$("form").submit(function (event) {
if ($(this).attr('id') !== 'modal_delete_animal_validate_delete') {
var formUrl = $(this).attr('action');
if (formUrl.startsWith('${animalDeleteURL}')) {
var formUrl = $(this).attr('action');
if (formUrl.startsWith('${animalDeleteURL}')) {
if ($(this).attr('id') !== 'modal_delete_animal_validate_delete') {
// Only for deleting animal forms
var animalName = $(this).find('input')[0].value;
$('#modal_delete_animal_title').text(animalName);
@ -138,6 +169,19 @@
});
event.preventDefault();
}
} else if (formUrl.startsWith('${animalEditURL}')) {
if ($(this).attr('id') !== 'modal_edit_animal_form') {
// Only for editing animal form
var animalName = $(this).find('input')[0].value;
$('#modal_edit_animal_title').text(animalName);
$('#modal_edit_animal_form').attr('action', formUrl);
$('#modal_edit_animal_form_name').val(animalName);
$('#modal_edit_animal').modal({
show: true
});
event.preventDefault();
}
}
});
});