Dorado 动态创建Dataset实例

Dorado 动态创建Dataset实例来自“dorado 5原理、特性与高级用法 V1.0.doc”.


package sample.skills.createDatasetTest;

import com.bstek.dorado.data.Dataset;
import com.bstek.dorado.data.DatasetListener;
import com.bstek.dorado.data.Field;
import com.bstek.dorado.data.ParameterSet;
import com.bstek.dorado.data.db.AutoDBField;
import com.bstek.dorado.data.db.AutoSqlDataset;
import com.bstek.dorado.data.db.BaseMatchRule;
import com.bstek.dorado.utils.collection.ObjectCollection;
import com.bstek.dorado.utils.variant.DataType;
import com.bstek.dorado.utils.variant.Variant;
import com.bstek.dorado.view.DefaultViewModel;
import com.bstek.dorado.view.EventHandler;
import com.bstek.dorado.view.ViewModel;
import com.bstek.dorado.view.control.Button;
import com.bstek.dorado.view.control.Control;
import com.bstek.dorado.view.control.DataPilot;
import com.bstek.dorado.view.control.PagePilot;
import com.bstek.dorado.view.control.autoform.AutoForm;
import com.bstek.dorado.view.control.autoform.CustomFormElement;
import com.bstek.dorado.view.control.autoform.FormGroup;
import com.bstek.dorado.view.control.command.Command;
import com.bstek.dorado.view.control.command.QueryCommand;
import com.bstek.dorado.view.control.command.UpdateCommand;
import com.bstek.dorado.view.control.dropdown.DatasetDropDown;
import com.bstek.dorado.view.control.dropdown.DropDown;
import com.bstek.dorado.view.control.dropdown.ListDropDown;
import com.bstek.dorado.view.control.table.DataTable;
import com.bstek.dorado.view.data.DatasetWrapper;
import com.bstek.dorado.view.data.FormDataset;
import com.bstek.dorado.view.data.ViewDataset;
import com.bstek.dorado.view.data.ViewField;
import com.bstek.dorado.view.data.validate.Validator;
import com.bstek.dorado.view.data.validate.impl.RequiredValidator;

public class Sample_4_1_All_ViewModel extends DefaultViewModel {

	public void init(int state) throws Exception {
		
		this.createDataset("Form", "datasetForm");
		this.createDataset("Wrapper", "datasetEmployee");
		this.createDataset("Wrapper", "datasetDept");

		super.init(state);

		if (state == ViewModel.STATE_VIEW) {
			this.createControl("ListDropDown", "dropdownSex");
			this.createControl("DatasetDropDown", "dropdownDept");
			this.createControl("QueryCommand", "commandQuery");
			this.createControl("Button", "buttonQuery");
			this.createControl("AutoForm", "formForm");
			this.createControl("PagePilot", "pagepilotEmployee");
			this.createControl("DataPilot", "datapilotEmployee");
			this.createControl("DataTable", "tableEmployee");
			this.createControl("UpdateCommand", "commandUpdate");
			this.createControl("Button", "buttonSave");

			EventHandler event = new EventHandler("onDatasetsPrepared");

			StringBuffer script = new StringBuffer();
			script.append("var record = datasetForm.getCurrent();");
			script.append("record.setValue(\"EMPLOYEE_ID\",\"ANLIN\");");
			event.setScript(script.toString());
			this.addEventHandler(event);
		}
	}

	protected void initDataset(ViewDataset dataset) throws Exception {
		super.initDataset(dataset);
		if (dataset instanceof FormDataset) {
			if ("datasetForm".equals(dataset.getId())) {
				if (this.getState() == ViewModel.STATE_VIEW) {
					dataset.setAutoLoadData(false);
					dataset.setInsertOnEmpty(true);
				}
				Field fieldEMPLOYEE_ID = dataset.addField("EMPLOYEE_ID");
				fieldEMPLOYEE_ID.setLabel("员工编号");
				Field fieldEMPLOYEE_NAME = dataset.addField("EMPLOYEE_NAME");
				fieldEMPLOYEE_NAME.setLabel("员工姓名");
				Field fieldDEPT_ID = dataset.addField("DEPT_ID");
				fieldDEPT_ID.setLabel("部门");
				if (this.getState() == ViewModel.STATE_VIEW) {
					((ViewField) fieldDEPT_ID).setDropDown("dropdownDept");
				}
				Field fieldSEX = dataset.addField("SEX");
				fieldSEX.setLabel("性别");
			}
		} else if (dataset instanceof DatasetWrapper) {
			if ("datasetEmployee".equals(dataset.getId())) {
				dataset.setPageSize(10);
				DatasetListener datasetListener = new CreateDSHand_datasetEmployeeListener();
				dataset.addDatasetListener(datasetListener);

				Variant v = new Variant(DataType.STRING);
				dataset.parameters().setVariant("EMPLOYEE_ID", v);

				DatasetWrapper datasetEmployeeDW = (DatasetWrapper) dataset;
				datasetEmployeeDW.setWrappedType("AutoSql");

				AutoSqlDataset datasetEmployee = (AutoSqlDataset) ((DatasetWrapper) dataset)
						.getWrappedDataset();
				datasetEmployee.setOriginTable("EMPLOYEE");
				datasetEmployee.setKeyFields("EMPLOYEE_ID");

				datasetEmployee.addDummyField("select");

				AutoDBField field = (AutoDBField) datasetEmployee
						.addField("EMPLOYEE_ID");
				field.setTable("EMPLOYEE");
				field.setOriginField("EMPLOYEE_ID");
				field.setLabel("员工编号");
				if (field instanceof ViewField) {
					Validator validator = new RequiredValidator();
					((ViewField) field).addValidator(validator);
				}

				field = (AutoDBField) datasetEmployee.addField("DEPT_ID");
				field.setTable("EMPLOYEE");
				field.setOriginField("DEPT_ID");
				field.setLabel("部门");

				field = (AutoDBField) datasetEmployee.addField("EMPLOYEE_NAME");
				field.setTable("EMPLOYEE");
				field.setOriginField("EMPLOYEE_NAME");
				field.setLabel("员工姓名");

				field = (AutoDBField) datasetEmployee.addField("SEX");
				field.setTable("EMPLOYEE");
				field.setOriginField("SEX");
				field.setLabel("性别");
				if (field instanceof ViewField) {
					((ViewField) field).setDropDown("dropdownSex");
				}

				field = (AutoDBField) datasetEmployee.addField("BIRTHDAY");
				field.setTable("EMPLOYEE");
				field.setOriginField("BIRTHDAY");
				field.setLabel("出生日期");
				field.setDataType(DataType.DATETIME);

				field = (AutoDBField) datasetEmployee.addField("MARRIED");
				field.setTable("EMPLOYEE");
				field.setOriginField("MARRIED");
				field.setDataType(DataType.BOOLEAN);

				field = (AutoDBField) datasetEmployee.addField("SALARY");
				field.setTable("EMPLOYEE");
				field.setOriginField("SALARY");
				field.setDataType(DataType.DOUBLE);

				field = (AutoDBField) datasetEmployee.addField("DEGREE");
				field.setTable("EMPLOYEE");
				field.setOriginField("DEGREE");

				field = (AutoDBField) datasetEmployee.addField("EMAIL");
				field.setTable("EMPLOYEE");
				field.setOriginField("EMAIL");

				field = (AutoDBField) datasetEmployee.addField("WEB");
				field.setTable("EMPLOYEE");
				field.setOriginField("WEB");

				field = (AutoDBField) datasetEmployee.addField("CMNT");
				field.setTable("EMPLOYEE");
				field.setOriginField("CMNT");

				BaseMatchRule baseMatchRule = datasetEmployee.addBaseMatchRule(
						"EMPLOYEE_ID", "=", ":EMPLOYEE_ID");
				baseMatchRule.setEscapeEnabled(true);
				baseMatchRule.setLevel(1);
				baseMatchRule.setTable("EMPLOYEE");

				baseMatchRule = datasetEmployee.addBaseMatchRule(
						"EMPLOYEE_NAME", "like", ":EMPLOYEE_NAME");
				baseMatchRule.setEscapeEnabled(true);
				baseMatchRule.setLevel(1);
				baseMatchRule.setTable("EMPLOYEE");

				baseMatchRule = datasetEmployee.addBaseMatchRule("DEPT_ID",
						"=", ":DEPT_ID");
				baseMatchRule.setEscapeEnabled(true);
				baseMatchRule.setLevel(1);
				baseMatchRule.setTable("EMPLOYEE");

				baseMatchRule = datasetEmployee.addBaseMatchRule("SEX", "=",
						":SEX");
				baseMatchRule.setEscapeEnabled(true);
				baseMatchRule.setLevel(1);
				baseMatchRule.setTable("EMPLOYEE");
				baseMatchRule.setDataType(DataType.BOOLEAN);

			} else if ("datasetDept".equals(dataset.getId())) {
				DatasetWrapper datasetDeptDW = (DatasetWrapper) dataset;
				datasetDeptDW.setWrappedType("AutoSql");

				AutoSqlDataset datasetDept = (AutoSqlDataset) ((DatasetWrapper) dataset)
						.getWrappedDataset();
				datasetDept.setOriginTable("DEPT");

				AutoDBField field = (AutoDBField) datasetDept
						.addField("DEPT_ID");
				field.setTable("DEPT");
				field.setOriginField("DEPT_ID");
				field.setLabel("部门");

				field = (AutoDBField) datasetDept.addField("BRANCH_ID");
				field.setTable("DEPT");
				field.setOriginField("BRANCH_ID");

				field = (AutoDBField) datasetDept.addField("DEPT_NAME");
				field.setTable("DEPT");
				field.setOriginField("DEPT_NAME");

			}
		}
	}

	protected void initControl(Control control) throws Exception {
		System.out.println(5);
		super.initControl(control);
		if (control instanceof DataTable) {
			if ("tableEmployee".equals(control.getId())) {
				DataTable tableEmployee = (DataTable) control;
				DatasetWrapper datasetEmployeeDW = (DatasetWrapper) this
						.getDataset("datasetEmployee");
				Dataset datasetEmployee = datasetEmployeeDW.getWrappedDataset();

				tableEmployee.setDataset(datasetEmployee.getId());
				tableEmployee.setHeight("100%");
				tableEmployee.setShowFilterBar(true);
				tableEmployee.setShowHScrollBar(false);
				tableEmployee.setWidth("100%");

				Field selectField = datasetEmployee.getField("select");
				tableEmployee.addColumnByField(selectField);
				Field EMPLOYEE_IDField = datasetEmployee
						.getField("EMPLOYEE_ID");
				tableEmployee.addColumnByField(EMPLOYEE_IDField);
				Field DEPT_IDField = datasetEmployee.getField("DEPT_ID");
				tableEmployee.addColumnByField(DEPT_IDField);
				Field EMPLOYEE_NAMEField = datasetEmployee
						.getField("EMPLOYEE_NAME");
				tableEmployee.addColumnByField(EMPLOYEE_NAMEField);
				Field SEXField = datasetEmployee.getField("SEX");
				tableEmployee.addColumnByField(SEXField);
				Field BIRTHDAYField = datasetEmployee.getField("BIRTHDAY");
				tableEmployee.addColumnByField(BIRTHDAYField);
			}
		} else if (control instanceof AutoForm) {
			if ("formForm".equals(control.getId())) {
				AutoForm formForm = (AutoForm) control;
				formForm.setDataset("datasetForm");
				formForm.generateDefaultElements();
				ObjectCollection groups = formForm.getGroups();
				if (groups.size() > 0) {
					FormGroup groupDefault = (FormGroup) groups.get(0);
					groupDefault.setTitle("员工查询");
					CustomFormElement customFormElement = (CustomFormElement) groupDefault
							.addElement("Custom", "buttonQuery");
					customFormElement.setControlId("buttonQuery");
					customFormElement.setColSpan(2);
					customFormElement.setShowLabel(false);
					customFormElement.setControlAlign("right");
				}

			}
		} else if (control instanceof Command) {
			if (control instanceof QueryCommand) {
				if ("commandQuery".equals(control.getId())) {
					QueryCommand commandQuery = (QueryCommand) control;
					commandQuery.setConditionDataset("datasetForm");
					commandQuery.setQueryDataset("datasetEmployee");
					EventHandler event = new EventHandler("beforeExecute");

					StringBuffer script = new StringBuffer();
					script.append("var record = datasetForm.getCurrent();");
					script.append("if(record){");
					script.append("	var EMPLOYEE_NAME = record.getValue(\"EMPLOYEE_NAME\");");
					script.append("	if(EMPLOYEE_NAME){");
					script.append("		EMPLOYEE_NAME = \"%\" + EMPLOYEE_NAME + \"%\";");
					script.append("		command.parameters().setValue(\"EMPLOYEE_NAME\",EMPLOYEE_NAME);");
					script.append("	}else{");
					script.append("		command.parameters().removeParameter(\"EMPLOYEE_NAME\");");
					script.append("	}");
					script.append("}");
					event.setScript(script.toString());
					commandQuery.addEventHandler(event);
				}
			} else if (control instanceof UpdateCommand) {
				if ("commandUpdate".equals(control.getId())) {
					UpdateCommand commandUpdate = (UpdateCommand) control;
					commandUpdate.addDatasetInfo("datasetEmployee");
				}
			}
		} else if (control instanceof Button) {
			if ("buttonQuery".equals(control.getId())) {
				Button buttonQuery = (Button) control;
				buttonQuery.setValue("查询");
				buttonQuery.setWidth("80");
				buttonQuery.setCommand("commandQuery");
			} else if ("buttonSave".equals(control.getId())) {
				Button buttonSave = (Button) control;
				buttonSave.setValue("保存");
				buttonSave.setWidth("80");
				buttonSave.setCommand("commandUpdate");
			}
		} else if (control instanceof PagePilot) {
			if ("pagepilotEmployee".equals(control.getId())) {
				PagePilot pagepilotEmployee = (PagePilot) control;
				pagepilotEmployee.setDataset("datasetEmployee");
			}
		} else if (control instanceof DropDown) {
			if (control instanceof ListDropDown) {
				if ("dropdownSex".equals(control.getId())) {
					ListDropDown dropdownSex = (ListDropDown) control;
					dropdownSex.setMapValue(true);
					dropdownSex.addItem(null, "<空>");
					dropdownSex.addItem("true", "男");
					dropdownSex.addItem("false", "女");
				}
			} else if (control instanceof DatasetDropDown) {
				if ("dropdownDept".equals(control.getId())) {
					DatasetDropDown dropdownDept = (DatasetDropDown) control;
					dropdownDept.setDataset("datasetDept");
					dropdownDept.setStartWithEmptyRecord(true);
					dropdownDept.setMapValue(true);
					dropdownDept.setLabelField("DEPT_NAME");
					dropdownDept.setValueField("DEPT_ID");
				}
			}
		} else if (control instanceof DataPilot) {
			if ("datapilotEmployee".equals(control.getId())) {
				DataPilot datapilotEmployee = (DataPilot) control;
				datapilotEmployee.setDataset("datasetEmployee");
			}
		}
	}

	protected void initDatasets() throws Exception {
		super.initDatasets();
	}

	protected void initControls() throws Exception {
		super.initControls();
	}

	protected void doLoadData(ViewDataset dataset) throws Exception {
		super.doLoadData(dataset);
	}

	protected void doLoadData() throws Exception {
		super.doLoadData();
	}

	protected void doUpdateData(ParameterSet parameters,
			ParameterSet outParameters) throws Exception {
		super.doUpdateData(parameters, outParameters);
	}

}

你可能感兴趣的:(Web)