ItEr35S15CUAdministracionCategoriaCosteItEr34S15: implemented unique validator for code in TypeOfWorkHours

Implemented a validator method in TypeOfWorkHours entity. It checks if the code of that entity is unique.
To do it, I had to implement methods to find a TypeOfWorkHours by its code in TypeOfWorkHoursDAO.
Some tests were written to verify it's working correcty.
This commit is contained in:
Jacobo Aragunde Pérez 2009-12-02 20:58:24 +01:00 committed by Javier Moran Rua
parent c37fba6fa7
commit 68335cf0e9
5 changed files with 100 additions and 1 deletions

View file

@ -21,6 +21,7 @@
package org.navalplanner.business.common;
import org.navalplanner.business.advance.daos.IAdvanceTypeDAO;
import org.navalplanner.business.costcategories.daos.ITypeOfWorkHoursDAO;
import org.navalplanner.business.resources.daos.ICriterionTypeDAO;
import org.navalplanner.business.resources.daos.IMachineDAO;
import org.navalplanner.business.users.daos.IUserDAO;
@ -57,6 +58,9 @@ public class Registry {
@Autowired
private IWorkReportTypeDAO workReportTypeDAO;
@Autowired
private ITypeOfWorkHoursDAO typeOfWorkHoursDAO;
private Registry() {
}
@ -83,4 +87,9 @@ public class Registry {
public static IWorkReportTypeDAO getWorkReportTypeDAO() {
return getInstance().workReportTypeDAO;
}
public static ITypeOfWorkHoursDAO getTypeOfWorkHoursDAO() {
return getInstance().typeOfWorkHoursDAO;
}
}

View file

@ -21,6 +21,7 @@
package org.navalplanner.business.costcategories.daos;
import org.navalplanner.business.common.daos.IGenericDAO;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.costcategories.entities.TypeOfWorkHours;
/**
@ -28,4 +29,11 @@ import org.navalplanner.business.costcategories.entities.TypeOfWorkHours;
*/
public interface ITypeOfWorkHoursDAO extends IGenericDAO<TypeOfWorkHours, Long> {
TypeOfWorkHours findUniqueByCode(TypeOfWorkHours typeOfWorkHours)
throws InstanceNotFoundException;
TypeOfWorkHours findUniqueByCode(String code)
throws InstanceNotFoundException;
boolean existsByCode(TypeOfWorkHours typeOfWorkHours);
}

View file

@ -20,7 +20,13 @@
package org.navalplanner.business.costcategories.daos;
import static org.navalplanner.business.i18n.I18nHelper._;
import org.apache.commons.lang.Validate;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.navalplanner.business.common.daos.GenericDAOHibernate;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.costcategories.entities.TypeOfWorkHours;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
@ -32,6 +38,36 @@ import org.springframework.stereotype.Repository;
@Repository
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class TypeOfWorkHoursDAO extends GenericDAOHibernate<TypeOfWorkHours, Long> implements
ITypeOfWorkHoursDAO {
ITypeOfWorkHoursDAO {
@Override
public TypeOfWorkHours findUniqueByCode(TypeOfWorkHours typeOfWorkHours)
throws InstanceNotFoundException {
Validate.notNull(typeOfWorkHours);
return findUniqueByCode(typeOfWorkHours.getCode());
}
@Override
public TypeOfWorkHours findUniqueByCode(String code)
throws InstanceNotFoundException {
Criteria c = getSession().createCriteria(TypeOfWorkHours.class);
c.add(Restrictions.eq("code", code));
TypeOfWorkHours found = (TypeOfWorkHours) c.uniqueResult();
if (found==null)
throw new InstanceNotFoundException(found,
_("Can't find a TypeOfWorkHours with code {0}", code));
return found;
}
@Override
public boolean existsByCode(TypeOfWorkHours typeOfWorkHours) {
try {
return findUniqueByCode(typeOfWorkHours) != null;
} catch (InstanceNotFoundException e) {
return false;
}
}
}

View file

@ -22,8 +22,12 @@ package org.navalplanner.business.costcategories.entities;
import java.math.BigDecimal;
import org.hibernate.validator.AssertTrue;
import org.hibernate.validator.NotEmpty;
import org.navalplanner.business.common.BaseEntity;
import org.navalplanner.business.common.Registry;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.costcategories.daos.ITypeOfWorkHoursDAO;
/**
* @author Jacobo Aragunde Perez <jaragunde@igalia.com>
@ -89,4 +93,21 @@ public class TypeOfWorkHours extends BaseEntity {
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@AssertTrue(message="this code is already being used")
public boolean checkConstraintUniqueTypeOfWorkHoursCode() {
ITypeOfWorkHoursDAO dao = Registry.getTypeOfWorkHoursDAO();
if (isNewObject()) {
return !dao.existsByCode(this);
} else {
try {
TypeOfWorkHours type = dao.findUniqueByCode(code);
return type.getId().equals(getId());
} catch (InstanceNotFoundException e) {
return true;
}
}
}
}

View file

@ -89,4 +89,29 @@ public class TypeOfWorkHoursDAOTest {
List<TypeOfWorkHours> list = typeOfWorkHoursDAO.list(TypeOfWorkHours.class);
assertEquals(previous + 1, list.size());
}
@Test
public void testFindTypesOfWorkHoursByCode() {
TypeOfWorkHours typeOfWorkHours = createValidTypeOfWorkHours();
typeOfWorkHoursDAO.save(typeOfWorkHours);
try {
TypeOfWorkHours found = typeOfWorkHoursDAO.findUniqueByCode(typeOfWorkHours.getCode());
assertNotNull(found);
assertTrue(found.equals(typeOfWorkHours));
}
catch (InstanceNotFoundException e) {
}
}
@Test(expected=InstanceNotFoundException.class)
public void testFindTypesOfWorkHoursByCodeException() throws InstanceNotFoundException{
TypeOfWorkHours typeOfWorkHours = createValidTypeOfWorkHours();
typeOfWorkHoursDAO.save(typeOfWorkHours);
typeOfWorkHoursDAO.remove(typeOfWorkHours.getId());
//this call should throw the exception
typeOfWorkHoursDAO.findUniqueByCode(typeOfWorkHours.getCode());
}
}