Friday, March 9, 2012

Defining Unique Key Constraints for Non-Primary Key Attributes in ADF-BC

Share this Post
Greetings,

ADF BC (Business Components) allows you to write code declaratively instead of writing spaghetti code in Java.

One of the feature is Alternate key constraint which is different from Primary and Unique key constraints available in database. Usually Primary key values are coming from database sequence so you don't have to worry about their uniqueness but if you like to validate uniqueness on other columns e.g. email address,Region or Country Name that's where Alternate key comes into action.

Following are some properties of alternate keys:

  • You can check Uniqueness at record level e.g. Department Name must be unique 
  • You can have many alternate keys as compare to Primary keys
  • You can look for a row in entity object using findByKey() method
  • If the key is found in entity objects ADF will throw TooManyObjectsException


Lets look into example (Download the complete example)

Use case:
Database schema: HR @ Oracle XE

Column Country name must be unique in Countries table. We have primary key CountryId in the table as shown in the slide




Solution:

  1. Create an Alternate key constraint on Entity object.
  2. Add Unique Key validation on Entity Object using alternate Key

Creating Alternate Key:

Right click the Entity Object and Choose "New Entity Constraint"



Enter the name of the constraint, Enter the Attribute where you want to add constraint in our case select Country Name





You can verify the Alternate key constraint Under general tab of entity object properties




Creating Unique Validation:

Go to Business Rules tab in Entity Object properties and Click Create New Validation by selecting "Entity Validators" as shown in the slide



Select "Unique Key" from Rule Type list. Select the Alternate key constraint from Keys


Go to Failure tab and Enter custom error message



Run the Application Module, Create new Record and Enter Unique country name like Brazil, You would see the similar error message.


Also on JSF page you would see error message on Commit



Happy JDeveloping,
Zeeshan Baig







Friday, March 2, 2012

Using Programmatic Validation in ADFBC

Share this Post
Greetings,

This month of March i will be focusing on ADF business components. So first entry in this series is how to write programmatic validations for entities in ADF business components.

I have covered a similar concept using ADF Domain types  which gives us global validation control instead of writing code for specific entities.

As you might be familiar that ADF BC provides facility to write validations in declarative manner but sometimes  the use case is complex for that you can write programmatic validations as follows

1. Add a method that will do validation in EntityImpl.java class
2. Call that method as part of validation cycle in validateEntity() method which is called at commit time when any attribute has been updated.

Let's get into detail

In this example use case (Download the example)

Our business rule says that all email address should be first letter of First name and full Last name of the employee

e.g. If the name is Zeeshan Baig then email address should be ZBAIG

1. Generate EntityImpl java class by double clicking on entity, go to Java tab and click on the pencil button choose generate employeeImpl class and click OK as shown in the slide



2. Write your custom method that will perform validation. In our use case is as follows

    public boolean checkValidEmail() {
        //Email must be first letter of First name and Full last name of employee
        //e.g Zeeshan Baig should be entered as zbaig
        String validEmailRule = this.getFirstName().substring(0,1) + this.getLastName();      

        if (getEmail().compareToIgnoreCase(validEmailRule) == 0 && getEmail() != null) {
            return true;
        } else {
            return false;
        }
    }

3. Override validateEntity() method but clicking override button enter text validate, select method and click OK



4. Override the code of validateEntity() method as follows

    protected void validateEntity() {
        if (!checkValidEmail()) {
            throw new JboException("Email Address should start with First letter of First Name and Complete Last Name.");
        }
        super.validateEntity();
    }

5. Compile the class and Run Application module to test in ADF business component browser




6. Enter wrong email address and click Validate Button to test you should receive Error alert



7. Enter valid email as per the rule and click Validate No alerts should come click Commit you should be able to see the successful commit message in the log




Happy JDeveloping,
Zeeshan Baig

Wednesday, February 22, 2012

How to Change Database Schema Password For Oracle Content Server

Share this Post
Hi,

Sometimes it is required to recover forgotten password of Oracle Content Server repository.

The purpose of this post is to identify where you are required to do changes if you are going to reset content repository database account password.

Usually the database schema owner of Oracle content server is <Your-Prefix>_OCS if your prefix during installation was DEV then username in database will be DEV_OCS.

Your DBA changed the password using following command

alter user DEV_OCS identified by newPassword;

Change In Weblogic Console:

1. Open Weblogic Admin console by following URL

http://mysername:myport/console
e.g http://myserver:7001/console

2. Go to Services > JDBC > Data Sources > CSDS > Configuration > Connection Pool




3. Enter new password and Press Save as shown




4. Weblogic will ask you to restart the data source, follow as shown in the slide






Change Password in Content Server Configuration:

To change password in Linux environment you need X-Windows / VNC or Login to Box using Tools like Reflections

1. Go to your content-server domain directory as follows


/u01/Oracle/Middleware/user_projects/domains/myDomain/ucm/cs/bin/


2. Run SystemProperties file

$ ./SystemProperties

3. A properties window will appear. Go to Database tab and change the password and hit OK



The changes will propogate out to the config.cfg file restart content server if required.

Hope this will help,
Zeeshan Baig










