use of org.jaffa.persistence.AtomicCriteria in project jaffa-framework by jaffa-projects.
the class CodeHelperTx method addCriteria.
private void addCriteria(CriteriaElementDto field, Criteria criteria) {
if (field != null) {
String name = field.getFieldName();
String operator = field.getCriteria() != null ? field.getCriteria().getOperator() : null;
Object[] values = field.getCriteria() != null ? field.getCriteria().returnValuesAsObjectArray() : null;
if (operator != null && operator.equals(CriteriaField.RELATIONAL_IS_NULL))
criteria.addCriteria(name, Criteria.RELATIONAL_IS_NULL);
else if (operator != null && operator.equals(CriteriaField.RELATIONAL_IS_NOT_NULL))
criteria.addCriteria(name, Criteria.RELATIONAL_IS_NOT_NULL);
else if (values.length > 0) {
if (operator == null || operator.equals(CriteriaField.RELATIONAL_EQUALS))
criteria.addCriteria(name, Criteria.RELATIONAL_EQUALS, values[0]);
else if (operator.equals(CriteriaField.RELATIONAL_NOT_EQUALS))
criteria.addCriteria(name, Criteria.RELATIONAL_NOT_EQUALS, values[0]);
else if (operator.equals(CriteriaField.RELATIONAL_GREATER_THAN))
criteria.addCriteria(name, Criteria.RELATIONAL_GREATER_THAN, values[0]);
else if (operator.equals(CriteriaField.RELATIONAL_SMALLER_THAN))
criteria.addCriteria(name, Criteria.RELATIONAL_SMALLER_THAN, values[0]);
else if (operator.equals(CriteriaField.RELATIONAL_GREATER_THAN_EQUAL_TO))
criteria.addCriteria(name, Criteria.RELATIONAL_GREATER_THAN_EQUAL_TO, values[0]);
else if (operator.equals(CriteriaField.RELATIONAL_SMALLER_THAN_EQUAL_TO))
criteria.addCriteria(name, Criteria.RELATIONAL_SMALLER_THAN_EQUAL_TO, values[0]);
else if (operator.equals(CriteriaField.RELATIONAL_BEGINS_WITH))
criteria.addCriteria(name, Criteria.RELATIONAL_BEGINS_WITH, values[0]);
else if (operator.equals(CriteriaField.RELATIONAL_ENDS_WITH))
criteria.addCriteria(name, Criteria.RELATIONAL_ENDS_WITH, values[0]);
else if (operator.equals(CriteriaField.RELATIONAL_LIKE))
criteria.addCriteria(name, Criteria.RELATIONAL_LIKE, values[0]);
else if (operator.equals(CriteriaField.RELATIONAL_BETWEEN)) {
criteria.addCriteria(name, Criteria.RELATIONAL_GREATER_THAN_EQUAL_TO, values[0]);
if (values.length > 1)
criteria.addCriteria(name, Criteria.RELATIONAL_SMALLER_THAN_EQUAL_TO, values[1]);
} else if (operator.equals(CriteriaField.RELATIONAL_IN)) {
if (values.length == 1) {
criteria.addCriteria(name, Criteria.RELATIONAL_EQUALS, values[0]);
} else {
AtomicCriteria atomic = new AtomicCriteria();
atomic.addCriteria(name, Criteria.RELATIONAL_EQUALS, values[0]);
for (int i = 1; i < values.length; i++) atomic.addOrCriteria(name, Criteria.RELATIONAL_EQUALS, values[i]);
criteria.addAtomic(atomic);
}
}
}
}
}
use of org.jaffa.persistence.AtomicCriteria in project jaffa-framework by jaffa-projects.
the class FinderTx method addCriteria.
/**
* This method will update the input criteria, based on the 'field' and 'name' inputs.
* It is invoked by the concrete class while building a Criteria object to query the database.
* @param field The criteria field which will have the operator and value(s).
* @param name The name of the field for which the criteria has been passed.
* @param criteria The Criteria object being built for the query, and which will be updated based on the 'field' and 'name' inputs.
*/
public static void addCriteria(DateTimeCriteriaField field, String name, Criteria criteria) {
if (field != null) {
// validate the criteria
field.validate();
String operator = field.getOperator();
// @todo
// if(!field.isOperatorValid()) {
// throw new IllegalArgumentException(MessageHelper.findToken("error.jaffa.finder.criteriaField",new String[] {name,operator}));
// }
Object[] values = field.returnValuesAsObjectArray();
if (operator != null && operator.equals(CriteriaField.RELATIONAL_IS_NULL))
criteria.addCriteria(name, Criteria.RELATIONAL_IS_NULL);
else if (operator != null && operator.equals(CriteriaField.RELATIONAL_IS_NOT_NULL))
criteria.addCriteria(name, Criteria.RELATIONAL_IS_NOT_NULL);
else if (values != null && values.length > 0) {
if (operator != null && operator.length() == 0) {
// A non-Jaffa tool may pass empty operator and value, even when they didn't intend to pass them. Bail out!
if (values[0] == null)
return;
// A non-empty value has been passed. So treat the operator like an Equals type
operator = null;
}
if (operator == null || operator.equals(CriteriaField.RELATIONAL_EQUALS))
criteria.addAtomic(interpretDateTime(name, Criteria.RELATIONAL_EQUALS, (DateTime) values[0]));
else if (operator.equals(CriteriaField.RELATIONAL_NOT_EQUALS))
criteria.addAtomic(interpretDateTime(name, Criteria.RELATIONAL_NOT_EQUALS, (DateTime) values[0]));
else if (operator.equals(CriteriaField.RELATIONAL_GREATER_THAN))
criteria.addAtomic(interpretDateTime(name, Criteria.RELATIONAL_GREATER_THAN, (DateTime) values[0]));
else if (operator.equals(CriteriaField.RELATIONAL_SMALLER_THAN))
criteria.addAtomic(interpretDateTime(name, Criteria.RELATIONAL_SMALLER_THAN, (DateTime) values[0]));
else if (operator.equals(CriteriaField.RELATIONAL_GREATER_THAN_EQUAL_TO))
criteria.addAtomic(interpretDateTime(name, Criteria.RELATIONAL_GREATER_THAN_EQUAL_TO, (DateTime) values[0]));
else if (operator.equals(CriteriaField.RELATIONAL_SMALLER_THAN_EQUAL_TO))
criteria.addAtomic(interpretDateTime(name, Criteria.RELATIONAL_SMALLER_THAN_EQUAL_TO, (DateTime) values[0]));
else if (operator.equals(CriteriaField.RELATIONAL_BEGINS_WITH))
criteria.addAtomic(interpretDateTime(name, Criteria.RELATIONAL_BEGINS_WITH, (DateTime) values[0]));
else if (operator.equals(CriteriaField.RELATIONAL_ENDS_WITH))
criteria.addAtomic(interpretDateTime(name, Criteria.RELATIONAL_ENDS_WITH, (DateTime) values[0]));
else if (operator.equals(CriteriaField.RELATIONAL_LIKE))
criteria.addAtomic(interpretDateTime(name, Criteria.RELATIONAL_LIKE, (DateTime) values[0]));
else if (operator.equals(CriteriaField.RELATIONAL_BETWEEN)) {
if (values.length > 0 && values[0] != null)
criteria.addCriteria(name, Criteria.RELATIONAL_GREATER_THAN_EQUAL_TO, determineDateTimeLimit((DateTime) values[0], true));
if (values.length > 1 && values[1] != null)
criteria.addCriteria(name, Criteria.RELATIONAL_SMALLER_THAN_EQUAL_TO, determineDateTimeLimit((DateTime) values[1], false));
} else if (operator.equals(CriteriaField.RELATIONAL_IN)) {
if (values.length == 1) {
if (values[0] == null)
criteria.addCriteria(name, Criteria.RELATIONAL_IS_NULL);
else
criteria.addAtomic(interpretDateTime(name, Criteria.RELATIONAL_EQUALS, (DateTime) values[0]));
} else {
AtomicCriteria atomic = new AtomicCriteria();
if (values[0] == null)
atomic.addCriteria(name, Criteria.RELATIONAL_IS_NULL);
else
atomic.addAtomic(interpretDateTime(name, Criteria.RELATIONAL_EQUALS, (DateTime) values[0]));
for (int i = 1; i < values.length; i++) {
if (values[i] == null)
atomic.addOrCriteria(name, Criteria.RELATIONAL_IS_NULL);
else
atomic.addOrAtomic(interpretDateTime(name, Criteria.RELATIONAL_EQUALS, (DateTime) values[i]));
}
criteria.addAtomic(atomic);
}
}
}
}
}
use of org.jaffa.persistence.AtomicCriteria in project jaffa-framework by jaffa-projects.
the class FinderTx method addCriteria.
/**
* This method will update the input criteria, based on the 'field' and 'name' inputs.
* It is invoked by the concrete class while building a Criteria object to query the database.
* @param field The criteria field which will have the operator and value(s).
* @param name The name of the field for which the criteria has been passed.
* @param criteria The Criteria object being built for the query, and which will be updated based on the 'field' and 'name' inputs.
*/
public static void addCriteria(CriteriaField field, String name, Criteria criteria) {
if (field != null) {
// validate the criteria
field.validate();
String operator = field.getOperator();
// @todo - Throw runtime exception if an invalid operator is supplied
// if(!field.isOperatorValid()) {
// throw new IllegalArgumentException(MessageHelper.findToken("error.jaffa.finder.criteriaField",new String[] {name,operator}));
// }
Object[] values = field.returnValuesAsObjectArray();
if (operator != null && operator.equals(CriteriaField.RELATIONAL_IS_NULL))
criteria.addCriteria(name, Criteria.RELATIONAL_IS_NULL);
else if (operator != null && operator.equals(CriteriaField.RELATIONAL_IS_NOT_NULL))
criteria.addCriteria(name, Criteria.RELATIONAL_IS_NOT_NULL);
else if (values != null && values.length > 0) {
if (operator != null && operator.length() == 0) {
// A non-Jaffa tool may pass empty operator and value, even when they didn't intend to pass them. Bail out!
if (values[0] == null || (values[0] instanceof String && values[0].toString().length() == 0))
return;
// A non-empty value has been passed. So treat the operator like an Equals type
operator = null;
}
if (operator == null || operator.equals(CriteriaField.RELATIONAL_EQUALS))
criteria.addCriteria(name, Criteria.RELATIONAL_EQUALS, values[0]);
else if (operator.equals(CriteriaField.RELATIONAL_NOT_EQUALS))
criteria.addCriteria(name, Criteria.RELATIONAL_NOT_EQUALS, values[0]);
else if (operator.equals(CriteriaField.RELATIONAL_GREATER_THAN))
criteria.addCriteria(name, Criteria.RELATIONAL_GREATER_THAN, values[0]);
else if (operator.equals(CriteriaField.RELATIONAL_SMALLER_THAN))
criteria.addCriteria(name, Criteria.RELATIONAL_SMALLER_THAN, values[0]);
else if (operator.equals(CriteriaField.RELATIONAL_GREATER_THAN_EQUAL_TO))
criteria.addCriteria(name, Criteria.RELATIONAL_GREATER_THAN_EQUAL_TO, values[0]);
else if (operator.equals(CriteriaField.RELATIONAL_SMALLER_THAN_EQUAL_TO))
criteria.addCriteria(name, Criteria.RELATIONAL_SMALLER_THAN_EQUAL_TO, values[0]);
else if (operator.equals(CriteriaField.RELATIONAL_BEGINS_WITH))
criteria.addCriteria(name, Criteria.RELATIONAL_BEGINS_WITH, values[0]);
else if (operator.equals(CriteriaField.RELATIONAL_ENDS_WITH))
criteria.addCriteria(name, Criteria.RELATIONAL_ENDS_WITH, values[0]);
else if (operator.equals(CriteriaField.RELATIONAL_LIKE))
criteria.addCriteria(name, Criteria.RELATIONAL_LIKE, values[0]);
else if (operator.equals(CriteriaField.RELATIONAL_BETWEEN)) {
if (values.length > 0 && values[0] != null)
criteria.addCriteria(name, Criteria.RELATIONAL_GREATER_THAN_EQUAL_TO, values[0]);
if (values.length > 1 && values[1] != null)
criteria.addCriteria(name, Criteria.RELATIONAL_SMALLER_THAN_EQUAL_TO, values[1]);
} else if (operator.equals(CriteriaField.RELATIONAL_IN)) {
if (values.length == 1) {
if (values[0] == null)
criteria.addCriteria(name, Criteria.RELATIONAL_IS_NULL);
else
criteria.addCriteria(name, Criteria.RELATIONAL_EQUALS, values[0]);
} else {
AtomicCriteria atomic = new AtomicCriteria();
if (values[0] == null)
atomic.addCriteria(name, Criteria.RELATIONAL_IS_NULL);
else
atomic.addCriteria(name, Criteria.RELATIONAL_EQUALS, values[0]);
for (int i = 1; i < values.length; i++) {
if (values[i] == null)
atomic.addOrCriteria(name, Criteria.RELATIONAL_IS_NULL);
else
atomic.addOrCriteria(name, Criteria.RELATIONAL_EQUALS, values[i]);
}
criteria.addAtomic(atomic);
}
} else if (operator.equals(CriteriaField.RELATIONAL_NOT_IN)) {
if (values.length == 1) {
if (values[0] == null)
criteria.addCriteria(name, Criteria.RELATIONAL_IS_NOT_NULL);
else
criteria.addCriteria(name, Criteria.RELATIONAL_NOT_EQUALS, values[0]);
} else {
AtomicCriteria atomic = new AtomicCriteria();
if (values[0] == null)
atomic.addCriteria(name, Criteria.RELATIONAL_NOT_EQUALS, Criteria.RELATIONAL_IS_NULL);
else
atomic.addCriteria(name, Criteria.RELATIONAL_NOT_EQUALS, values[0]);
for (int i = 1; i < values.length; i++) {
if (values[i] == null)
atomic.addCriteria(name, Criteria.RELATIONAL_NOT_EQUALS, Criteria.RELATIONAL_IS_NULL);
else
atomic.addCriteria(name, Criteria.RELATIONAL_NOT_EQUALS, values[i]);
}
criteria.addAtomic(atomic);
}
}
}
}
}
use of org.jaffa.persistence.AtomicCriteria in project jaffa-framework by jaffa-projects.
the class CandidateKeyValidator method validateProperty.
/**
* {@inheritDoc}
*/
@Override
protected void validateProperty(T targetObject, String targetPropertyName, Object targetPropertyValue, List<RuleMetaData> rules, UOW uow) throws ApplicationException, FrameworkException {
String targetClassName = getActualClassName(targetObject.getClass());
IPersistent persistentTargetObject = null;
try {
// Use the associated persistentObject for a FlexBean instance
if (targetObject instanceof FlexBean && ((FlexBean) targetObject).getPersistentObject() != null) {
persistentTargetObject = ((FlexBean) targetObject).getPersistentObject();
targetClassName = persistentTargetObject.getClass().getName();
}
// This rules makes sense on IPersistent instances only. Bail out on all other instances
if (!(targetObject instanceof IPersistent)) {
return;
} else if (persistentTargetObject == null) {
persistentTargetObject = (IPersistent) targetObject;
}
// Obtain a Map containing key fields and corresponding values
Map<String, Object> keyValueMap = getKeyValueMap(targetClassName, (IPersistent) targetObject);
// Create a criteria such that the current object is not included in the search for candidate-key violations
AtomicCriteria criteriaToDiscardCurrentObject = createCriteriaToDiscardCurrentObject(persistentTargetObject, keyValueMap);
// Invoke each rule
for (RuleMetaData rule : rules) {
invoke(targetClassName, persistentTargetObject, uow, rule, criteriaToDiscardCurrentObject, keyValueMap);
}
} catch (ApplicationExceptions e) {
if (log.isDebugEnabled()) {
log.debug("Error in validateProperty method for object: " + targetClassName, e);
}
throw new JaffaRulesFrameworkException("Error in validateProperty method for object: " + targetClassName, null, e);
}
}
use of org.jaffa.persistence.AtomicCriteria in project jaffa-framework by jaffa-projects.
the class CandidateKeyValidator method invoke.
/**
* Apply the candidate-key validation.
*/
private void invoke(String targetClassName, IPersistent targetObject, UOW uow, RuleMetaData rule, AtomicCriteria criteriaToDiscardCurrentObject, Map<String, Object> keyValueMap) throws ApplicationException, ApplicationExceptions, FrameworkException {
if (log.isDebugEnabled()) {
log.debug("Applying " + rule + " on " + targetObject);
}
Criteria criteria = null;
String ck = rule.getParameter(RuleMetaData.PARAMETER_VALUE);
String[] ckFields = ck.split(",");
Boolean ignoreNull = Parser.parseBoolean(rule.getParameter("ignore-null"));
// Flex fields having null values are deleted from the database table.
// The persistence engine does not support queries of the type 'where not exists...'.
// Hence we'll retrieve all rows and throw an exception only if the fields in the collection are null in any of the rows
Collection<String> nullFields = null;
FlexClass flexClass = targetObject instanceof IFlexFields && ((IFlexFields) targetObject).getFlexBean() != null ? (FlexClass) ((IFlexFields) targetObject).getFlexBean().getDynaClass() : null;
boolean keyModified = false;
for (String ckField : ckFields) {
// determine if this is a flex field
FlexProperty flexProperty = flexClass != null ? flexClass.getDynaProperty(ckField) : null;
// Obtain the value
Object ckValue;
try {
ckValue = BeanHelper.getField(flexProperty != null ? ((IFlexFields) targetObject).getFlexBean() : targetObject, ckField);
if (ignoreNull != null && ignoreNull && ckValue == null) {
if (log.isDebugEnabled())
log.debug("Rule will be ignored since " + ckField + " is null");
return;
}
} catch (Exception e) {
if (log.isDebugEnabled())
log.debug("Exception thrown while trying to get value for the property " + ckField + ". It may not exist in the targetObject", e);
return;
}
// Perform the check only if the field is modified, or is NULL and Object has not been saved yet
if (!keyModified)
keyModified = isModified(flexProperty != null ? ((IFlexFields) targetObject).getFlexBean() : targetObject, ckField) || (ckValue == null && !targetObject.isDatabaseOccurence());
// and any other rule may cause the actual update to be executed.
if (flexProperty != null && flexProperty.getFlexInfo() != null && flexProperty.getFlexInfo().getProperty("domain-mapping") != null) {
ckField = flexProperty.getFlexInfo().getProperty("domain-mapping");
flexProperty = null;
}
// Add to criteria
if (criteria == null)
criteria = new Criteria();
if (ckValue == null) {
if (flexProperty != null) {
if (nullFields == null)
nullFields = new LinkedList<String>();
nullFields.add(ckField);
} else {
criteria.addCriteria(StringHelper.getUpper1(ckField), Criteria.RELATIONAL_IS_NULL);
}
} else {
if (flexProperty != null) {
Criteria flexCriteria = new Criteria();
flexCriteria.setTable(FlexFieldMeta.getName());
int i = 0;
for (String key : keyValueMap.keySet()) flexCriteria.addInnerCriteria("Key" + ++i, StringHelper.getUpper1(key));
flexCriteria.addCriteria(FlexFieldMeta.OBJECT_NAME, flexClass.getLogicalName());
flexCriteria.addCriteria(FlexFieldMeta.FIELD_NAME, flexProperty.getLogicalName());
flexCriteria.addCriteria(FlexFieldMeta.VALUE, DataTypeMapper.instance().map(ckValue, String.class, flexProperty.getFlexInfo() != null ? flexProperty.getFlexInfo().getProperty("layout") : null));
criteria.addAggregate(flexCriteria);
} else {
criteria.addCriteria(StringHelper.getUpper1(ckField), ckValue);
}
}
}
if (keyModified && criteria != null) {
if (criteriaToDiscardCurrentObject != null)
criteria.addAtomic(criteriaToDiscardCurrentObject);
criteria.setTable(targetClassName);
// try {
for (Object o : uow.query(criteria)) {
boolean violation = nullFields == null;
if (!violation) {
// Not a violation if any of the fields is not null
for (String ckField : nullFields) {
try {
violation = BeanHelper.getField(targetObject, ckField) == null;
if (!violation) {
break;
}
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug("Error in invoke method during query execution.", e);
}
throw new JaffaRulesFrameworkException("Error in invoke method during query execution on object: " + targetClassName, null, e);
}
}
}
if (violation) {
if (log.isDebugEnabled()) {
log.debug("Candidate key '" + ck + "' is not unique for the object " + targetObject);
}
String objectLabel = getObjectLabel(targetClassName, targetObject);
StringBuilder ckLabel = new StringBuilder();
for (int i = 0; i < ckFields.length; i++) {
if (i > 0) {
ckLabel.append(',');
}
ckLabel.append(getPropertyLabel(targetObject, ckFields[i]));
}
throw wrapException(new DuplicateCandidateKeyException(objectLabel, ckLabel.toString()), (T) targetObject, rule);
}
}
}
}
Aggregations