package com.mycompany.client;

import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.OperationBinding;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.DSProtocol;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;

public class TestForm extends DynamicForm {

	class PersonDS extends RestDataSource {

		public PersonDS() {
			super();

			setID("PERSON_DS");
			setDataFormat(DSDataFormat.JSON);
			setShowPrompt(false);

			OperationBinding fetchOperation = new OperationBinding();
			fetchOperation.setOperationType(DSOperationType.FETCH);
			fetchOperation.setDataProtocol(DSProtocol.POSTMESSAGE);
			fetchOperation.setDataURL("test_data/person.data.js");

			OperationBinding addOperation = new OperationBinding();
			addOperation.setOperationType(DSOperationType.ADD);
			addOperation.setDataProtocol(DSProtocol.POSTMESSAGE);
			addOperation.setDataURL("test_data/person.add.js");

			OperationBinding updateOperation = new OperationBinding();
			updateOperation.setOperationType(DSOperationType.UPDATE);
			updateOperation.setDataProtocol(DSProtocol.POSTMESSAGE);
			updateOperation.setDataURL("test_data/person.update.js");

			this.setOperationBindings(fetchOperation, addOperation, updateOperation);
		}
	}

	class AddressDS extends RestDataSource {

		public AddressDS() {
			super();

			setID("ADDRESS_ID");
			setDataFormat(DSDataFormat.JSON);
			setShowPrompt(false);

			OperationBinding fetchOperation = new OperationBinding();
			fetchOperation.setOperationType(DSOperationType.FETCH);
			fetchOperation.setDataProtocol(DSProtocol.POSTMESSAGE);
			fetchOperation.setDataURL("test_data/address.data.js");

			this.setOperationBindings(fetchOperation);
		}
	}

	public TestForm() {
		AddressDS addressDS = new AddressDS();
		DataSourceIntegerField addressId = new DataSourceIntegerField("id");
		addressId.setPrimaryKey(true);
		DataSourceTextField addressStreet = new DataSourceTextField("street", "Street");

		addressDS.setFields(addressId, addressStreet);

		PersonDS personDS = new PersonDS();
		DataSourceIntegerField personId = new DataSourceIntegerField("id");
		personId.setPrimaryKey(true);
		personDS.addField(personId);

		DataSourceTextField personName = new DataSourceTextField("name", "Name");
		personDS.addField(personName);

		DataSourceField addressField = new DataSourceField();
		addressField.setName("address");
		addressField.setTypeAsDataSource(addressDS);
		personDS.addField(addressField);

		TextItem nameItem = new TextItem("name");
		nameItem.setTitle("Person");
		SelectItem addressItem = new SelectItem("address");
		addressItem.setTitle("Address");
		addressItem.setOptionDataSource(addressDS);
		addressItem.setDisplayField("street");
		addressItem.setValueField("id");
		addressItem.setDataPath("address/id");
		addressItem.setAutoFetchData(false);

		this.setAutoWidth();
		this.setAutoFetchData(false);
		this.setValidateOnExit(true);
		this.setWrapItemTitles(false);

		this.setDataSource(personDS);
		this.setFields(nameItem, addressItem);
	}
}
