package com.smartgwt.sample.server.listener;

import java.util.Map;

import com.isomorphic.datasource.DSRequest;
import com.isomorphic.datasource.DataSource;
import com.isomorphic.datasource.DynamicDSGenerator;
import com.isomorphic.servlet.DataSourceLoader;

public class MyDataSourceLoader extends DataSourceLoader {
	private static final long serialVersionUID = -3039977589678144063L;

	public MyDataSourceLoader() {
		super();
		DataSource.addDynamicDSGenerator(new DynamicDSGenerator() {
			@SuppressWarnings("rawtypes")
			@Override public DataSource getDataSource(String arg0, DSRequest dsRequest) {
				DataSource dataSource = null;
				System.out.println("Looking for ID:" + arg0);
				if("employees".equals(arg0)) {
					StringBuffer stringBuffer = new StringBuffer();
					stringBuffer.append("<DataSource");
					stringBuffer.append("    ID=\"employees\"");
					stringBuffer.append("    serverType=\"sql\"");
					stringBuffer.append("    tableName=\"employeeTable\"");
					stringBuffer.append("    recordName=\"employee\"");
					stringBuffer.append("    testFileName=\"/examples/shared/ds/test_data/employees.data.xml\"");
					stringBuffer.append("    titleField=\"Name\"");
					stringBuffer.append(">");
					stringBuffer.append("    <fields>");
					stringBuffer.append("        <field name=\"EmployeeId\"      title=\"Employee ID\"     type=\"integer\"  primaryKey=\"true\"  required=\"true\"/>");
//					stringBuffer.append("        <field name=\"Name\"            title=\"Name\"            type=\"text\"     length=\"128\"/>");
					stringBuffer.append("        <field name=\"ReportsTo\"       title=\"Manager\"         type=\"integer\"  required=\"true\" ");
					stringBuffer.append("               foreignKey=\"employees.EmployeeId\"  rootValue=\"1\" detail=\"true\"/>");
					stringBuffer.append("        <field name=\"Job\"             title=\"Title\"           type=\"text\"     length=\"128\"/> ");
//					stringBuffer.append("        <field name=\"Email\"           title=\"Email\"           type=\"text\"     length=\"128\"/>");
					stringBuffer.append("        <field name=\"EmployeeType\"    title=\"Employee Type\"   type=\"text\"     length=\"40\"/>");
					stringBuffer.append("        <field name=\"EmployeeStatus\"  title=\"Status\"          type=\"text\"     length=\"40\"/>");
//					stringBuffer.append("        <field name=\"Salary\"          title=\"Salary\"          type=\"float\"/>");
					stringBuffer.append("        <field name=\"OrgUnit\"         title=\"Org Unit\"        type=\"text\"     length=\"128\"/>");
					stringBuffer.append("        <field name=\"Gender\"          title=\"Gender\"          type=\"text\"     length=\"7\">");
					stringBuffer.append("            <valueMap>");
					stringBuffer.append("                <value>male</value>");
					stringBuffer.append("                <value>female</value>");
					stringBuffer.append("            </valueMap>");
					stringBuffer.append("        </field>");
					stringBuffer.append("        <field name=\"MaritalStatus\"   title=\"Marital Status\"  type=\"text\"     length=\"10\">");
					stringBuffer.append("            <valueMap>");
					stringBuffer.append("                <value>married</value>");
					stringBuffer.append("                <value>single</value>");
					stringBuffer.append("            </valueMap>");
					stringBuffer.append("        </field>");
					
					Map criteria = dsRequest == null ? null : dsRequest.getCriteria();
					if(criteria != null) {
						for(Object obj : criteria.keySet()) {
							if(criteria.get(obj) != null && criteria.get(obj).toString().equals("true")) {
								stringBuffer.append(" <field name=\"" + obj + "\"   type=\"text\"     length=\"128\"/>");
							}
						}
					}
					
					
					stringBuffer.append("    </fields>");
					stringBuffer.append("<operationBindings><operationBinding operationType=\"fetch\"><whereClause> Gender = 'male' </whereClause></operationBinding></operationBindings>");
					stringBuffer.append("</DataSource>");
					try {
						System.out.println("Xml:" + stringBuffer.toString());
						dataSource = DataSource.fromXML(stringBuffer.toString());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				return dataSource;
			}
		});
	}
}
