Showing posts with label View Object. Show all posts
Showing posts with label View Object. Show all posts

Wednesday, November 24, 2010

Bit #27 - Setting a View Object attribute's Queryable property

Expanding on Bit #26 - Overriding getAttributeDef() to conditionally set an attribute property, you can set a View Object's attribute queryable property simply by calling setQueriable() on its ViewAttributeDefImpl. As explained in Bit #26, you can get the AttributeDef attribute definition by calling getAttributeDef() and passing the attribute index as a parameter. In order to call any of the attribute set...() methods, you will have to cast the AttributeDef interface to an ViewAttributeDefImpl implementation. By the way, did you know that by setting an attribute's queryable property will show or hide a View Criteria Item (i.e. the criterion) based on the attribute on the af:query dialog? Another way to hide the criterion was outlined in Bit #25 - Hiding a View Criteria item based on some condition. It will also show or hide the attribute from the Add Fields drop-down in the Advanced mode of af:query. One last thing: check-out the plethora of set...() methods available in the ViewAttributeDefImpl. As you may have guessed it, pretty much everything that is available declaratively on the Edit Attribute dialog, can be set (or reset) dynamically in your code based on some business condition. For a complete use case, check out the reference listed below.

Example:

  /**
   * Helper to set an attribute's queriable property.
   * Example:
   *    setQueriable(ViewRowImpl.SOMEFIELDINDEX, something.equals(somethingElse));
   *
   * @param attribIndex the attribute index
   * @param condition the condition (true/false)
   */
  protected void setQueriable(int attribIndex, boolean condition) {
      // set the attribute queryable as needed
      AttributeDef def = super.getAttributeDef(attribIndex);
      boolean queryable = condition;

      // set/reset queriable only if needed
      if (def != null && def.isQueriable() != queryable) {
          ViewAttributeDefImpl attributeDef = (ViewAttributeDefImpl)def;
          attributeDef.setQueriable(queryable);
      }
  }


Context:

View Object Implementation Class

References:

http://dstas.blogspot.com/2010/03/set-queryable-property-on-attribute.html

Monday, November 22, 2010

Bit #25 - Hiding a View Criteria item based on some condition

You can hide or show a View Criteria item (i.e. a criterion), based on some business logic condition by calling setProperty() on an oracle.jbo.ViewCriteriaItem. The method accepts a ViewCriteriaItemHints parameter indicating the view criteria item property to set - in this case CRITERIA_RENDERED_MODE - and a second ViewCriteriaItemHints parameter indicating the property value - in this case any of the CRITERIA_RENDERED_MODE_BOTH, CRITERIA_RENDERED_MODE_BASIC, CRITERIA_RENDERED_MODE_ADVANCED, CRITERIA_RENDERED_MODE_NEVER, CRITERIA_RENDERED_MODE_DEFAULT. To hide or show a criterion, you will have to look for it by iterating through all the criteria rows and all the criteria items for each row. Once found, call setProperty() to set its render mode. Here is an example helper method that illustrates this case.

Example:

    /**
     * Hides a view criterion.
     * Example: The following will hide the SourcePolicyNbr criterio if the condition
     *          is true. If the condition is false, it will show the criterio in BOTH basic and
     *          advanced.
     *        
     *          hideCriteriaItem("SomeCriteria", "SomeCriteriaItem",
     *             someCondition==true, ViewCriteriaItemHints.CRITERIA_RENDERED_MODE_BOTH);
     *
     *
     * @param viewCriteriaName the view criteria name
     * @param criteriaItemName the criterio name
     * @param condition the HIDE condition
     * @param showHint the SHOW hint
     */
     protected void hideCriteriaItem(String viewCriteriaName,
                                    String criteriaItemName, boolean condition,
                                    String showHint) {
        if (viewCriteriaName != null) {
            ViewCriteria v = this.getViewCriteria(viewCriteriaName);
            if (v != null) {
                boolean found = false;
                while (v.hasNext() && !found) {
                    ViewCriteriaRow vcr = (ViewCriteriaRow)v.next();
                    if (vcr != null) {
                        ViewCriteriaItem[] vcis = vcr.getCriteriaItemArray();
                        if (vcis != null && vcis.length > 0) {
                            for (int j = 0; j < vcis.length && !found; j++) {
                                ViewCriteriaItem vci = vcis[j];
                                if (vci != null && criteriaItemName != null &&
                                    criteriaItemName.equals(vci.getName())) {
                                    found = true;
                                    vci.setProperty(ViewCriteriaItemHints.CRITERIA_RENDERED_MODE,
                                                    condition ?
                                                    ViewCriteriaItemHints.CRITERIA_RENDERED_MODE_NEVER :
                                                    showHint);
                                    v.saveState();
                                }
                            }
                        }
                    }
                }
            }
        }
    }


