package org.GreenCat.rest;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.OptimisticLockException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
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.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriBuilder;
import org.GreenCat.model.Websites;

/**
 * 
 */
@Stateless
@Path("/websites")
public class WebsitesEndpoint {
	@PersistenceContext(unitName = "GreenCat-persistence-unit")
	private EntityManager em;

	@POST
	@Consumes("application/json")
	public Response create(Websites entity) {
		em.persist(entity);
		return Response.created(
				UriBuilder.fromResource(WebsitesEndpoint.class)
						.path(String.valueOf(entity.getId())).build()).build();
	}

	@DELETE
	@Path("/{id:[0-9][0-9]*}")
	public Response deleteById(@PathParam("id") Long id) {
		Websites entity = em.find(Websites.class, id);
		if (entity == null) {
			return Response.status(Status.NOT_FOUND).build();
		}
		em.remove(entity);
		return Response.noContent().build();
	}

	@GET
	@Path("/{id:[0-9][0-9]*}")
	@Produces("application/json")
	public Response findById(@PathParam("id") Long id) {
		TypedQuery<Websites> findByIdQuery = em
				.createQuery(
						"SELECT DISTINCT w FROM Websites w LEFT JOIN FETCH w.wordsId WHERE w.id = :entityId ORDER BY w.id",
						Websites.class);
		findByIdQuery.setParameter("entityId", id);
		Websites entity;
		try {
			entity = findByIdQuery.getSingleResult();
		} catch (NoResultException nre) {
			entity = null;
		}
		if (entity == null) {
			return Response.status(Status.NOT_FOUND).build();
		}
		return Response.ok(entity).build();
	}

	@GET
	@Produces("application/json")
	public List<Websites> listAll(@QueryParam("start") Integer startPosition,
			@QueryParam("max") Integer maxResult) {
		TypedQuery<Websites> findAllQuery = em
				.createQuery(
						"SELECT DISTINCT w FROM Websites w LEFT JOIN FETCH w.wordsId ORDER BY w.id",
						Websites.class);
		if (startPosition != null) {
			findAllQuery.setFirstResult(startPosition);
		}
		if (maxResult != null) {
			findAllQuery.setMaxResults(maxResult);
		}
		final List<Websites> results = findAllQuery.getResultList();
		return results;
	}

	@PUT
	@Path("/{id:[0-9][0-9]*}")
	@Consumes("application/json")
	public Response update(@PathParam("id") Long id, Websites entity) {
		if (entity == null) {
			return Response.status(Status.BAD_REQUEST).build();
		}
		if (id == null) {
			return Response.status(Status.BAD_REQUEST).build();
		}
		if (!id.equals(entity.getId())) {
			return Response.status(Status.CONFLICT).entity(entity).build();
		}
		if (em.find(Websites.class, id) == null) {
			return Response.status(Status.NOT_FOUND).build();
		}
		try {
			entity = em.merge(entity);
		} catch (OptimisticLockException e) {
			return Response.status(Response.Status.CONFLICT)
					.entity(e.getEntity()).build();
		}

		return Response.noContent().build();
	}
}

@@ -38,19 +38,100 @@
 va.util.
-Lis
+Collection;%0Aimport java.util.Collections;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0Aimpo
@@ -729,42 +729,212 @@
 er;%0A
-import org.GreenCat.model.Websites
+%0Aimport org.GreenCat.logic.WordStatistics;%0Aimport org.GreenCat.model.Websites;%0Aimport org.GreenCat.model.Words;%0Aimport org.GreenCat.util.HTMLReader;%0Aimport org.GreenCat.logic.*;%0Aimport org.GreenCat.rest.*
 ;%0A%0A/
@@ -1161,48 +1161,670 @@
 ate(
-Websites entity) %7B%0A%09%09em.persist(entity);
+@QueryParam(%22url%22) String url) %7B%0A%09%09HTMLReader reader = new HTMLReader();%0A%09%09String content = reader.readHTMLAsText(url);%0A%09%09WordStatistics wstats = new WordStatistics();%0A%09%09Stats allStats = wstats.getWordCounts(content, Collections.EMPTY_LIST);%0A%09%09%0A%09%09Websites website = new Websites();%0A%09%09website.setUrl(url);%0A%09%09em.persist(website);%0A%09%09%0A%09%09for(String w : allStats.wordCounts.keySet())%7B%0A%09%09%09Words word = new Words();%0A%09%09%09word.setFiltered(false);%0A%09%09%09word.setWord(w);%0A%09%09%09word.setCount(Long.valueOf(allStats.wordCounts.get(w)));%0A%09%09%09Set%3CWebsites%3E websitesId = word.getWebsitesId();%0A%09%09%09websitesId.add(website);%0A%09%09%09word.setWebsitesId(websitesId);%0A%09%09%09%0A%09%09%09em.persist(word);%0A%09%09%7D%0A%09%09
 %0A%09%09r
@@ -1926,22 +1926,23 @@
 valueOf(
-entity
+website
 .getId()
@@ -1965,16 +1965,19 @@
 d();%0A%09%7D%0A
+%09%0A%09
 %0A%09@DELET

Compute stats in endpoint