diff --git a/navalplanner-webapp/pom.xml b/navalplanner-webapp/pom.xml index 55afa9d86..d3e94e9e5 100644 --- a/navalplanner-webapp/pom.xml +++ b/navalplanner-webapp/pom.xml @@ -116,5 +116,19 @@ javax.servlet servlet-api + + + javax.ws.rs + jsr311-api + + + + org.apache.cxf + cxf-rt-transports-http + + + org.apache.cxf + cxf-rt-frontend-jaxrs + \ No newline at end of file diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/api/WSError.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/api/WSError.java new file mode 100644 index 000000000..5ff2f8732 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/api/WSError.java @@ -0,0 +1,55 @@ +/* + * 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.ws.common.api; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * It represents an individual error in the service response. + * + * @author Fernando Bellas Permuy + */ +@XmlRootElement(name="error") +public class WSError { + + @XmlAttribute + public String instanceId; + + @XmlAttribute + public WSErrorType errorType; + + public String explanation; + + public WSError() {} + + public WSError(WSErrorType errorType, String explanation) { + this.errorType = errorType; + this.explanation = explanation; + } + + public WSError(String instanceId, WSErrorType errorType, String explanation) { + this.instanceId = instanceId; + this.errorType = errorType; + this.explanation = explanation; + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/api/WSErrorList.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/api/WSErrorList.java new file mode 100644 index 000000000..fdd473cfa --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/api/WSErrorList.java @@ -0,0 +1,46 @@ +/* + * 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.ws.common.api; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * It represents list of errors in the service response. + * + * @author Fernando Bellas Permuy + */ +@XmlRootElement(name="errors") +public class WSErrorList { + + @XmlElement(name="error") + public List errors = new ArrayList(); + + public WSErrorList() {} + + public WSErrorList(List errors) { + this.errors = errors; + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/api/WSErrorType.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/api/WSErrorType.java new file mode 100644 index 000000000..8264c075f --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/api/WSErrorType.java @@ -0,0 +1,34 @@ +/* + * 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.ws.common.api; + +import javax.xml.bind.annotation.XmlEnum; + +/** + * Enumeration type for the types of errors in WSError. + * + * @author Fernando Bellas Permuy + */ +@XmlEnum +public enum WSErrorType { + SERVICE_INTERNAL_ERROR, + VALIDATION_ERROR +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/api/WSGlobalNames.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/api/WSGlobalNames.java new file mode 100644 index 000000000..c0d24fb3f --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/api/WSGlobalNames.java @@ -0,0 +1,35 @@ +/* + * 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.ws.common.api; + +/** + * Global names. + * + * @author Fernando Bellas Permuy + */ +public class WSGlobalNames { + + public final static String REST_NAMESPACE = + "http://rest.ws.navalplanner.org"; + + private WSGlobalNames() {} + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/api/package-info.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/api/package-info.java new file mode 100644 index 000000000..fc0cf5103 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/api/package-info.java @@ -0,0 +1,30 @@ +/* + * 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 . + */ + +/** + * Specification of namespace for REST-based services. + * + * @author Fernando Bellas Permuy + */ +@javax.xml.bind.annotation.XmlSchema( + elementFormDefault=javax.xml.bind.annotation.XmlNsForm.QUALIFIED, + namespace=WSGlobalNames.REST_NAMESPACE +) +package org.navalplanner.ws.common.api; \ No newline at end of file diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/impl/RuntimeExceptionMapper.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/impl/RuntimeExceptionMapper.java new file mode 100644 index 000000000..38094015d --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/impl/RuntimeExceptionMapper.java @@ -0,0 +1,62 @@ +/* + * 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.ws.common.impl; + +import java.io.PrintWriter; +import java.io.StringWriter; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +import org.navalplanner.ws.common.api.WSError; +import org.navalplanner.ws.common.api.WSErrorType; +import org.springframework.stereotype.Component; + +/** + * Exception mapper for RuntimeException. + * + * @author Fernando Bellas Permuy + */ +@Provider +@Component("runtimeExceptionMapper") +public class RuntimeExceptionMapper + implements ExceptionMapper { + + public Response toResponse(RuntimeException e) { + return Response.status(Response.Status.INTERNAL_SERVER_ERROR). + entity(new WSError(WSErrorType.SERVICE_INTERNAL_ERROR, + getStackTrace(e))). + type("application/xml"). + build(); + } + + private String getStackTrace(RuntimeException e) { + + StringWriter stringWriter = new StringWriter(); + + e.printStackTrace(new PrintWriter(stringWriter)); + + return stringWriter.toString(); + + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/api/CriterionDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/api/CriterionDTO.java new file mode 100644 index 000000000..d8bbe767b --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/api/CriterionDTO.java @@ -0,0 +1,60 @@ +/* + * 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.ws.resources.criterion.api; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * DTO for Criterion entity. + * + * @author Fernando Bellas Permuy + */ +@XmlRootElement(name="criterion") +public class CriterionDTO { + + @XmlAttribute + public String name; + + @XmlAttribute + public boolean active = true; + + @XmlElementWrapper(name="children") + @XmlElement(name="criterion") + public List children = new ArrayList(); + + public CriterionDTO() {} + + public CriterionDTO(String name, boolean active, + List children) { + + this.name = name; + this.active = active; + this.children = children; + + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/api/CriterionTypeDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/api/CriterionTypeDTO.java new file mode 100644 index 000000000..05d65f70f --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/api/CriterionTypeDTO.java @@ -0,0 +1,79 @@ +/* + * 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.ws.resources.criterion.api; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * DTO for CriterionType entity. + * + * @author Fernando Bellas Permuy + */ +@XmlRootElement(name="criterion-type") +public class CriterionTypeDTO { + + @XmlAttribute + public String name; + + @XmlAttribute + public String description; + + @XmlAttribute + public boolean allowHierarchy = true; + + @XmlAttribute + public boolean allowSimultaneousCriterionsPerResource = true; + + @XmlAttribute + public boolean enabled = true; + + @XmlAttribute + public ResourceEnumDTO resource = ResourceEnumDTO.RESOURCE; + + @XmlElementWrapper(name="criterion-list") + @XmlElement(name="criterion") + public List criterions = new ArrayList(); + + public CriterionTypeDTO() {} + + public CriterionTypeDTO(String name, String description, + boolean allowHierarchy, boolean allowSimultaneousCriterionsPerResource, + boolean enabled, ResourceEnumDTO resource, + List criterions) { + + this.name = name; + this.description = description; + this.allowHierarchy = allowHierarchy; + this.allowSimultaneousCriterionsPerResource = + allowSimultaneousCriterionsPerResource; + this.enabled = enabled; + this.resource = resource; + this.criterions = criterions; + + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/api/CriterionTypeListDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/api/CriterionTypeListDTO.java new file mode 100644 index 000000000..33929e53f --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/api/CriterionTypeListDTO.java @@ -0,0 +1,47 @@ +/* + * 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.ws.resources.criterion.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 CriterionType entities. + * + * @author Fernando Bellas Permuy + */ +@XmlRootElement(name="criterion-type-list") +public class CriterionTypeListDTO { + + @XmlElement(name="criterion-type") + public List criterionTypes = + new ArrayList(); + + public CriterionTypeListDTO() {} + + public CriterionTypeListDTO(List criterionTypes) { + this.criterionTypes = criterionTypes; + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/api/ICriterionService.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/api/ICriterionService.java new file mode 100644 index 000000000..350035e13 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/api/ICriterionService.java @@ -0,0 +1,36 @@ +/* + * 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.ws.resources.criterion.api; + +import org.navalplanner.ws.common.api.WSErrorList; + +/** + * Service for managing criterion-related entities. + * + * @author Fernando Bellas Permuy + */ +public interface ICriterionService { + + public CriterionTypeListDTO getCriterionTypes(); + + public WSErrorList addCriterionTypes(CriterionTypeListDTO criterionTypes); + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/api/ResourceEnumDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/api/ResourceEnumDTO.java new file mode 100644 index 000000000..1213e3b6b --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/api/ResourceEnumDTO.java @@ -0,0 +1,34 @@ +/* + * 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.ws.resources.criterion.api; + +import javax.xml.bind.annotation.XmlEnum; + +/** + * DTO for ResourceEnum entity. + * + * @author Fernando Bellas Permuy + */ +@XmlEnum +public enum ResourceEnumDTO { + RESOURCE, + WORKER +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/api/package-info.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/api/package-info.java new file mode 100644 index 000000000..7a3facd7d --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/api/package-info.java @@ -0,0 +1,31 @@ +/* + * 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 . + */ + +/** + * Specification of namespace for REST-based services. + * + * @author Fernando Bellas Permuy + */ +@javax.xml.bind.annotation.XmlSchema( + elementFormDefault=javax.xml.bind.annotation.XmlNsForm.QUALIFIED, + namespace=WSGlobalNames.REST_NAMESPACE +) +package org.navalplanner.ws.resources.criterion.api; +import org.navalplanner.ws.common.api.WSGlobalNames; diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/impl/CriterionConverter.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/impl/CriterionConverter.java new file mode 100644 index 000000000..f47990531 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/impl/CriterionConverter.java @@ -0,0 +1,199 @@ +/* + * 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.ws.resources.criterion.impl; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.navalplanner.business.resources.entities.Criterion; +import org.navalplanner.business.resources.entities.CriterionType; +import org.navalplanner.business.resources.entities.ResourceEnum; +import org.navalplanner.ws.resources.criterion.api.CriterionDTO; +import org.navalplanner.ws.resources.criterion.api.CriterionTypeDTO; +import org.navalplanner.ws.resources.criterion.api.CriterionTypeListDTO; +import org.navalplanner.ws.resources.criterion.api.ResourceEnumDTO; + +/** + * Converter from/to criterion-related entities to/from DTOs. + * + * @author Fernando Bellas Permuy + */ +public final class CriterionConverter { + + private final static Map + resourceEnumToDTO = + new HashMap(); + + private final static Map + resourceEnumFromDTO = + new HashMap(); + + static { + + resourceEnumToDTO.put(ResourceEnum.RESOURCE, + ResourceEnumDTO.RESOURCE); + resourceEnumFromDTO.put(ResourceEnumDTO.RESOURCE, + ResourceEnum.RESOURCE); + + resourceEnumToDTO.put(ResourceEnum.WORKER, + ResourceEnumDTO.WORKER); + resourceEnumFromDTO.put(ResourceEnumDTO.WORKER, + ResourceEnum.WORKER); + + } + + private CriterionConverter() {} + + public final static CriterionTypeListDTO toDTO( + Collection criterionTypes) { + + List criterionTypeDTOs = + new ArrayList(); + + for (CriterionType c : criterionTypes) { + criterionTypeDTOs.add(toDTO(c)); + } + + return new CriterionTypeListDTO(criterionTypeDTOs); + + } + + public final static CriterionTypeDTO toDTO(CriterionType criterionType) { + + List criterionDTOs = new ArrayList(); + + for (Criterion c : criterionType.getCriterions()) { + if (c.getParent() == null) { + criterionDTOs.add(toDTO(c)); + } + } + + if (criterionDTOs.isEmpty()) { + criterionDTOs = null; + } + + return new CriterionTypeDTO( + criterionType.getName(), + criterionType.getDescription(), + criterionType.allowHierarchy(), + criterionType.isAllowSimultaneousCriterionsPerResource(), + criterionType.isEnabled(), + toDTO(criterionType.resource()), + criterionDTOs); + + } + + public final static CriterionDTO toDTO(Criterion criterion) { + + List childrenDTOs = new ArrayList(); + + for (Criterion c : criterion.getChildren()) { + childrenDTOs.add(toDTO(c)); + } + + if (childrenDTOs.isEmpty()) { + childrenDTOs = null; + } + + return new CriterionDTO(criterion.getName(), criterion.isActive(), + childrenDTOs); + + } + + + public final static ResourceEnumDTO toDTO(ResourceEnum resource) { + + ResourceEnumDTO value = resourceEnumToDTO.get(resource); + + if (value == null) { + throw new RuntimeException("Unable to convert '" + + resource.toString() + "' value to ResourceEnumDTO"); + } else { + return value; + } + + } + + public final static CriterionType toEntity( + CriterionTypeDTO criterionTypeDTO) { + + CriterionType criterionType = CriterionType.create( + criterionTypeDTO.name, + criterionTypeDTO.description, + criterionTypeDTO.allowHierarchy, + criterionTypeDTO.allowSimultaneousCriterionsPerResource, + criterionTypeDTO.enabled, + CriterionConverter.fromDTO(criterionTypeDTO.resource)); + + for (CriterionDTO criterionDTO : criterionTypeDTO.criterions) { + addCriterion(criterionType, criterionDTO, null); + } + + return criterionType; + + } + + private static ResourceEnum fromDTO(ResourceEnumDTO resource) { + + ResourceEnum value = resourceEnumFromDTO.get(resource); + + if (value == null) { + throw new RuntimeException("Unable to convert '" + + resource.toString() + "' value to ResourceEnum"); + } else { + return value; + } + + } + + private static Criterion addCriterion(CriterionType criterionType, + CriterionDTO criterionDTO, Criterion criterionParent) { + + Criterion criterion = toEntityWithoutChildren(criterionDTO, + criterionType, criterionParent); + criterionType.getCriterions().add(criterion); + + for (CriterionDTO childDTO : criterionDTO.children) { + Criterion child = addCriterion(criterionType, childDTO, criterion); + criterion.getChildren().add(child); + } + + return criterion; + + } + + private static Criterion toEntityWithoutChildren( + CriterionDTO childDTO, CriterionType criterionType, + Criterion criterionParent) { + + Criterion criterion = Criterion.create(childDTO.name, criterionType); + + criterion.setActive(childDTO.active); + criterion.setParent(criterionParent); + + return criterion; + + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/impl/CriterionServiceREST.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/impl/CriterionServiceREST.java new file mode 100644 index 000000000..e554f2c6b --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/impl/CriterionServiceREST.java @@ -0,0 +1,95 @@ +/* + * 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.ws.resources.criterion.impl; + +import java.util.ArrayList; +import java.util.List; + +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.resources.daos.CriterionTypeDAO; +import org.navalplanner.ws.common.api.WSError; +import org.navalplanner.ws.common.api.WSErrorList; +import org.navalplanner.ws.common.api.WSErrorType; +import org.navalplanner.ws.resources.criterion.api.CriterionTypeDTO; +import org.navalplanner.ws.resources.criterion.api.CriterionTypeListDTO; +import org.navalplanner.ws.resources.criterion.api.ICriterionService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * REST-based implementation of ICriterionService. + * + * @author Fernando Bellas Permuy + */ +@Path("/criteriontypes/") +@Produces("application/xml") +@Service("criterionServiceREST") +public class CriterionServiceREST implements ICriterionService { + + @Autowired + private CriterionTypeDAO criterionTypeDAO; + + @Override + @GET + @Transactional(readOnly = true) + public CriterionTypeListDTO getCriterionTypes() { + return CriterionConverter.toDTO(criterionTypeDAO.getCriterionTypes()); + } + + @Override + @POST + @Consumes("application/xml") + @Transactional + public WSErrorList addCriterionTypes(CriterionTypeListDTO criterionTypes) { + + List errorList = new ArrayList(); + + for (CriterionTypeDTO criterionTypeDTO : + criterionTypes.criterionTypes) { + + try { + + criterionTypeDAO.save( + CriterionConverter.toEntity(criterionTypeDTO)); + + } catch (Exception e) { + e.printStackTrace(System.out); + errorList.add(new WSError(criterionTypeDTO.name, + WSErrorType.VALIDATION_ERROR, e.getMessage())); + } + + } + + if (errorList.isEmpty()) { + return null; + } else { + return new WSErrorList(errorList); + } + + } + +} 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 d24f3a4ac..f2607f770 100644 --- a/navalplanner-webapp/src/main/resources/navalplanner-webapp-spring-config.xml +++ b/navalplanner-webapp/src/main/resources/navalplanner-webapp-spring-config.xml @@ -1,12 +1,20 @@ + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd + http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd + http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd + http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> + + + + + + + + + + + + + + + + diff --git a/navalplanner-webapp/src/main/webapp/WEB-INF/web.xml b/navalplanner-webapp/src/main/webapp/WEB-INF/web.xml index d42116b48..1de262dbc 100644 --- a/navalplanner-webapp/src/main/webapp/WEB-INF/web.xml +++ b/navalplanner-webapp/src/main/webapp/WEB-INF/web.xml @@ -89,6 +89,20 @@ + + + CXFServlet + CXF Servlet + + org.apache.cxf.transport.servlet.CXFServlet + + 1 + + + CXFServlet + /ws/rest/* + + /planner/main.zul diff --git a/pom.xml b/pom.xml index b0c2b1a5b..b7814602d 100644 --- a/pom.xml +++ b/pom.xml @@ -387,6 +387,55 @@ jar provided + + + javax.ws.rs + jsr311-api + 1.0 + + + + org.apache.cxf + cxf-rt-transports-http + 2.2.3 + + + org.springframework + spring-core + + + org.springframework + spring-beans + + + org.springframework + spring-context + + + org.springframework + spring-web + + + + + org.apache.cxf + cxf-rt-frontend-jaxrs + 2.2.3 + + + org.springframework + spring-core + + + org.springframework + spring-beans + + + org.springframework + spring-context + + +