Context:

View Object Implementation Class

Tuesday, September 7, 2010

Bit #23 - Using findAndSetCurrentRowByKey() to set the View Object currency

You can set the currency on a View Object (VO) by calling its findAndSetCurrentRowByKey() method. The method accepts two arguments: a Key object that is used to locate the row in the VO and an integer indicating the range position of row (for VOs configured with Range Paging Access Mode). Below is an example. The example saves the VO currency, queries the VO and finally restores its currency. Click on the references below to get all the details on the example.

Example:

   /**
   * Helper to requery the view.
   */
   public void requery() {
      Row currentRow = getCurrentRow();
      Key currentRowKey = currentRow.getKey();
      int rangePosOfCurrentRow = getRangeIndexOf(currentRow);
      int rangeStartBeforeQuery = getRangeStart();
      executeQuery();
      setRangeStart(rangeStartBeforeQuery);
      findAndSetCurrentRowByKey(currentRowKey,rangePosOfCurrentRow);
}

Context

View Object Implementation Class
Application Module Implementation Class

References
http://radio-weblogs.com/0118231/2004/11/22.html


Saturday, May 15, 2010

Bit #18 - Dynamically changing the View Object's query WHERE clause

You can dynamically change the View Object's (VO) query WHERE clause by overriding the buildWhereClause() VO method. When doing so, ensure that you call the base class' buildWhereClause() first, to let the framework do its processing before making your own changes. The StringBuffer parameter that is passed to the method is the complete query SQL statement. Do your changes directly onto it. When done, make sure that you return appropriately a true/false boolean to indicate whether a WHERE clause was appended to the query or not. Here is an example.

Example:


    @Override
    protected boolean buildWhereClause(StringBuffer sqlBuffer, int noBindVars) {

        // call ViewObjectImpl's buildWhereClause() to allow the framework to do its processing
        boolean hasWhereClause = super.buildWhereClause(sqlBuffer, noBindVars);

        if (hasWhereClause) { // framework added a WHERE clause
            // modify the WHERE clause as needed
        }
        else { // framework did not add a WHERE clause, so we need to add it ourselves
            // add a WHERE clause here
            hasWhereClause = true; // ensure that is set to notify the framework
        }

        return hasWhereClause; // return true/false to indicate whether a WHERE clause was added
    }


Context:

View Object Implementation

Tuesday, May 4, 2010

Bit #16 - Removing a row from a query collection without deleting it from the database

There are times when you want to remove a row from a query collection (the query result) without actually removing it from the database. The query collection - oracle.jbo.server.QueryCollection - gets popullated each time the View is executed - when the View's associated query is run, and represents the query result. While the Row.remove() will remove the query collection row it will also remove the underlying Entity row - for an Entity-based View - and post a deletion to the database. If your programming task requires that the row is removed from the query collection only, i.e. removing a table row in the UI without actually posting a delete to the database, use the Row method removeFromCollection() instead. Just be aware that each time the View is re-executed the Row will show up once again!

