package com.tandbergtv.workflow.driver.search;

import java.util.ArrayList;
import java.util.List;
/**
 * This class is used to searche if a number of variable instances have the save value for a 
 * workflowprocess. The paramList specifies the names of the variable instances and the value 
 * Object holds the value.
 * @author inaqvi
 *
 */
public class OrParameterMap extends SearchParameterBase {

	private List<String> paramList = new ArrayList<String>();
	
	private Object value;
	
	public OrParameterMap(String name, SearchType fieldType, Object val) {
		super(name, fieldType,true);
		value = val;
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}
	
	public void addParam(String fieldName){
		paramList.add(fieldName);
	}
	
	@Override
	public String getPredicate() {
		if(this.paramList == null || paramList.size() == 0)
			return "";
		/* Joining VariableInstance with ProcessInstance */
		String predicate = null;
		
		for (String fieldName : paramList) {
			/*
			 * We check to see that the tokenVariableMap of the variable instance is not null to avoid task level variable
			 * instances being included in the result.
			 */
			if(predicate == null)
				predicate = PROCESS_ALIAS + " =" + this.alias + ".processInstance AND " +
						this.alias + ".tokenVariableMap is not null AND (";
			else
				predicate+= " OR ";
			predicate += " ( ";
			/* Adding VariableInstance name predicate. */
			predicate += this.alias + ".name = '" + fieldName + "'";
			/* Adding VariableInstance value predicate. */
			predicate += " AND " + this.alias	+ ".value LIKE '%" + this.value + "%'";
			predicate += " ) ";
			
		}
		if (predicate == null)
			return "";
		return predicate + ") ";
	}

}
