diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/resources/daos/IMachineDAO.java b/navalplanner-business/src/main/java/org/navalplanner/business/resources/daos/IMachineDAO.java new file mode 100644 index 000000000..1aa402661 --- /dev/null +++ b/navalplanner-business/src/main/java/org/navalplanner/business/resources/daos/IMachineDAO.java @@ -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 . + */ + +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 Machine entity. + * + * @author Diego Pino Garcia + * + */ +public interface IMachineDAO extends IGenericDAO { + + /** + * 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 findByNameAndCriterions(String name, List criterions); + + /** + * Returns machines which name/NIF partially matches with name + * + * @param name + * search machine by name/Code + * + */ + List 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 getAll(); +} diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/resources/daos/MachineDAO.java b/navalplanner-business/src/main/java/org/navalplanner/business/resources/daos/MachineDAO.java new file mode 100644 index 000000000..faf8533e3 --- /dev/null +++ b/navalplanner-business/src/main/java/org/navalplanner/business/resources/daos/MachineDAO.java @@ -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 . + */ + +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 Machine entity. + * + * @author Diego Pino Garcia + */ +@Repository +@Scope(BeanDefinition.SCOPE_SINGLETON) +public class MachineDAO extends GenericDAOHibernate + implements IMachineDAO { + + @SuppressWarnings("unchecked") + @Override + public List findByNameAndCriterions(String name, + List criterions) { + + // Find machines by name + List 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 result = new ArrayList(); + for (Machine machine : machines) { + if (machine.satisfiesCriterions(new HashSet(criterions))) { + result.add(machine); + } + } + return result; + } + + @Override + public List getAll() { + return list(Machine.class); + } + + @SuppressWarnings("unchecked") + @Override + public List 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 list = criteria.list(); + if (list.size() != 1) { + throw new InstanceNotFoundException(code, Machine.class.getName()); + } + return list.get(0); + } + +} diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/resources/entities/Machine.java b/navalplanner-business/src/main/java/org/navalplanner/business/resources/entities/Machine.java new file mode 100644 index 000000000..b72847198 --- /dev/null +++ b/navalplanner-business/src/main/java/org/navalplanner/business/resources/entities/Machine.java @@ -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 criterions) { + ICriterion compositedCriterion = CriterionCompounder.buildAnd( + new ArrayList(criterions)).getResult(); + return compositedCriterion.isSatisfiedBy(this); + } + +} diff --git a/navalplanner-business/src/main/resources/org/navalplanner/business/resources/entities/Resources.hbm.xml b/navalplanner-business/src/main/resources/org/navalplanner/business/resources/entities/Resources.hbm.xml index 08692f421..e1f800025 100644 --- a/navalplanner-business/src/main/resources/org/navalplanner/business/resources/entities/Resources.hbm.xml +++ b/navalplanner-business/src/main/resources/org/navalplanner/business/resources/entities/Resources.hbm.xml @@ -32,6 +32,13 @@ + + + + + + + diff --git a/navalplanner-business/src/test/java/org/navalplanner/business/test/resources/daos/MachineDAOTest.java b/navalplanner-business/src/test/java/org/navalplanner/business/test/resources/daos/MachineDAOTest.java new file mode 100644 index 000000000..29b385c88 --- /dev/null +++ b/navalplanner-business/src/test/java/org/navalplanner/business/test/resources/daos/MachineDAOTest.java @@ -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 . + */ + +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 + * + */ +@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 list = machineDAO.list(Machine.class); + assertEquals(previous + 1, list.size()); + } +}