ItEr39S17CUImportacionOrganizacionsTraballo: Created first version of service to import/export orders.

* Added mapper for InstanceNotFoundExcpetion in web service.
* Created utilities class with methods related with web services.
* Created a new package "org.navalplanner.ws.orders.api" with web service specification and implementation.
This commit is contained in:
Manuel Rego Casasnovas 2009-12-16 07:28:57 +01:00 committed by Javier Moran Rua
parent 7d5ca35e26
commit 0c158fc5cc
25 changed files with 889 additions and 98 deletions

View file

@ -83,8 +83,13 @@ public class BaseCalendarDAO extends GenericDAOHibernate<BaseCalendar, Long>
return new ArrayList<BaseCalendar>();
}
return findByName(baseCalendar.getName());
}
@Override
public List<BaseCalendar> findByName(String name) {
Criteria c = getSession().createCriteria(BaseCalendar.class);
c.add(Restrictions.eq("name", baseCalendar.getName()));
c.add(Restrictions.eq("name", name));
List<BaseCalendar> list = (List<BaseCalendar>) c.list();
removeResourceCalendarInstances(list);

View file

@ -38,6 +38,8 @@ public interface IBaseCalendarDAO extends IGenericDAO<BaseCalendar, Long> {
List<BaseCalendar> findByName(BaseCalendar baseCalendar);
List<BaseCalendar> findByName(String name);
boolean thereIsOtherWithSameName(BaseCalendar baseCalendar);
}

View file

@ -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 <ogonzalez@igalia.com>
* @author Fernando Bellas Permuy <fbellas@udc.es>
* @author Javier Moran Rua <jmoran@igalia.com>
* @author Diego Pino Garcia <dpino@igalia.com>
* @author Manuel Rego Casasnovas <mrego@igalia.com>
*/
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;
}
}

View file

@ -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;

View file

@ -179,7 +179,7 @@ public class Order extends OrderLineGroup {
}
public void setDependenciesConstraintsHavePriority(
boolean dependenciesConstraintsHavePriority) {
Boolean dependenciesConstraintsHavePriority) {
this.dependenciesConstraintsHavePriority = dependenciesConstraintsHavePriority;
}

View file

@ -18,7 +18,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.ws.resources.criterion.api;
package org.navalplanner.ws.common.api;
import javax.xml.bind.annotation.XmlEnum;

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <code>InstanceNotFoundException</code>.
*
* @author Fernando Bellas Permuy <fbellas@udc.es>
*/
@Provider
@Component("instanceNotFoundExceptionMapper")
public class InstanceNotFoundExceptionMapper implements
ExceptionMapper<InstanceNotFoundException> {
public Response toResponse(InstanceNotFoundException e) {
return Response.status(Response.Status.NOT_FOUND).entity(
new InternalErrorDTO(e.getMessage(), Util.getStackTrace(e)))
.type("application/xml").build();
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <fbellas@udc.es>
*/
public class ResourceEnumConverter {
private final static Map<ResourceEnum, ResourceEnumDTO> resourceEnumToDTO = new HashMap<ResourceEnum, ResourceEnumDTO>();
private final static Map<ResourceEnumDTO, ResourceEnum> resourceEnumFromDTO = new HashMap<ResourceEnumDTO, ResourceEnum>();
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;
}
}
}

View file

