package com.temenos.interaction.commands.odata;
/*
* #%L
* interaction-commands-odata
* %%
* Copyright (C) 2012 - 2013 Temenos Holdings N.V.
* %%
* 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 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/>.
* #L%
*/
import javax.ws.rs.core.Response.Status;
import org.odata4j.core.OEntityKey;
import org.odata4j.edm.EdmEntitySet;
import org.odata4j.exceptions.ODataProducerException;
import org.odata4j.producer.ODataProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.temenos.interaction.core.command.InteractionCommand;
import com.temenos.interaction.core.command.InteractionContext;
import com.temenos.interaction.core.command.InteractionException;
public class DeleteEntityCommand extends AbstractODataCommand implements InteractionCommand {
private final Logger logger = LoggerFactory.getLogger(DeleteEntityCommand.class);
public DeleteEntityCommand(ODataProducer producer) {
super(producer);
}
protected ODataProducer getProducer() {
return producer;
}
/* Implement InteractionCommand interface */
@Override
public Result execute(InteractionContext ctx) throws InteractionException {
assert(ctx != null);
assert(ctx.getCurrentState() != null);
assert(ctx.getCurrentState().getEntityName() != null && !ctx.getCurrentState().getEntityName().equals(""));
assert(ctx.getResource() == null);
String entity = getEntityName(ctx);
logger.debug("Deleting entity for " + entity);
OEntityKey key = null;
try {
EdmEntitySet entitySet = getEdmEntitySet(entity);
if (entitySet == null) {
throw new InteractionException(Status.NOT_FOUND, "Entity set not found [" + entity + "]");
}
assert(entity.equals(entitySet.getName()));
// Create entity key (simple types only)
try {
key = CommandHelper.createEntityKey(getEdmMetadata(), entity, ctx.getId());
} catch (Exception e) {
throw new InteractionException(Status.INTERNAL_SERVER_ERROR, e);
}
// delete the entity
producer.deleteEntity(entity, key);
}
catch(ODataProducerException ope) {
logger.debug("Failed to delete entity [" + key.toKeyString() + "]: ", ope);
throw new InteractionException(ope.getHttpStatus(), ope);
}
catch(Exception e) {
logger.debug("Error while deleting entity [" + key.toKeyString() + "]: ", e);
throw new InteractionException(Status.INTERNAL_SERVER_ERROR, e);
}
return Result.SUCCESS;
}
}