/*
 * Isomorphic SmartGWT web presentation layer
 * Copyright 2000 and beyond Isomorphic Software, Inc.
 * 
 * OWNERSHIP NOTICE
 * Isomorphic Software owns and reserves all rights not expressly granted in
 * this source code,
 * including all intellectual property rights to the structure, sequence, and
 * format of this code
 * and to all designs, interfaces, algorithms, schema, protocols, and inventions
 * expressed herein.
 * 
 * If you have any questions, please email <sourcecode@isomorphic.com>.
 * 
 * This entire comment must accompany any portion of Isomorphic Software source
 * code that is
 * copied or moved from this file.
 */

package com.smartgwt.sample.showcase.client.dataintegration.java.others;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.cube.CubeGrid;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.BooleanItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.CellFormatter;
import com.smartgwt.client.widgets.grid.SortNormalizer;
import com.smartgwt.client.types.Autofit;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ExportDisplay;
import com.smartgwt.client.types.ExportFormat;
import com.smartgwt.client.util.EnumUtil;
import com.smartgwt.client.util.JSOHelper;
import com.smartgwt.sample.showcase.client.PanelFactory;
import com.smartgwt.sample.showcase.client.ShowcasePanel;

import java.util.LinkedHashMap;
import java.util.Date;

public class CubeGridExportSample extends ShowcasePanel
{
	private static final String DESCRIPTION = "You can export the client-side data from a DataBoundComponent. That is, the data as "
			+ "seen in a component, including the effects of client-side formatters."
			+ "<p>In the example below, choose an export format from the select-list, decide whether "
			+ "to download the results or view them in a window using the checkbox and click the "
			+ "Export button."
			+ "<p>Data is exported according to the filters and sort-order on the grid and includes "
			+ "the formatted values and field-titles as seen in the grid.";

	public static class Factory implements PanelFactory
	{

		private String id;

		public Canvas create()
		{
			CubeGridExportSample panel = new CubeGridExportSample();
			id = panel.getID();
			return panel;
		}

		public String getID()
		{
			return id;
		}

		public String getDescription()
		{
			return DESCRIPTION;
		}
	}

	protected boolean isTopIntro()
	{
		return true;
	}

	public Canvas getViewPanel()
	{
		final DataSource dataSource = DataSource.get("largeValueMap_orderItem");

		final CubeGrid cubeGrid = new CubeGrid();
		cubeGrid.setData(ProductRevenueData.getData());

		cubeGrid.setWidth100();
		cubeGrid.setHeight100();
		cubeGrid.setHideEmptyFacetValues(true);
		cubeGrid.setShowCellContextMenus(true);

		final NumberFormat numberFormat = NumberFormat.getFormat("0,000");

		cubeGrid.setCellFormatter(new CellFormatter()
		{
			public String format(Object value, ListGridRecord record,
					int rowNum, int colNum)
			{
				if (value == null) return null;
				try
				{
					return numberFormat.format(((Number) value).longValue());
				} catch (Exception e)
				{
					return value.toString();
				}
			}
		});

		cubeGrid.setColumnFacets("quarter", "month", "metric");
		cubeGrid.setRowFacets("region", "product");

		final DynamicForm exportForm = new DynamicForm();

		exportForm.setWidth(300);

		SelectItem exportTypeItem = new SelectItem("exportType", "Export Type");
		exportTypeItem.setWidth("*");
		exportTypeItem.setDefaultToFirstOption(true);

		LinkedHashMap valueMap = new LinkedHashMap();
		valueMap.put("csv", "CSV (Excel)");
		valueMap.put("xml", "XML");
		valueMap.put("json", "JSON");
		valueMap.put("xls", "XLS (Excel97)");
		valueMap.put("ooxml", "XLSX (Excel2007/OOXML)");

		exportTypeItem.setValueMap(valueMap);

		BooleanItem showInWindowItem = new BooleanItem();
		showInWindowItem.setName("showInWindow");
		showInWindowItem.setTitle("Show in Window");
		showInWindowItem.setAlign(Alignment.LEFT);

		exportForm.setItems(exportTypeItem, showInWindowItem);

		IButton exportButton = new IButton("Export");
		exportButton.addClickHandler(new ClickHandler()
		{
			public void onClick(com.smartgwt.client.widgets.events.ClickEvent event)
			{
				String exportAs = (String) exportForm.getField("exportType").getValue();

				FormItem item = exportForm.getField("showInWindow");
				boolean showInWindow = item.getValue() == null ? false : (Boolean) item.getValue();

				if (exportAs.equals("json"))
				{
					// JSON exports are server-side only, so use the
					// OperationBinding on the DataSource
					DSRequest dsRequestProperties = new DSRequest();
					dsRequestProperties.setOperationId("customJSONExport");
					dsRequestProperties.setExportDisplay(showInWindow ? ExportDisplay.WINDOW : ExportDisplay.DOWNLOAD);

					cubeGrid.exportClientData(dsRequestProperties);
				} else
				{
					// exportAs is either XML or CSV, which we can do with
					// requestProperties
					DSRequest dsRequestProperties = new DSRequest();
					dsRequestProperties.setExportAs((ExportFormat) EnumUtil.getEnum(ExportFormat.values(), exportAs));
					dsRequestProperties.setExportDisplay(showInWindow ? ExportDisplay.WINDOW : ExportDisplay.DOWNLOAD);

					// No error:
					//cubeGrid.exportClientData(dsRequestProperties);
					
					// _2 is undefined
					cubeGrid.exportClientData();
				}
			}
		});

		VLayout layout = new VLayout(15);
		layout.setHeight100();

		HLayout formLayout = new HLayout(15);
		formLayout.setAutoHeight();
		formLayout.addMember(exportForm);
		formLayout.addMember(exportButton);
		layout.addMember(formLayout);

		layout.addMember(cubeGrid);

		return layout;
	}

