Avoid to delete an order element with expenses

ItEr76S24AdapatingProjectsToExpenses
This commit is contained in:
Susana Montes Pedreira 2012-04-23 11:00:16 +01:00
parent c0dc6dede3
commit 97530cbc99
8 changed files with 91 additions and 0 deletions

View file

@ -19,11 +19,19 @@
package org.libreplan.business.expensesheet.daos;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.libreplan.business.common.daos.IntegrationEntityDAO;
import org.libreplan.business.expensesheet.entities.ExpenseSheetLine;
import org.libreplan.business.orders.entities.OrderElement;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
/**
* DAO for {@link ExpenseSheetLine}
@ -35,6 +43,39 @@ import org.springframework.stereotype.Repository;
public class ExpenseSheetLineDAO extends IntegrationEntityDAO<ExpenseSheetLine> implements
IExpenseSheetLineDAO {
@SuppressWarnings("unchecked")
@Override
@Transactional(readOnly = true)
public List<ExpenseSheetLine> findByOrderElement(OrderElement orderElement) {
if (orderElement.isNewObject()) {
return new ArrayList<ExpenseSheetLine>();
}
// Prepare criteria
final Criteria criteria = getSession().createCriteria(ExpenseSheetLine.class);
criteria.add(Restrictions.eq("orderElement", orderElement));
return criteria.list();
}
@Override
public List<ExpenseSheetLine> findByOrderElementAndChildren(OrderElement orderElement) {
if (orderElement.isNewObject()) {
return new ArrayList<ExpenseSheetLine>();
}
return findByOrderAndItsChildren(orderElement);
}
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)
public List<ExpenseSheetLine> findByOrderAndItsChildren(OrderElement orderElement) {
// Create collection with current orderElement and all its children
Collection<OrderElement> orderElements = orderElement.getAllChildren();
orderElements.add(orderElement);
// Prepare criteria
final Criteria criteria = getSession().createCriteria(ExpenseSheetLine.class);
criteria.add(Restrictions.in("orderElement", orderElements));
return criteria.list();
}
}

View file

@ -19,8 +19,11 @@
package org.libreplan.business.expensesheet.daos;
import java.util.List;
import org.libreplan.business.common.daos.IIntegrationEntityDAO;
import org.libreplan.business.expensesheet.entities.ExpenseSheetLine;
import org.libreplan.business.orders.entities.OrderElement;
/**
* Interface for ExpenseSheetLine DAO
@ -30,4 +33,8 @@ import org.libreplan.business.expensesheet.entities.ExpenseSheetLine;
*/
public interface IExpenseSheetLineDAO extends IIntegrationEntityDAO<ExpenseSheetLine> {
List<ExpenseSheetLine> findByOrderElement(OrderElement orderElement);
List<ExpenseSheetLine> findByOrderElementAndChildren(OrderElement orderElement);
}

View file

@ -126,4 +126,6 @@ public interface IOrderElementDAO extends IIntegrationEntityDAO<OrderElement> {
*/
OrderElement findRepeatedOrderCodeInDB(OrderElement order);
boolean hasImputedExpenseSheet(Long id) throws InstanceNotFoundException;
}

View file

@ -40,6 +40,7 @@ import org.hibernate.criterion.Restrictions;
import org.libreplan.business.common.IAdHocTransactionService;
import org.libreplan.business.common.daos.IntegrationEntityDAO;
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
import org.libreplan.business.expensesheet.daos.IExpenseSheetLineDAO;
import org.libreplan.business.orders.entities.OrderElement;
import org.libreplan.business.orders.entities.SchedulingDataForVersion;
import org.libreplan.business.orders.entities.TaskSource;
@ -73,6 +74,9 @@ public class OrderElementDAO extends IntegrationEntityDAO<OrderElement>
@Autowired
private IWorkReportLineDAO workReportLineDAO;
@Autowired
private IExpenseSheetLineDAO expenseSheetLineDAO;
@Autowired
private IWorkReportDAO workReportDAO;
@ -486,4 +490,9 @@ public class OrderElementDAO extends IntegrationEntityDAO<OrderElement>
return result;
}
@Override
public boolean hasImputedExpenseSheet(Long id) throws InstanceNotFoundException {
OrderElement orderElement = find(id);
return (!expenseSheetLineDAO.findByOrderElementAndChildren(orderElement).isEmpty());
}
}

View file

@ -128,4 +128,6 @@ public interface IOrderModel extends IIntegrationEntityModel {
PlanningState getPlanningState();
boolean hasImputedExpenseSheets(OrderElement order);
}

View file

@ -855,6 +855,16 @@ public class OrderCRUDController extends GenericForwardComposer {
}
private void remove(Order order) {
boolean hasImputedExpenseSheets = orderModel.hasImputedExpenseSheets(order);
if (hasImputedExpenseSheets) {
messagesForUser
.showMessage(
Level.ERROR,
_("You can not remove the project \"{0}\" because this one has imputed expense sheets.",
order.getName()));
return;
}
boolean alreadyInUse = orderModel.isAlreadyInUseAndIsOnlyInCurrentScenario(order);
if (alreadyInUse) {
messagesForUser

View file

@ -679,6 +679,16 @@ public class OrderElementTreeController extends TreeController<OrderElement> {
@Override
public void remove(OrderElement element) {
boolean hasImputedExpenseSheets = orderModel.hasImputedExpenseSheets(element);
if (hasImputedExpenseSheets) {
messagesForUser
.showMessage(
Level.ERROR,
_("You can not remove the project \"{0}\" because this one has imputed expense sheets.",
element.getName()));
return;
}
boolean alreadyInUse = orderModel.isAlreadyInUse(element);
if (alreadyInUse) {
messagesForUser

View file

@ -850,4 +850,14 @@ public class OrderModel extends IntegrationEntityModel implements IOrderModel {
return planningState;
}
@Override
@Transactional(readOnly = true)
public boolean hasImputedExpenseSheets(OrderElement order) {
try {
return orderElementDAO.hasImputedExpenseSheet(order.getId());
} catch (InstanceNotFoundException e) {
throw new RuntimeException(e);
}
}
}