[Bug #991] When removing an hours type check is not referenced by other entities

FEA: ItEr74S04BugFixing
This commit is contained in:
Diego Pino Garcia 2011-04-18 11:49:59 +02:00
parent 1e701e1adc
commit da24837c75
7 changed files with 90 additions and 54 deletions

View file

@ -53,13 +53,4 @@ public class HourCostDAO extends IntegrationEntityDAO<HourCost> implements
super.remove(id);
}
@SuppressWarnings("unchecked")
@Override
public Collection<HourCost> hoursCostsByTypeOfWorkHour(
TypeOfWorkHours typeOfWorkHours) {
return getSession().createCriteria(HourCost.class)
.add(Restrictions.eq("type", typeOfWorkHours))
.list();
}
}

View file

@ -21,12 +21,9 @@
package org.navalplanner.business.costcategories.daos;
import java.util.Collection;
import org.navalplanner.business.common.daos.IIntegrationEntityDAO;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.costcategories.entities.HourCost;
import org.navalplanner.business.costcategories.entities.TypeOfWorkHours;
/**
* @author Jacobo Aragunde Perez <jaragunde@igalia.com>
@ -37,6 +34,4 @@ public interface IHourCostDAO extends IIntegrationEntityDAO<HourCost> {
@Override
public void remove(Long id) throws InstanceNotFoundException;
public Collection<HourCost> hoursCostsByTypeOfWorkHour(TypeOfWorkHours typeOfWorkHours);
}

View file

@ -25,34 +25,46 @@ import java.util.List;
import org.navalplanner.business.common.daos.IIntegrationEntityDAO;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.costcategories.entities.TypeOfWorkHours;
/**
* @author Jacobo Aragunde Perez <jaragunde@igalia.com>
* @author Diego Pino García <dpino@igalia.com>
*/
public interface ITypeOfWorkHoursDAO extends
IIntegrationEntityDAO<TypeOfWorkHours> {
TypeOfWorkHours findUniqueByCode(TypeOfWorkHours typeOfWorkHours)
throws InstanceNotFoundException;
/**
* Checks if type is referenced by other entities examining the entities
* that are related with {@link TypeOfWorkHours} via a foreign key
*
* @param type
* @throws ValidationException
*/
void checkIsReferencedByOtherEntities(TypeOfWorkHours type) throws ValidationException;
boolean existsByCode(TypeOfWorkHours typeOfWorkHours);
boolean existsTypeWithCodeInAnotherTransaction(String code);
List<TypeOfWorkHours> findActive();
TypeOfWorkHours findUniqueByCode(String code)
throws InstanceNotFoundException;
boolean existsByCode(TypeOfWorkHours typeOfWorkHours);
List<TypeOfWorkHours> findActive();
boolean existsTypeWithCodeInAnotherTransaction(String code);
TypeOfWorkHours findUniqueByCode(TypeOfWorkHours typeOfWorkHours)
throws InstanceNotFoundException;
TypeOfWorkHours findUniqueByCodeInAnotherTransaction(String code)
throws InstanceNotFoundException;
TypeOfWorkHours findUniqueByNameInAnotherTransaction(String name)
throws InstanceNotFoundException;
TypeOfWorkHours findUniqueByName(String name)
throws InstanceNotFoundException;
TypeOfWorkHours findUniqueByNameInAnotherTransaction(String name)
throws InstanceNotFoundException;
List<TypeOfWorkHours> hoursTypeByNameAsc();
}
}

View file

@ -30,7 +30,10 @@ import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.navalplanner.business.common.daos.IntegrationEntityDAO;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.costcategories.entities.HourCost;
import org.navalplanner.business.costcategories.entities.TypeOfWorkHours;
import org.navalplanner.business.workreports.entities.WorkReportLine;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
@ -39,6 +42,7 @@ import org.springframework.transaction.annotation.Transactional;
/**
* @author Jacobo Aragunde Perez <jaragunde@igalia.com>
* @author Diego Pino García <dpino@igalia.com>
*/
@Repository
@Scope(BeanDefinition.SCOPE_SINGLETON)
@ -134,4 +138,33 @@ public class TypeOfWorkHoursDAO extends IntegrationEntityDAO<TypeOfWorkHours>
.addOrder(Order.asc("name")).list();
}
@Override
public void checkIsReferencedByOtherEntities(TypeOfWorkHours type) throws ValidationException {
checkHasHourCost(type);
checkHasWorkReportLine(type);
}
private void checkHasWorkReportLine(TypeOfWorkHours type) {
List workReportLines = getSession()
.createCriteria(WorkReportLine.class)
.add(Restrictions.eq("typeOfWorkHours", type)).list();
if (!workReportLines.isEmpty()) {
throw ValidationException
.invalidValue(
"Cannot delete type of work hours. It is being used at this moment in some work report line.",
type);
}
}
private void checkHasHourCost(TypeOfWorkHours type) {
List hoursCost = getSession().createCriteria(HourCost.class)
.add(Restrictions.eq("type", type)).list();
if (!hoursCost.isEmpty()) {
throw ValidationException
.invalidValue(
"Cannot delete type of work hours. It is being used at this moment in some cost category.",
type);
}
}
}

View file

@ -36,6 +36,8 @@ import org.navalplanner.web.common.IIntegrationEntityModel;
*/
public interface ITypeOfWorkHoursModel extends IIntegrationEntityModel {
void checkIsReferencedByOtherEntities(TypeOfWorkHours typeOfWorkHours) throws ValidationException;
void confirmRemove(TypeOfWorkHours typeOfWorkHours);
/**
@ -46,8 +48,6 @@ public interface ITypeOfWorkHoursModel extends IIntegrationEntityModel {
*/
void confirmSave() throws ValidationException;
boolean existsCostCategoriesUsing(TypeOfWorkHours typeOfWorkHours);
/**
* Gets the current {@link TypeOfWorkHours}.
*

View file

@ -159,15 +159,33 @@ public class TypeOfWorkHoursCRUDController extends GenericForwardComposer implem
}
public void confirmDelete(TypeOfWorkHours typeOfWorkHours) {
boolean canDelete = existsCostCategoriesUsing(typeOfWorkHours);
if (!canDelete) {
showCannotDeleteWorkHoursTypeDialog(typeOfWorkHours);
return;
if (!isReferencedByOtherEntities(typeOfWorkHours)) {
int result = showConfirmDeleteWorkHoursType(typeOfWorkHours);
if (result == Messagebox.OK) {
typeOfWorkHoursModel.confirmRemove(typeOfWorkHours);
Util.reloadBindings(listing);
}
}
int result = showConfirmDeleteWorkHoursType(typeOfWorkHours);
if (result == Messagebox.OK) {
typeOfWorkHoursModel.confirmRemove(typeOfWorkHours);
Util.reloadBindings(listing);
}
private boolean isReferencedByOtherEntities(TypeOfWorkHours typeOfWorkHours) {
try {
typeOfWorkHoursModel.checkIsReferencedByOtherEntities(typeOfWorkHours);
return false;
} catch (ValidationException e) {
showCannotDeleteWorkHoursTypeDialog(e.getInvalidValue()
.getMessage(), typeOfWorkHours);
}
return true;
}
private void showCannotDeleteWorkHoursTypeDialog(String message,
TypeOfWorkHours typeOfWorkHours) {
try {
Messagebox.show(_(message), _("Warning"), Messagebox.OK,
Messagebox.EXCLAMATION);
} catch (InterruptedException e) {
LOG.error(_("Error on showing warning message removing typeOfWorkHours: ", typeOfWorkHours.getId()), e);
}
}
@ -181,17 +199,4 @@ public class TypeOfWorkHoursCRUDController extends GenericForwardComposer implem
return Messagebox.CANCEL;
}
private boolean existsCostCategoriesUsing(TypeOfWorkHours typeOfWorkHours) {
return typeOfWorkHoursModel.existsCostCategoriesUsing(typeOfWorkHours);
}
private void showCannotDeleteWorkHoursTypeDialog(TypeOfWorkHours typeOfWorkHours) {
try {
Messagebox.show(_("Cannot delete type of work hours. It is being used at this moment in some cost category."),
_("Warning"), Messagebox.OK, Messagebox.EXCLAMATION);
} catch (InterruptedException e) {
LOG.error(_("Error on showing warning message removing typeOfWorkHours: ", typeOfWorkHours.getId()), e);
}
}
}

View file

@ -80,6 +80,12 @@ public class TypeOfWorkHoursModel extends IntegrationEntityModel implements
}
}
@Override
@Transactional(readOnly = true)
public void checkIsReferencedByOtherEntities(TypeOfWorkHours typeOfWorkHours) throws ValidationException {
typeOfWorkHoursDAO.checkIsReferencedByOtherEntities(typeOfWorkHours);
}
@Override
public TypeOfWorkHours getTypeOfWorkHours() {
return typeOfWorkHours;
@ -132,10 +138,4 @@ public class TypeOfWorkHoursModel extends IntegrationEntityModel implements
return this.typeOfWorkHours;
}
@Override
@Transactional(readOnly = true)
public boolean existsCostCategoriesUsing(TypeOfWorkHours typeOfWorkHours) {
return hourCostDAO.hoursCostsByTypeOfWorkHour(typeOfWorkHours).isEmpty();
}
}