ItEr33S13ArquitecturaServidorItEr32S16: Default DAO save now checks for invalid values
This commit is contained in:
parent
a1845dd6de
commit
ab66e0dd1e
4 changed files with 23 additions and 12 deletions
|
|
@ -32,7 +32,10 @@ import org.hibernate.SessionFactory;
|
|||
import org.hibernate.StaleObjectStateException;
|
||||
import org.hibernate.criterion.Projections;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.hibernate.validator.ClassValidator;
|
||||
import org.hibernate.validator.InvalidValue;
|
||||
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
|
|
@ -77,10 +80,19 @@ public class GenericDAOHibernate<E, PK extends Serializable> implements
|
|||
return sessionFactory.getCurrentSession();
|
||||
}
|
||||
|
||||
public void save(E entity) {
|
||||
public void save(E entity) throws ValidationException {
|
||||
checkIsValid(entity);
|
||||
getSession().saveOrUpdate(entity);
|
||||
}
|
||||
|
||||
private void checkIsValid(E entity) throws ValidationException {
|
||||
ClassValidator<E> classValidator = new ClassValidator<E>(entityClass);
|
||||
InvalidValue[] invalidValues = classValidator.getInvalidValues(entity);
|
||||
if (invalidValues.length > 0) {
|
||||
throw new ValidationException(invalidValues);
|
||||
}
|
||||
}
|
||||
|
||||
public void reattachUnmodifiedEntity(E entity) {
|
||||
|
||||
getSession().lock(entity, LockMode.NONE);
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ import java.io.Serializable;
|
|||
import java.util.List;
|
||||
|
||||
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||
|
||||
/**
|
||||
* The interface all DAOs (Data Access Objects) must implement. In general,
|
||||
|
|
@ -46,8 +47,10 @@ public interface IGenericDAO <E, PK extends Serializable>{
|
|||
* executed (if the entity has version control enabled) with the possible
|
||||
* <code>org.springframework.dao.OptimisticLockingFailureException</code>
|
||||
* being thrown.
|
||||
* @throws ValidationException
|
||||
* if the entity has some invalid values
|
||||
*/
|
||||
public void save(E entity);
|
||||
public void save(E entity) throws ValidationException;
|
||||
|
||||
/**
|
||||
* It inserts the object passed as a parameter in the ORM session. Unlike
|
||||
|
|
|
|||
|
|
@ -32,7 +32,6 @@ import static org.navalplanner.business.test.BusinessGlobalNames.BUSINESS_SPRING
|
|||
import java.util.List;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.validator.InvalidStateException;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
|
@ -41,6 +40,7 @@ import org.navalplanner.business.calendars.daos.IBaseCalendarDAO;
|
|||
import org.navalplanner.business.calendars.entities.BaseCalendar;
|
||||
import org.navalplanner.business.calendars.entities.ResourceCalendar;
|
||||
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||
import org.navalplanner.business.test.calendars.entities.BaseCalendarTest;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
|
|
@ -94,6 +94,7 @@ public class BaseCalendarDAOTest {
|
|||
baseCalendarDAO.save(calendar);
|
||||
|
||||
BaseCalendar derivedCalendar = calendar.newDerivedCalendar();
|
||||
derivedCalendar.setName("derived");
|
||||
baseCalendarDAO.save(derivedCalendar);
|
||||
|
||||
try {
|
||||
|
|
@ -229,22 +230,20 @@ public class BaseCalendarDAOTest {
|
|||
baseCalendarDAO.flush();
|
||||
}
|
||||
|
||||
@Test(expected = InvalidStateException.class)
|
||||
@Test(expected = ValidationException.class)
|
||||
public void notAllowTwoCalendarsWithNullName() {
|
||||
BaseCalendar calendar = BaseCalendarTest.createBasicCalendar();
|
||||
calendar.setName(null);
|
||||
|
||||
baseCalendarDAO.save(calendar);
|
||||
baseCalendarDAO.flush();
|
||||
}
|
||||
|
||||
@Test(expected = InvalidStateException.class)
|
||||
@Test(expected = ValidationException.class)
|
||||
public void notAllowTwoCalendarsWithEmptyName() {
|
||||
BaseCalendar calendar = BaseCalendarTest.createBasicCalendar();
|
||||
calendar.setName("");
|
||||
|
||||
baseCalendarDAO.save(calendar);
|
||||
baseCalendarDAO.flush();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
|
|
@ -31,12 +31,10 @@ import java.util.List;
|
|||
import java.util.UUID;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.validator.InvalidStateException;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.navalplanner.business.common.IAdHocTransactionService;
|
||||
import org.navalplanner.business.common.IOnTransaction;
|
||||
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||
import org.navalplanner.business.resources.daos.ICriterionDAO;
|
||||
import org.navalplanner.business.resources.daos.ICriterionTypeDAO;
|
||||
|
|
@ -83,12 +81,11 @@ public class CriterionModelTest {
|
|||
|
||||
private Criterion criterion;
|
||||
|
||||
@Test(expected = InvalidStateException.class)
|
||||
public void cantSaveCriterionWithoutName() throws Exception {
|
||||
@Test(expected = ValidationException.class)
|
||||
public void cantSaveCriterionWithoutName() {
|
||||
givenValidCriterion();
|
||||
criterion.setName("");
|
||||
criterionModel.save(criterion);
|
||||
sessionFactory.getCurrentSession().flush();
|
||||
}
|
||||
|
||||
private Criterion givenValidCriterion() {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue