ItEr30S06CUAltaMaquina: Create Machine entity, DAO and DAOTest

This commit is contained in:
Diego Pino Garcia 2009-10-19 09:37:01 +02:00 committed by Javier Moran Rua
parent 22ebc6333c
commit 0d0f1081ff
5 changed files with 344 additions and 0 deletions

View file

@ -0,0 +1,78 @@
/*
* This file is part of ###PROJECT_NAME###
*
* Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e
* Desenvolvemento Tecnolóxico de Galicia
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.business.resources.daos;
import java.util.List;
import org.navalplanner.business.common.daos.IGenericDAO;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.resources.entities.Criterion;
import org.navalplanner.business.resources.entities.Machine;
/**
* DAO interface for the <code>Machine</code> entity.
*
* @author Diego Pino Garcia <dpino@igalia.com>
*
*/
public interface IMachineDAO extends IGenericDAO<Machine, Long> {
/**
* Returns machines which name/NIF partially matches with name, and complies
* all of the given criterions
*
* @param name
* search machine by name/NIF
* @param criterions
* search machine that matches with criterions
* @return
*/
@SuppressWarnings("unchecked")
List<Machine> findByNameAndCriterions(String name, List<Criterion> criterions);
/**
* Returns machines which name/NIF partially matches with name
*
* @param name
* search machine by name/Code
*
*/
List<Machine> findByNameOrCode(String name);
/**
* Finds a {@link Machine} with the Code param that should be unique.
*
* @param nif
* The Code to search the {@link Machine}
* @return The {@link Machine} with this Code
* @throws InstanceNotFoundException
* If there're more than one {@link Machine} with this Code or
* there isn't any {@link Machine} with this Code
*/
Machine findUniqueByCode(String code) throws InstanceNotFoundException;
/**
* Return list of machines
*
* @return
*/
List<Machine> getAll();
}

View file

@ -0,0 +1,102 @@
/*
* This file is part of ###PROJECT_NAME###
*
* Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e
* Desenvolvemento Tecnolóxico de Galicia
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.business.resources.daos;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.navalplanner.business.common.daos.GenericDAOHibernate;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.resources.entities.Criterion;
import org.navalplanner.business.resources.entities.Machine;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
/**
* Hibernate DAO for the <code>Machine</code> entity.
*
* @author Diego Pino Garcia <dpino@igalia.com>
*/
@Repository
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class MachineDAO extends GenericDAOHibernate<Machine, Long>
implements IMachineDAO {
@SuppressWarnings("unchecked")
@Override
public List<Machine> findByNameAndCriterions(String name,
List<Criterion> criterions) {
// Find machines by name
List<Machine> machines;
if (name == null || name.isEmpty()) {
machines = getAll();
} else {
machines = findByNameOrCode(name);
}
// If no criterions selected, returned found machines
if (criterions.isEmpty()) {
return machines;
}
// Filter by criterion
final List<Machine> result = new ArrayList<Machine>();
for (Machine machine : machines) {
if (machine.satisfiesCriterions(new HashSet(criterions))) {
result.add(machine);
}
}
return result;
}
@Override
public List<Machine> getAll() {
return list(Machine.class);
}
@SuppressWarnings("unchecked")
@Override
public List<Machine> findByNameOrCode(String name) {
name = "%" + name + "%";
return getSession().createCriteria(Machine.class).add(
Restrictions.or(Restrictions.ilike("name", name), Restrictions
.ilike("code", name))).list();
}
@Override
public Machine findUniqueByCode(String code)
throws InstanceNotFoundException {
Criteria criteria = getSession().createCriteria(Machine.class);
criteria.add(Restrictions.eq("code", code).ignoreCase());
List<Machine> list = criteria.list();
if (list.size() != 1) {
throw new InstanceNotFoundException(code, Machine.class.getName());
}
return list.get(0);
}
}

View file

@ -0,0 +1,64 @@
package org.navalplanner.business.resources.entities;
import java.util.ArrayList;
import java.util.Set;
import org.hibernate.validator.NotEmpty;
public class Machine extends Resource {
@NotEmpty
private String code;
@NotEmpty
private String name;
@NotEmpty
private String description;
protected Machine() {
}
protected Machine(String code, String name, String description) {
this.code = code;
this.name = name;
this.description = description;
}
public static Machine create() {
return (Machine) create(new Machine());
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean satisfiesCriterions(Set<Criterion> criterions) {
ICriterion compositedCriterion = CriterionCompounder.buildAnd(
new ArrayList<ICriterion>(criterions)).getResult();
return compositedCriterion.isSatisfiedBy(this);
}
}

View file

@ -32,6 +32,13 @@
<property name="surname"/>
<property name="nif"/>
</joined-subclass>
<joined-subclass name="org.navalplanner.business.resources.entities.Machine">
<key column="MACHINE_ID"/>
<property name="code"/>
<property name="name"/>
<property name="description"/>
</joined-subclass>
</class>
<!-- Criterion -->

View file

@ -0,0 +1,93 @@
/*
* This file is part of ###PROJECT_NAME###
*
* Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e
* Desenvolvemento Tecnolóxico de Galicia
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.business.test.resources.daos;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.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.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.resources.daos.IMachineDAO;
import org.navalplanner.business.resources.entities.Machine;
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 })
/**
* Test for {@MachineDAO}
*
* @author Diego Pino Garcia <dpino@igalia.com>
*
*/
@Transactional
public class MachineDAOTest {
@Autowired
IMachineDAO machineDAO;
private Machine createValidMachine() {
Machine machine = Machine.create();
machine.setCode("code");
machine.setName("name");
machine.setDescription("description");
return machine;
}
@Test
public void testInSpringContainer() {
assertNotNull(machineDAO);
}
@Test
public void testSaveMachine() {
Machine machine = createValidMachine();
machineDAO.save(machine);
assertTrue(machine.getId() != null);
}
@Test
public void testRemoveMachine() throws InstanceNotFoundException {
Machine machine = createValidMachine();
machineDAO.save(machine);
machineDAO.remove(machine.getId());
assertFalse(machineDAO.exists(machine.getId()));
}
@Test
public void testListMachines() {
int previous = machineDAO.list(Machine.class).size();
Machine machine = createValidMachine();
machineDAO.save(machine);
List<Machine> list = machineDAO.list(Machine.class);
assertEquals(previous + 1, list.size());
}
}