Tuesday, December 10, 2013

ADF Query Component

one of the components I find very helpful in ADF is the ADF:QueryComponent.
since you can simply drag the ViewCriteria to the page and add it as a component, and it will be rendered based on the design time metadata.
but it can be a bit stressful, since you cannot edit it as freely as other components.

one such example is the operations allowed in the advaned mode :equal, not equal, is blank ,......
and if for some reason you want to change the text of the operation.

you can achieve this by doing the following:
1. open the desire View Object.
2. define a viewCriteria as needed.
3. in the source mode of the View Object add the following:
 <CompOper
          Name="Between"
          Oper="BETWEEN"
          ToDo="-1"
            MinCardinality="0"
            MaxCardinality="0"/>
          <CompOper
          Name="NotBetween"
          Oper="NOTBETWEEN"
          ToDo="-1"
            MinCardinality="0"
            MaxCardinality="0"/>
          <CompOper
          Name="GreaterThan"
          Oper=">"
          ToDo="-1"
            MinCardinality="0"
            MaxCardinality="0"/>
          <CompOper
          Name="GreaterThanEq"
          Oper=">="
          ToDo="-1"
            MinCardinality="0"
            MaxCardinality="0"/>
          <CompOper
          Name="LessThan"
          Oper="&lt;"
          ToDo="-1"
            MinCardinality="0"
            MaxCardinality="0"/>
          <CompOper
          Name="LessThanEq"
            Oper="&lt;="
          ToDo="-1"
            MinCardinality="0"
            MaxCardinality="0"/>
            <CompOper
          Name="IsNotBlank"
            Oper="ISNOTBLANK"
          ToDo="1"
            MinCardinality="0"
            MaxCardinality="0"
            OperDesc="Employee Does Have ID"/>
inside ViewCriteriaItem.

ToDo=-1 indicates that the operator will not be added, 1 the operator will be added.
OperDesc= the string that will be used in the select item from which you select your operation



for a list of operators see:

Adding busniess rule validation at runtime

Recently I came across the following link:
and tried to test it myself, there were some issue but it worked out eventually.
 I modified the requirements to achieve the following:
a. add an expression validator at runtime with custom message.
b. remove the added validator at runtime.
here are the detailed steps to do it:
  1.  create a new ADF application.
  2. in the model project create new business components from tables - will use the employees table from the HR schema.
  3. open the entity and generate :EntityObjectImpl and EntityDefImpl
  4. in the entityDefImpl add the following code:

     String addExpressionValidator(String attributeName, String groovyExpression,
                                String errorMessage) {
        AttributeDef attributeDef = findAttributeDef(attributeName);

        JboExpressionValidator jboExpressionValidator =
            new JboExpressionValidator(false, groovyExpression);
        jboExpressionValidator.setErrorMsgId("dynamic_Message");
        Map map = new HashMap();
        map.put("0", errorMessage);
        jboExpressionValidator.setErrorMsgExpressions(map);
        ((AttributeDefImpl)attributeDef).addValidator(jboExpressionValidator);
        jboExpressionValidator.setName(jboExpressionValidator.getName()+System.currentTimeMillis());
        return jboExpressionValidator.getName();

    }

    void removeExpressionValidator(String attributeName,
                                   String validatorName) {
        AttributeDefImpl attributeDef =
            (AttributeDefImpl)findAttributeDef(attributeName);
        List validators = attributeDef.getValidators();
        JboAbstractValidator targetValidator = null;
        for (Object _validator : validators) {
            targetValidator = (JboAbstractValidator)_validator;
            if (targetValidator.getName().equalsIgnoreCase(validatorName)) {
                break;
            }
        }
        attributeDef.removeValidator(targetValidator);

    }
  5.in the bundle used for the messages of the model add the following:
dynamic_Message={0}
6. expose the above method as a viewobject method.
 7, design the apge as you see fit
now at runtime  
as you can see I added the folloing:
attribute name:Salary
Expression:newValue<=oldValue*1.5
message: "new salary value("+newValue+") should be less than or equal to old salary value("+oldValue+"*1.5)"
please note the message, the adf processes this message as a groovy expression.