ItEr19S10CUIntroducionPartesTraballoManualmenteItEr18S11: [Fix bug] Find OrderElement by code

This commit is contained in:
Diego Pino Garcia 2009-07-30 15:50:50 +02:00 committed by Javier Moran Rua
parent f5316e0882
commit 4fdd78604e
6 changed files with 76 additions and 20 deletions

View file

@ -12,16 +12,24 @@ import org.navalplanner.business.orders.entities.OrderElement;
* @author Susana Montes Pedreira <smontes@wirelessgalicia.com>
*/
public interface IOrderElementDao extends IGenericDao<OrderElement, Long> {
public OrderElement findByCode(String code);
public List<OrderElement> findByCode(String code);
public OrderElement findUniqueByCode(String code)
throws InstanceNotFoundException;
public List<OrderElement> findByCodeAndParent(OrderElement parent,
String code);
/**
* Find an order element with the <code>code</code> passed as parameter
* and which is a son of the <code>parent</code> {@link OrderElement}
*
* @param parent Parent {@link OrderElement}
* @param code code of the {@link OrderElement} to find
* @return the {@link OrderElement} found
*/
public OrderElement findByCode(OrderElement parent, String code);
public OrderElement findUniqueByCodeAndParent(OrderElement parent,
String code) throws InstanceNotFoundException;
public List<OrderElement> findParent(
OrderElement orderElement);

View file

@ -13,6 +13,7 @@ import org.springframework.stereotype.Repository;
/**
* Dao for {@link OrderElement}
*
* @author Manuel Rego Casasnovas <mrego@igalia.com>
* @author Diego Pino García <dpino@igalia.com>
* @author Susana Montes Pedreira <smontes@wirelessgalicia.com>
@ -22,17 +23,42 @@ import org.springframework.stereotype.Repository;
public class OrderElementDao extends GenericDaoHibernate<OrderElement, Long>
implements IOrderElementDao {
public OrderElement findByCode(String code) {
public List<OrderElement> findByCode(String code) {
Criteria c = getSession().createCriteria(OrderElement.class);
c.add(Restrictions.eq("code", code));
return (OrderElement) c.uniqueResult();
return (List<OrderElement>) c.list();
}
public OrderElement findByCode(OrderElement orderElement, String code) {
public OrderElement findUniqueByCode(String code)
throws InstanceNotFoundException {
List<OrderElement> list = findByCode(code);
if (list.size() > 1) {
throw new InstanceNotFoundException(code, OrderElement.class
.getName());
}
return list.get(0);
}
public List<OrderElement> findByCodeAndParent(OrderElement parent,
String code) {
Criteria c = getSession().createCriteria(OrderElement.class);
c.add(Restrictions.eq("code", code));
c.add(Restrictions.eq("parent", orderElement));
return (OrderElement) c.uniqueResult();
if (parent != null) {
c.add(Restrictions.eq("parent", parent));
} else {
c.add(Restrictions.isNull("parent"));
}
return c.list();
}
public OrderElement findUniqueByCodeAndParent(OrderElement parent,
String code) throws InstanceNotFoundException {
List<OrderElement> list = findByCodeAndParent(parent, code);
if (list.isEmpty() || list.size() > 1) {
throw new InstanceNotFoundException(code, OrderElement.class
.getName());
}
return list.get(0);
}
@Override

View file

@ -1,10 +1,12 @@
package org.navalplanner.business.test.orders.daos;
import static junit.framework.Assert.assertNotNull;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.navalplanner.business.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_FILE;
import static org.navalplanner.business.test.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_TEST_FILE;
import java.util.List;
import java.util.UUID;
import org.junit.Test;
@ -68,19 +70,21 @@ public class OrderElementDAOTest {
}
@Test
public void testFindByCode() {
public void testFindUniqueByCode() throws InstanceNotFoundException {
OrderLine orderLine = createValidOrderLine();
orderElementDAO.save(orderLine);
orderLine.setCode(((Long) orderLine.getId()).toString());
orderElementDAO.save(orderLine);
OrderLine found = (OrderLine) orderElementDAO.findByCode(orderLine
OrderLine found = (OrderLine) orderElementDAO
.findUniqueByCode(orderLine
.getCode());
assertTrue(found != null && found.getCode().equals(orderLine.getCode()));
}
@Test
public void testFindByCodeAndOrderLineGroup() {
public void testFindUniqueByCodeAndOrderLineGroup()
throws InstanceNotFoundException {
// Create OrderLineGroupLine
OrderLineGroup orderLineGroup = createValidOrderLineGroup();
orderElementDAO.save(orderLineGroup);
@ -94,11 +98,25 @@ public class OrderElementDAOTest {
orderLine.setParent(orderLineGroup);
orderElementDAO.save(orderLine);
OrderLine found = (OrderLine) orderElementDAO.findByCode(
OrderLine found = (OrderLine) orderElementDAO
.findUniqueByCodeAndParent(
orderLineGroup, orderLine.getCode());
assertTrue(found != null && found.getCode().equals(orderLine.getCode()));
}
@Test
public void testFindByCodeInRoot() throws InstanceNotFoundException {
// Create OrderLineGroupLine
OrderLineGroup orderLineGroup = createValidOrderLineGroup();
orderElementDAO.save(orderLineGroup);
orderLineGroup.setCode(((Long) orderLineGroup.getId()).toString());
orderElementDAO.save(orderLineGroup);
List<OrderElement> list = orderElementDAO.findByCodeAndParent(null,
orderLineGroup.getCode());
assertFalse(list.isEmpty());
}
@Test
public void testFindDistinguishedCode() {
// Create OrderLineGroupLine

View file

@ -51,7 +51,7 @@ public interface IWorkReportModel {
* @param code
* @return
*/
OrderElement findOrderElement(String code);
OrderElement findOrderElement(String code) throws InstanceNotFoundException;
/**
* Find a @{link Worker} by nif

View file

@ -414,13 +414,13 @@ public class WorkReportCRUDController extends GenericForwardComposer implements
@Override
public void set(String value) {
if (value.length() > 0) {
OrderElement orderElement = workReportModel
.findOrderElement(value);
if (orderElement == null) {
try {
workReportLine.setOrderElement(workReportModel
.findOrderElement(value));
} catch (InstanceNotFoundException e) {
throw new WrongValueException(txtOrder,
"OrderElement not found");
"OrderElement not found");
}
workReportLine.setOrderElement(orderElement);
}
}
});

View file

@ -146,11 +146,15 @@ public class WorkReportModel implements IWorkReportModel {
@Override
@Transactional
public OrderElement findOrderElement(String orderCode) {
public OrderElement findOrderElement(String orderCode)
throws InstanceNotFoundException {
String[] parts = orderCode.split("-");
OrderElement parent = orderElementDAO.findByCode(parts[0]);
OrderElement parent = orderElementDAO.findUniqueByCodeAndParent(null,
parts[0]);
for (int i = 1; i < parts.length && parent != null; i++) {
OrderElement child = orderElementDAO.findByCode(parent, parts[i]);
OrderElement child = orderElementDAO.findUniqueByCodeAndParent(
parent, parts[i]);
parent = child;
}