
package org.xlcloud.service.rest;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.xlcloud.rest.RequestAwareSupport;
import org.xlcloud.service.VirtualClusterDefinition;
import org.xlcloud.service.VirtualClusterDefinitions;
import org.xlcloud.service.api.VirtualClusterDefinitionsApi;
import org.xlcloud.service.exception.VcmsException;
import org.xlcloud.service.exception.VcmsObjectNotFoundException;
import org.xlcloud.service.exception.VcmsValidationException;
import org.xlcloud.service.manager.VirtualClusterDefinitionsManager;

/**
 * REST service for VirtualClusterDefinition resources
 * 
 * @author Andrzej Stasiak, AMG.net
 * @author Piotr Kulasek-Szwed, AMG.net
 */
@Path( "virtual-cluster-definitions" )
@Consumes( { "application/json", "application/xml" } )
@Produces( { "application/json", "application/xml" } )
@Stateless
@LocalBean
@VcmsRestResource
public class VirtualClusterDefinitionsResource extends RequestAwareSupport implements VirtualClusterDefinitionsApi {

    private static final String VCD_ID = "vcd-id";

    @Inject
    private VirtualClusterDefinitionsApi vcDefsApi;

    @Inject
    private VirtualClusterDefinitionsManager vcDefsManager;

    /** {@inheritDoc} */
    @Override
    @GET
    public VirtualClusterDefinitions list(@QueryParam( "appId" ) List<Long> appIds, @QueryParam( "type" ) String type,
            @QueryParam( "tag" ) List<String> tags) {
        return vcDefsApi.list(appIds, type, tags);
    }

    /** {@inheritDoc} */
    @Override
    @GET
    @Path( "{vcd-id}" )
    public VirtualClusterDefinition get(@PathParam( VCD_ID ) Long id) {
        return vcDefsApi.get(id);
    }

    /** {@inheritDoc} */
    @Override
    @POST
    @Path( "/validate" )
    public VirtualClusterDefinition validate(VirtualClusterDefinition definition) throws VcmsValidationException {
        return vcDefsApi.validate(definition);
    }

    /** {@inheritDoc} */
    @Override
    @POST
    public VirtualClusterDefinition create(VirtualClusterDefinition definition) throws VcmsValidationException, VcmsException {
        return vcDefsApi.create(definition);
    }

    /** {@inheritDoc} */
    @Override
    @DELETE
    @Path( "{vcd-id}" )
    public void remove(@PathParam( VCD_ID ) Long id) {
        vcDefsApi.remove(id);
    }

    /** {@inheritDoc} */
    @Override
    @PUT
    @Path( "{vcd-id}" )
    public VirtualClusterDefinition update(@PathParam( VCD_ID ) Long id, VirtualClusterDefinition definition)
            throws VcmsObjectNotFoundException, VcmsValidationException {
        return vcDefsApi.update(id, definition);
    }

    /** {@inheritDoc} */
    @Override
    public String normalizePath() {
        List<String> vcdIds = uriInfo.getPathParameters().get(VCD_ID);

        if (vcdIds == null || vcdIds.size() == 0) {
            return uriInfo.getPath(true);
        }

        Long vcdId = Long.valueOf(vcdIds.get(0));
        Long accountId = vcDefsManager.getAccountId(vcdId);
        if (accountId == null) {
            return uriInfo.getPath(true);
        } else {
            return "accounts/" + accountId + "/" + uriInfo.getPath(true);
        }
    }
}
