package org.openforis.collect.datacleansing;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.openforis.collect.model.CollectSurvey;
import org.openforis.idm.metamodel.AttributeDefinition;
import org.openforis.idm.metamodel.NodeDefinition;
import org.openforis.idm.metamodel.NodeDefinitionVisitor;

/**
 * 
 * @author S. Ricci
 *
 */
public class DataErrorQueryGenerator {
	
	private static final String MISSING_DATA_QUERY_TITLE_FORMAT = "Missing data for '%s' attribute";
	private static final String MISSING_DATA_CONDITION_FORMAT = "idm:blank(%s)";

	public List<DataErrorQuery> generateMissingDataQueries(final CollectSurvey survey, final DataErrorType type) {
		List<DataErrorQuery> result = new ArrayList<DataErrorQuery>();
		survey.getSchema().traverse(new NodeDefinitionVisitor() {
			public void visit(NodeDefinition def) {
				if (def instanceof AttributeDefinition) {
					if ((def.getMinCount() != null && def.getMinCount() > 0) 
							|| StringUtils.isNotBlank(def.getRequiredExpression())) {
						DataErrorQuery query = new DataErrorQuery(survey);
						query.setTitle(String.format(MISSING_DATA_QUERY_TITLE_FORMAT, def.getName()));
						query.setType(type);
						query.setAttributeDefinition((AttributeDefinition) def);
						query.setEntityDefinition(def.getParentEntityDefinition());
						query.setConditions(String.format(MISSING_DATA_CONDITION_FORMAT, def.getName()));
					}
				}
			}
		});
		return result;
	}

}

@@ -350,16 +350,69 @@
 Visitor;
+%0Aimport org.openforis.idm.metamodel.validation.Check;
 %0A%0A/**%0A *
@@ -1542,11 +1542,1095 @@
 ult;%0A%09%7D%0A
+%09%0A%09public List%3CDataErrorQuery%3E generateValidationCheckQueries(CollectSurvey survey) %7B%0A%09%09final List%3CDataErrorQuery%3E result = new ArrayList%3CDataErrorQuery%3E();%0A%09%09survey.getSchema().traverse(new NodeDefinitionVisitor() %7B%0A%09%09%09public void visit(NodeDefinition def) %7B%0A%09%09%09%09if (def instanceof AttributeDefinition) %7B%0A%09%09%09%09%09for (Check%3C?%3E check : ((AttributeDefinition) def).getChecks()) %7B%0A%09%09%09%09%09%09DataErrorQuery query = createQuery(def, check);%0A%09%09%09%09%09%09result.add(query);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09return result;%0A%09%7D%0A%0A%09private DataErrorQuery createQuery(NodeDefinition def, Check%3C?%3E check) %7B%0A%09%09StringBuilder sb = new StringBuilder();%0A%09%09if (StringUtils.isNotBlank(check.getCondition())) %7B%0A%09%09%09sb.append(check.getCondition());%0A%09%09%09sb.append(%22 and %22);%0A%09%09%7D%0A%09%09sb.append(check.getExpression());%0A%09%09DataErrorQuery query = new DataErrorQuery((CollectSurvey) def.getSurvey());%0A%09%09//TODO%0A%09%09query.setTitle(%22%22);%0A//%09%09%09%09query.setType(type);%0A%09%09query.setAttributeDefinition((AttributeDefinition) def);%0A%09%09query.setEntityDefinition(def.getParentEntityDefinition());%0A%09%09query.setConditions(sb.toString());%0A%09%09return query;%0A%09%7D%0A%09
 %0A%7D%0A

Prepare data error query generation