Example:

        // in the context of the ApplModuleImpl

        // remove the current row from the query collection
        EmployeesRowImpl employee = (EmployeesRowImpl)(this.getEmployees().getCurrentRow());
        employee.removeFromCollection();

        // the employee row has been removed from the result set and cannot be used anymore


Context:

Application Module Implementation Class
View Object Implementation Class

Sunday, May 2, 2010

Bit #15 - Using a Key to locate a Row in a View Object, Pt. 2

Instead of using the findByKey() method to locate a number of rows in the View Object identified by a Key attribute - explained in Bit #14 - Using a Key to locate a Row in a View Object, you can use the View Object getRow() method supplying the Key as an argument. This method will return the Row identified by the Key supplied as an argument to it. An example follows.

Example:

    // in the context of the ApplModuleImpl

    // locate the employee's department
    Number departmentId =
        ((EmployeesRowImpl)(this.getEmployees().getCurrentRow())).getDepartmentId();
    Key keyDepartment = new Key(new Object[] { departmentId });

    // get the department based on the department identifier
    DepartmentsRowImpl department =
        (DepartmentsRowImpl)this.getDepartments().getRow(keyDepartment);

    if (department != null) {
        // you can access the Department's attributes here....
    }



Context:

Application Module Implementation Class
View Object Implementation Class

Wednesday, April 21, 2010

Bit #14 - Using a Key to locate a Row in a View Object

Instead of iterating a View Object using a RowSetIterator - as described in Bit #4 - Iterating a View Object using a secondary RowSetIterator, you can locate a row directly using the ViewObject method findByKey(). This will work as long as you indicate a View Object attribute as a Key Attribute. To use this method, you will need to first instantiate a jbo.Key object and then pass it as an argument to findByKey(). findByKey() will return an array of rows that match the Key object. Special attention should be given when constructing Key objects for multi-part keys and for View Objects that are based on more than one Entity Objects. These cases are explained in detail in the Oracle Fusion Middleware Java API Reference for Oracle ADF Model documentation referenced below.

Example:

        // in the context of the ApplModuleImpl
      
        // locate the employee's department
        Number departmentId =
            ((EmployeesRowImpl)(this.getEmployees().getCurrentRow())).getDepartmentId();
        Key keyDepartment = new Key(new Object[] { departmentId });
      
        // the second argument indicates the maximum number of rows to return
        Row[] departments = this.getDepartments().findByKey(keyDepartment, 1);
        if (departments != null && departments.length > 0) {
            DepartmentsRowImpl department = (DepartmentsRowImpl)departments[0];
          
            // you can access the Department's attributes here....
        }

Context:

Application Module Implementation Class
View Object Implementation Class

Reference:

Oracle Fusion Middleware Java API Reference for Oracle ADF Model, findByKey

Monday, April 19, 2010

Bit #13 - Overriding create() to set the default value for a View Row attribute

One way to set the default value for a View Row attribute is to override its create() method in your custom View Row Implementation and call the attribute setter method to set its default value. Calling the attribute setter from inside the overridden create() method does not mark the new row as changed and it behaves like declaratively assigning a default value for the attribute. An example follows.

Example:

    @Override
    protected void create(AttributeList attributeList) {

        super.create(attributeList);

        // set main company's telephone number as default
        this.setPhoneNumber("6145551212");
    }


Context:

View Object Row Implementation

Tuesday, April 13, 2010

Bit #9 - Controlling the updatability of View Object attributes programmatically

The updatability of a View Object attribute can be controlled programmatically via the isAttributeUpdateable() method. When you look at its documentation, the declarative precedence that determines the attribute updatability should become clear. Programmatically, you override this method in your View Object Implementation Java file that you generate declaratively in JDeveloper. isAttributeUpdateable() is called for each attribute in the View Object. The index that is passed as an argument to the method determines the attribute index as it is returned by the AttributesEnum enumeration defined in the View Object Implementation source - for the specific attribute. To indicate that the specific attribute is updateable, isAttributeUpdateable() returns true; it returns false otherwise.

