From 311b1a9f285104d0761c6bece78e9f22b4822e32 Mon Sep 17 00:00:00 2001 From: Susana Montes Pedreira Date: Mon, 26 Oct 2009 19:26:33 +0100 Subject: [PATCH] ItEr32S09ValidacionEProbasFuncionaisItEr31S12: [Bug #68] Fix to remove OrderElement in DB when they are deleted from interface. --- .../orders/daos/IOrderElementDAO.java | 3 ++ .../business/orders/daos/OrderElementDAO.java | 7 +++ .../business/orders/entities/Orders.hbm.xml | 2 +- .../navalplanner/web/orders/OrderModel.java | 44 +++++++++++++------ 4 files changed, 41 insertions(+), 15 deletions(-) diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/orders/daos/IOrderElementDAO.java b/navalplanner-business/src/main/java/org/navalplanner/business/orders/daos/IOrderElementDAO.java index a0d21e413..cb957d58a 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/orders/daos/IOrderElementDAO.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/orders/daos/IOrderElementDAO.java @@ -34,6 +34,9 @@ import org.navalplanner.business.orders.entities.OrderElement; * @author Susana Montes Pedreira */ public interface IOrderElementDAO extends IGenericDAO { + + public List findWithoutParent(); + public List findByCode(String code); public OrderElement findUniqueByCode(String code) diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/orders/daos/OrderElementDAO.java b/navalplanner-business/src/main/java/org/navalplanner/business/orders/daos/OrderElementDAO.java index 8f9c46d41..e16064887 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/orders/daos/OrderElementDAO.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/orders/daos/OrderElementDAO.java @@ -53,6 +53,13 @@ public class OrderElementDAO extends GenericDAOHibernate @Autowired private IWorkReportLineDAO workReportLineDAO; + @Override + public List findWithoutParent() { + Criteria c = getSession().createCriteria(OrderElement.class); + c.add(Restrictions.isNull("parent")); + return (List) c.list(); + } + public List findByCode(String code) { Criteria c = getSession().createCriteria(OrderElement.class); c.add(Restrictions.eq("code", code)); diff --git a/navalplanner-business/src/main/resources/org/navalplanner/business/orders/entities/Orders.hbm.xml b/navalplanner-business/src/main/resources/org/navalplanner/business/orders/entities/Orders.hbm.xml index 49775343e..775432a91 100644 --- a/navalplanner-business/src/main/resources/org/navalplanner/business/orders/entities/Orders.hbm.xml +++ b/navalplanner-business/src/main/resources/org/navalplanner/business/orders/entities/Orders.hbm.xml @@ -57,7 +57,7 @@ - + diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/orders/OrderModel.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/orders/OrderModel.java index 67fb67a65..99a107f4d 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/orders/OrderModel.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/orders/OrderModel.java @@ -2,20 +2,20 @@ * This file is part of ###PROJECT_NAME### * * Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e - * Desenvolvemento Tecnolóxico de Galicia + * Desenvolvemento Tecnolóxico de Galicia * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Affero General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more + * details. * * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . + * along with this program. If not, see . */ package org.navalplanner.web.orders; @@ -128,8 +128,7 @@ public class OrderModel implements IOrderModel { List criterionTypes = criterionTypeDAO .getCriterionTypes(); for (CriterionType criterionType : criterionTypes) { - List criterions = new ArrayList( - criterionDAO + List criterions = new ArrayList(criterionDAO .findByType(criterionType)); mapCriterions.put(criterionType, criterions); @@ -218,6 +217,23 @@ public class OrderModel implements IOrderModel { order.checkValid(); this.orderDAO.save(order); + deleteOrderElementNotParent(); + } + + private void deleteOrderElementNotParent() throws ValidationException { + List listToBeRemoved = orderElementDAO + .findWithoutParent(); + for (OrderElement orderElement : listToBeRemoved) { + if (!(orderElement instanceof Order)) { + try { + orderElementDAO.remove(orderElement.getId()); + } catch (InstanceNotFoundException e) { + throw new ValidationException(_("" + + "It not could remove the order element " + + orderElement.getName())); + } + } + } } private void reattachCriterions() { @@ -324,8 +340,8 @@ public class OrderModel implements IOrderModel { } else { OrderLine line = (OrderLine) order; if (line.getHoursGroups().isEmpty()) - throw new IllegalArgumentException( - _("The line must have at least one {0} associated", + throw new IllegalArgumentException(_( + "The line must have at least one {0} associated", HoursGroup.class.getSimpleName())); return line.getHoursGroups().size() > 1 ? convertToTaskGroup(line) : convertToTask(line);