From 111b780752e6dd75abdf43c8c0505134581d3ce9 Mon Sep 17 00:00:00 2001 From: miciele Ghiorghis Date: Tue, 26 Mar 2013 15:57:04 +0100 Subject: [PATCH] jira and tim-connector: Constraints for OrderSyncInfo --- .../orders/daos/IOrderSyncInfoDAO.java | 26 ++++++++++++++++ .../orders/daos/OrderSyncInfoDAO.java | 31 +++++++++++++++++++ .../orders/entities/OrderSyncInfo.java | 22 +++++++++++++ 3 files changed, 79 insertions(+) diff --git a/libreplan-business/src/main/java/org/libreplan/business/orders/daos/IOrderSyncInfoDAO.java b/libreplan-business/src/main/java/org/libreplan/business/orders/daos/IOrderSyncInfoDAO.java index 4b8fc7cdb..d81d7078f 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/orders/daos/IOrderSyncInfoDAO.java +++ b/libreplan-business/src/main/java/org/libreplan/business/orders/daos/IOrderSyncInfoDAO.java @@ -82,4 +82,30 @@ public interface IOrderSyncInfoDAO extends IGenericDAO { * @return a list of OrderSyncInfo if found and null if not */ List findByConnectorName(String connectorName); + + /** + * Returns true if there exists other {@link OrderSyncInfo} with the same + * {@link OrderSyncInfo#getKey()}, + * {@link OrderSyncInfo#getOrder()} and + * {@link OrderSyncInfo#getConnectorName()} + * + * @param orderSyncInfo + * the {@link OrderSyncInfo} + */ + boolean existsByKeyOrderAndConnectorNameAnotherTransaction( + OrderSyncInfo orderSyncInfo); + + /** + * Returns unique {@link OrderSyncInfo} for the specified key, + * order and connectorName + * + * @param key + * the key + * @param order + * an order + * @param connectorName + * the name of the connector + */ + OrderSyncInfo findUniqueByKeyOrderAndConnectorNameAnotherTransaction( + String key, Order order, String connectorName); } diff --git a/libreplan-business/src/main/java/org/libreplan/business/orders/daos/OrderSyncInfoDAO.java b/libreplan-business/src/main/java/org/libreplan/business/orders/daos/OrderSyncInfoDAO.java index e13323566..eb4c916bf 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/orders/daos/OrderSyncInfoDAO.java +++ b/libreplan-business/src/main/java/org/libreplan/business/orders/daos/OrderSyncInfoDAO.java @@ -29,6 +29,8 @@ import org.libreplan.business.orders.entities.OrderSyncInfo; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; /** * DAO for {@link OrderSyncInfo} @@ -79,4 +81,33 @@ public class OrderSyncInfoDAO extends GenericDAOHibernate return criteria.list(); } + @Override + @Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW) + public boolean existsByKeyOrderAndConnectorNameAnotherTransaction( + OrderSyncInfo orderSyncInfo) { + return existsOtherOrderSyncInfoByKeyOrderAndConnectorName(orderSyncInfo); + } + + /** + * Returns true if other {@link OrderSyncInfo} which is the same + * as the given {@link OrderSyncInfo} already exists + * + * @param orderSyncInfo + * the {@link OrderSyncInfo} + */ + private boolean existsOtherOrderSyncInfoByKeyOrderAndConnectorName( + OrderSyncInfo orderSyncInfo) { + OrderSyncInfo found = findByKeyOrderAndConnectorName( + orderSyncInfo.getKey(), orderSyncInfo.getOrder(), + orderSyncInfo.getConnectorName()); + return found != null && found != orderSyncInfo; + } + + @Override + @Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW) + public OrderSyncInfo findUniqueByKeyOrderAndConnectorNameAnotherTransaction( + String key, Order order, String connectorName) { + return findByKeyOrderAndConnectorName(key, order, connectorName); + } + } diff --git a/libreplan-business/src/main/java/org/libreplan/business/orders/entities/OrderSyncInfo.java b/libreplan-business/src/main/java/org/libreplan/business/orders/entities/OrderSyncInfo.java index c4efb7992..3cec95fec 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/orders/entities/OrderSyncInfo.java +++ b/libreplan-business/src/main/java/org/libreplan/business/orders/entities/OrderSyncInfo.java @@ -21,10 +21,14 @@ package org.libreplan.business.orders.entities; import java.util.Date; +import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.Validate; +import org.hibernate.validator.AssertTrue; import org.hibernate.validator.NotNull; import org.libreplan.business.common.BaseEntity; +import org.libreplan.business.common.Registry; import org.libreplan.business.common.entities.Connector; +import org.libreplan.business.orders.daos.IOrderSyncInfoDAO; /** * OrderSyncInfo entity. This entity holds order synchronization info. Each time @@ -105,4 +109,22 @@ public class OrderSyncInfo extends BaseEntity { public void setOrder(Order order) { this.order = order; } + + @AssertTrue(message = "project sync info is already being used") + public boolean checkConstraintUniqueOrderSyncInfo() { + if (StringUtils.isBlank(key) && order == null + && StringUtils.isBlank(connectorName)) { + return true; + } + IOrderSyncInfoDAO orderSyncInfoDAO = Registry.getOrderSyncInfoDAO(); + if (isNewObject()) { + return !orderSyncInfoDAO + .existsByKeyOrderAndConnectorNameAnotherTransaction(this); + } else { + OrderSyncInfo found = orderSyncInfoDAO + .findUniqueByKeyOrderAndConnectorNameAnotherTransaction( + key, order, connectorName); + return found == null || found.getId().equals(getId()); + } + } }