diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/costcategories/daos/ITypeOfWorkHoursDAO.java b/navalplanner-business/src/main/java/org/navalplanner/business/costcategories/daos/ITypeOfWorkHoursDAO.java index 211b251a2..fc6ddff69 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/costcategories/daos/ITypeOfWorkHoursDAO.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/costcategories/daos/ITypeOfWorkHoursDAO.java @@ -22,14 +22,15 @@ package org.navalplanner.business.costcategories.daos; import java.util.List; -import org.navalplanner.business.common.daos.IGenericDAO; +import org.navalplanner.business.common.daos.IIntegrationEntityDAO; import org.navalplanner.business.common.exceptions.InstanceNotFoundException; import org.navalplanner.business.costcategories.entities.TypeOfWorkHours; /** * @author Jacobo Aragunde Perez */ -public interface ITypeOfWorkHoursDAO extends IGenericDAO { +public interface ITypeOfWorkHoursDAO extends + IIntegrationEntityDAO { TypeOfWorkHours findUniqueByCode(TypeOfWorkHours typeOfWorkHours) throws InstanceNotFoundException; diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/costcategories/daos/TypeOfWorkHoursDAO.java b/navalplanner-business/src/main/java/org/navalplanner/business/costcategories/daos/TypeOfWorkHoursDAO.java index 9438e3209..c3c1ecd26 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/costcategories/daos/TypeOfWorkHoursDAO.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/costcategories/daos/TypeOfWorkHoursDAO.java @@ -26,7 +26,7 @@ import java.util.List; import org.apache.commons.lang.Validate; import org.hibernate.Criteria; import org.hibernate.criterion.Restrictions; -import org.navalplanner.business.common.daos.GenericDAOHibernate; +import org.navalplanner.business.common.daos.IntegrationEntityDAO; import org.navalplanner.business.common.exceptions.InstanceNotFoundException; import org.navalplanner.business.costcategories.entities.TypeOfWorkHours; import org.springframework.beans.factory.config.BeanDefinition; @@ -40,7 +40,8 @@ import org.springframework.transaction.annotation.Transactional; */ @Repository @Scope(BeanDefinition.SCOPE_SINGLETON) -public class TypeOfWorkHoursDAO extends GenericDAOHibernate implements +public class TypeOfWorkHoursDAO extends IntegrationEntityDAO + implements ITypeOfWorkHoursDAO { @Override diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/costcategories/entities/TypeOfWorkHours.java b/navalplanner-business/src/main/java/org/navalplanner/business/costcategories/entities/TypeOfWorkHours.java index 94e1962d5..3e082f945 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/costcategories/entities/TypeOfWorkHours.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/costcategories/entities/TypeOfWorkHours.java @@ -22,9 +22,10 @@ package org.navalplanner.business.costcategories.entities; import java.math.BigDecimal; +import org.apache.commons.lang.StringUtils; import org.hibernate.validator.AssertTrue; import org.hibernate.validator.NotEmpty; -import org.navalplanner.business.common.BaseEntity; +import org.navalplanner.business.common.IntegrationEntity; import org.navalplanner.business.common.Registry; import org.navalplanner.business.common.exceptions.InstanceNotFoundException; import org.navalplanner.business.costcategories.daos.ITypeOfWorkHoursDAO; @@ -32,12 +33,10 @@ import org.navalplanner.business.costcategories.daos.ITypeOfWorkHoursDAO; /** * @author Jacobo Aragunde Perez */ -public class TypeOfWorkHours extends BaseEntity { +public class TypeOfWorkHours extends IntegrationEntity { - @NotEmpty private String code; - @NotEmpty private String name; private BigDecimal defaultPrice; @@ -57,11 +56,40 @@ public class TypeOfWorkHours extends BaseEntity { return (TypeOfWorkHours) create(new TypeOfWorkHours(code, name)); } + public static TypeOfWorkHours createUnvalidated(String code, String name, + Boolean enabled, BigDecimal defaultPrice) { + + TypeOfWorkHours typeOfWorkHours = create( + new TypeOfWorkHours(code, name), code); + + if (enabled != null) { + typeOfWorkHours.enabled = enabled; + } + if (defaultPrice != null) { + typeOfWorkHours.defaultPrice = defaultPrice; + } + return typeOfWorkHours; + } + + public void updateUnvalidated(String name, Boolean enabled, + BigDecimal defaultPrice) { + if (!StringUtils.isBlank(name)) { + this.name = name; + } + if (enabled != null) { + this.enabled = enabled; + } + if (defaultPrice != null) { + this.defaultPrice = defaultPrice; + } + } + protected TypeOfWorkHours(String code, String name) { this.code = code; this.name = name; } + @NotEmpty(message = "name not specified") public String getName() { return name; } @@ -70,6 +98,7 @@ public class TypeOfWorkHours extends BaseEntity { this.name = name; } + @NotEmpty(message = "code not specified") public String getCode() { return code; } @@ -96,6 +125,11 @@ public class TypeOfWorkHours extends BaseEntity { @AssertTrue(message="type code has to be unique. It is already used") public boolean checkConstraintUniqueCode() { + + if (code == null) { + return true; + } + boolean result; if (isNewObject()) { result = !existsTypeWithTheCode(); @@ -120,4 +154,9 @@ public class TypeOfWorkHours extends BaseEntity { return true; } } + + @Override + protected ITypeOfWorkHoursDAO getIntegrationEntityDAO() { + return Registry.getTypeOfWorkHoursDAO(); + } } diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/costcategories/api/CostCategoryDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/costcategories/api/CostCategoryDTO.java index 95cb443b9..fc70ee241 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/ws/costcategories/api/CostCategoryDTO.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/costcategories/api/CostCategoryDTO.java @@ -1,3 +1,23 @@ +/* + * 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.ws.costcategories.api; import java.util.HashSet; diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/costcategories/impl/CostCategoryConverter.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/costcategories/impl/CostCategoryConverter.java index 1323b527f..73f782627 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/ws/costcategories/impl/CostCategoryConverter.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/costcategories/impl/CostCategoryConverter.java @@ -136,7 +136,7 @@ public class CostCategoryConverter { public final static void updateCostCategory(CostCategory costCategory, CostCategoryDTO costCategoryDTO) throws ValidationException { /* - * 1: Update the existing work report line or add newwork report line. + * 1: Update the existing hour cost or add new hour cost. */ for (HourCostDTO hourCostDTO : costCategoryDTO.hourCostDTOs) { diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/typeofworkhours/api/ITypeOfWorkHoursService.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/typeofworkhours/api/ITypeOfWorkHoursService.java new file mode 100644 index 000000000..f02b35296 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/typeofworkhours/api/ITypeOfWorkHoursService.java @@ -0,0 +1,35 @@ +/* + * 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.ws.typeofworkhours.api; + +import org.navalplanner.ws.common.api.InstanceConstraintViolationsListDTO; + +/** + * Service for managing cost categories. + * @author Susana Montes Pedreira + */ +public interface ITypeOfWorkHoursService { + + InstanceConstraintViolationsListDTO addTypeOfWorkHours( + TypeOfWorkHoursListDTO typeOfWorkHoursListDTO); + + public TypeOfWorkHoursListDTO getTypeOfWorkHours(); +} \ No newline at end of file diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/typeofworkhours/api/TypeOfWorkHoursDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/typeofworkhours/api/TypeOfWorkHoursDTO.java new file mode 100644 index 000000000..d84904157 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/typeofworkhours/api/TypeOfWorkHoursDTO.java @@ -0,0 +1,76 @@ +/* + * 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.ws.typeofworkhours.api; + +import java.math.BigDecimal; + +import javax.xml.bind.annotation.XmlAttribute; + +import org.navalplanner.business.costcategories.entities.TypeOfWorkHours; +import org.navalplanner.ws.common.api.IntegrationEntityDTO; + +/** + * DTO for {@link TypeOfWorkHours} entity. + * @author Susana Montes Pedreira + */ +public class TypeOfWorkHoursDTO extends IntegrationEntityDTO { + + public final static String ENTITY_TYPE = "type-work-hours"; + + @XmlAttribute + public String name; + + @XmlAttribute + public BigDecimal defaultPrice; + + @XmlAttribute + public Boolean enabled; + + public TypeOfWorkHoursDTO() { + } + + public TypeOfWorkHoursDTO(String code, String name, Boolean enabled, + BigDecimal defaultPrice) { + + super(code); + this.name = name; + this.enabled = enabled; + this.defaultPrice = defaultPrice; + } + + /** + * This constructor automatically generates a unique code. It is intended to + * facilitate the implementation of test cases that add new instances (such + * instances will have a unique code). + */ + public TypeOfWorkHoursDTO(String name, Boolean enabled, + BigDecimal defaultPrice) { + + this(generateCode(), name, enabled, defaultPrice); + + } + + @Override + public String getEntityType() { + return ENTITY_TYPE; + } + +} \ No newline at end of file diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/typeofworkhours/api/TypeOfWorkHoursListDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/typeofworkhours/api/TypeOfWorkHoursListDTO.java new file mode 100644 index 000000000..196216dcc --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/typeofworkhours/api/TypeOfWorkHoursListDTO.java @@ -0,0 +1,46 @@ +/* + * 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.ws.typeofworkhours.api; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * DTO for a list of TypeOfWorkHours entities. + * @author Susana Montes Pedreira + */ +@XmlRootElement(name = "type-work-hours-list") +public class TypeOfWorkHoursListDTO { + + @XmlElement(name = "type-work-hours") + public List typeOfWorkHoursDTOs = new ArrayList(); + + public TypeOfWorkHoursListDTO() { + } + + public TypeOfWorkHoursListDTO(List typeOfWorkHoursDTOs) { + this.typeOfWorkHoursDTOs = typeOfWorkHoursDTOs; + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/typeofworkhours/api/package-info.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/typeofworkhours/api/package-info.java new file mode 100644 index 000000000..244ed9337 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/typeofworkhours/api/package-info.java @@ -0,0 +1,27 @@ +/* + * 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 . + */ + +/** + * Specification of namespace for REST-based services. + * @author Susana Montes Pedreira + */ +@javax.xml.bind.annotation.XmlSchema(elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED, namespace = WSCommonGlobalNames.REST_NAMESPACE) +package org.navalplanner.ws.typeofworkhours.api; +import org.navalplanner.ws.common.api.WSCommonGlobalNames; diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/typeofworkhours/impl/TypeOfWorkHoursConverter.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/typeofworkhours/impl/TypeOfWorkHoursConverter.java new file mode 100644 index 000000000..fabb3e4ba --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/typeofworkhours/impl/TypeOfWorkHoursConverter.java @@ -0,0 +1,65 @@ +/* + * 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.ws.typeofworkhours.impl; + +import org.apache.commons.lang.StringUtils; +import org.navalplanner.business.common.exceptions.ValidationException; +import org.navalplanner.business.costcategories.entities.TypeOfWorkHours; +import org.navalplanner.ws.typeofworkhours.api.TypeOfWorkHoursDTO; + +/** + * Converter from/to Type-Of-Work-Hours-related entities to/from DTOs. + * @author Susana Montes Pedreira + */ +public class TypeOfWorkHoursConverter { + + private TypeOfWorkHoursConverter() { + } + + public final static TypeOfWorkHoursDTO toDTO(TypeOfWorkHours typeOfWorkHours) { + return new TypeOfWorkHoursDTO(typeOfWorkHours.getCode(), + typeOfWorkHours.getName(), typeOfWorkHours.getEnabled(), + typeOfWorkHours.getDefaultPrice()); + + } + + public final static TypeOfWorkHours toEntity( + TypeOfWorkHoursDTO typeOfWorkHoursDTO) { + + TypeOfWorkHours typeOfWorkHours = TypeOfWorkHours.createUnvalidated( + StringUtils.trim(typeOfWorkHoursDTO.code), StringUtils + .trim(typeOfWorkHoursDTO.name), + typeOfWorkHoursDTO.enabled, typeOfWorkHoursDTO.defaultPrice); + + return typeOfWorkHours; + + } + + public final static void updateTypeOfWorkHours( + TypeOfWorkHours typeOfWorkHours, + TypeOfWorkHoursDTO typeOfWorkHoursDTO) throws ValidationException { + /* 1: Update typeOfWorkHours basic properties. */ + typeOfWorkHours.updateUnvalidated(StringUtils + .trim(typeOfWorkHoursDTO.name), typeOfWorkHoursDTO.enabled, + typeOfWorkHoursDTO.defaultPrice); + + } +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/typeofworkhours/impl/TypeOfWorkHoursServiceREST.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/typeofworkhours/impl/TypeOfWorkHoursServiceREST.java new file mode 100644 index 000000000..558a91ca6 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/typeofworkhours/impl/TypeOfWorkHoursServiceREST.java @@ -0,0 +1,97 @@ +/* + * 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.ws.typeofworkhours.impl; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; + +import org.navalplanner.business.common.daos.IIntegrationEntityDAO; +import org.navalplanner.business.common.exceptions.ValidationException; +import org.navalplanner.business.costcategories.daos.ITypeOfWorkHoursDAO; +import org.navalplanner.business.costcategories.entities.TypeOfWorkHours; +import org.navalplanner.ws.common.api.InstanceConstraintViolationsListDTO; +import org.navalplanner.ws.common.impl.GenericRESTService; +import org.navalplanner.ws.typeofworkhours.api.ITypeOfWorkHoursService; +import org.navalplanner.ws.typeofworkhours.api.TypeOfWorkHoursDTO; +import org.navalplanner.ws.typeofworkhours.api.TypeOfWorkHoursListDTO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * REST-based implementation of ITypeOfWorkHoursService. + * @author Susana Montes Pedreira + */ + +@Path("/typeofworkhours/") +@Produces("application/xml") +@Service("typeOfWorkHoursServiceREST") +public class TypeOfWorkHoursServiceREST extends + GenericRESTService implements + ITypeOfWorkHoursService { + + @Autowired + private ITypeOfWorkHoursDAO typeOfWorkHoursDAO; + + @Override + @GET + @Transactional(readOnly = true) + public TypeOfWorkHoursListDTO getTypeOfWorkHours() { + return new TypeOfWorkHoursListDTO(findAll()); + } + + @Override + @POST + @Consumes("application/xml") + public InstanceConstraintViolationsListDTO addTypeOfWorkHours( + TypeOfWorkHoursListDTO typeOfWorkHoursListDTO) { + + return save(typeOfWorkHoursListDTO.typeOfWorkHoursDTOs); + + } + + @Override + protected TypeOfWorkHours toEntity(TypeOfWorkHoursDTO entityDTO) { + return TypeOfWorkHoursConverter.toEntity(entityDTO); + } + + @Override + protected TypeOfWorkHoursDTO toDTO(TypeOfWorkHours entity) { + return TypeOfWorkHoursConverter.toDTO(entity); + } + + @Override + protected IIntegrationEntityDAO getIntegrationEntityDAO() { + return typeOfWorkHoursDAO; + } + + @Override + protected void updateEntity(TypeOfWorkHours entity, + TypeOfWorkHoursDTO entityDTO) + throws ValidationException { + + TypeOfWorkHoursConverter.updateTypeOfWorkHours(entity, entityDTO); + + } +} diff --git a/navalplanner-webapp/src/main/resources/navalplanner-webapp-spring-config.xml b/navalplanner-webapp/src/main/resources/navalplanner-webapp-spring-config.xml index 95217476f..042a06024 100644 --- a/navalplanner-webapp/src/main/resources/navalplanner-webapp-spring-config.xml +++ b/navalplanner-webapp/src/main/resources/navalplanner-webapp-spring-config.xml @@ -66,6 +66,7 @@ + diff --git a/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/costcategories/CostCategoryServiceTest.java b/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/costcategories/CostCategoryServiceTest.java index 34eb07c98..cb093cbbf 100644 --- a/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/costcategories/CostCategoryServiceTest.java +++ b/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/costcategories/CostCategoryServiceTest.java @@ -132,7 +132,7 @@ public class CostCategoryServiceTest { cc4_HourCostDTOs.add(cc4_1_HourCostDTO); CostCategoryDTO cc4 = new CostCategoryDTO("cc4", true, cc4_HourCostDTOs); - /* Criterion type list. */ + /* Cost category type list. */ CostCategoryListDTO costCategoryListDTO = createCostCategoryListDTO( cc1, cc2, cc3, cc4); @@ -178,7 +178,6 @@ public class CostCategoryServiceTest { cc1_HourCostDTOs.add(cc1_1_HourCostDTO); CostCategoryDTO cc1 = new CostCategoryDTO(costCategoryCode,"newCC1", true, cc1_HourCostDTOs); - /* Criterion type list. */ CostCategoryListDTO costCategoryListDTO = createCostCategoryListDTO(cc1); List instanceConstraintViolationsList = costCategoryService @@ -209,20 +208,19 @@ public class CostCategoryServiceTest { cc2_HourCostDTOs.add(cc2_1_HourCostDTO); CostCategoryDTO cc2 = new CostCategoryDTO(costCategoryCode,"updateCC1", false, cc2_HourCostDTOs); - /* Criterion type list. */ + /* Cost category type list. */ costCategoryListDTO = createCostCategoryListDTO(cc2); instanceConstraintViolationsList = costCategoryService .addCostCategories(costCategoryListDTO).instanceConstraintViolationsList; /* Test. */ - // the end date cannot be before the initDate assertTrue(instanceConstraintViolationsList.toString(), instanceConstraintViolationsList.size() == 0); assertTrue(costCategoryDAO.existsByCode(cc1.code)); assertTrue(hourCostDAO.existsByCode(cc1_1_HourCostDTO.code)); - // Check if the changes was updated + // Check if the changes was updated try { costCategory = costCategoryDAO .findByCode(costCategoryCode); diff --git a/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/typeofworkhours/TypeOfWorkHoursServiceTest.java b/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/typeofworkhours/TypeOfWorkHoursServiceTest.java new file mode 100644 index 000000000..e8a658a83 --- /dev/null +++ b/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/typeofworkhours/TypeOfWorkHoursServiceTest.java @@ -0,0 +1,168 @@ +/* + * 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.web.test.ws.typeofworkhours; + +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.web.WebappGlobalNames.WEBAPP_SPRING_CONFIG_FILE; +import static org.navalplanner.web.test.WebappGlobalNames.WEBAPP_SPRING_CONFIG_TEST_FILE; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.SessionFactory; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.navalplanner.business.common.exceptions.InstanceNotFoundException; +import org.navalplanner.business.costcategories.daos.ITypeOfWorkHoursDAO; +import org.navalplanner.business.costcategories.entities.TypeOfWorkHours; +import org.navalplanner.ws.common.api.InstanceConstraintViolationsDTO; +import org.navalplanner.ws.typeofworkhours.api.ITypeOfWorkHoursService; +import org.navalplanner.ws.typeofworkhours.api.TypeOfWorkHoursDTO; +import org.navalplanner.ws.typeofworkhours.api.TypeOfWorkHoursListDTO; +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; + +/** + * Tests for ITypeOfWorkHoursService. + * @author Susana Montes Pedreira + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = { BUSINESS_SPRING_CONFIG_FILE, + WEBAPP_SPRING_CONFIG_FILE, WEBAPP_SPRING_CONFIG_TEST_FILE }) +@Transactional +public class TypeOfWorkHoursServiceTest { + + @Autowired + private ITypeOfWorkHoursService typeOfWorkHoursService; + + @Autowired + private ITypeOfWorkHoursDAO typeOfWorkHoursDAO; + + @Autowired + private SessionFactory sessionFactory; + + @Test + public void testAddAndGetTypeOfWorkHours() { + + // Missing TypeOfWorkHours name. + TypeOfWorkHoursDTO cc1 = new TypeOfWorkHoursDTO(null, true, + new BigDecimal(5)); + // Valid TypeOfWorkHours DTO without hour cost + TypeOfWorkHoursDTO cc2 = new TypeOfWorkHoursDTO("codeB", "cc2", true, + new BigDecimal(5)); + + /* TypeOfWorkHours list. */ + TypeOfWorkHoursListDTO typeOfWorkHoursListDTO = createTypeOfWorkHoursListDTO( + cc1, cc2); + + List instanceConstraintViolationsList = typeOfWorkHoursService + .addTypeOfWorkHours(typeOfWorkHoursListDTO).instanceConstraintViolationsList; + + assertTrue(instanceConstraintViolationsList.toString(), + instanceConstraintViolationsList.size() == 1); + assertTrue(instanceConstraintViolationsList.get(0).constraintViolations + .toString(), + instanceConstraintViolationsList.get(0).constraintViolations + .size() == 1); // cc1 constraint violations. + + /* Test. */ + assertFalse(typeOfWorkHoursDAO.existsByCode(cc1.code)); + assertTrue(typeOfWorkHoursDAO.existsByCode(cc2.code)); + } + + @Test + public void testUpdateTypeOfWorkHours() throws InstanceNotFoundException { + + // First one it creates valid type of work hours + + TypeOfWorkHoursDTO cc1 = new TypeOfWorkHoursDTO("newTypeOfWorkHours", + true, new BigDecimal(5)); + TypeOfWorkHoursListDTO typeOfWorkHoursListDTO = createTypeOfWorkHoursListDTO(cc1); + + List instanceConstraintViolationsList = typeOfWorkHoursService + .addTypeOfWorkHours(typeOfWorkHoursListDTO).instanceConstraintViolationsList; + typeOfWorkHoursDAO.flush(); + + /* Test. */ + assertTrue(instanceConstraintViolationsList.toString(), + instanceConstraintViolationsList.size() == 0); + assertTrue(typeOfWorkHoursDAO.existsByCode(cc1.code)); + + TypeOfWorkHours typeOfWorkHours = null; + try { + typeOfWorkHours = typeOfWorkHoursDAO.findByCode(cc1.code); + assertTrue(typeOfWorkHours.getName().equalsIgnoreCase( + "newTypeOfWorkHours")); + assertTrue(typeOfWorkHours.getEnabled()); + assertTrue(typeOfWorkHours.getDefaultPrice().compareTo( + new BigDecimal(5)) == 0); + } catch (InstanceNotFoundException e) { + assertTrue(false); + } + typeOfWorkHoursDAO.flush(); + sessionFactory.getCurrentSession().evict(typeOfWorkHours); + typeOfWorkHours.dontPoseAsTransientObjectAnymore(); + + // Update the previous type of work hours + TypeOfWorkHoursDTO cc2 = new TypeOfWorkHoursDTO(cc1.code, "updateCC1", + false, new BigDecimal(100)); + + typeOfWorkHoursListDTO = createTypeOfWorkHoursListDTO(cc2); + + instanceConstraintViolationsList = typeOfWorkHoursService + .addTypeOfWorkHours(typeOfWorkHoursListDTO).instanceConstraintViolationsList; + + /* Test. */ + assertTrue(instanceConstraintViolationsList.toString(), + instanceConstraintViolationsList.size() == 0); + assertTrue(typeOfWorkHoursDAO.existsByCode(cc1.code)); + + // Check if the changes was updated + try { + typeOfWorkHours = typeOfWorkHoursDAO.findByCode(cc1.code); + assertTrue(typeOfWorkHours.getName().equalsIgnoreCase("updateCC1")); + assertFalse(typeOfWorkHours.getEnabled()); + assertTrue(typeOfWorkHours.getDefaultPrice().compareTo( + new BigDecimal(100)) == 0); + } catch (InstanceNotFoundException e) { + assertTrue(false); + } + } + + private TypeOfWorkHoursListDTO createTypeOfWorkHoursListDTO( + TypeOfWorkHoursDTO... typeOfWorkHours) { + + List typeOfWorkHoursList = new ArrayList(); + + for (TypeOfWorkHoursDTO c : typeOfWorkHours) { + typeOfWorkHoursList.add(c); + } + + return new TypeOfWorkHoursListDTO(typeOfWorkHoursList); + + } + +} diff --git a/scripts/rest-clients/README b/scripts/rest-clients/README index 3ec8f75c7..a64335c4b 100644 --- a/scripts/rest-clients/README +++ b/scripts/rest-clients/README @@ -42,6 +42,15 @@ - export-label-types.sh (authenticate with wsreader/wsreader) + * Import type of work hours: + + - import-type-work-hours.sh type-work-hours-sample.xml + (authenticate with wswriter/wswriter) + + * Export type of work hours: + + - export-type-work-hours.sh (authenticate with wsreader/wsreader) + * Import resources: - import-resources.sh resources-sample-mini.xml (or resources-sample.xml) diff --git a/scripts/rest-clients/export-type-work-hours.sh b/scripts/rest-clients/export-type-work-hours.sh new file mode 100755 index 000000000..a8d966bb1 --- /dev/null +++ b/scripts/rest-clients/export-type-work-hours.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +. ./rest-common-env.sh + +printf "Login name: " +read loginName +printf "Password: " +read password + +if [ "$1" = "--prod" ]; then + baseServiceURL=$PRODUCTION_BASE_SERVICE_URL + certificate=$PRODUCTION_CERTIFICATE +else + baseServiceURL=$DEVELOPMENT_BASE_SERVICE_URL + certificate=$DEVELOPMENT_CERTIFICATE +fi + +authorization=`./base64.sh $loginName:$password` + +curl -sv -X GET $certificate --header "Authorization: Basic $authorization" \ + $baseServiceURL/typeofworkhours | tidy -xml -i -q -utf8 diff --git a/scripts/rest-clients/import-type-work-hours.sh b/scripts/rest-clients/import-type-work-hours.sh new file mode 100755 index 000000000..0761b9385 --- /dev/null +++ b/scripts/rest-clients/import-type-work-hours.sh @@ -0,0 +1,33 @@ +#!/bin/sh + +. ./rest-common-env.sh + +printf "Login name: " +read loginName +printf "Password: " +read password + +baseServiceURL=$DEVELOPMENT_BASE_SERVICE_URL +certificate=$DEVELOPMENT_CERTIFICATE + +for i in "$@" +do + if [ "$i" = "--prod" ]; then + baseServiceURL=$PRODUCTION_BASE_SERVICE_URL + certificate=$PRODUCTION_CERTIFICATE + else + file=$i + fi +done + +if [ "$file" = "" ]; then + printf "Missing file\n" 1>&2 + exit 1 +fi + +authorization=`./base64.sh $loginName:$password` + +curl -sv -X POST $certificate -d @$file \ + --header "Content-type: application/xml" \ + --header "Authorization: Basic $authorization" \ + $baseServiceURL/typeofworkhours | tidy -xml -i -q -utf8 diff --git a/scripts/rest-clients/type-work-hours-sample.xml b/scripts/rest-clients/type-work-hours-sample.xml new file mode 100644 index 000000000..431965275 --- /dev/null +++ b/scripts/rest-clients/type-work-hours-sample.xml @@ -0,0 +1,9 @@ + + + + + +