Friday, January 6, 2012

How to set Different JVM Heap Sizes for Admin and Managed Servers in Weblogic

Share this Post
Hi,

As per performance tuning guidelines it is good practice to start your Admin server with lower JVM heap size than your other managed servers because the Admin server doesn't required much resources.
By doing this you can utilize the free memory with others where it is needed (This makes sense when you are running Admin and Managed servers on same host)

Problem:
As i mentioned above our requirement is to use different JVM heapsize for Adminserver and only specific webcenter WLS_Spaces servers of a cluster.

One way to change the heap size is to set in weblogic admin console click here

In the example i will set JVM heap size -Xms and -Xmx values to1MB for AdminServer and 2MB each for WLS_Spaces managed servers of a cluster.

Solution:

1. Shutdown Admin and Managed servers

2. Add the following line of code in setDomain.env file. you can find the file under bin directory of weblogic domain home

Make sure you add these lines at-least after "export XMX_JROCKIT_32BIT" so it will override any other default settings.

e.g. /u01/Oracle/Middleware/user_projects/domains/myDomain/bin

# Set 1024MB for AdminServer

if [ "${SERVER_NAME}" == "AdminServer" ] ; then
      USER_MEM_ARGS="-Xms1024m -Xmx1024m"
    export USER_MEM_ARGS
fi

#Set 2MB if server name contains WLS_Spaces ( it will cover WLS_Spaces1,2,3,4..)

if [[ "${SERVER_NAME}" == *WLS_Spaces* ]] ; then
    USER_MEM_ARGS="-Xms2048m -Xmx2048m"
    export USER_MEM_ARGS
fi

3. Start Adminserver to verify if your change is working (if ok then start all others)

4. Verify by check the JVM heapsize using Jrockit mission control or Linux process
e.g. $ ps -ef|grep AdminServer




Have a nice day,
Zeeshan Baig



Wednesday, December 28, 2011

Oracle ADF - Storing temporary values in PageFlowScope created at Run time

Share this Post
Greetings,

Some time you need to store temporary values on the page like a global variable. Oracle ADF provides various bean scopes like pageflow scope, session scope etc etc we can definitely use them

But can you create pageflow scope at run-time? the answer is YES


Lets explore a simple example

Example:
When you press the button on the screen it will count and display how many times you have pressed the button and counter will be different for each browser window or tab



Technical detail:

  • We have an input text component and its value using a page flow scope bean attribute called 'counter'
  • We got a button on screen which is calling an action listener method doCounting()

How Example works:

  • When we pressed the button on screen it creates a pageflow scope bean on runtime called 'myCounter' and increment this value on every button pressed.
  • The value of myCounter then assigned to pageFlowscope attribute in a managed bean called 'counter' which is visible on screen as output text

Following is the code on button with other helping methods

    //Method on the button
    public void doCounting(ActionEvent actionEvent) {
      
        Number value = (Number)getPageFlowScopeValue("myCounter");
        if (value.intValue() >= 0) {
            setPageFlowScopeValue("myCounter", value.intValue() + 1);
        }
        
        setManagedBeanValue("pageFlowScope.pFlowBean.counter",getPageFlowScopeValue("myCounter"));

    }
    //Method to set the value of page flow scope created on runtime
    public void setPageFlowScopeValue(String name, Number value) {
        ADFContext adfCtx = ADFContext.getCurrent();
        Map pageFlowScope = adfCtx.getPageFlowScope();
        pageFlowScope.put(name, value);
    }

   //method to get the value of page flow scope created on runtime
    public Object getPageFlowScopeValue(String name) {
        ADFContext adfCtx = ADFContext.getCurrent();
        Map pageFlowScope = adfCtx.getPageFlowScope();
        Object val = pageFlowScope.get(name);
    
        if (val == null)
            return 0;
        else
            return val;
    }

   //Methods used to get and set the values in a Managed bean
    public Object getManagedBeanValue(String beanName) {
        StringBuffer buff = new StringBuffer("#{");
        buff.append(beanName);
        buff.append("}");
        return resolveExpression(buff.toString());
    }

    public Object resolveExpression(String expression) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application app = facesContext.getApplication();
        ExpressionFactory elFactory = app.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        ValueExpression valueExp = elFactory.createValueExpression(elContext, expression, Object.class);
        return valueExp.getValue(elContext);
    }

  
    public void setManagedBeanValue(String beanName, Object newValue) {
        StringBuffer buff = new StringBuffer("#{");
        buff.append(beanName);
        buff.append("}");
        setExpressionValue(buff.toString(), newValue);
    }

    public static void setExpressionValue(String expression, Object newValue) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application app = facesContext.getApplication();
        ExpressionFactory elFactory = app.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        ValueExpression valueExp = elFactory.createValueExpression(elContext, expression, Object.class);
     
        Class bindClass = valueExp.getType(elContext);
        if (bindClass.isPrimitive() || bindClass.isInstance(newValue)) {
            valueExp.setValue(elContext, newValue);
        }
    }


Download the sample code

Note: There are many ways to do the same task one technique is demonstrated by Andrejus click here

Have a nice day,
Zeeshan Baig

Thursday, December 15, 2011