	private static class ProductRevenueData
	{
		private static ProductRevenue[] data = new ProductRevenue[] {
				new ProductRevenue("Q1, 2002", "January", "Western U.S.",
						"Pens", "Revenue", 10000, 25),
				new ProductRevenue("Q1, 2002", "January", "Western U.S.",
						"Chairs", "Revenue", 50000, 45),
				new ProductRevenue("Q1, 2002", "January", "Western U.S.",
						"Monitors", "Revenue", 120000, 49),

				new ProductRevenue("Q1, 2002", "January", "Western U.S.",
						"Pens", "Profit", 2000, 25),
				new ProductRevenue("Q1, 2002", "January", "Western U.S.",
						"Chairs", "Profit", 5000, 45),
				new ProductRevenue("Q1, 2002", "January", "Western U.S.",
						"Monitors", "Profit", 44000, 59, "over50"),

				new ProductRevenue("Q1, 2002", "January", "Midwest U.S.",
						"Pens", "Revenue", 8000, 20),
				new ProductRevenue("Q1, 2002", "January", "Midwest U.S.",
						"Chairs", "Revenue", 22000, 20),
				new ProductRevenue("Q1, 2002", "January", "Midwest U.S.",
						"Monitors", "Revenue", 20000, 8, "under10"),

				new ProductRevenue("Q1, 2002", "January", "Midwest U.S.",
						"Pens", "Profit", 2000, 25),
				new ProductRevenue("Q1, 2002", "January", "Midwest U.S.",
						"Chairs", "Profit", 2000, 18),
				new ProductRevenue("Q1, 2002", "January", "Midwest U.S.",
						"Monitors", "Profit", 5000, 7, "under10"),

				new ProductRevenue("Q1, 2002", "January", "Eastern U.S.",
						"Pens", "Revenue", 22000, 55, "over50"),
				new ProductRevenue("Q1, 2002", "January", "Eastern U.S.",
						"Chairs", "Revenue", 40000, 36),
				new ProductRevenue("Q1, 2002", "January", "Eastern U.S.",
						"Monitors", "Revenue", 105000, 43),

				new ProductRevenue("Q1, 2002", "January", "Eastern U.S.",
						"Pens", "Profit", 4000, 50, "over50"),
				new ProductRevenue("Q1, 2002", "January", "Eastern U.S.",
						"Chairs", "Profit", 4000, 36),
				new ProductRevenue("Q1, 2002", "January", "Eastern U.S.",
						"Monitors", "Profit", 25000, 34),

				new ProductRevenue("Q1, 2002", "February", "Western U.S.",
						"Pens", "Revenue", 12000, 23),
				new ProductRevenue("Q1, 2002", "February", "Western U.S.",
						"Chairs", "Revenue", 42000, 47),
				new ProductRevenue("Q1, 2002", "February", "Western U.S.",
						"Monitors", "Revenue", 160000, 40),

				new ProductRevenue("Q1, 2002", "February", "Western U.S.",
						"Pens", "Profit", 4000, 23),
				new ProductRevenue("Q1, 2002", "February", "Western U.S.",
						"Chairs", "Profit", 4000, 47),
				new ProductRevenue("Q1, 2002", "February", "Western U.S.",
						"Monitors", "Profit", 68000, 40),

				new ProductRevenue("Q1, 2002", "February", "Midwest U.S.",
						"Pens", "Revenue", 10000, 19),
				new ProductRevenue("Q1, 2002", "February", "Midwest U.S.",
						"Chairs", "Revenue", 12000, 13),
				new ProductRevenue("Q1, 2002", "February", "Midwest U.S.",
						"Monitors", "Revenue", 75000, 19),

				new ProductRevenue("Q1, 2002", "February", "Midwest U.S.",
						"Pens", "Profit", 3000, 20),
				new ProductRevenue("Q1, 2002", "February", "Midwest U.S.",
						"Chairs", "Profit", 1000, 11),
				new ProductRevenue("Q1, 2002", "February", "Midwest U.S.",
						"Monitors", "Profit", 32000, 17),

				new ProductRevenue("Q1, 2002", "February", "Eastern U.S.",
						"Pens", "Revenue", 31000, 58, "over50"),
				new ProductRevenue("Q1, 2002", "February", "Eastern U.S.",
						"Chairs", "Revenue", 35000, 39),
				new ProductRevenue("Q1, 2002", "February", "Eastern U.S.",
						"Monitors", "Revenue", 164000, 41),

				new ProductRevenue("Q1, 2002", "February", "Eastern U.S.",
						"Pens", "Profit", 8000, 53, "over50"),
				new ProductRevenue("Q1, 2002", "February", "Eastern U.S.",
						"Chairs", "Profit", 4000, 44),
				new ProductRevenue("Q1, 2002", "February", "Eastern U.S.",
						"Monitors", "Profit", 88000, 47),

				new ProductRevenue("Q1, 2002", "March", "Western U.S.", "Pens",
						"Revenue", 18000, 26),
				new ProductRevenue("Q1, 2002", "March", "Western U.S.",
						"Chairs", "Revenue", 25000, 54, "over50"),
				new ProductRevenue("Q1, 2002", "March", "Western U.S.",
						"Monitors", "Revenue", 220000, 40),

				new ProductRevenue("Q1, 2002", "March", "Western U.S.", "Pens",
						"Profit", 9000, 29),
				new ProductRevenue("Q1, 2002", "March", "Western U.S.",
						"Chairs", "Profit", 2000, 40),
				new ProductRevenue("Q1, 2002", "March", "Western U.S.",
						"Monitors", "Profit", 112000, 38),

				new ProductRevenue("Q1, 2002", "March", "Midwest U.S.", "Pens",
						"Revenue", 7000, 10),
				new ProductRevenue("Q1, 2002", "March", "Midwest U.S.",
						"Chairs", "Revenue", 6000, 13),
				new ProductRevenue("Q1, 2002", "March", "Midwest U.S.",
						"Monitors", "Revenue", 135000, 25),

				new ProductRevenue("Q1, 2002", "March", "Midwest U.S.", "Pens",
						"Profit", 2000, 6, "under10"),
				new ProductRevenue("Q1, 2002", "March", "Midwest U.S.",
						"Chairs", "Profit", 1000, 20),
				new ProductRevenue("Q1, 2002", "March", "Midwest U.S.",
						"Monitors", "Profit", 66000, 23),

				new ProductRevenue("Q1, 2002", "March", "Eastern U.S.", "Pens",
						"Revenue", 44000, 64, "over50"),
				new ProductRevenue("Q1, 2002", "March", "Eastern U.S.",
						"Chairs", "Revenue", 15000, 33),
				new ProductRevenue("Q1, 2002", "March", "Eastern U.S.",
						"Monitors", "Revenue", 190000, 35),

				new ProductRevenue("Q1, 2002", "March", "Eastern U.S.", "Pens",
						"Profit", 20000, 65, "over50"),
				new ProductRevenue("Q1, 2002", "March", "Eastern U.S.",
						"Chairs", "Profit", 2000, 40),
				new ProductRevenue("Q1, 2002", "March", "Eastern U.S.",
						"Monitors", "Profit", 115000, 39) };

