ItEr18S11CUIntroducionPartesTraballoManualmenteItEr17S16: Added findByCode to OrderElementDao
Javier Moran Rua <jmoran@igalia.com>: Corrected formatting errors and comments.
This commit is contained in:
parent
71da412317
commit
adb994ffff
5 changed files with 151 additions and 14 deletions
|
|
@ -2,12 +2,23 @@ package org.navalplanner.business.orders.daos;
|
|||
|
||||
import org.navalplanner.business.common.daos.IGenericDao;
|
||||
import org.navalplanner.business.orders.entities.OrderElement;
|
||||
import org.navalplanner.business.resources.entities.Worker;
|
||||
|
||||
/**
|
||||
* Contract for {@link OrderElementDao}
|
||||
*
|
||||
* @author Manuel Rego Casasnovas <mrego@igalia.com>
|
||||
* @author Diego Pino García <dpino@igalia.com>
|
||||
*/
|
||||
public interface IOrderElementDao extends IGenericDao<OrderElement, Long> {
|
||||
public OrderElement findByCode(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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package org.navalplanner.business.orders.daos;
|
||||
|
||||
import org.hibernate.Criteria;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.navalplanner.business.common.daos.impl.GenericDaoHibernate;
|
||||
import org.navalplanner.business.orders.entities.OrderElement;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
|
|
@ -10,10 +12,23 @@ import org.springframework.stereotype.Repository;
|
|||
* Dao for {@link OrderElement}
|
||||
*
|
||||
* @author Manuel Rego Casasnovas <mrego@igalia.com>
|
||||
* @author Diego Pino García <dpino@igalia.com>
|
||||
*/
|
||||
@Repository
|
||||
@Scope(BeanDefinition.SCOPE_SINGLETON)
|
||||
public class OrderElementDao extends GenericDaoHibernate<OrderElement, Long>
|
||||
implements
|
||||
IOrderElementDao {
|
||||
implements IOrderElementDao {
|
||||
|
||||
public OrderElement findByCode(String code) {
|
||||
Criteria c = getSession().createCriteria(OrderElement.class);
|
||||
c.add(Restrictions.eq("code", code));
|
||||
return (OrderElement) c.uniqueResult();
|
||||
}
|
||||
|
||||
public OrderElement findByCode(OrderElement orderElement, String code) {
|
||||
Criteria c = getSession().createCriteria(OrderElement.class);
|
||||
c.add(Restrictions.eq("code", code));
|
||||
c.add(Restrictions.eq("parent", orderElement));
|
||||
return (OrderElement) c.uniqueResult();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,6 +33,16 @@ public abstract class OrderElement {
|
|||
|
||||
private Set<TaskElement> taskElements = new HashSet<TaskElement>();
|
||||
|
||||
private OrderLineGroup parent;
|
||||
|
||||
public OrderLineGroup getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(OrderLineGroup parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public abstract Integer getWorkHours();
|
||||
|
||||
public abstract List<HoursGroup> getHoursGroups();
|
||||
|
|
@ -52,19 +62,10 @@ public abstract class OrderElement {
|
|||
return name;
|
||||
}
|
||||
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public abstract boolean isLeaf();
|
||||
|
||||
public abstract List<OrderElement> getChildren();
|
||||
|
|
@ -151,12 +152,20 @@ public abstract class OrderElement {
|
|||
return (getHoursGroups().size() > 0);
|
||||
}
|
||||
|
||||
public boolean isFormatCodeValid(String code) {
|
||||
public boolean isFormatCodeValid(String code) {
|
||||
|
||||
if (code.contains("_")) return false;
|
||||
if (code.equals("")) return false;
|
||||
if (code.contains("_"))
|
||||
return false;
|
||||
if (code.equals(""))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,6 +37,9 @@
|
|||
<one-to-many class="org.navalplanner.business.planner.entities.TaskElement"></one-to-many>
|
||||
</set>
|
||||
|
||||
<!-- Inverse navigation from OrderElement to OrderLineGroup -->
|
||||
<many-to-one name="parent" access="field"/>
|
||||
|
||||
<joined-subclass name="OrderLineGroup">
|
||||
<key column="ORDERELEMENTID"></key>
|
||||
<list name="children" access="field" cascade="all">
|
||||
|
|
|
|||
|
|
@ -0,0 +1,99 @@
|
|||
package org.navalplanner.business.test.orders.daos;
|
||||
|
||||
import static junit.framework.Assert.assertNotNull;
|
||||
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.UUID;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.navalplanner.business.orders.daos.IOrderElementDao;
|
||||
import org.navalplanner.business.orders.entities.OrderLine;
|
||||
import org.navalplanner.business.orders.entities.OrderLineGroup;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { BUSINESS_SPRING_CONFIG_FILE,
|
||||
BUSINESS_SPRING_CONFIG_TEST_FILE })
|
||||
/*
|
||||
* @author Diego Pino García <dpino@igalia.com>
|
||||
*/
|
||||
@Transactional
|
||||
public class OrderElementDAOTest {
|
||||
|
||||
@Autowired
|
||||
private IOrderElementDao orderElementDAO;
|
||||
|
||||
@Test
|
||||
public void testInSpringContainer() {
|
||||
assertNotNull(orderElementDAO);
|
||||
}
|
||||
|
||||
private OrderLine createValidOrderLine() {
|
||||
String unique = UUID.randomUUID().toString();
|
||||
return createValidOrderLine(unique, unique);
|
||||
}
|
||||
|
||||
private OrderLine createValidOrderLine(String name, String code) {
|
||||
OrderLine orderLine = new OrderLine();
|
||||
orderLine.setName(name);
|
||||
orderLine.setCode(code);
|
||||
return orderLine;
|
||||
}
|
||||
|
||||
private OrderLineGroup createValidOrderLineGroup() {
|
||||
String unique = UUID.randomUUID().toString();
|
||||
return createValidOrderLineGroup(unique, unique);
|
||||
}
|
||||
|
||||
private OrderLineGroup createValidOrderLineGroup(String name, String code) {
|
||||
OrderLineGroup orderLineGroup = new OrderLineGroup();
|
||||
orderLineGroup.setName(name);
|
||||
orderLineGroup.setCode(code);
|
||||
return orderLineGroup;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSaveOrderLine() {
|
||||
OrderLine orderLine = createValidOrderLine();
|
||||
orderElementDAO.save(orderLine);
|
||||
assertTrue(orderElementDAO.exists(orderLine.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByCode() {
|
||||
OrderLine orderLine = createValidOrderLine();
|
||||
orderElementDAO.save(orderLine);
|
||||
orderLine.setCode(((Long) orderLine.getId()).toString());
|
||||
orderElementDAO.save(orderLine);
|
||||
|
||||
OrderLine found = (OrderLine) orderElementDAO.findByCode(orderLine
|
||||
.getCode());
|
||||
assertTrue(found != null && found.getCode().equals(orderLine.getCode()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByCodeAndOrderLineGroup() {
|
||||
// Create OrderLineGroupLine
|
||||
OrderLineGroup orderLineGroup = createValidOrderLineGroup();
|
||||
orderElementDAO.save(orderLineGroup);
|
||||
orderLineGroup.setCode(((Long) orderLineGroup.getId()).toString());
|
||||
orderElementDAO.save(orderLineGroup);
|
||||
|
||||
// Create OrderLineGroup
|
||||
OrderLine orderLine = createValidOrderLine();
|
||||
orderElementDAO.save(orderLine);
|
||||
orderLine.setCode(((Long) orderLine.getId()).toString());
|
||||
orderLine.setParent(orderLineGroup);
|
||||
orderElementDAO.save(orderLine);
|
||||
|
||||
OrderLine found = (OrderLine) orderElementDAO.findByCode(
|
||||
orderLineGroup, orderLine.getCode());
|
||||
assertTrue(found != null && found.getCode().equals(orderLine.getCode()));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue