ItEr41S23CUImportarTraballoRealizadoPorSubcontrata: Adding unique code constraint on HoursGroup entity.

This commit is contained in:
Manuel Rego Casasnovas 2010-01-03 15:22:53 +01:00 committed by Javier Moran Rua
parent 671560f129
commit 33d243b33d
4 changed files with 82 additions and 1 deletions

View file

@ -28,6 +28,7 @@ import org.navalplanner.business.labels.daos.ILabelDAO;
import org.navalplanner.business.labels.daos.ILabelTypeDAO;
import org.navalplanner.business.materials.daos.IMaterialCategoryDAO;
import org.navalplanner.business.materials.daos.IMaterialDAO;
import org.navalplanner.business.orders.daos.IHoursGroupDAO;
import org.navalplanner.business.orders.daos.IOrderElementDAO;
import org.navalplanner.business.qualityforms.daos.IQualityFormDAO;
import org.navalplanner.business.resources.daos.ICriterionDAO;
@ -112,6 +113,9 @@ public class Registry {
@Autowired
private ICriterionDAO criterionDAO;
@Autowired
private IHoursGroupDAO hoursGroupDAO;
private Registry() {
}
@ -190,4 +194,8 @@ public class Registry {
return getInstance().criterionDAO;
}
public static IHoursGroupDAO getHoursGroupDAO() {
return getInstance().hoursGroupDAO;
}
}

View file

@ -20,11 +20,16 @@
package org.navalplanner.business.orders.daos;
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.orders.entities.HoursGroup;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
* Dao for {@link HoursGroup}
@ -35,4 +40,45 @@ import org.springframework.stereotype.Repository;
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class HoursGroupDAO extends GenericDAOHibernate<HoursGroup, Long>
implements IHoursGroupDAO {
}
@Override
@Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
public boolean existsByCodeAnotherTransaction(HoursGroup hoursGroup) {
return existsByCode(hoursGroup);
}
private boolean existsByCode(HoursGroup hoursGroup) {
try {
HoursGroup result = findUniqueByCode(hoursGroup);
return result != null && result != hoursGroup;
} catch (InstanceNotFoundException e) {
return false;
}
}
@Override
@Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
public HoursGroup findUniqueByCodeAnotherTransaction(HoursGroup hoursGroup)
throws InstanceNotFoundException {
return findUniqueByCode(hoursGroup);
}
private HoursGroup findUniqueByCode(HoursGroup hoursGroup)
throws InstanceNotFoundException {
if ((hoursGroup == null) || (hoursGroup.getCode() == null)) {
return null;
}
Criteria c = getSession().createCriteria(HoursGroup.class);
c.add(Restrictions.eq("code", hoursGroup.getCode()));
HoursGroup result = (HoursGroup) c.uniqueResult();
if (result == null) {
throw new InstanceNotFoundException(hoursGroup.getCode(),
HoursGroup.class.getName());
} else {
return result;
}
}
}

View file

@ -21,6 +21,7 @@
package org.navalplanner.business.orders.daos;
import org.navalplanner.business.common.daos.IGenericDAO;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.orders.entities.HoursGroup;
/**
@ -30,4 +31,9 @@ import org.navalplanner.business.orders.entities.HoursGroup;
*/
public interface IHoursGroupDAO extends IGenericDAO<HoursGroup, Long> {
boolean existsByCodeAnotherTransaction(HoursGroup hoursGroup);
HoursGroup findUniqueByCodeAnotherTransaction(HoursGroup hoursGroup)
throws InstanceNotFoundException;
}

View file

@ -26,10 +26,14 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.validator.AssertTrue;
import org.hibernate.validator.NotEmpty;
import org.hibernate.validator.NotNull;
import org.hibernate.validator.Valid;
import org.navalplanner.business.common.BaseEntity;
import org.navalplanner.business.common.Registry;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.orders.daos.IHoursGroupDAO;
import org.navalplanner.business.requirements.entities.CriterionRequirement;
import org.navalplanner.business.requirements.entities.DirectCriterionRequirement;
import org.navalplanner.business.requirements.entities.IndirectCriterionRequirement;
@ -275,4 +279,21 @@ public class HoursGroup extends BaseEntity implements Cloneable,
return false;
}
@AssertTrue(message = "code is already being used")
public boolean checkConstraintUniqueCode() {
IHoursGroupDAO hoursGroupDAO = Registry.getHoursGroupDAO();
if (isNewObject()) {
return !hoursGroupDAO.existsByCodeAnotherTransaction(this);
} else {
try {
HoursGroup hoursGroup = hoursGroupDAO
.findUniqueByCodeAnotherTransaction(this);
return hoursGroup.getId().equals(getId());
} catch (InstanceNotFoundException e) {
return true;
}
}
}
}