package com.bergcomputers.rest.accounts;

import java.net.URI;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;

import com.bergcomputers.domain.Account;
import com.bergcomputers.domain.Transaction;
import com.bergcomputers.ejb.ITransactionController;

@Stateless
@Path("transactions")
public class TransactionsResource {

	@Context
	private UriInfo uriInfo;
	@EJB
	private ITransactionController transactionController;

	public TransactionsResource() {
		// TODO Auto-generated constructor stub
	}
	@GET
	@Path("/{transactionid}")
	public Transaction getTransaction(@PathParam("transactionid") Long transactionid) {
		 return transactionController.findTransaction(transactionid);
		
	}
	@GET
	@Produces("application/json")
	public List<Transaction> getTransactions() {
		return transactionController.getTransactions();
	}
	@GET
    @Path("/detail/{transactionid}")
    @Produces("application/json")
    public Transaction getTransactionDetails(@PathParam("transactionid") Long transactionid) {
     return transactionController.findTransaction(transactionid);
	}
    @GET
    @Produces("application/json")
    @Path("uris")
    public String getTransactionsURIs() {
        JSONArray uriArray = new JSONArray();
             for (Transaction transaction :  transactionController.getTransactions()) {
                UriBuilder ub = uriInfo.getAbsolutePathBuilder();
                URI transactionUri = ub.path(((Transaction)transaction).getId().toString()).build();
                uriArray.put(transactionUri.toASCIIString());
            }
        return uriArray.toString();
    }
    @PUT
    @Consumes("application/json")
    @Produces("application/json")
    public Transaction createTransaction(final Transaction jsonTransaction) throws JSONException {

    	jsonTransaction.setCreationDate(null ==jsonTransaction.getCreationDate() ? new Date():jsonTransaction.getCreationDate());
    	Transaction transactionEntity = transactionController.create(jsonTransaction);
        return transactionEntity;
    }

	/*
	 * @PUT
	 * 
	 * @Consumes("application/json")
	 * @Produces("application/json")
	 *  public Transaction createTransaction(final Transaction jsonTransaction) throws JSOnException{
	 * jsonTransaction.setCreationDate(null=jsonTransaction.getCreationDate()) 
	 * }
	 */
	@DELETE
	@Path("/{transactionid}")
	public void delete(@PathParam("transactionid") Long transactionId) {	
		transactionController.delete(transactionId);
	}
}

@@ -2445,280 +2445,8 @@
  %7D%0A%0A
-%09/*%0A%09 * @PUT%0A%09 * %0A%09 * @Consumes(%22application/json%22)%0A%09 * @Produces(%22application/json%22)%0A%09 *  public Transaction createTransaction(final Transaction jsonTransaction) throws JSOnException%7B%0A%09 * jsonTransaction.setCreationDate(null=jsonTransaction.getCreationDate()) %0A%09 * %7D%0A%09 */
 %0A%09@D

create transaction