ItEr37S11CUAdministracionMateriaisItEr36S13: Validate uniqueness for Material
This commit is contained in:
parent
b95801945e
commit
d1d5884eae
5 changed files with 108 additions and 13 deletions
|
|
@ -22,6 +22,7 @@ package org.navalplanner.business.common;
|
|||
|
||||
import org.navalplanner.business.advance.daos.IAdvanceTypeDAO;
|
||||
import org.navalplanner.business.costcategories.daos.ITypeOfWorkHoursDAO;
|
||||
import org.navalplanner.business.materials.daos.IMaterialDAO;
|
||||
import org.navalplanner.business.resources.daos.ICriterionTypeDAO;
|
||||
import org.navalplanner.business.resources.daos.IMachineDAO;
|
||||
import org.navalplanner.business.users.daos.IUserDAO;
|
||||
|
|
@ -37,6 +38,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||
* @author Fernando Bellas Permuy <fbellas@udc.es>
|
||||
* @author Javier Moran Rua <jmoran@igalia.com>
|
||||
* @author Diego Pino Garcia <dpino@igalia.com>
|
||||
*/
|
||||
public class Registry {
|
||||
|
||||
|
|
@ -61,6 +63,9 @@ public class Registry {
|
|||
@Autowired
|
||||
private ITypeOfWorkHoursDAO typeOfWorkHoursDAO;
|
||||
|
||||
@Autowired
|
||||
private IMaterialDAO materialDAO;
|
||||
|
||||
private Registry() {
|
||||
}
|
||||
|
||||
|
|
@ -92,4 +97,7 @@ public class Registry {
|
|||
return getInstance().typeOfWorkHoursDAO;
|
||||
}
|
||||
|
||||
public static IMaterialDAO getMaterialDAO() {
|
||||
return getInstance().materialDAO;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ import java.util.List;
|
|||
import java.util.Set;
|
||||
|
||||
import org.navalplanner.business.common.daos.IGenericDAO;
|
||||
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.navalplanner.business.materials.entities.Material;
|
||||
import org.navalplanner.business.materials.entities.MaterialCategory;
|
||||
|
||||
|
|
@ -35,6 +36,14 @@ import org.navalplanner.business.materials.entities.MaterialCategory;
|
|||
*/
|
||||
public interface IMaterialDAO extends IGenericDAO<Material, Long> {
|
||||
|
||||
/**
|
||||
* Returns true if {@link Material} exits
|
||||
*
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
boolean existsMaterialWithCodeInAnotherTransaction(String code);
|
||||
|
||||
/**
|
||||
* Searches {@link Material} by code and description (ilike matching) within
|
||||
* categories
|
||||
|
|
@ -57,6 +66,14 @@ public interface IMaterialDAO extends IGenericDAO<Material, Long> {
|
|||
List<Material> findMaterialsInCategoryAndSubCategories(String text,
|
||||
MaterialCategory materialCategory);
|
||||
|
||||
/**
|
||||
* Returns {@link Material} by code
|
||||
*
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
Material findUniqueByCodeInAnotherTransaction(String code) throws InstanceNotFoundException;
|
||||
|
||||
List<Material> getAll();
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -27,11 +27,14 @@ import java.util.Set;
|
|||
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.materials.entities.Material;
|
||||
import org.navalplanner.business.materials.entities.MaterialCategory;
|
||||
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 Material}
|
||||
|
|
@ -89,4 +92,40 @@ public class MaterialDAO extends GenericDAOHibernate<Material, Long> implements
|
|||
return criteria.list();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly= true, propagation = Propagation.REQUIRES_NEW)
|
||||
public boolean existsMaterialWithCodeInAnotherTransaction(String code) {
|
||||
try {
|
||||
findUniqueByCode(code);
|
||||
return true;
|
||||
} catch (InstanceNotFoundException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private Material findUniqueByCode(String code)
|
||||
throws InstanceNotFoundException {
|
||||
Criteria criteria = getSession().createCriteria(Material.class);
|
||||
criteria.add(Restrictions.eq("code", code).ignoreCase());
|
||||
|
||||
List<Material> list = criteria.list();
|
||||
if (list.size() != 1) {
|
||||
throw new InstanceNotFoundException(code, Material.class.getName());
|
||||
}
|
||||
return list.get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Material findUniqueByCodeInAnotherTransaction(String code)
|
||||
throws InstanceNotFoundException {
|
||||
Criteria criteria = getSession().createCriteria(Material.class);
|
||||
criteria.add(Restrictions.eq("code", code).ignoreCase());
|
||||
|
||||
List<Material> list = criteria.list();
|
||||
if (list.size() != 1) {
|
||||
throw new InstanceNotFoundException(code, Material.class.getName());
|
||||
}
|
||||
return list.get(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,9 +22,13 @@ package org.navalplanner.business.materials.entities;
|
|||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import org.hibernate.validator.AssertTrue;
|
||||
import org.hibernate.validator.NotEmpty;
|
||||
import org.hibernate.validator.NotNull;
|
||||
import org.navalplanner.business.common.BaseEntity;
|
||||
import org.navalplanner.business.common.Registry;
|
||||
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.navalplanner.business.materials.daos.IMaterialDAO;
|
||||
|
||||
/**
|
||||
* Material entity
|
||||
|
|
@ -116,4 +120,31 @@ public class Material extends BaseEntity implements Comparable {
|
|||
return code.compareTo(material.getCode());
|
||||
}
|
||||
|
||||
}
|
||||
@AssertTrue(message="machine code has to be unique. It is already used")
|
||||
public boolean checkConstraintUniqueCode() {
|
||||
boolean result;
|
||||
if (isNewObject()) {
|
||||
result = !existsMaterialWithTheCode();
|
||||
} else {
|
||||
result = isIfExistsTheExistentMaterialThisOne();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private boolean existsMaterialWithTheCode() {
|
||||
IMaterialDAO materialDAO = Registry.getMaterialDAO();
|
||||
return materialDAO.existsMaterialWithCodeInAnotherTransaction(code);
|
||||
}
|
||||
|
||||
private boolean isIfExistsTheExistentMaterialThisOne() {
|
||||
IMaterialDAO materialDAO = Registry.getMaterialDAO();
|
||||
try {
|
||||
Material material =
|
||||
materialDAO.findUniqueByCodeInAnotherTransaction(code);
|
||||
return material.getId().equals(getId());
|
||||
} catch (InstanceNotFoundException e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -104,16 +104,16 @@ public class MaterialCategory extends BaseEntity {
|
|||
materials.remove(material);
|
||||
}
|
||||
|
||||
@AssertTrue(message="material code must be unique within a category")
|
||||
public boolean checkConstraintNonRepeatedMaterialCodes() {
|
||||
Set<String> materialCodes = new HashSet<String>();
|
||||
for (Material each: materials) {
|
||||
final String code = each.getCode();
|
||||
if (materialCodes.contains(code)) {
|
||||
return false;
|
||||
}
|
||||
materialCodes.add(code);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// @AssertTrue(message="material code must be unique within a category")
|
||||
// public boolean checkConstraintNonRepeatedMaterialCodes() {
|
||||
// Set<String> materialCodes = new HashSet<String>();
|
||||
// for (Material each: materials) {
|
||||
// final String code = each.getCode();
|
||||
// if (materialCodes.contains(code)) {
|
||||
// return false;
|
||||
// }
|
||||
// materialCodes.add(code);
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue