package at.fhj.itm.pswe.rest;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.GET;
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 org.json.JSONArray;
import org.json.JSONObject;

import at.fhj.itm.pswe.model.Website;
import at.fhj.itm.pswe.model.Word;

@Stateless
@Path("/word")
public class WordEndpoint{
	@PersistenceContext(unitName = "TermStatistics")
	private EntityManager em;

	/*
	@GET
	@Produces("application/json")
	public List<Word> listAll(@QueryParam("start") Integer startPosition, @QueryParam("max") Integer maxResult)
	{
		TypedQuery<Word> findAllQuery = em.createQuery("SELECT DISTINCT w FROM Word w ORDER BY w.text", Word.class);

		if (startPosition != null)
		{
			findAllQuery.setFirstResult(startPosition);
		}
		if (maxResult != null)
		{
			findAllQuery.setMaxResults(maxResult);
		}

		final List<Word> results = findAllQuery.getResultList();
		return results;
	}*/

	@GET
	@Produces("application/json")
	public Response listAllwithAmount()
	{
		List<Object[]> results = em
				.createQuery("SELECT w.text, w.active, sum(c.amount)  FROM Container c JOIN c.word w  GROUP BY w.text, w.active")
				.getResultList();
		JSONArray returnResult=new JSONArray();

		for(Object[] wo: results){
			JSONObject temp= new JSONObject();
			temp.put("word", wo[0]);
			temp.put("amount", wo[2]);
			temp.put("active",wo[1]);

			returnResult.put(temp);
		}

		JSONObject my=new JSONObject();
		my.put("data", returnResult);

		return Response.ok(my.toString()).build();
	}


	@GET
	@Path("/{id:[a-zA-Z][a-zA-Z]*}")
	@Produces("application/json")
	public Response findById(@PathParam("id") String id)
	{
		TypedQuery<Word> findByIdQuery = em.createQuery("SELECT DISTINCT w FROM Word w WHERE w.text = :text", Word.class);
		findByIdQuery.setParameter("text", id);
		Word 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();
	}
}
@@ -20,24 +20,111 @@
 pswe.rest;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.Comparator;%0A
 import java.
@@ -596,24 +596,64 @@
 SONObject;%0A%0A
+import at.fhj.itm.pswe.model.Container;%0A
 import at.fh
@@ -2490,9 +2490,453 @@
 d();%0A%09%7D%0A
+%09%0A%09@GET%0A    @Path(%22/visibility/%7Bword:%5Ba-zA-Z%5D%5Ba-zA-Z%5D*%7D%22)%0A%09public void changeVisibility(@PathParam(%22word%22) String word)%7B%0A%09%09TypedQuery%3CWord%3E visibility = em.createQuery(%22SELECT w FROM Word w WHERE w.text = :word%22, Word.class);%0A%09%09visibility.setParameter(%22word%22,word);%0A%09%09%0A%09%09List%3CWord%3E words=visibility.getResultList();%0A%09%09for(Word w : words)%7B%0A%09%09%09if(w.getActive()==0)%7B%0A%09%09%09%09w.setActive((byte) 1);%0A%09%09%09%7Delse %7B%0A%09%09%09%09w.setActive((byte) 0);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%09%09%0A
 %7D

set words active/inactive