Example:

    @Override
    public boolean isAttributeUpdateable(int index) {
        boolean isUpdateable = super.isAttributeUpdateable(index);
        
        // do not allow updating first and last name
        if (index == FIRSTNAME || index == LASTNAME) {
            isUpdateable = false;
        }
        
        return isUpdateable;
    }

Context:

View Object Row Implementation

Reference:

Oracle Fusion Middleware Java API Reference for Oracle ADF Model, Row Interface


Thursday, April 8, 2010

Bit #7 - Reseting the View Criteria associated with a View Object

To reset the View Criteria defined for a specific View Object, first you will need to retrieve the View Criteria from the View Object. You do this by calling the View Object method getViewCriteria() specifying the name of the View Criteria. This method will return a ViewCriteria object. Proceed with removing the criteria from the View Object by calling removeViewCriteria() on it. Call resetCriteria() on the ViewCriteria to reset them and finally re-apply the criteria to the View Object by calling applyViewCriteria() on it. Here is an example.

Example:

        // in the context of the Application Module Implementation

        ViewObjectImpl vo = this.getSomeViewObject();

        ViewCriteria vc = vo.getViewCriteria("criteria_name");
        vo.removeViewCriteria("criteria_name");

        vc.resetCriteria();
        vo.applyViewCriteria(vc);


Context:

Application Module Implementation
View Object Implementation


Wednesday, April 7, 2010

Bit #6 - Removing all rows from a View Object

To remove all rows from a View Object programmatically - i.e. clear or reset the View Object, call its executeEmptyRowSet() method. You can do this anywhere in the context of the Application Module or View Object Implementation.

Example:

    // in the context of the Application Module Implementation

   ViewObjectImpl vo = this.getSomeViewObject();
   vo.executeEmptyRowSet();


Context:

Application Module Implementation
View Object Implementation


Reference:

What Does executeEmptyRowSet() Do?

Thursday, April 1, 2010

Bit #4 - Iterating a View Object using a secondary RowSetIterator

The recommended way to iterate a View Object is via a secondary RowSetIterator returned after calling createRowSetIterator() on the View Object. It is suggested to call reset() on the RowSetIterator before iterating it. This will move the currency to the slot before the first row. Iterate while calling hasNext() to check for next row availability and subsequently by calling next() to get the next row. When done, call closeRowSetIterator() to close the row set iterator. Failure to use a secondary RowSetIterator when directly iterating the View Object, could lead to end user confusion as the row set currency changes affecting the user interface.

Example:

// in the context of the AppModuleImpl

       RowSetIterator iterator = viewObject.createRowSetIterator(null);
       

       iterator.reset();

while (iterator.hasNext()) {
   Row row = iterator.next();
 

// process the row here
}

iterator.closeRowSetIterator();


Context:

Application Module Implementation class
View Object Implementation class


Reference:

Oracle® Fusion Middleware Fusion Developer's Guide for Oracle Application Development Framework 11g Release 1 (11.1.1) Part Number B31974-05 [Section 9.7.6 What You May Need to Know About Programmatic Row Set Iteration]

Tuesday, March 30, 2010

Bit #2 - Setting a bind variable value programmatically

To set the value of a bind variable programmatically, you will need to first get the View Object where the bind variable is defined. Then from the View Object you need to get access to the VariableValueManager via the call ensureVariableManager(). Finally you need to call setVariableValue() on the VariableValueManager to set the bind variable to a specific value. Once all these are done, execute the View Object query with the new bind variable value by calling executeQuery()  on the view as shown below.

Example:

        ViewObjectImpl view = this.getSomeView();
        VariableValueManager vm = view.ensureVariableManager();
        vm.setVariableValue("bindVariableName", value);
        view.executeQuery();

Context:

Application Module Implementation class
View Object Implementation class


Related Posts Plugin for WordPress, Blogger...