@ -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<RuntimeException> {
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();
}
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.ws.common.impl;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
* Utilities class related with web service.
*
* @author Manuel Rego Casasnovas <mrego@igalia.com>
*/
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();
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <mrego@igalia.com>
*/
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;
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <mrego@igalia.com>
*/
public interface IOrderElementService {
OrderElementDTO getOrderElement(Long id) throws InstanceNotFoundException;
InstanceConstraintViolationsListDTO addOrder(OrderDTO order);
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <mrego@igalia.com>
*/
@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<OrderElementDTO> children,
Boolean dependenciesConstraintsHavePriority, String calendarName) {
super(name, code, initDate, deadline, description, children);
this.dependenciesConstraintsHavePriority = dependenciesConstraintsHavePriority;
this.calendarName = calendarName;
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <mrego@igalia.com>
*/
@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;
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <mrego@igalia.com>
*/
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;
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <mrego@igalia.com>
*/
@XmlRootElement(name = "order-line")
public class OrderLineDTO extends OrderElementDTO {
@XmlElementWrapper(name = "hours-groups")
@XmlElement(name = "hours-group")
public Set<HoursGroupDTO> hoursGroups;
public OrderLineDTO() {
super();
}
public OrderLineDTO(String name, String code, Date initDate, Date deadline,
String description, Set<HoursGroupDTO> hoursGroups) {
super(name, code, initDate, deadline, description);
this.hoursGroups = hoursGroups;
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <mrego@igalia.com>
*/
@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<OrderElementDTO> children;
public OrderLineGroupDTO() {
super();
}
public OrderLineGroupDTO(String name, String code, Date initDate,
Date deadline, String description, List<OrderElementDTO> children) {
super(name, code, initDate, deadline, description);
this.children = children;
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
/**
* Specification of namespace for REST-based services.
*
* @author Manuel Rego Casasnovas <mrego@igalia.com>
*/
@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;

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <mrego@igalia.com>
*/
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<HoursGroupDTO> hoursGroups = new HashSet<HoursGroupDTO>();
for (HoursGroup hoursGroup : ((OrderLine) orderElement)
.getHoursGroups()) {
hoursGroups.add(toDTO(hoursGroup));
}
return new OrderLineDTO(name, code, initDate, deadline,
description, hoursGroups);
} else { // orderElement instanceof OrderLineGroup
List<OrderElementDTO> children = new ArrayList<OrderElementDTO>();
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<OrderElement> children = new ArrayList<OrderElement>();
for (OrderElementDTO element : ((OrderLineGroupDTO) orderElementDTO).children) {
children.add(toEntity(element));
}
if (orderElementDTO instanceof OrderDTO) {
orderElement = Order.create();
((Order) orderElement)
.setDependenciesConstraintsHavePriority(((OrderDTO) orderElementDTO).dependenciesConstraintsHavePriority);
List<BaseCalendar> 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;
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <mrego@igalia.com>
*/
@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<InstanceConstraintViolationsDTO> instanceConstraintViolationsList = new ArrayList<InstanceConstraintViolationsDTO>();
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);
}
}

View file

@ -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 <code>CriterionType</code> entity.
*

View file

@ -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<ResourceEnum, ResourceEnumDTO>
resourceEnumToDTO =
new HashMap<ResourceEnum, ResourceEnumDTO>();
private final static Map<ResourceEnumDTO, ResourceEnum>
resourceEnumFromDTO =
new HashMap<ResourceEnumDTO, ResourceEnum>();
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) {

View file

@ -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<ConstraintViolationDTO>());
}
@ -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;
}
}

View file

@ -46,9 +46,11 @@
<jaxrs:server id="rest" address="/">
<jaxrs:serviceBeans>
<ref bean="criterionServiceREST"/>
<ref bean="orderElementServiceREST"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="runtimeExceptionMapper" />
<ref bean="instanceNotFoundExceptionMapper" />
</jaxrs:providers>
<!-- FIXME: in root pom.xml, enable CXF logging on development and
disable it in production.

View file

@ -37,11 +37,11 @@ import org.navalplanner.business.common.IOnTransaction;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.ws.common.api.InstanceConstraintViolationsDTO;
import org.navalplanner.ws.common.api.InstanceConstraintViolationsListDTO;
import org.navalplanner.ws.common.api.ResourceEnumDTO;
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.ICriterionService;
import org.navalplanner.ws.resources.criterion.api.ResourceEnumDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.NotTransactional;
import org.springframework.test.context.ContextConfiguration;