<commit_before>package org.openforis.collect.presenter {
	import flash.events.Event;
	import flash.events.FocusEvent;
	
	import mx.collections.IList;
	
	import org.openforis.collect.client.ClientFactory;
	import org.openforis.collect.metamodel.proxy.RangeAttributeDefinitionProxy;
	import org.openforis.collect.metamodel.proxy.UnitProxy;
	import org.openforis.collect.remoting.service.UpdateRequest;
	import org.openforis.collect.remoting.service.UpdateRequestOperation;
	import org.openforis.collect.remoting.service.UpdateRequestOperation$Method;
	import org.openforis.collect.ui.component.input.RangeAttributeRenderer;
	
	/**
	 * 
	 * @author <NAME>
	 * */
	public class RangeAttributePresenter extends CompositeAttributePresenter {
		
		public function RangeAttributePresenter(view:RangeAttributeRenderer) {
			_view = view;
			super(view);
			view.rangeInputField.applyChangesOnFocusOut = false;
			//depends on view.currentState
			if(view.unitInputField != null) {
				view.unitInputField.applyChangesOnFocusOut = false;
				view.unitInputField.dropDownList.addEventListener(Event.CHANGE, unitInputFieldChangeHandler);
			}
		}
		
		private function get view():RangeAttributeRenderer {
			return RangeAttributeRenderer(_view);
		}
		
		override internal function initEventListeners():void {
			super.initEventListeners();
			view.rangeInputField.addEventListener(FocusEvent.FOCUS_OUT, rangeInputFieldFocusOutHandler);
		}
		
		protected function rangeInputFieldFocusOutHandler(event:FocusEvent):void {
			if(view.rangeInputField.changed) {
				updateValue();
			}
		}
		
		protected function unitInputFieldChangeHandler(event:Event):void {
			if(! view.rangeInputField.isEmpty()) {
				updateValue();
			}
		}
		
		protected function updateValue():void {
			var updReq:UpdateRequest = new UpdateRequest();
			var updateValueOp:UpdateRequestOperation = view.rangeInputField.presenter.createUpdateValueOperation();
			updReq.addOperation(updateValueOp);
			var updateUnitOp:UpdateRequestOperation = createUpdateUnitOperation();
			if(updateValueOp.value == null) {
				//clear unit
				updateUnitOp.value = null;
			}
			updReq.addOperation(updateUnitOp);
			ClientFactory.dataClient.updateActiveRecord(updReq, null, faultHandler);
		}

		protected function createUpdateUnitOperation():UpdateRequestOperation {
			var attrDefn:RangeAttributeDefinitionProxy = RangeAttributeDefinitionProxy(view.attributeDefinition);
			var result:UpdateRequestOperation = null;
			if(view.unitInputField) {
				result = view.unitInputField.presenter.createUpdateValueOperation();
			} else {
				result = new UpdateRequestOperation();
				result.method = UpdateRequestOperation$Method.UPDATE;
				result.parentEntityId = view.attribute.parentId;
				result.nodeName = view.attributeDefinition.name;
				result.nodeId = view.attribute.id;
				result.fieldIndex = 1;
				result.value = attrDefn.defaultUnit.name;
			}
			return result;
		}
		
		override protected function initViewState():void {
			var attrDefn:RangeAttributeDefinitionProxy = RangeAttributeDefinitionProxy(view.attributeDefinition);
			var units:IList = attrDefn.units;
			if(units.length > 0) {
				if(units.length == 1) {
					view.currentState = RangeAttributeRenderer.SINGLE_UNIT_STATE;
					var unit:UnitProxy = UnitProxy(units.getItemAt(0));
					view.unitLabel.text = unit.name;
				} else {
					view.currentState = RangeAttributeRenderer.MULTIPLE_UNIT_STATE;
					view.unitInputField.dataProvider = units;
					if(attrDefn.defaultUnit != null) {
						view.unitInputField.defaultValue = attrDefn.defaultUnit.name;<commit_msg>OFC-485 SE form (HH income F.1) - Cash income column do not accept multiple values eg Agriculture,livestock & wage<commit_after>package org.openforis.collect.presenter {
	import flash.events.Event;
	import flash.events.FocusEvent;
	
	import mx.collections.IList;
	
	import org.openforis.collect.client.ClientFactory;
	import org.openforis.collect.metamodel.proxy.RangeAttributeDefinitionProxy;
	import org.openforis.collect.metamodel.proxy.UnitProxy;
	import org.openforis.collect.remoting.service.UpdateRequest;
	import org.openforis.collect.remoting.service.UpdateRequestOperation;
	import org.openforis.collect.remoting.service.UpdateRequestOperation$Method;
	import org.openforis.collect.ui.component.input.RangeAttributeRenderer;
	
	/**
	 * 
	 * @author <NAME>
	 * */
	public class RangeAttributePresenter extends CompositeAttributePresenter {
		
		public function RangeAttributePresenter(view:RangeAttributeRenderer) {
			_view = view;
			super(view);
			view.rangeInputField.applyChangesOnFocusOut = false;
			//depends on view.currentState
			if(view.unitInputField != null) {
				view.unitInputField.applyChangesOnFocusOut = false;
				view.unitInputField.dropDownList.addEventListener(Event.CHANGE, unitInputFieldChangeHandler);
			}
		}
		
		private function get view():RangeAttributeRenderer {
			return RangeAttributeRenderer(_view);
		}
		
		override internal function initEventListeners():void {
			super.initEventListeners();
			view.rangeInputField.addEventListener(FocusEvent.FOCUS_OUT, rangeInputFieldFocusOutHandler);
		}
		
		protected function rangeInputFieldFocusOutHandler(event:FocusEvent):void {
			if(view.rangeInputField.changed) {
				updateValue();
			}
		}
		
		protected function unitInputFieldChangeHandler(event:Event):void {
			if(! view.rangeInputField.isEmpty()) {
				updateValue();
			}
		}
		
		protected function updateValue():void {
			var updReq:UpdateRequest = new UpdateRequest();
			var updateValueOp:UpdateRequestOperation = view.rangeInputField.presenter.createUpdateValueOperation();
			updReq.addOperation(updateValueOp);
			var updateUnitOp:UpdateRequestOperation = createUpdateUnitOperation();
			if(updateValueOp.value == null) {
				//clear unit
				updateUnitOp.value = null;
			}
			updReq.addOperation(updateUnitOp);
			ClientFactory.dataClient.updateActiveRecord(updReq, null, faultHandler);
		}

		protected function createUpdateUnitOperation():UpdateRequestOperation {
			var attrDefn:RangeAttributeDefinitionProxy = RangeAttributeDefinitionProxy(view.attributeDefinition);
			var result:UpdateRequestOperation = null;
			if(view.unitInputField) {
				result = view.unitInputField.presenter.createUpdateValueOperation();
			} else {
				result = new UpdateRequestOperation();
				result.method = UpdateRequestOperation$Method.UPDATE;
				result.parentEntityId = view.attribute.parentId;
				result.nodeName = view.attributeDefinition.name;
				result.nodeId = view.attribute.id;
				result.fieldIndex = 2;
				result.value = attrDefn.defaultUnit.name;
			}
			return result;
		}
		
		override protected function initViewState():void {
			var attrDefn:RangeAttributeDefinitionProxy = RangeAttributeDefinitionProxy(view.attributeDefinition);
			var units:IList = attrDefn.units;
			if(units.length > 0) {
				if(units.length == 1) {
					view.currentState = RangeAttributeRenderer.SINGLE_UNIT_STATE;
					var unit:UnitProxy = UnitProxy(units.getItemAt(0));
					view.unitLabel.text = unit.name;
				} else {
					view.currentState = RangeAttributeRenderer.MULTIPLE_UNIT_STATE;
					view.unitInputField.dataProvider = units;
					if(attrDefn.defaultUnit != null) {
						view.unitInputField.defaultValue = attrDefn.defaultUnit.name;
{'fraction_non_alphanumeric': 0.09413219733406623, 'fraction_numerical': 0.001649031194173423, 'mean_word_length': 4.907467532467533, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6966790', 'n_tokens_mistral': 2413, 'n_tokens_neox': 2037, 'n_words': 323}