AccountEventRestView.java 4.77 KB
/*
 * Copyright Codecrew Ry
 * 
 * All rights reserved.
 * 
 * This license applies to any software containing a notice placed by the 
 * copyright holder. Such software is herein referred to as the Software. 
 * This license covers modification, distribution and use of the Software. 
 * 
 * Any distribution and use in source and binary forms, with or without 
 * modification is not permitted without explicit written permission from the 
 * copyright owner. 
 * 
 * A non-exclusive royalty-free right is granted to the copyright owner of the 
 * Software to use, modify and distribute all modifications to the Software in 
 * future versions of the Software. 
 * 
 */
package fi.codecrew.moya.rest;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiResponse;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import fi.codecrew.moya.beans.PlaceGroupBeanLocal;
import fi.codecrew.moya.beans.ProductBeanLocal;
import fi.codecrew.moya.model.AccountEvent;
import fi.codecrew.moya.model.Product;
import fi.codecrew.moya.rest.highcharts.HcSeries;
import fi.codecrew.moya.rest.highcharts.HcSeriesRoot;

@RequestScoped
@Path("/acc")
//@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
//@Produces({ MediaType.APPLICATION_JSON })
@Api(value = "/acc", description = "Access account events")
public class AccountEventRestView {

	@EJB
	private PlaceGroupBeanLocal pgbean;
	@EJB
	private ProductBeanLocal productBean;

	@GET
	@Produces({ MediaType.APPLICATION_JSON + "; charset=UTF-8" })
	@Path("/boughtTimecount/{productId}")
	@ApiOperation("Ask how many times a product has been bought")
	@ApiResponse(code = 200, message = "What is this this returned?")
	public ArrayList<HcSeriesRoot> boughtTimecount(@PathParam("productId") Integer productId) {

		Product product = productBean.findById(productId);
		SortedMap<Integer, SortedMap<Integer, SortedMap<Integer, Integer>>> counts = new TreeMap<>();
		for (AccountEvent p : product.getAccountEvents())
		{

			Calendar d = p.getEventTime();
			Integer year = d.get(Calendar.YEAR);
			Integer month = d.get(Calendar.MONTH);
			Integer day = d.get(Calendar.DAY_OF_MONTH);

			SortedMap<Integer, SortedMap<Integer, Integer>> yearmap = counts.get(year);
			if (yearmap == null) {
				yearmap = new TreeMap<>();
				counts.put(year, yearmap);
			}

			SortedMap<Integer, Integer> monthMap = yearmap.get(month);
			if (monthMap == null) {
				monthMap = new TreeMap<>();
				yearmap.put(month, monthMap);
			}
			Integer dayValue = monthMap.get(day);
			if (dayValue == null) {
				dayValue = 0;
			}
			monthMap.put(day, dayValue += p.getQuantity().intValueExact());
		}


		HcSeries data1 = new HcSeries("Daily");
		HcSeries data2 = new HcSeries("Stacked");

		Long data2Value = 0L;
		for (Entry<Integer, SortedMap<Integer, SortedMap<Integer, Integer>>> ym : counts.entrySet()) {
			Integer year = ym.getKey();
			for (Entry<Integer, SortedMap<Integer, Integer>> mm : ym.getValue().entrySet()) {
				Integer month = mm.getKey();
				for (Entry<Integer, Integer> dm : mm.getValue().entrySet()) {
					Integer day = dm.getKey();
					//					if (!empty) {
					//						data1.append(",");
					//						data2.append(",");
					//					} else {
					//						data1.append("\n");
					//						data2.append("\n");
					//						empty = false;
					//					}

					Calendar c = Calendar.getInstance();
					c.clear();
					c.set(year, month, day);
					//
					data1.addDataentry(c, Long.valueOf(dm.getValue()));
					data2.addDataentry(c, data2Value += dm.getValue());

					//
					//					data1.append("\n  [");
					//					data2.append("\n  [");
					//
					//					data1.append(c.getTimeInMillis());
					//					data2.append(c.getTimeInMillis());
					//
					//					data1.append(", ");
					//					data2.append(", ");
					//
					//					data1.append(dm.getValue()).append(" ] ");
					//					data2.append(data2Value += dm.getValue()).append(" ] ");
				}

			}
		}
		//
		//		StringBuilder retSb = new StringBuilder();
		//
		//		retSb.append("[  { [ 	{\"name\": \"Set 1\", \"data\" : [ \n");
		//		retSb.append(data1.toString());
		//		retSb.append("\n ] } \n,  {\"name\": \"Set 2\", \"data\": \n[\n");
		//		retSb.append(data2.toString());
		//		retSb.append("\n ] } ] \n} ]\n");

		ArrayList<HcSeriesRoot> ret = new ArrayList<HcSeriesRoot>();
		HcSeriesRoot seriesRoot = new HcSeriesRoot();
		seriesRoot.getSeries().add(data1);
		seriesRoot.getSeries().add(data2);
		ret.add(seriesRoot);
		ret.add(new HcSeriesRoot());
		return ret;
	}
}