package org.tarent.cvio.server.cv;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

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 javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.tarent.cvio.server.common.CVIOConfiguration;
import org.tarent.cvio.server.doc.CVIODocumentGenerator;

import com.google.inject.Inject;
import com.yammer.dropwizard.auth.Auth;
import com.yammer.metrics.annotation.Timed;

/**
 * The cv resource encapsulates all management operations for the cv handling.
 * It does not provide further rights checking, so the enabled user has full
 * access to all the cvs.
 * 
 * @author smancke
 * 
 */

@Path("/cv")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class CVResource {

    /**
     * The db implementation for the cvs.
     */
    private CVDB cvdb;

    /**
     * The global configuration.
     */
    private CVIOConfiguration configuration;
    
    /**
     * CVIO Document Generator
     */
    private CVIODocumentGenerator cvioDocGen;

    /**
     * Creates a new CV Resource.
     * 
     * @param cvdbImpl The db implementation for the cvs.
     * @param cfg The global configuration.
     */
    @Inject
    public CVResource(final CVDB cvdbImpl, final CVIOConfiguration cfg, final CVIODocumentGenerator docGen) {
        this.cvdb = cvdbImpl;
        this.configuration = cfg;
        this.cvioDocGen = docGen;
    }

    /**
     * Returns all CVs within the system.
     * 
     * @param fields the fields of the cv object, which should be returned
     * @return Response Object with the list of CVs
     */
    @Timed
    @GET
    @Path("/cvs")
    public List<Map<String, Object>> getCVs(
            @QueryParam("fields") final List<String> fields, @Auth Boolean isAuthenticated) {
        List<Map<String, Object>> cvs = cvdb.getAllCVs(fields.toArray(new String[0]));
        for (Map<String, Object> entry : cvs) {
            entry.put("ref",
                    configuration.getUriPrefix() + "/cv/cvs/" + entry.get("id"));
        }
        return cvs;
    }

    /**
     * Returns one CV by the supplied id.
     * 
     * @param id the id of the cv
     * @return Returns the cv as json string.
     */
    @Timed
    @GET
    @Path("/cvs/{id}")
    public String getCV(@PathParam("id") final String id, @Auth Boolean isAuthenticated) {
        return cvdb.getCVById(id);
    }

    /**
     * Overwrite a CV.
     * 
     * @param id the id of the cv
     * @param content the json document of the cv as string
     * @return Response Object with the status
     */
    @Timed
    @PUT()
    @Path("/cvs/{id}")
    public Response updateCV(@PathParam("id") final String id,
            final String content, @Auth Boolean isAuthenticated) {
        cvdb.updateCV(id, content);
        return Response.ok().build();
    }

    /**
     * Creates a new CV.
     * 
     * @param content the json document of the cv as string
     * @return Response Object with the status of the action
     * @throws URISyntaxException may only occur in cases of miss configuration
     *             of the uri prefix
     */
    @Timed
    @POST
    @Path("/cvs")
    public Response createCV(final String content, @Auth Boolean isAuthenticated) throws URISyntaxException {
        // Use JsonNode for json handling, if needed
        String cvid = cvdb.createCV(content);
        return Response
                .created(new URI(configuration.getUriPrefix() + "/cv/cvs/" + cvid))
                .build();
    }
    
    /**
     * Delte CV
     * @param id the id of the cv
     * @return Response Objet with the status of the action
     * 
     * */
    @DELETE
    @Path("/cvs/{id}")
    public Response deleteCV(@PathParam("id") final String id,@Auth Boolean isAuthenticated){
    	cvdb.deleteCV(id);
    	return Response.noContent().build();
    }

    /**
     * Generates a document for one cv
     * 
     * @param id - the id of the cv
     * @param isAuthenticated - to protect this resource
     * 
     * @return Response Object
     * 
     */
    @Path("cvs/export/{id}")
    public Response exportCV(@PathParam("id") final String id, @Auth Boolean isAuthenticated) {
    	Map<String, Object> cvMapById = cvdb.getCVMapById(id);
    	cvioDocGen.parseCVIOData(cvMapById);
    	return Response.noContent().build();
    }
}

@@ -82,24 +82,50 @@
 xException;%0A
+import java.util.HashMap;%0A
 import java.
@@ -4492,23 +4492,20 @@
 ject%3E cv
-MapById
+Data
  = cvdb.
@@ -4531,44 +4531,165 @@
    %09
-cvioDocGen.parseCVIOData(cvMapById);
+%0A    %09HashMap%3CObject, Object%3E dataModel = new HashMap%3CObject, Object%3E();%0A    %09dataModel.put(%22cv%22, cvData);%0A    %09cvioDocGen.generateDocument(dataModel);%0A    %09
 %0A   

clean up code