jira and tim-connector: Constraints for OrderSyncInfo
This commit is contained in:
parent
94f3763e32
commit
111b780752
3 changed files with 79 additions and 0 deletions
|
|
@ -82,4 +82,30 @@ public interface IOrderSyncInfoDAO extends IGenericDAO<OrderSyncInfo, Long> {
|
|||
* @return a list of OrderSyncInfo if found and null if not
|
||||
*/
|
||||
List<OrderSyncInfo> findByConnectorName(String connectorName);
|
||||
|
||||
/**
|
||||
* Returns true if there exists other {@link OrderSyncInfo} with the same
|
||||
* <code>{@link OrderSyncInfo#getKey()}</code>,
|
||||
* <code>{@link OrderSyncInfo#getOrder()}</code> and
|
||||
* <code>{@link OrderSyncInfo#getConnectorName()}</code>
|
||||
*
|
||||
* @param orderSyncInfo
|
||||
* the <code>{@link OrderSyncInfo}</code>
|
||||
*/
|
||||
boolean existsByKeyOrderAndConnectorNameAnotherTransaction(
|
||||
OrderSyncInfo orderSyncInfo);
|
||||
|
||||
/**
|
||||
* Returns unique {@link OrderSyncInfo} for the specified <code>key</code>,
|
||||
* <code>order</code> and <code>connectorName</code>
|
||||
*
|
||||
* @param key
|
||||
* the key
|
||||
* @param order
|
||||
* an order
|
||||
* @param connectorName
|
||||
* the name of the connector
|
||||
*/
|
||||
OrderSyncInfo findUniqueByKeyOrderAndConnectorNameAnotherTransaction(
|
||||
String key, Order order, String connectorName);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<OrderSyncInfo, Long>
|
|||
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 <code>{@link OrderSyncInfo}</code> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue