com.imsweb.validation.entities.Condition Maven / Gradle / Ivy
/*
* Copyright (C) 2007 Information Management Services, Inc.
*/
package com.imsweb.validation.entities;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import org.codehaus.groovy.control.CompilationFailedException;
import com.imsweb.validation.ConstructionException;
import com.imsweb.validation.ValidationServices;
/**
* A Condition allows a pre-condition to be set for one or several rules.
*
* Created on Nov 9, 2007 by depryf
*/
public class Condition {
/**
* DB ID for this condition
*/
protected Long _conditionId;
/**
* Application ID (same as a display ID) for this condition
*/
protected String _id;
/**
* Name for this condition
*/
protected String _name;
/**
* Java path for this condition
*/
protected String _javaPath;
/**
* Groovy expression for this condition
*/
protected String _expression;
/**
* Description for this condition
*/
protected String _description;
/**
* Validator under which this condition is registered
*/
protected Validator _validator;
/**
* Set of properties contained in the expression
*/
protected Set _rawProperties;
/**
* Set of lookup IDs used in the expression
*/
protected Set _usedLookupIds;
/**
* Set of potiental context entries (they are potential because they might not all be context entries; but if a context entry is used, it will be in this list...
*/
protected Set _potentialContextEntries;
/**
* Constructor.
*
* Created on Nov 9, 2007 by depryf
*/
public Condition() {
_rawProperties = new HashSet<>();
_usedLookupIds = new HashSet<>();
_potentialContextEntries = new HashSet<>();
}
/**
* Getter for the condition persistence ID. Must be unique within the validation engine.
*
* In a system persisting the conditions in a database, that would represent a database primary key.
* In a system using only memory objects loaded from XML, it should be assigned using the
* getNextConditionSequence() method in ValidationServices.
*
* Created on Mar 10, 2011 by depryf
* @return the condition persistence ID
*/
public Long getConditionId() {
return _conditionId;
}
/**
* Setter for the condition persistence ID. Must be unique within the validation engine.
*
* In a system persisting the conditions in a database, that would represent a database primary key.
* In a system using only memory objects loaded from XML, it should be assigned using the
* getNextConditionSequence() method in ValidationServices.
*
* Created on Mar 10, 2011 by depryf
* @param conditionId condition persistence ID
*/
public void setConditionId(Long conditionId) {
_conditionId = conditionId;
}
/**
* Getter for the ID. The condition ID must be unique within the validation engine.
*
* Created on Mar 10, 2011 by depryf
* @return the condition ID
*/
public String getId() {
return _id;
}
/**
* Setter for the ID. The condition ID must be unique within the validation engine.
*
* Created on Mar 10, 2011 by depryf
* @param id the condition ID
*/
public void setId(String id) {
_id = id;
}
/**
* Getter for the name.
*
* Created on Mar 10, 2011 by depryf
* @return the condition name
*/
public String getName() {
return _name;
}
/**
* Setter for the name.
*
* Created on Mar 10, 2011 by depryf
* @param name the condition name
*/
public void setName(String name) {
_name = name;
}
/**
* Getter for the java path.
*
* Created on Mar 10, 2011 by depryf
* @return the condition java path
*/
public String getJavaPath() {
return _javaPath;
}
/**
* Setter for the java path.
*
* Created on Mar 10, 2011 by depryf
* @param path the java path, cannot be null or blank
*/
public void setJavaPath(String path) {
_javaPath = path;
}
/**
* Getter for the expression (Groovy script).
*
* Created on Mar 10, 2011 by depryf
* @return the condition expression
*/
public String getExpression() {
return _expression;
}
/**
* Setter for the expression (Groovy script).
*
* This method will compile the script and gather the used properties.
*
* Created on Mar 10, 2011 by depryf
* @param expression the condition expression
* @throws ConstructionException if the expression is not valid Groovy
*/
public void setExpression(String expression) throws ConstructionException {
_expression = expression;
if (expression != null && !expression.trim().isEmpty()) {
synchronized (this) {
try {
ValidationServices.getInstance().parseExpression("condition", _expression, _rawProperties, _potentialContextEntries, _usedLookupIds);
}
catch (CompilationFailedException e) {
throw new ConstructionException("Unable to parse condition " + getId(), e);
}
}
}
}
/**
* Getter for the description.
*
* Created on Mar 10, 2011 by depryf
* @return the condition description
*/
public String getDescription() {
return _description;
}
/**
* Setter for the description.
*
* Created on Mar 10, 2011 by depryf
* @param description the condition description
*/
public void setDescription(String description) {
_description = description;
}
/**
* Getter for the parent Validator.
*
* Created on Mar 10, 2011 by depryf
* @return a Validator
*/
public Validator getValidator() {
return _validator;
}
/**
* Setter for the parent Valdidator.
*
* Created on Mar 10, 2011 by depryf
* @param validator the parent Validator.
*/
public void setValidator(Validator validator) {
_validator = validator;
}
/**
* Getter for the properties used in the condition.
*
* Created on Mar 10, 2011 by depryf
* @return the set of properties, maybe empty but never null
*/
public Set getRawProperties() {
return _rawProperties;
}
/**
* Getter for the lookup IDs used in the condition.
*
* Created on Mar 10, 2011 by depryf
* @return the set of used lookup IDs, maybe empty but never null
*/
public Set getUsedLookupIds() {
return _usedLookupIds;
}
/**
* Getter for the potential context entries referenced in the condition.
*
* Created on Mar 10, 2011 by depryf
* @return the set of potential context entries, maybe empty but never null
*/
public Set getPotentialContextEntries() {
return _potentialContextEntries;
}
@Override
public String toString() {
return getId();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Condition)) return false;
Condition condition = (Condition)o;
if (_conditionId != null && condition._conditionId != null)
return Objects.equals(_conditionId, condition._conditionId);
return Objects.equals(_id, condition._id);
}
@Override
public int hashCode() {
if (_conditionId != null)
return Objects.hash(_conditionId);
return Objects.hash(_id);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy