<commit_before>package at.fhj.itm.pswe.rest;

import java.util.Iterator;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
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.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.pagecrawler.MainCrawler;

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

	@GET
	@Produces("application/json")
	public Response listAll()
	{

		TypedQuery<Website> findAllQuery = em.createQuery("SELECT DISTINCT w FROM Website w ORDER BY w.id", Website.class);
		final List<Website> results = findAllQuery.getResultList();		
		JSONArray returnResult=new JSONArray();

		for(Website ws: results){
			JSONObject temp= new JSONObject();
			temp.put("id", ws.getId());
			temp.put("address", ws.getDomain());
			temp.put("description", ws.getDescription());
			temp.put("active",ws.getActive());
			temp.put("depth", ws.getCrawldepth());
			returnResult.put(temp);
		}

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

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

	//For Datatable on Subiste "Website"
	@GET
	@Path("/{id:[0-9][0-9]*}/words")
	@Produces("application/json")
	public Response wordsOfSite(@PathParam("id") int id)
	{

		JSONObject my=new JSONObject();
		my.put("data", findWordsOFSite(id, 0));

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

	@GET
	@Path("/{id:[0-9][0-9]*}/words/{num:[0-9][0-9]*}")
	@Produces("application/json")
	public Response wordsOfSiteNumbered(@PathParam("id") int id, @PathParam("num") int num)
	{

		JSONObject my=new JSONObject();
		my.put("data", findWordsOFSite(id, num));

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


	private JSONArray findWordsOFSite(int id, int maxNum){
		Query q = em
				.createQuery("SELECT c.word.text, sum(c.amount)  "
						+ "FROM Container c WHERE c.website.id=:id AND c.word.active = TRUE "
						+ "GROUP BY c.word ORDER BY sum(c.amount) DESC")
						.setParameter("id", id);

		if(maxNum > 0){
			q.setMaxResults(maxNum);
		}
		List<Object[]> results	= q.getResultList();

		JSONArray returnResult=new JSONArray();

		for(Object[] wo: results){
			System.out.println(wo[0]+" | "+wo[1]);

			JSONObject temp= new JSONObject();
			temp.put("word", wo[0]);
			temp.put("amount", wo[1]);

			returnResult.put(temp);
		}
		return returnResult;

	}




	@GET
	@Path("/{id:[0-9][0-9]*}")
	@Produces("application/json")
	public Response findById(@PathParam("id") int id)
	{
		TypedQuery<Website> findByIdQuery = em.createQuery("SELECT DISTINCT w FROM Website w WHERE w.id = :id", Website.class);
		findByIdQuery.setParameter("id", id);
		Website 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();
	}


	@POST
	@Produces("application/json")
	@Consumes("application/json")
	public Response addSite(String incoming)
	{
		System.out.println("Received Request");
		JSONObject json= new JSONObject(incoming);
		System.out.println("JSON: "+json.toString());

		//Create Website object
		Website ws = new Website();
		ws.setDomain(json.getString("address"));
		ws.setDescription(json.getString("description"));
		ws.setCrawldepth(json.getInt("depth"));
		ws.setActive(true);

		//Save to DB
		em.persist(ws);
		em.flush();

		System.out.println("ID:" +ws.getId());

		//Add info for Return object
		json.put("id", ws.getId());
		json.put("active", ws.getActive());
		System.out.println("JSON: "+json.toString());

		//TODO STart crawler
		Thread t = new Thread(new MainCrawler(ws.getDomain(),1));
		t.start();

		return Response.ok(new JSONObject().put("data", json).toString()).build();
	}

	@DELETE
	@Produces("application/json")
	@Consumes("application/json")<commit_msg>Bugfix for adding new WebPages<commit_after>package at.fhj.itm.pswe.rest;

import java.util.Iterator;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
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.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.pagecrawler.MainCrawler;

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

	@GET
	@Produces("application/json")
	public Response listAll()
	{

		TypedQuery<Website> findAllQuery = em.createQuery("SELECT DISTINCT w FROM Website w ORDER BY w.id", Website.class);
		final List<Website> results = findAllQuery.getResultList();		
		JSONArray returnResult=new JSONArray();

		for(Website ws: results){
			JSONObject temp= new JSONObject();
			temp.put("id", ws.getId());
			temp.put("address", ws.getDomain());
			temp.put("description", ws.getDescription());
			temp.put("active",ws.getActive());
			temp.put("depth", ws.getCrawldepth());
			returnResult.put(temp);
		}

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

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

	//For Datatable on Subiste "Website"
	@GET
	@Path("/{id:[0-9][0-9]*}/words")
	@Produces("application/json")
	public Response wordsOfSite(@PathParam("id") int id)
	{

		JSONObject my=new JSONObject();
		my.put("data", findWordsOFSite(id, 0));

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

	@GET
	@Path("/{id:[0-9][0-9]*}/words/{num:[0-9][0-9]*}")
	@Produces("application/json")
	public Response wordsOfSiteNumbered(@PathParam("id") int id, @PathParam("num") int num)
	{

		JSONObject my=new JSONObject();
		my.put("data", findWordsOFSite(id, num));

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


	private JSONArray findWordsOFSite(int id, int maxNum){
		Query q = em
				.createQuery("SELECT c.word.text, sum(c.amount)  "
						+ "FROM Container c WHERE c.website.id=:id AND c.word.active = TRUE "
						+ "GROUP BY c.word ORDER BY sum(c.amount) DESC")
						.setParameter("id", id);

		if(maxNum > 0){
			q.setMaxResults(maxNum);
		}
		List<Object[]> results	= q.getResultList();

		JSONArray returnResult=new JSONArray();

		for(Object[] wo: results){
			System.out.println(wo[0]+" | "+wo[1]);

			JSONObject temp= new JSONObject();
			temp.put("word", wo[0]);
			temp.put("amount", wo[1]);

			returnResult.put(temp);
		}
		return returnResult;

	}




	@GET
	@Path("/{id:[0-9][0-9]*}")
	@Produces("application/json")
	public Response findById(@PathParam("id") int id)
	{
		TypedQuery<Website> findByIdQuery = em.createQuery("SELECT DISTINCT w FROM Website w WHERE w.id = :id", Website.class);
		findByIdQuery.setParameter("id", id);
		Website 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();
	}


	@POST
	@Produces("application/json")
	@Consumes("application/json")
	public Response addSite(String incoming)
	{
		System.out.println("Received Request");
		JSONObject json= new JSONObject(incoming);
		System.out.println("JSON: "+json.toString());

		//Create Website object
		Website ws = new Website();
		ws.setDomain(json.getString("address"));
		ws.setDescription(json.getString("description"));
		ws.setCrawldepth(json.getInt("depth"));
		ws.setActive(true);

		//Save to DB
		em.persist(ws);
		em.flush();

		//Add info for Return object
		json.put("id", ws.getId());
		json.put("active", ws.getActive());
		System.out.println("JSON: "+json.toString());

		
		Thread t = new Thread(new MainCrawler(ws.getDomain(),1));
		t.start();

		return Response.ok(new JSONObject().put("data", new JSONArray().put(json)).toString()).build();
	}

	@DELETE
	@Produces("application/json")
	@Consumes("application/json")
{'fraction_non_alphanumeric': 0.16880881340371814, 'fraction_numerical': 0.005278861602019738, 'mean_word_length': 4.611719253058596, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6710924', 'n_tokens_mistral': 3314, 'n_tokens_neox': 3002, 'n_words': 593}