		public static ProductRevenue[] getData()
		{
			return data;
		}
	}

	/**
	 * ProductRevenue record class.
	 */
	private static class ProductRevenue extends ListGridRecord
	{

		public ProductRevenue(String quarter, String month, String region,
				String product, String metric, int value, int percentNational)
		{
			setQuarter(quarter);
			setMonth(month);
			setRegion(region);
			setProduct(product);
			setMetric(metric);
			setValue(value);
			setPercentNational(percentNational);
		}

		public ProductRevenue(String quarter, String month, String region,
				String product, String metric, int value, int percentNational,
				String hilite)
		{
			setQuarter(quarter);
			setMonth(month);
			setRegion(region);
			setProduct(product);
			setMetric(metric);
			setValue(value);
			setPercentNational(percentNational);
		}

		public void setQuarter(String quarter)
		{
			setAttribute("quarter", quarter);
		}

		public String getQuarter(String quarter)
		{
			return getAttribute("quarter");
		}

		public void setRegion(String region)
		{
			setAttribute("region", region);
		}

		public String getRegion(String region)
		{
			return getAttribute("region");
		}

		public void setMonth(String month)
		{
			setAttribute("month", month);
		}

		public String getMonth(String month)
		{
			return getAttribute("month");
		}

		public void setProduct(String product)
		{
			setAttribute("product", product);
		}

		public String getProduct(String product)
		{
			return getAttribute("product");
		}

		public void setMetric(String metric)
		{
			setAttribute("metric", metric);
		}

		public String getMetric(String metric)
		{
			return getAttribute("metric");
		}

		public void setValue(Integer value)
		{
			setAttribute("_value", value);
		}

		public Integer getValue(Integer value)
		{
			return getAttributeAsInt("_value");
		}

		public void setPercentNational(Integer percentNational)
		{
			setAttribute("percentNational", percentNational);
		}

		public Integer getPercentNational(Integer percentNational)
		{
			return getAttributeAsInt("percentNational");
		}

		public void setHilite(String hilite)
		{
			setAttribute("_hilite", hilite);
		}

		public String getHilite(String hilite)
		{
			return getAttribute("_hilite");
		}
	}

	public String getIntro()
	{
		return DESCRIPTION;
	}
}