package com.dianping.cat.report.page.database;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;

import org.unidal.lookup.annotation.Inject;
import org.unidal.web.mvc.PageHandler;
import org.unidal.web.mvc.annotation.InboundActionMeta;
import org.unidal.web.mvc.annotation.OutboundActionMeta;
import org.unidal.web.mvc.annotation.PayloadMeta;

import com.dianping.cat.consumer.company.model.entity.ProductLine;
import com.dianping.cat.consumer.metric.ProductLineConfigManager;
import com.dianping.cat.helper.TimeHelper;
import com.dianping.cat.report.ReportPage;
import com.dianping.cat.report.page.LineChart;
import com.dianping.cat.report.page.PayloadNormalizer;
import com.site.lookup.util.StringUtils;

public class Handler implements PageHandler<Context> {
	@Inject
	private JspViewer m_jspViewer;

	@Inject
	private PayloadNormalizer m_normalizePayload;

	@Inject
	private ProductLineConfigManager m_productLineConfigManager;

	@Inject
	private GraphCreator m_graphCreator;

	@Override
	@PayloadMeta(Payload.class)
	@InboundActionMeta(name = "database")
	public void handleInbound(Context ctx) throws ServletException, IOException {
		// display only, no action here
	}

	@Override
	@OutboundActionMeta(name = "database")
	public void handleOutbound(Context ctx) throws ServletException, IOException {
		Model model = new Model(ctx);
		Payload payload = ctx.getPayload();
		normalize(model, payload);

		long date = payload.getDate();
		int timeRange = payload.getTimeRange();
		Date start = new Date(date - (timeRange - 1) * TimeHelper.ONE_HOUR);
		Date end = new Date(date + TimeHelper.ONE_HOUR);

		switch (payload.getAction()) {
		case VIEW:
			Map<String, LineChart> charts = m_graphCreator.buildChartsByProductLine(payload.getProduct(), start, end);

			model.setLineCharts(new ArrayList<LineChart>(charts.values()));
			break;
		}

		m_jspViewer.view(ctx, model);
	}

	private void normalize(Model model, Payload payload) {
		List<ProductLine> databases = new ArrayList<ProductLine>(m_productLineConfigManager.queryDatabases().values());

		model.setPage(ReportPage.DATABASE);
		model.setProductLines(databases);
		m_normalizePayload.normalize(model, payload);

		if (StringUtils.isEmpty(payload.getProduct()) && databases.size() > 0) {
			payload.setProduct(databases.get(0).getId());
		}

		int timeRange = payload.getTimeRange();
		Date startTime = new Date(payload.getDate() - (timeRange - 1) * TimeHelper.ONE_HOUR);
		Date endTime = new Date(payload.getDate() + TimeHelper.ONE_HOUR - 1);

		model.setStartTime(startTime);
		model.setEndTime(endTime);

	}

}

@@ -2335,12 +2335,19 @@
 t())
- && 
+) %7B%0A%09%09%09if (
 data
@@ -2366,16 +2366,17 @@
  %3E 0) %7B%0A
+%09
 %09%09%09paylo
@@ -2416,16 +2416,68 @@
 tId());%0A
+%09%09%09%7D else %7B%0A%09%09%09%09payload.setProduct(%22Default%22);%0A%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%09i
@@ -2738,15 +2738,14 @@
 dTime);%0A
-%0A
 %09%7D%0A%0A%7D%0A

fix npe