package com.hp.isomorphic.client.issues;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.data.AdvancedCriteria;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.Criterion;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.VLayout;

public class GridCriteriaTestSample extends VLayout{
	private ListGrid grid;
	
	public GridCriteriaTestSample() {
		DataSourceField[] dsFields = new DataSourceField[3];
		dsFields[0] = new DataSourceField("id", FieldType.INTEGER, "ID");
		dsFields[0].setPrimaryKey(true);
		dsFields[1] = new DataSourceField("job_status", FieldType.TEXT, "Status");
		dsFields[2] = new DataSourceField("job_deleting", FieldType.TEXT, "Deleting");

		DataSource ds = new DataSource();
		ds.setFields(dsFields);
		
		grid = new ListGrid();
		grid.setDataSource(ds);

		Criteria advCriteria = SmartGWTFilterBuilder.convertToCriteria("((job_status != 'Completed') AND (job_status != 'Aborted')) AND (job_deleting = false)", ds);
		GWT.log("advCriteria.isAdvanced(): " + advCriteria.isAdvanced());
		GWT.log("advCriteria instanceof AdvancedCriteria: " + (advCriteria instanceof AdvancedCriteria));
		
		grid.setCriteria(advCriteria);
		
		Criteria criteria = grid.getCriteria();
		GWT.log("criteria.isAdvanced(): " + criteria.isAdvanced());
		GWT.log("criteria instanceof AdvancedCriteria: " + (criteria instanceof AdvancedCriteria));
		
		Criterion c = new Criterion();
		try {
			c.addCriteria(criteria);
		} catch (Throwable caught) {
			GWT.log("Exception: " + caught.getLocalizedMessage());
		}
	}
}
