package com.ray.face.gwt.server;

import java.util.List;

import javax.ejb.EJB;

import com.google.gson.Gson;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.ray.face.entity.BurdenRate;
import com.ray.face.entity.LaborRate;
import com.ray.face.entity.RateSheet;
import com.ray.face.gwt.client.rpc.RateSheetService;
import com.ray.face.model.FaceAppLocal;

public class RateSheetServiceImpl extends RemoteServiceServlet implements RateSheetService {

	private static final long serialVersionUID = 1L;

	@EJB private FaceAppLocal app;
	
	@Override
	public List<RateSheet> fetchRateSheets(String businessUnit) {
		return app.findByBusinessUnit(businessUnit);
	}

	@Override
	public String fetchLaborRates(String rateSheetId) {
		List<LaborRate> laborRates = app.findLaborRatesByRateIndex(rateSheetId);
		
		//return LaborRateRecord.toValueObjects(laborRates);
		
		Gson gson = new Gson();
		String json = gson.toJson(laborRates);
		
		StringBuilder sb = new StringBuilder();
		sb.append("{\"status\":0, \"startRow\":0, \"endRow\":50, \"totalRows\":" + laborRates.size() +	", \"data\":");
		sb.append(json);
		sb.append("}");
		
		json = sb.toString();
		System.err.println(json);
		return json;
	}
	
	@Override
	public List<BurdenRate> fetchBurdenRates(String rateSheetId) {
		return app.findBurdenRatesByRateIndex(rateSheetId);
	}
}
