ItEr60S04ValidacionEProbasFuncionaisItEr59S04 : Fixing bug in Worker class.

it changes the checkConstraintUniqueFirstNameSurnameNif() method by
the checkConstraintUniqueNif() method, because the id must be unique but,
the firstname and  surname can be repeated.
This commit is contained in:
Susana Montes Pedreira 2010-07-06 15:07:59 +02:00 committed by Javier Moran Rua
parent 1cb4401a44
commit 61c011e52f
5 changed files with 49 additions and 22 deletions

View file

@ -118,4 +118,12 @@ public interface IWorkerDAO extends IIntegrationEntityDAO<Worker> {
List<Object[]> getWorkingHoursGroupedPerWorker(List<String> workerNifs,
Date startingDate, Date endingDate);
Worker findByNifAnotherTransaction(String nif)
throws InstanceNotFoundException;
public List<Worker> findByFirstNameSecondName(String firstname,
String secondname);
public List<Worker> findByFirstNameSecondNameAnotherTransaction(
String firstname, String secondname);
}

View file

@ -53,10 +53,9 @@ public class WorkerDAO extends IntegrationEntityDAO<Worker>
@Override
public Worker findUniqueByNif(String nif) throws InstanceNotFoundException {
Criteria criteria = getSession().createCriteria(Worker.class);
criteria.add(Restrictions.eq("nif", nif).ignoreCase());
criteria.add(Restrictions.eq("nif", nif.trim()).ignoreCase());
List<Worker> list = criteria.list();
if (list.size() != 1) {
throw new InstanceNotFoundException(nif, Worker.class.getName());
}
@ -64,6 +63,13 @@ public class WorkerDAO extends IntegrationEntityDAO<Worker>
return list.get(0);
}
@Override
@Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
public Worker findByNifAnotherTransaction(String nif)
throws InstanceNotFoundException {
return findUniqueByNif(nif);
}
@Override
public List<Worker> getWorkers() {
return getSession().createQuery(
@ -122,6 +128,22 @@ public class WorkerDAO extends IntegrationEntityDAO<Worker>
return findByFirstNameSecondNameAndNif(firstname, secondname, nif);
}
@SuppressWarnings("unchecked")
@Override
public List<Worker> findByFirstNameSecondName(String firstname,
String secondname) {
return getSession().createCriteria(Worker.class).add(
Restrictions.and(Restrictions.ilike("firstName", firstname),
Restrictions.ilike("surname", secondname))).list();
}
@Override
@Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
public List<Worker> findByFirstNameSecondNameAnotherTransaction(
String firstname, String secondname) {
return findByFirstNameSecondName(firstname, secondname);
}
@Override
@Transactional(readOnly = true)
public List<HoursWorkedPerWorkerDTO> getWorkingHoursPerWorker(List<Worker> workers, Date startingDate, Date endingDate) {

View file

@ -75,7 +75,7 @@ public class VirtualWorker extends Worker {
@AssertTrue
@Override
public boolean checkConstraintUniqueFirstNameSurnameNif() {
public boolean checkConstraintUniqueNif() {
return true;
}

View file

@ -21,12 +21,11 @@
package org.navalplanner.business.resources.entities;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.hibernate.validator.AssertTrue;
import org.hibernate.validator.NotEmpty;
import org.navalplanner.business.common.Registry;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
/**
* This class models a worker.
@ -142,31 +141,27 @@ public class Worker extends Resource {
return !isVirtual();
}
@AssertTrue(message = "Worker with the same first name, surname and nif previously existed")
public boolean checkConstraintUniqueFirstNameSurnameNif() {
@AssertTrue(message = "Worker with the same nif previously existed")
public boolean checkConstraintUniqueNif() {
if (!areFirstNameSurnameNifSpecified()) {
return true;
}
try {
/* Check the constraint. */
List<Worker> list = Registry.getWorkerDAO()
.findByFirstNameSecondNameAndNifAnotherTransaction(firstName,
surname, nif);
if (isNewObject()) {
return list.isEmpty();
} else {
if (list.isEmpty()) {
return true;
Worker worker = Registry.getWorkerDAO()
.findByNifAnotherTransaction(nif);
if (isNewObject()) {
return false;
} else {
return list.get(0).getId().equals(getId());
return worker.getId().equals(getId());
}
} catch (InstanceNotFoundException e) {
return isNewObject();
}
}
private boolean areFirstNameSurnameNifSpecified() {
protected boolean areFirstNameSurnameNifSpecified() {
return !StringUtils.isBlank(firstName) &&
!StringUtils.isBlank(surname) &&

View file

@ -395,7 +395,8 @@ public class ResourceServiceTest {
machineDTO.criterionSatisfactions.add(
new CriterionSatisfactionDTO(workerCt.getName() , "c1",
getDate(2001, 1, 1), null)); // Incorrect type.
WorkerDTO workerDTO = new WorkerDTO(getUniqueName(), "surname", "nif");
WorkerDTO workerDTO = new WorkerDTO(getUniqueName(), "surname",
getUniqueName());
workerDTO.criterionSatisfactions.add(
new CriterionSatisfactionDTO(machineCt.getName() , "c1",
getDate(2001, 1, 1), null)); // Incorrect type.
@ -746,7 +747,8 @@ public class ResourceServiceTest {
m1.resourcesCostCategoryAssignments.add(m1a1);
/* Create a worker DTO. */
WorkerDTO w1 = new WorkerDTO(getUniqueName(), "surname", "nif");
String nif = getUniqueName();
WorkerDTO w1 = new WorkerDTO(getUniqueName(), "surname", nif);
CriterionSatisfactionDTO w1s1 = new CriterionSatisfactionDTO(ct
.getName(), "c1", getDate(2000, 1, 1), getDate(2000, 2, 1));
w1.criterionSatisfactions.add(w1s1);