Basic implementation of DELETE operation in order elements web service

FEA: ItEr77S13AllowDeleteOrderElements
This commit is contained in:
Manuel Rego Casasnovas 2012-11-05 14:11:05 +01:00
parent a8e57499af
commit 4ea3d95c96
3 changed files with 89 additions and 0 deletions

View file

@ -39,4 +39,6 @@ public interface IOrderElementService {
Response getOrderElement(String code);
Response removeOrderElement(String code);
}

View file

@ -22,17 +22,24 @@
package org.libreplan.ws.orders.impl;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
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 javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.libreplan.business.common.daos.IIntegrationEntityDAO;
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
import org.libreplan.business.common.exceptions.ValidationException;
import org.libreplan.business.orders.daos.IOrderDAO;
import org.libreplan.business.orders.daos.IOrderElementDAO;
import org.libreplan.business.orders.entities.Order;
import org.libreplan.business.orders.entities.OrderElement;
import org.libreplan.business.orders.entities.OrderLineGroup;
import org.libreplan.business.scenarios.IScenarioManager;
import org.libreplan.ws.common.api.InstanceConstraintViolationsListDTO;
import org.libreplan.ws.common.api.OrderDTO;
import org.libreplan.ws.common.impl.ConfigurationOrderElementConverter;
@ -60,6 +67,12 @@ public class OrderElementServiceREST extends
@Autowired
private IOrderDAO orderDAO;
@Autowired
private IOrderElementDAO orderElementDAO;
@Autowired
private IScenarioManager scenarioManager;
@Override
@GET
@Transactional(readOnly = true)
@ -108,4 +121,44 @@ public class OrderElementServiceREST extends
return getDTOByCode(code);
}
@Override
@DELETE
@Path("/{code}/")
@Transactional
public Response removeOrderElement(@PathParam("code") String code) {
try {
OrderElement orderElement = orderElementDAO.findByCode(code);
if (orderElement.isOrder()) {
orderDAO.remove(orderElement.getId());
} else {
Order order = orderDAO.loadOrderAvoidingProxyFor(orderElement);
order.useSchedulingDataFor(scenarioManager.getCurrent());
orderElement = findOrderElement(order, orderElement.getId());
OrderLineGroup parent = orderElement.getParent();
parent.remove(orderElement);
orderDAO.save(order);
}
return Response.ok().build();
} catch (InstanceNotFoundException e) {
return Response.status(Status.NOT_FOUND).build();
}
}
private OrderElement findOrderElement(OrderElement orderElement, Long id) {
if (orderElement.getId().equals(id)) {
return orderElement;
}
for (OrderElement child : orderElement.getChildren()) {
OrderElement found = findOrderElement(child, id);
if (found != null) {
return found;
}
}
return null;
}
}

View file

@ -0,0 +1,34 @@
#!/bin/sh
. ./rest-common-env.sh
printf "Username: "
read loginName
printf "Password: "
read password
code=$1
if [ "$1" = "--prod" ]; then
baseServiceURL=$PRODUCTION_BASE_SERVICE_URL
certificate=$PRODUCTION_CERTIFICATE
code=$2
elif [ "$1" = "--dev" ]; then
baseServiceURL=$DEVELOPMENT_BASE_SERVICE_URL
certificate=$DEVELOPMENT_CERTIFICATE
code=$2
else
baseServiceURL=$DEMO_BASE_SERVICE_URL
certificate=$DEMO_CERTIFICATE
fi
authorization=`echo -n "$loginName:$password" | base64`
result=`curl -sv -X DELETE $certificate --header "Authorization: Basic $authorization" \
$baseServiceURL/orderelements/$code`
if hash tidy &> /dev/null; then
echo $result | tidy -xml -i -q -utf8
else
echo $result
fi