diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/materials/daos/IMaterialAssignmentDAO.java b/navalplanner-business/src/main/java/org/navalplanner/business/materials/daos/IMaterialAssignmentDAO.java new file mode 100644 index 000000000..e4ea1e197 --- /dev/null +++ b/navalplanner-business/src/main/java/org/navalplanner/business/materials/daos/IMaterialAssignmentDAO.java @@ -0,0 +1,41 @@ +/* + * This file is part of NavalPlan + * + * 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.materials.daos; + + + +import java.util.List; + +import org.navalplanner.business.common.daos.IGenericDAO; +import org.navalplanner.business.materials.entities.Material; +import org.navalplanner.business.materials.entities.MaterialAssignment; + +/** + * Interface MaterialAssignmentDAO + * + * @author Jacobo Aragunde Perez + */ +public interface IMaterialAssignmentDAO extends + IGenericDAO { + + List getByMaterial(Material material); + +} diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/materials/daos/MaterialAssignmentDAO.java b/navalplanner-business/src/main/java/org/navalplanner/business/materials/daos/MaterialAssignmentDAO.java new file mode 100644 index 000000000..e8e28ed40 --- /dev/null +++ b/navalplanner-business/src/main/java/org/navalplanner/business/materials/daos/MaterialAssignmentDAO.java @@ -0,0 +1,52 @@ +/* + * This file is part of NavalPlan + * + * 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.materials.daos; + +import java.util.List; + +import org.hibernate.criterion.Restrictions; +import org.navalplanner.business.common.daos.GenericDAOHibernate; +import org.navalplanner.business.materials.entities.Material; +import org.navalplanner.business.materials.entities.MaterialAssignment; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Repository; + +/** + * Class MaterialAssignmentDAO + * + * @author Jacobo Aragunde Perez + */ + +@Repository +@Scope(BeanDefinition.SCOPE_SINGLETON) +public class MaterialAssignmentDAO extends GenericDAOHibernate + implements IMaterialAssignmentDAO { + + @Override + public List getByMaterial(Material material) { + + return (List)getSession(). + createCriteria(MaterialAssignment.class) + .add(Restrictions.eq("materialInfo.material", material)).list(); + } + +} diff --git a/navalplanner-business/src/test/java/org/navalplanner/business/test/materials/daos/MaterialAssignmentDAOTest.java b/navalplanner-business/src/test/java/org/navalplanner/business/test/materials/daos/MaterialAssignmentDAOTest.java new file mode 100644 index 000000000..fc449c606 --- /dev/null +++ b/navalplanner-business/src/test/java/org/navalplanner/business/test/materials/daos/MaterialAssignmentDAOTest.java @@ -0,0 +1,101 @@ +/* + * This file is part of NavalPlan + * + * 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.materials.daos; + +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.UUID; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.navalplanner.business.materials.daos.IMaterialAssignmentDAO; +import org.navalplanner.business.materials.daos.IMaterialCategoryDAO; +import org.navalplanner.business.materials.daos.IMaterialDAO; +import org.navalplanner.business.materials.daos.IUnitTypeDAO; +import org.navalplanner.business.materials.entities.Material; +import org.navalplanner.business.materials.entities.MaterialAssignment; +import org.navalplanner.business.materials.entities.MaterialCategory; +import org.navalplanner.business.materials.entities.UnitType; +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 {@ResourcesCostCategoryDAO} + * + * @author Jacobo Aragunde Perez + * + */ +@Transactional +public class MaterialAssignmentDAOTest { + + @Autowired + IUnitTypeDAO unitTypeDAO; + + @Autowired + IMaterialDAO materialDAO; + + @Autowired + IMaterialCategoryDAO materialCategoryDAO; + + @Autowired + IMaterialAssignmentDAO materialAssignmentDAO; + + private Material createValidMaterial() { + MaterialCategory materialCategory = MaterialCategory.create(UUID.randomUUID().toString()); + materialCategoryDAO.save(materialCategory); + UnitType unitType = UnitType.create("m"); + unitTypeDAO.save(unitType); + Material material = Material.create(UUID.randomUUID().toString()); + material.setCategory(materialCategory); + material.setUnitType(unitType); + materialDAO.save(material); + return material; + } + + private MaterialAssignment createValidMaterialAssignment() { + MaterialAssignment assignment = + MaterialAssignment.create(createValidMaterial()); + return assignment; + } + + @Test + public void testGetByMaterial() { + MaterialAssignment assignment1 = createValidMaterialAssignment(); + MaterialAssignment assignment2 = createValidMaterialAssignment(); + materialAssignmentDAO.save(assignment1); + materialAssignmentDAO.save(assignment2); + + assertTrue(materialAssignmentDAO.getByMaterial(assignment1.getMaterial()).contains(assignment1)); + assertFalse(materialAssignmentDAO.getByMaterial(assignment1.getMaterial()).contains(assignment2)); + + assignment2.setMaterial(assignment1.getMaterial()); + assertTrue(materialAssignmentDAO.getByMaterial(assignment1.getMaterial()).contains(assignment2)); + } + +}