Fix bug detect if there are duplicated codes between the order to be
saved and that order already saved in the DB It's necessary to check if any of the codes in an order that's going to be saved contain duplicated values comparing also against that very same order in the DB. Order codes must be unique. There's a case where it may not be any duplicated code in an order, neither in other orders, but a unique constraint violation still may happen. Imagine the following scenario: order |-- order1(1, 'code1') |-- order2(2, 'code2') The first time _order_ is persisted, everything goes OK. However if the value of codes are swapped, that is: order |-- order1(1, 'code2') |-- order2(2, 'code1') None code is repeated within the order, however as data is persisted one by one, when _order1_ is going to be saved into DB, the value of _order2.code_ is still 'code2', resulting into an unique code constraint violation. This is a limitation of the backend database and how unique fields work. The best thing to do is to check there's no other orderelement with the same code value in the DB (except for the one that's going to be saved). FEA: ItEr60S04ValidacionEProbasFuncionaisItEr59S04
This commit is contained in:
parent
711ef2be11
commit
a3cded16db
3 changed files with 48 additions and 24 deletions
|
|
@ -129,4 +129,13 @@ public interface IOrderElementDAO extends IIntegrationEntityDAO<OrderElement> {
|
|||
*/
|
||||
Set<String> getAllCodesExcluding(List<OrderElement> orderElements);
|
||||
|
||||
/**
|
||||
* Checks if there's another {@link OrderElement} in DB which code is the same as
|
||||
* some of the ones in order (and its children)
|
||||
*
|
||||
* @param order
|
||||
* @return
|
||||
*/
|
||||
OrderElement findRepeatedOrderCodeInDB(Order order);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ package org.navalplanner.business.orders.daos;
|
|||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Iterator;
|
||||
|
|
@ -599,4 +600,40 @@ public class OrderElementDAO extends IntegrationEntityDAO<OrderElement>
|
|||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly= true, propagation = Propagation.REQUIRES_NEW)
|
||||
public OrderElement findRepeatedOrderCodeInDB(Order order) {
|
||||
final Map<String, OrderElement> orderElements = createMapByCode(getOrderAndAllChildren(order));
|
||||
final Map<String, OrderElement> orderElementsInDB = createMapByCode(getAll());
|
||||
|
||||
for (String code : orderElements.keySet()) {
|
||||
OrderElement orderElement = orderElements.get(code);
|
||||
OrderElement orderElementInDB = orderElementsInDB.get(code);
|
||||
|
||||
// There's an element in the DB with the same code and it's a
|
||||
// different element
|
||||
if (orderElementInDB != null
|
||||
&& !orderElementInDB.getId().equals(orderElement.getId())) {
|
||||
return orderElement;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private List<OrderElement> getOrderAndAllChildren(Order order) {
|
||||
List<OrderElement> result = new ArrayList<OrderElement>();
|
||||
result.add(order);
|
||||
result.addAll(order.getAllChildren());
|
||||
return result;
|
||||
}
|
||||
|
||||
private Map<String, OrderElement> createMapByCode(List<OrderElement> orderElements) {
|
||||
Map<String, OrderElement> result = new HashMap<String, OrderElement>();
|
||||
for (OrderElement each: orderElements) {
|
||||
final String code = each.getCode();
|
||||
result.put(code, each);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -89,6 +89,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.zkoss.ganttz.IPredicate;
|
||||
import org.zkoss.zul.Messagebox;
|
||||
|
|
@ -550,7 +551,7 @@ public class OrderModel implements IOrderModel {
|
|||
repeatedOrder.getCode(), repeatedOrder.getName()));
|
||||
}
|
||||
|
||||
repeatedOrder = findRepeatedOrderCodeInAnotherOrder(order);
|
||||
repeatedOrder = orderElementDAO.findRepeatedOrderCodeInDB(order);
|
||||
if (repeatedOrder != null) {
|
||||
throw new ValidationException(_(
|
||||
"Repeated Order code {0} in Order {1}",
|
||||
|
|
@ -558,29 +559,6 @@ public class OrderModel implements IOrderModel {
|
|||
}
|
||||
}
|
||||
|
||||
private OrderElement findRepeatedOrderCodeInAnotherOrder(Order order) {
|
||||
final List<OrderElement> orderElements = getOrderAndAllChildren(order);
|
||||
|
||||
// Codes in other orders but not in this one
|
||||
Set<String> otherCodes = orderElementDAO
|
||||
.getAllCodesExcluding(orderElements);
|
||||
|
||||
// Find codes in this order that are in codes of other order elements
|
||||
for (OrderElement each : orderElements) {
|
||||
if (otherCodes.contains(each.getCode())) {
|
||||
return each;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private List<OrderElement> getOrderAndAllChildren(Order order) {
|
||||
List<OrderElement> result = new ArrayList<OrderElement>();
|
||||
result.add(order);
|
||||
result.addAll(order.getAllChildren());
|
||||
return result;
|
||||
}
|
||||
|
||||
private void calculateAdvancePercentageIncludingChildren(OrderElement order) {
|
||||
calculateAdvancePercentage(order);
|
||||
for (OrderElement orderElement : order.getAllChildren()) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue