diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/calendars/daos/BaseCalendarDAO.java b/navalplanner-business/src/main/java/org/navalplanner/business/calendars/daos/BaseCalendarDAO.java index b897e4a95..09086c6a8 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/calendars/daos/BaseCalendarDAO.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/calendars/daos/BaseCalendarDAO.java @@ -83,8 +83,13 @@ public class BaseCalendarDAO extends GenericDAOHibernate return new ArrayList(); } + return findByName(baseCalendar.getName()); + } + + @Override + public List findByName(String name) { Criteria c = getSession().createCriteria(BaseCalendar.class); - c.add(Restrictions.eq("name", baseCalendar.getName())); + c.add(Restrictions.eq("name", name)); List list = (List) c.list(); removeResourceCalendarInstances(list); diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/calendars/daos/IBaseCalendarDAO.java b/navalplanner-business/src/main/java/org/navalplanner/business/calendars/daos/IBaseCalendarDAO.java index 5f009aa7d..015f803b3 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/calendars/daos/IBaseCalendarDAO.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/calendars/daos/IBaseCalendarDAO.java @@ -38,6 +38,8 @@ public interface IBaseCalendarDAO extends IGenericDAO { List findByName(BaseCalendar baseCalendar); + List findByName(String name); + boolean thereIsOtherWithSameName(BaseCalendar baseCalendar); } diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/common/Registry.java b/navalplanner-business/src/main/java/org/navalplanner/business/common/Registry.java index 76f4dbde5..fcebec0dd 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/common/Registry.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/common/Registry.java @@ -21,6 +21,7 @@ package org.navalplanner.business.common; import org.navalplanner.business.advance.daos.IAdvanceTypeDAO; +import org.navalplanner.business.calendars.daos.IBaseCalendarDAO; import org.navalplanner.business.costcategories.daos.ITypeOfWorkHoursDAO; import org.navalplanner.business.materials.daos.IMaterialCategoryDAO; import org.navalplanner.business.materials.daos.IMaterialDAO; @@ -37,10 +38,12 @@ import org.springframework.beans.factory.annotation.Autowired; * to access DAOs. For the rest of classes (e.g. services, tests, etc.), Spring * DI is a more convenient option. The DAOs or services are added to the * registry as needed. + * * @author Óscar González Fernández * @author Fernando Bellas Permuy * @author Javier Moran Rua * @author Diego Pino Garcia + * @author Manuel Rego Casasnovas */ public class Registry { @@ -74,6 +77,9 @@ public class Registry { @Autowired private IQualityFormDAO qualityFormDAO; + @Autowired + private IBaseCalendarDAO baseCalendarDAO; + private Registry() { } @@ -116,4 +122,9 @@ public class Registry { public static IQualityFormDAO getQualityFormDAO() { return getInstance().qualityFormDAO; } + + public static IBaseCalendarDAO getBaseCalendarDAO() { + return getInstance().baseCalendarDAO; + } + } diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/HoursGroup.java b/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/HoursGroup.java index 343a5cc2a..5dd90a2d7 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/HoursGroup.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/HoursGroup.java @@ -43,6 +43,15 @@ public class HoursGroup extends BaseEntity implements Cloneable { return result; } + public static HoursGroup createUnvalidated(String name, + ResourceEnum resourceType, Integer workingHours) { + HoursGroup result = new HoursGroup(); + result.setName(name); + result.setResourceType(resourceType); + result.setWorkingHours(workingHours); + return create(result); + } + private String name; private ResourceEnum resourceType = ResourceEnum.WORKER; diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/Order.java b/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/Order.java index 0753241e1..38528fabd 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/Order.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/Order.java @@ -179,7 +179,7 @@ public class Order extends OrderLineGroup { } public void setDependenciesConstraintsHavePriority( - boolean dependenciesConstraintsHavePriority) { + Boolean dependenciesConstraintsHavePriority) { this.dependenciesConstraintsHavePriority = dependenciesConstraintsHavePriority; } 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/common/api/ResourceEnumDTO.java similarity index 95% rename from navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/api/ResourceEnumDTO.java rename to navalplanner-webapp/src/main/java/org/navalplanner/ws/common/api/ResourceEnumDTO.java index c4f899be6..f86f465d1 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/ws/resources/criterion/api/ResourceEnumDTO.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/api/ResourceEnumDTO.java @@ -18,7 +18,7 @@ * along with this program. If not, see . */ -package org.navalplanner.ws.resources.criterion.api; +package org.navalplanner.ws.common.api; import javax.xml.bind.annotation.XmlEnum; diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/impl/InstanceNotFoundExceptionMapper.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/impl/InstanceNotFoundExceptionMapper.java new file mode 100644 index 000000000..7ca3fccda --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/impl/InstanceNotFoundExceptionMapper.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.common.impl; + +import javax.management.InstanceNotFoundException; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +import org.navalplanner.ws.common.api.InternalErrorDTO; +import org.springframework.stereotype.Component; + +/** + * Exception mapper for InstanceNotFoundException. + * + * @author Fernando Bellas Permuy + */ +@Provider +@Component("instanceNotFoundExceptionMapper") +public class InstanceNotFoundExceptionMapper implements + ExceptionMapper { + + public Response toResponse(InstanceNotFoundException e) { + return Response.status(Response.Status.NOT_FOUND).entity( + new InternalErrorDTO(e.getMessage(), Util.getStackTrace(e))) + .type("application/xml").build(); + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/impl/ResourceEnumConverter.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/impl/ResourceEnumConverter.java new file mode 100644 index 000000000..e3921fd76 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/impl/ResourceEnumConverter.java @@ -0,0 +1,80 @@ +/* + * 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 static org.navalplanner.web.I18nHelper._; + +import java.util.HashMap; +import java.util.Map; + +import org.navalplanner.business.resources.entities.ResourceEnum; +import org.navalplanner.ws.common.api.ResourceEnumDTO; + +/** + * Converter from/to {@link ResourceEnum.type} entities to/from DTOs.. + * + * @author Fernando Bellas Permuy + */ +public class ResourceEnumConverter { + + + 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); + + resourceEnumToDTO.put(ResourceEnum.MACHINE, ResourceEnumDTO.MACHINE); + resourceEnumFromDTO.put(ResourceEnumDTO.MACHINE, ResourceEnum.MACHINE); + + } + + public final static ResourceEnumDTO toDTO(ResourceEnum resource) { + ResourceEnumDTO value = resourceEnumToDTO.get(resource); + + if (value == null) { + throw new RuntimeException(_("Unable to convert {0} " + + "value to {1} type", resource.toString(), + ResourceEnumDTO.class.getName())); + } else { + return value; + } + } + + public final static ResourceEnum fromDTO(ResourceEnumDTO resource) { + ResourceEnum value = resourceEnumFromDTO.get(resource); + + if (value == null) { + throw new RuntimeException(_("Unable to convert value to {0} type", + ResourceEnum.class.getName())); + } else { + return value; + } + } + +} 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 index 94c64227a..82f51a012 100644 --- 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 @@ -20,9 +20,6 @@ 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; @@ -41,19 +38,9 @@ public class RuntimeExceptionMapper implements ExceptionMapper { public Response toResponse(RuntimeException e) { - return Response.status(Response.Status.INTERNAL_SERVER_ERROR). - entity(new InternalErrorDTO(e.getMessage(), - getStackTrace(e))).type("application/xml").build(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( + new InternalErrorDTO(e.getMessage(), Util.getStackTrace(e))) + .type("application/xml").build(); } - private String getStackTrace(RuntimeException e) { - - StringWriter stringWriter = new StringWriter(); - - e.printStackTrace(new PrintWriter(stringWriter)); - - return stringWriter.toString(); - - } - -} +} \ No newline at end of file diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/impl/Util.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/impl/Util.java new file mode 100644 index 000000000..6e7ff0b4f --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/impl/Util.java @@ -0,0 +1,50 @@ +/* + * 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; + +/** + * Utilities class related with web service. + * + * @author Manuel Rego Casasnovas + */ +public class Util { + + public static String generateInstanceId(int instanceNumber, + String instanceIdentifier) { + String instanceId = instanceNumber + ""; + + if (instanceIdentifier != null && instanceIdentifier.length() >= 0) { + instanceId += " (" + instanceIdentifier + ")"; + } + + return instanceId; + } + + public static String getStackTrace(Exception e) { + StringWriter stringWriter = new StringWriter(); + e.printStackTrace(new PrintWriter(stringWriter)); + return stringWriter.toString(); + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/HoursGroupDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/HoursGroupDTO.java new file mode 100644 index 000000000..96fd9e2fc --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/HoursGroupDTO.java @@ -0,0 +1,54 @@ +/* + * 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.orders.api; + +import javax.xml.bind.annotation.XmlAttribute; + +import org.navalplanner.business.orders.entities.HoursGroup; +import org.navalplanner.ws.common.api.ResourceEnumDTO; + +/** + * DTO for {@link HoursGroup} entity. + * + * @author Manuel Rego Casasnovas + */ +public class HoursGroupDTO { + + @XmlAttribute(name = "code") + public String name; + + @XmlAttribute(name = "resource-type") + public ResourceEnumDTO resourceType; + + @XmlAttribute(name = "working-hours") + public Integer workingHours; + + public HoursGroupDTO() { + } + + public HoursGroupDTO(String name, ResourceEnumDTO resourceType, + Integer workingHours) { + this.name = name; + this.resourceType = resourceType; + this.workingHours = workingHours; + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/IOrderElementService.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/IOrderElementService.java new file mode 100644 index 000000000..1f42b7837 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/IOrderElementService.java @@ -0,0 +1,38 @@ +/* + * 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.orders.api; + +import org.navalplanner.business.common.exceptions.InstanceNotFoundException; +import org.navalplanner.business.orders.entities.OrderElement; +import org.navalplanner.ws.common.api.InstanceConstraintViolationsListDTO; + +/** + * Service for managing {@link OrderElement} entities. + * + * @author Manuel Rego Casasnovas + */ +public interface IOrderElementService { + + OrderElementDTO getOrderElement(Long id) throws InstanceNotFoundException; + + InstanceConstraintViolationsListDTO addOrder(OrderDTO order); + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/OrderDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/OrderDTO.java new file mode 100644 index 000000000..313465a99 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/OrderDTO.java @@ -0,0 +1,57 @@ +/* + * 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.orders.api; + +import java.util.Date; +import java.util.List; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; + +import org.navalplanner.business.orders.entities.Order; + +/** + * DTO for {@link Order} entity. + * + * @author Manuel Rego Casasnovas + */ +@XmlRootElement(name = "order") +public class OrderDTO extends OrderLineGroupDTO { + + @XmlAttribute(name = "dependencies-constraints-have-priority") + public Boolean dependenciesConstraintsHavePriority; + + @XmlAttribute(name = "calendar-name") + public String calendarName; + + public OrderDTO() { + super(); + } + + public OrderDTO(String name, String code, Date initDate, Date deadline, + String description, List children, + Boolean dependenciesConstraintsHavePriority, String calendarName) { + super(name, code, initDate, deadline, description, children); + this.dependenciesConstraintsHavePriority = dependenciesConstraintsHavePriority; + this.calendarName = calendarName; + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/OrderElementDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/OrderElementDTO.java new file mode 100644 index 000000000..72f775375 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/OrderElementDTO.java @@ -0,0 +1,65 @@ +/* + * 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.orders.api; + +import java.util.Date; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; + +import org.navalplanner.business.orders.entities.OrderElement; + +/** + * DTO for {@link OrderElement} entity. + * + * @author Manuel Rego Casasnovas + */ +@XmlRootElement(name = "order-element") +public class OrderElementDTO { + + @XmlAttribute + public String name; + + @XmlAttribute + public String code; + + @XmlAttribute(name = "init-date") + public Date initDate; + + @XmlAttribute + public Date deadline; + + @XmlAttribute + public String description; + + public OrderElementDTO() { + } + + public OrderElementDTO(String name, String code, Date initDate, + Date deadline, String description) { + this.name = name; + this.code = code; + this.initDate = initDate; + this.deadline = deadline; + this.description = description; + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/OrderElementHolderDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/OrderElementHolderDTO.java new file mode 100644 index 000000000..275175f61 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/OrderElementHolderDTO.java @@ -0,0 +1,48 @@ +/* + * 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.orders.api; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElements; + +import org.navalplanner.business.orders.entities.OrderElement; + +/** + * Holder for {@link OrderElement} DTO. + * + * @author Manuel Rego Casasnovas + */ +public class OrderElementHolderDTO { + + @XmlElements( { + @XmlElement(name = "order-line", type = OrderLineDTO.class), + @XmlElement(name = "order-line-group", type = OrderLineGroupDTO.class), + @XmlElement(name = "order", type = OrderDTO.class) }) + public OrderElementDTO orderElementDTO; + + public OrderElementHolderDTO() { + } + + public OrderElementHolderDTO(OrderElementDTO orderElementDTO) { + this.orderElementDTO = orderElementDTO; + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/OrderLineDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/OrderLineDTO.java new file mode 100644 index 000000000..5da8c2666 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/OrderLineDTO.java @@ -0,0 +1,54 @@ +/* + * 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.orders.api; + +import java.util.Date; +import java.util.Set; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; + +import org.navalplanner.business.orders.entities.OrderLine; + +/** + * DTO for {@link OrderLine} entity. + * + * @author Manuel Rego Casasnovas + */ +@XmlRootElement(name = "order-line") +public class OrderLineDTO extends OrderElementDTO { + + @XmlElementWrapper(name = "hours-groups") + @XmlElement(name = "hours-group") + public Set hoursGroups; + + public OrderLineDTO() { + super(); + } + + public OrderLineDTO(String name, String code, Date initDate, Date deadline, + String description, Set hoursGroups) { + super(name, code, initDate, deadline, description); + this.hoursGroups = hoursGroups; + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/OrderLineGroupDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/OrderLineGroupDTO.java new file mode 100644 index 000000000..1e6491682 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/OrderLineGroupDTO.java @@ -0,0 +1,58 @@ +/* + * 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.orders.api; + +import java.util.Date; +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlElements; +import javax.xml.bind.annotation.XmlRootElement; + +import org.navalplanner.business.orders.entities.OrderLineGroup; + +/** + * DTO for {@link OrderLineGroup} entity. + * + * @author Manuel Rego Casasnovas + */ +@XmlRootElement(name = "order-line-group") +public class OrderLineGroupDTO extends OrderElementDTO { + + @XmlElementWrapper(name = "children") + @XmlElements( { + @XmlElement(name = "order-line", type = OrderLineDTO.class), + @XmlElement(name = "order-line-group", type = OrderLineGroupDTO.class), + @XmlElement(name = "order", type = OrderDTO.class) }) + public List children; + + public OrderLineGroupDTO() { + super(); + } + + public OrderLineGroupDTO(String name, String code, Date initDate, + Date deadline, String description, List children) { + super(name, code, initDate, deadline, description); + this.children = children; + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/package-info.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/package-info.java new file mode 100644 index 000000000..e4c162752 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/api/package-info.java @@ -0,0 +1,29 @@ +/* + * 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 Manuel Rego Casasnovas + */ +@javax.xml.bind.annotation.XmlSchema(elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED, namespace = WSCommonGlobalNames.REST_NAMESPACE) +package org.navalplanner.ws.orders.api; + +import org.navalplanner.ws.common.api.WSCommonGlobalNames; diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/impl/OrderElementConverter.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/impl/OrderElementConverter.java new file mode 100644 index 000000000..b44afb7a5 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/impl/OrderElementConverter.java @@ -0,0 +1,155 @@ +/* + * 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.orders.impl; + +import java.util.ArrayList; +import java.util.Date; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.navalplanner.business.calendars.entities.BaseCalendar; +import org.navalplanner.business.common.Registry; +import org.navalplanner.business.orders.entities.HoursGroup; +import org.navalplanner.business.orders.entities.Order; +import org.navalplanner.business.orders.entities.OrderElement; +import org.navalplanner.business.orders.entities.OrderLine; +import org.navalplanner.business.orders.entities.OrderLineGroup; +import org.navalplanner.business.resources.entities.ResourceEnum; +import org.navalplanner.ws.common.api.ResourceEnumDTO; +import org.navalplanner.ws.common.impl.ResourceEnumConverter; +import org.navalplanner.ws.orders.api.HoursGroupDTO; +import org.navalplanner.ws.orders.api.OrderDTO; +import org.navalplanner.ws.orders.api.OrderElementDTO; +import org.navalplanner.ws.orders.api.OrderLineDTO; +import org.navalplanner.ws.orders.api.OrderLineGroupDTO; + +/** + * Converter from/to {@link OrderElement} entities to/from DTOs. + * + * @author Manuel Rego Casasnovas + */ +public final class OrderElementConverter { + + private OrderElementConverter() { + } + + public final static OrderElementDTO toDTO(OrderElement orderElement) { + String name = orderElement.getName(); + String code = orderElement.getCode(); + Date initDate = orderElement.getInitDate(); + Date deadline = orderElement.getDeadline(); + String description = orderElement.getDescription(); + + if (orderElement instanceof OrderLine) { + Set hoursGroups = new HashSet(); + for (HoursGroup hoursGroup : ((OrderLine) orderElement) + .getHoursGroups()) { + hoursGroups.add(toDTO(hoursGroup)); + } + + return new OrderLineDTO(name, code, initDate, deadline, + description, hoursGroups); + } else { // orderElement instanceof OrderLineGroup + List children = new ArrayList(); + for (OrderElement element : orderElement.getChildren()) { + children.add(toDTO(element)); + } + + if (orderElement instanceof Order) { + Boolean dependenciesConstraintsHavePriority = ((Order) orderElement) + .getDependenciesConstraintsHavePriority(); + BaseCalendar calendar = ((Order) orderElement).getCalendar(); + String calendarName = null; + if (calendar != null) { + calendarName = calendar.getName(); + } + + return new OrderDTO(name, code, initDate, deadline, + description, children, + dependenciesConstraintsHavePriority, calendarName); + } else { // orderElement instanceof OrderLineGroup + return new OrderLineGroupDTO(name, code, initDate, deadline, + description, children); + } + } + } + + public final static HoursGroupDTO toDTO(HoursGroup hoursGroup) { + ResourceEnumDTO resourceType = ResourceEnumConverter.toDTO(hoursGroup + .getResourceType()); + return new HoursGroupDTO(hoursGroup.getName(), resourceType, hoursGroup + .getWorkingHours()); + } + + public final static OrderElement toEntity(OrderElementDTO orderElementDTO) { + OrderElement orderElement; + + if (orderElementDTO instanceof OrderLineDTO) { + orderElement = OrderLine.create(); + + for (HoursGroupDTO hoursGroupDTO : ((OrderLineDTO) orderElementDTO).hoursGroups) { + HoursGroup hoursGroup = toEntity(hoursGroupDTO); + ((OrderLine) orderElement).addHoursGroup(hoursGroup); + } + } else { // orderElementDTO instanceof OrderLineGroupDTO + List children = new ArrayList(); + for (OrderElementDTO element : ((OrderLineGroupDTO) orderElementDTO).children) { + children.add(toEntity(element)); + } + + if (orderElementDTO instanceof OrderDTO) { + orderElement = Order.create(); + + ((Order) orderElement) + .setDependenciesConstraintsHavePriority(((OrderDTO) orderElementDTO).dependenciesConstraintsHavePriority); + List calendars = Registry.getBaseCalendarDAO() + .findByName(((OrderDTO) orderElementDTO).calendarName); + if ((calendars != null) && (calendars.size() == 1)) { + ((Order) orderElement).setCalendar(calendars.get(0)); + } + } else { // orderElementDTO instanceof OrderLineGroupDTO + orderElement = OrderLineGroup.create(); + } + + for (OrderElement child : children) { + ((OrderLineGroup) orderElement).add(child); + } + } + + orderElement.setName(orderElementDTO.name); + orderElement.setCode(orderElementDTO.code); + orderElement.setInitDate(orderElementDTO.initDate); + orderElement.setDeadline(orderElementDTO.deadline); + orderElement.setDescription(orderElementDTO.description); + + return orderElement; + } + + private static HoursGroup toEntity(HoursGroupDTO hoursGroupDTO) { + ResourceEnum resourceType = ResourceEnumConverter + .fromDTO(hoursGroupDTO.resourceType); + HoursGroup hoursGroup = HoursGroup.createUnvalidated( + hoursGroupDTO.name, resourceType, hoursGroupDTO.workingHours); + return hoursGroup; + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/impl/OrderElementServiceREST.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/impl/OrderElementServiceREST.java new file mode 100644 index 000000000..8b28554ca --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/orders/impl/OrderElementServiceREST.java @@ -0,0 +1,107 @@ +/* + * 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.orders.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.PathParam; +import javax.ws.rs.Produces; + +import org.navalplanner.business.common.exceptions.InstanceNotFoundException; +import org.navalplanner.business.common.exceptions.ValidationException; +import org.navalplanner.business.orders.daos.IOrderDAO; +import org.navalplanner.business.orders.daos.IOrderElementDAO; +import org.navalplanner.business.orders.entities.OrderElement; +import org.navalplanner.ws.common.api.InstanceConstraintViolationsDTO; +import org.navalplanner.ws.common.api.InstanceConstraintViolationsListDTO; +import org.navalplanner.ws.common.impl.ConstraintViolationConverter; +import org.navalplanner.ws.common.impl.Util; +import org.navalplanner.ws.orders.api.IOrderElementService; +import org.navalplanner.ws.orders.api.OrderDTO; +import org.navalplanner.ws.orders.api.OrderElementDTO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * REST-based implementation of {@link IOrderElementService}. + * + * @author Manuel Rego Casasnovas + */ +@Path("/orderelements/") +@Produces("application/xml") +@Service("orderElementServiceREST") +public class OrderElementServiceREST implements IOrderElementService { + + @Autowired + private IOrderElementDAO orderElementDAO; + + @Autowired + private IOrderDAO orderDAO; + + @Override + @GET + @Path("/{id}") + @Transactional(readOnly = true) + public OrderElementDTO getOrderElement(@PathParam("id") Long id) + throws InstanceNotFoundException { + try { + return OrderElementConverter.toDTO(orderElementDAO.find(id)); + } catch (InstanceNotFoundException e) { + // FIXME it should throw a InstanceNotFoundException + throw new RuntimeException(e); + } + } + + @Override + @POST + @Consumes("application/xml") + @Transactional + public InstanceConstraintViolationsListDTO addOrder(OrderDTO orderDTO) { + + List instanceConstraintViolationsList = new ArrayList(); + + OrderElement orderElement = OrderElementConverter.toEntity(orderDTO); + InstanceConstraintViolationsDTO instanceConstraintViolationsDTO = null; + try { + orderElement.validate(); + orderElementDAO.save(orderElement); + } catch (ValidationException e) { + instanceConstraintViolationsDTO = ConstraintViolationConverter + .toDTO(Util.generateInstanceId(1, orderDTO.code), e + .getInvalidValues()); + } + + if (instanceConstraintViolationsDTO != null) { + instanceConstraintViolationsList + .add(instanceConstraintViolationsDTO); + } + + return new InstanceConstraintViolationsListDTO( + instanceConstraintViolationsList); + } + +} 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 index 8839debcf..2b8e7211f 100644 --- 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 @@ -27,6 +27,8 @@ import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; +import org.navalplanner.ws.common.api.ResourceEnumDTO; + /** * DTO for CriterionType entity. * 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 index 7424ac133..ef32aae77 100644 --- 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 @@ -20,21 +20,16 @@ package org.navalplanner.ws.resources.criterion.impl; -import static org.navalplanner.web.I18nHelper._; - 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.common.impl.ResourceEnumConverter; 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. @@ -43,33 +38,6 @@ import org.navalplanner.ws.resources.criterion.api.ResourceEnumDTO; */ 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); - - resourceEnumToDTO.put(ResourceEnum.MACHINE, - ResourceEnumDTO.MACHINE); - resourceEnumFromDTO.put(ResourceEnumDTO.MACHINE, - ResourceEnum.MACHINE); - - } - private CriterionConverter() {} public final static CriterionTypeListDTO toDTO( @@ -106,7 +74,8 @@ public final class CriterionConverter { criterionType.allowHierarchy(), criterionType.isAllowSimultaneousCriterionsPerResource(), criterionType.isEnabled(), - toDTO(criterionType.getResource()), + ResourceEnumConverter + .toDTO(criterionType.getResource()), criterionDTOs); } @@ -128,21 +97,6 @@ public final class CriterionConverter { } - - public final static ResourceEnumDTO toDTO(ResourceEnum resource) { - - ResourceEnumDTO value = resourceEnumToDTO.get(resource); - - if (value == null) { - throw new RuntimeException(_("Unable to convert {0} " + - "value to {1} type", resource.toString(), - ResourceEnumDTO.class.getName())); - } else { - return value; - } - - } - public final static CriterionType toEntity( CriterionTypeDTO criterionTypeDTO) { @@ -151,7 +105,8 @@ public final class CriterionConverter { criterionTypeDTO.allowHierarchy, criterionTypeDTO.allowSimultaneousCriterionsPerResource, criterionTypeDTO.enabled, - CriterionConverter.fromDTO(criterionTypeDTO.resource)); + ResourceEnumConverter + .fromDTO(criterionTypeDTO.resource)); for (CriterionDTO criterionDTO : criterionTypeDTO.criterions) { addCriterion(criterionType, criterionDTO, null); @@ -161,19 +116,6 @@ public final class CriterionConverter { } - private static ResourceEnum fromDTO(ResourceEnumDTO resource) { - - ResourceEnum value = resourceEnumFromDTO.get(resource); - - if (value == null) { - throw new RuntimeException(_("Unable to convert value to {0} type", - ResourceEnum.class.getName())); - } else { - return value; - } - - } - private static Criterion addCriterion(CriterionType criterionType, CriterionDTO criterionDTO, Criterion criterionParent) { 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 index bd05cf77a..53c20467a 100644 --- 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 @@ -40,6 +40,7 @@ import org.navalplanner.ws.common.api.ConstraintViolationDTO; import org.navalplanner.ws.common.api.InstanceConstraintViolationsDTO; import org.navalplanner.ws.common.api.InstanceConstraintViolationsListDTO; import org.navalplanner.ws.common.impl.ConstraintViolationConverter; +import org.navalplanner.ws.common.impl.Util; import org.navalplanner.ws.resources.criterion.api.CriterionTypeDTO; import org.navalplanner.ws.resources.criterion.api.CriterionTypeListDTO; import org.navalplanner.ws.resources.criterion.api.ICriterionService; @@ -115,7 +116,7 @@ public class CriterionServiceREST implements ICriterionService { } catch (ValidationException e) { instanceConstraintViolationsDTO = ConstraintViolationConverter.toDTO( - generateInstanceId(instanceNumber, + Util.generateInstanceId(instanceNumber, criterionTypeDTO.name), e.getInvalidValues()); } @@ -128,7 +129,7 @@ public class CriterionServiceREST implements ICriterionService { if (instanceConstraintViolationsDTO == null) { instanceConstraintViolationsDTO = new InstanceConstraintViolationsDTO( - generateInstanceId(instanceNumber, + Util.generateInstanceId(instanceNumber, criterionTypeDTO.name), new ArrayList()); } @@ -161,16 +162,4 @@ public class CriterionServiceREST implements ICriterionService { } - private String generateInstanceId(int instanceNumber, String name) { - - String instanceId = instanceNumber + ""; - - if (name != null && name.length() >= 0) { - instanceId += " (" + name + ")"; - } - - return instanceId; - - } - } 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 dfa2b997f..4fe79f7fb 100644 --- a/navalplanner-webapp/src/main/resources/navalplanner-webapp-spring-config.xml +++ b/navalplanner-webapp/src/main/resources/navalplanner-webapp-spring-config.xml @@ -46,9 +46,11 @@ + +