/**
 * 
 */
package org.openforis.collect.designer.form.validator;

import org.apache.commons.lang3.StringUtils;
import org.openforis.idm.metamodel.NodeDefinition;
import org.zkoss.bind.ValidationContext;
import org.zkoss.util.resource.Labels;

/**
 * @author S. Ricci
 *
 */
public class DistanceCheckFormValidator extends CheckFormValidator {

	private String MIN_DISTANCE_FIELD = "minDistanceExpression";
	private String MAX_DISTANCE_FIELD = "maxDistanceExpression";
	
	private String MIN_OR_MAX_REQUIRED_MESSAGE_KEY = "survey.schema.node.check.distance.validation.min_or_max_distance_not_specified";
	
	@Override
	protected void internalValidate(ValidationContext ctx) {
		super.internalValidate(ctx);
		if ( validateMinOrMaxExpressionRequireness(ctx) ) {
			NodeDefinition contextNode = getContextNode(ctx);
			validateValueExpression(ctx, contextNode, MIN_DISTANCE_FIELD);
			validateValueExpression(ctx, contextNode, MAX_DISTANCE_FIELD);
		}
	}

	protected boolean validateMinOrMaxExpressionRequireness(ValidationContext ctx) {
		String minExpr = getValue(ctx, MIN_DISTANCE_FIELD);
		String maxExpr = getValue(ctx, MAX_DISTANCE_FIELD);
		if ( StringUtils.isBlank(minExpr) && StringUtils.isBlank(maxExpr) ) {
			this.addInvalidMessage(ctx, MIN_DISTANCE_FIELD, Labels.getLabel(MIN_OR_MAX_REQUIRED_MESSAGE_KEY));
			this.addInvalidMessage(ctx, MAX_DISTANCE_FIELD, Labels.getLabel(MIN_OR_MAX_REQUIRED_MESSAGE_KEY));
			return false;
		} else {
			return true;
		}
	}
	
}

@@ -340,32 +340,227 @@
 tor %7B%0A%0A%09private 
+static final String SOURCE_POINT_EXPRESSION_FIELD = %22sourcePointExpression%22;%0A%09private static final String DESTINATION_POINT_EXPRESSION_FIELD = %22destinationPointExpression%22;%0A%09private static final 
 String MIN_DISTA
@@ -597,32 +597,45 @@
 sion%22;%0A%09private 
+static final 
 String MAX_DISTA
@@ -907,16 +907,68 @@
 e(ctx);%0A
+%09%09NodeDefinition contextNode = getContextNode(ctx);%0A
 %09%09if ( v
@@ -1017,61 +1017,8 @@
 ) %7B%0A
-%09%09%09NodeDefinition contextNode = getContextNode(ctx);%0A
 %09%09%09v
@@ -1145,20 +1145,177 @@
 FIELD);%0A
-
 %09%09%7D%0A
+%09%09validateValueExpression(ctx, contextNode, SOURCE_POINT_EXPRESSION_FIELD);%0A%09%09validateValueExpression(ctx, contextNode, DESTINATION_POINT_EXPRESSION_FIELD);%0A
 %09%7D%0A%0A%09pro

Fix distance check source and distance point expressions validation