ItEr45S23CUImportacionTiposEtiquetasEEtiquetas: Added constraints to LabelType entity.

This commit is contained in:
Manuel Rego Casasnovas 2010-01-28 13:08:31 +01:00 committed by Javier Moran Rua
parent 1e28b48fb0
commit 8be0a0c135
3 changed files with 63 additions and 0 deletions

View file

@ -41,4 +41,9 @@ public interface ILabelTypeDAO extends IIntegrationEntityDAO<LabelType> {
LabelType findUniqueByName(String type) throws InstanceNotFoundException,
NonUniqueResultException;
boolean existsByNameAnotherTransaction(LabelType labelType);
LabelType findUniqueByNameAnotherTransaction(String name)
throws InstanceNotFoundException;
}

View file

@ -96,4 +96,17 @@ public class LabelTypeDAO extends IntegrationEntityDAO<LabelType> implements
return labelType;
}
@Override
@Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
public boolean existsByNameAnotherTransaction(LabelType labelType) {
return existsByName(labelType);
}
@Override
@Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
public LabelType findUniqueByNameAnotherTransaction(String name)
throws InstanceNotFoundException {
return findUniqueByName(name);
}
}

View file

@ -24,10 +24,13 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.hibernate.validator.AssertTrue;
import org.hibernate.validator.NotEmpty;
import org.navalplanner.business.common.IntegrationEntity;
import org.navalplanner.business.common.Registry;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.labels.daos.ILabelTypeDAO;
/**
@ -96,4 +99,46 @@ public class LabelType extends IntegrationEntity implements Comparable {
return Registry.getLabelTypeDAO();
}
@AssertTrue(message = "label names must be unique inside a label type")
public boolean checkConstraintNonRepeatedLabelNames() {
Set<String> labelNames = new HashSet<String>();
for (Label label : labels) {
if (!StringUtils.isBlank(label.getName())) {
if (labelNames.contains(label.getName().toLowerCase())) {
return false;
} else {
labelNames.add(label.getName().toLowerCase());
}
}
}
return true;
}
@AssertTrue(message = "label type name is already being used")
public boolean checkConstraintUniqueLabelTypeName() {
if (!firstLevelValidationsPassed()) {
return true;
}
ILabelTypeDAO labelTypeDAO = Registry.getLabelTypeDAO();
if (isNewObject()) {
return !labelTypeDAO.existsByNameAnotherTransaction(this);
} else {
try {
LabelType c = labelTypeDAO
.findUniqueByNameAnotherTransaction(name);
return c.getId().equals(getId());
} catch (InstanceNotFoundException e) {
return true;
}
}
}
private boolean firstLevelValidationsPassed() {
return !StringUtils.isBlank(name);
}
}