ItEr60S04ValidacionEProbasFuncionaisItEr59S04: [Bug #557] Fix bug.

Now criterion types can be renamed and they are not created again. It
won't work for legacy data the first time.
This commit is contained in:
Óscar González Fernández 2010-08-03 14:03:08 +02:00
parent 1e22a94a6d
commit 722b57c36b
3 changed files with 57 additions and 13 deletions

View file

@ -28,7 +28,6 @@ import java.util.Map.Entry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.resources.daos.ICriterionDAO;
import org.navalplanner.business.resources.daos.ICriterionTypeDAO;
import org.navalplanner.business.resources.entities.Criterion;
@ -87,21 +86,11 @@ public class CriterionsBootstrap implements ICriterionsBootstrap {
}
private CriterionType retrieveOrCreate(CriterionType criterionType) {
if (!criterionTypeDAO.exists(criterionType.getId())
&& !criterionTypeDAO
.existsOtherCriterionTypeByName(criterionType)) {
if (!criterionTypeDAO.existsPredefinedType(criterionType)) {
criterionTypeDAO.save(criterionType);
return criterionType;
} else {
try {
CriterionType result = criterionTypeDAO
.findUniqueByName(criterionType
.getName());
return result;
} catch (InstanceNotFoundException e) {
throw new RuntimeException(e);
}
}
return criterionTypeDAO.findPredefined(criterionType);
}
private Map<CriterionType, List<String>> getTypesWithCriterions() {

View file

@ -99,6 +99,40 @@ public class CriterionTypeDAO extends IntegrationEntityDAO<CriterionType>
return found != null && criterionType != found;
}
@Override
public boolean existsPredefinedType(CriterionType criterionType) {
Validate.notNull(criterionType);
Validate.notNull(criterionType.getPredefinedTypeInternalName());
if (existsOtherCriterionTypeByName(criterionType)) {
return true;
}
return uniqueByInternalName(criterionType) != null;
}
@Override
public CriterionType findPredefined(CriterionType criterionType) {
Validate.notNull(criterionType);
Validate.notNull(criterionType.getPredefinedTypeInternalName());
CriterionType result = uniqueByName(criterionType.getName());
if (result != null) {
return result;
}
return uniqueByInternalName(criterionType);
}
private Criteria byInternalName(String predefinedTypeInternalName) {
Criteria result = getSession().createCriteria(CriterionType.class);
result.add(Restrictions.eq("predefinedTypeInternalName",
predefinedTypeInternalName).ignoreCase());
return result;
}
private CriterionType uniqueByInternalName(CriterionType criterionType) {
Criteria c = byInternalName(criterionType
.getPredefinedTypeInternalName());
return (CriterionType) c.uniqueResult();
}
@Override
@Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
public boolean existsByNameAnotherTransaction(CriterionType criterionType) {

View file

@ -26,6 +26,7 @@ import java.util.List;
import org.navalplanner.business.common.daos.IIntegrationEntityDAO;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.resources.entities.CriterionType;
import org.navalplanner.business.resources.entities.PredefinedCriterionTypes;
import org.navalplanner.business.resources.entities.ResourceEnum;
/**
@ -58,4 +59,24 @@ public interface ICriterionTypeDAO
List<CriterionType> getCriterionTypesByResources(
Collection<ResourceEnum> resources);
/**
* Checks if exists the equivalent {@link CriterionType} on the DB for a
* {@link CriterionType} created from a {@link PredefinedCriterionTypes}
*
* @param criterionType
* @return
*/
boolean existsPredefinedType(CriterionType criterionType);
/**
* Searches for the equivalent {@link CriterionType} on the DB for a
* CriterionType created from a {@link PredefinedCriterionTypes}
*
* @param predefinedCriterionType
* @return <code>null</code> if there is no {@link CriterionType} for the
* predefinedCriterionType. Otherwise the equivalent
* {@link CriterionType} on DB
*/
CriterionType findPredefined(CriterionType predefinedCriterionType);
}