Oracle’s ADF (Application Developer Framework) is J2EE technology stack to rapidly develop enterprise applications. As with all enterprise applications, ADF focuses on data handling and makes it quite easy to present the data to the users in variety of ways. As you will already know that ADF is a blanket term which encompasses many layers of the full tech stack. So ADF is divided into: –
* ADF Faces – Based on Java Server Faces 1.2 specification, this provides the UI of the application. It is built over Apache MyFaces Trinidad 1.2.x. So this is the View layer of ADF stack.
* ADFm – ADF Model provides the brick and mortar to bind ADF Faces to the Model layer. In fact ADFm includes the Binding layer. This allows for loose coupling between the View and Model layers.
* ADF BC – ADF Business Components form the Model layer. It forms the data abstraction layer which does quite a lot of heavy lifting for the ADF stack.

The aim of this post is to list out some tips and code snippets which you may undoubtedly need while coding in ADF.

Some Important notes first

• Do not reference or cast to classes in oracle.adfinternal package. This includes classes which have names that start with FacesCtrl. The package is named internal for a reason. This means that ADF has the liberty to modify the behavior of the classes and methods or remove them altogether if they feel that it is needed.
• Good articles on row selection and SelectionListener for Tables and TreeTables.

1 Clearing the selection on a ADF tree/tree-table/table from bean

First make sure the tree or tree-table or table does not have selectedRowKeys attribute set. Now you will notice that no row will be selected when the tree loads for the first time, but when user makes a selection then tree builds and stores the RowKeySet of the node the user selected. So, now whenever the tree loads and if the last selected row is visible then it will be shown selected. To work around this, we need to manually clear the tree’s selected rowkeyset after the user has made his selection (e.g. When we hide the tree, just before the tree is shown). Use the following code for that:-

RowKeySet rks = getTree().getSelectedRowKeys();
if (rks != null) {
rks.clear();
}

2 Get the VO bound to an iterator

Sometimes we may need to manually execute an iterator, for this we need to get the handle to the VO instance bound to an iterator.

private ViewObject getVOFromIterator(DCIteratorBinding it) {
ViewObject vo = null;
if (it.hasRSI()) {
return it.getRowSetIterator().getRowSet().getViewObject();
} else {
return it.getViewObject();
}
}

AdfFacesContext.getCurrentInstance().addPartialTarget(targetComponent);

Make sure that targetComponent is not null else we may get NullPointerException.

4 Get query entered by user in Adf quickQuery component

QueryDescriptor qd = queryEvent.getDescriptor();
AttributeCriterion ac = qd.getCurrentCriterion();
String attrLabel = ac.getAttribute().getLabel();
Object attrValue = ac.getValues().get(0);

Note if you try ac.getValues() then it will mention that it has zero elements, even when get(0) returns a value.

5 Building Disclosed RowKeySet for nodes in a tree/tree-table

/**
* @param treeBinding
* @param keys
* @return
*/
private RowKeySet buildDiscloseRowKeySet(JUCtrlHierBinding treeBinding, RowKeySet keys) {
RowKeySetImpl discloseRowKeySet = new RowKeySetImpl();
Iterator iter = keys.iterator();
while (iter.hasNext()) {
List keyPath = (List)iter.next();
JUCtrlHierNodeBinding node = treeBinding.findNodeByKeyPath(keyPath);
if (node != null && node.getParent() != null && !node.getParent().getKeyPath().isEmpty()) {
//store the parent path
//call method recursively until no parents are found
RowKeySetImpl parentKeySet = new RowKeySetImpl();
RowKeySet rks = buildDiscloseRowKeySet(treeBinding, parentKeySet);
}
}
return discloseRowKeySet;
}

6 Select a node in tree/tree-table

Variant 1

private void selectNode(List<List<?>> pathKeys, org.apache.myfaces.trinidad.component.UIXTree targetComponent) {
List<Object> lstKeys = new ArrayList<Object>();
for(int level = 0; level < pathKeys.size(); level++) {
List<?> keys = pathKeys.get(level);
Object[] oKeys = new Object[keys.size()];
for (int index = 0; index < oKeys.length; index++) {
oKeys[index] = keys.get(index);
}
}
if(lstKeys.size() == 1) {
lstKeys = Collections.singletonList(lstKeys.get(0));
} else {
lstKeys = Collections.unmodifiableList(lstKeys);
}
RowKeySet keyset = new RowKeySetImpl();
keyset.setCollectionModel((CollectionModel)(targetComponent.getValue()));
targetComponent.setSelectedRowKeys(keyset); //After this targetComponent.setDisclosedRowKeys()
//needs to be called to reveal the node is its parents
//are collapsed.
}

Where pathKeys is of the following form:- Take an example, where we want to select node C, in the following tree.

A [Row from VO1, keys A1]
|--B [Row from VO2, keys B1, B2, B3]
|--C [Row from VO3, keys C1, C2]

Then pathKeys will be a list like shown below:-

{ {A1}, {B1, B2, B3}, {C1, C2} }

Variant 2 (Maybe this is a better approach, but not tested)

private void selectNode(List<List<?>> pathKeys, org.apache.myfaces.trinidad.component.UIXTree targetComponent) {
List<Object> lstKeys = new ArrayList<Object>();
for(int level = 0; level < pathKeys.size(); level++) {
List<?> keys = pathKeys.get(level);
Object[] oKeys = new Object[keys.size()];
for (int index = 0; index < oKeys.length; index++) {
oKeys[index] = keys.get(index);
}
}
if(lstKeys.size() == 1) {
lstKeys = Collections.singletonList(lstKeys.get(0));
} else {
lstKeys = Collections.unmodifiableList(lstKeys);
}
JUCtrlHierBinding treeBinding = (JUCtrlHierBinding) ((CollectionModel)targetComponent.getValue()).getWrappedData();
JUCtrlHierNodeBinding nodeBinding = treeBinding.findNodeByKeyPath(lstKeys);
Key rowKey = nodeBinding.getRowKey();
JUIteratorBinding iterator = nodeBinding.getIteratorBinding();
iterator.setCurrentRowWithKey(rowKey.toStringFormat(true));
}

7 Create a flexible query (search box) which allows you to use custom ViewCriteria with ADF quickQuery

To create search box like the one above.

UI code

<af:quickQuery id="qq1" queryListener="#{pageFlowScope.Bean.invokeSearch}"
searchDesc="Quick Search" shortDesc="Quick Search" label="Quick Search">
<af:inputText id="itQq1" value="#{pageFlowScope.Bean.searchKeyword}"
simple="true" partialTriggers="cb3" shortDesc="Quick Search"/>
<f:facet name="end">
<af:commandButton text="Clear" id="cb3" actionListener="#{pageFlowScope.Bean.clearSearch}"/>
</f:facet>
</af:quickQuery>

• We have defined our own input box so that Go button and the auto-generated input boxes get disabled if quickQuery doesn’t have any model.
• Our custom input box allows us to directly read and modify the input box’s content. This is needed by Clear functionality.
• Input box has simple set to true, so that its label is not displayed.

Bean code (in Bean.java)

private String searchKeyword;

public String getSearchKeyword() {
return searchKeyword;
}

public void setSearchKeyword(String keyword) {
searchKeyword = keyword;
}

public void invokeHierSearch(QueryEvent queryEvent) {
if (searchKeyword != null && !searchKeyword.isEmpty()) {
ViewObject vo = getVOFromIterator(it);
vo.applyViewCriteria(vo.getViewCriteriaManager().getViewCriteria("SearchCriteria"));
vo.setNamedWhereClauseParam("keyword", searchKeyword);
vo.executeQuery(); //Iterator will be executed after UI is updated.
searchVisible = true;
} else {
searchVisible = false;
}
}

public void clearSearch(ActionEvent e) {
searchKeyword = null;
invokeHierSearch(null);
}

private ViewObject getVOFromIterator(DCIteratorBinding it) {
ViewObject vo = null;
if (it.hasRSI()) {
return it.getRowSetIterator().getRowSet().getViewObject();
} else {
return it.getViewObject();
}
}

8 Get reference to Bindings from bean

DCBindingContainer bindings = ((DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry());

9 Programmatically execute iterator

DCBindingContainer bindings = ((DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry());
bindings.findIteratorBinding("Iterator").executeQuery();

10 Get reference to method binding (operation binding) in bean

Get bindings from 8.

OperationBinding meth = bindings.getOperationBinding("methodBindingId");
Map args = meth.getParamsMap();
args.put(“arg1”, arg1Value);
…
meth.execute();

11 Get reference to pageFlowScope in bean

AdfFacesContext.getCurrentInstance().getPageFlowScope()

This returns a Map which we can use to read or set values.

12 Evaluate random EL value expressions in bean

FacesContext fc = FacesContext.getCurrentInstance();
ELContext elContext = fc.getELContext();
ExpressionFactory ef = fc.getApplication().getExpressionFactory();
ValueExpression exp = ef.createValueExpression(elContext, "#{expression}", ClassOfOutcomeOfExpression.class);
ClassOfOutcomeOfExpression obj = (ClassOfOutcomeOfExpression) exp.getValue(elContext);

ClassOfOutcomeOfExpression can be Object if you don’t care about the outcome. It has too much boilerplate code.

So place the following code in bean.

private <T> T evaluateEL(String el, Class<T> type) {
FacesContext fc = FacesContext.getCurrentInstance();
ELContext elContext = fc.getELContext();
ExpressionFactory ef = fc.getApplication().getExpressionFactory();
ValueExpression exp = ef.createValueExpression(elContext, el, type);
return (T) exp.getValue(elContext);
}

13 Evaluate random EL method expressions in bean

FacesContext fc = FacesContext.getCurrentInstance();
ELContext elContext = fc.getELContext();
ExpressionFactory ef = fc.getApplication().getExpressionFactory();
MethodExpression meth = ef.createMethodExpression(elContext, "#{expression}", Outcome.class, new Class[]{Arg1.class, Arg2.class, …});
Outcome obj = (Outcome) meth.invoke(elContext, new Object[]{arg1Obj, arg2Obj, …});

Example: You need to code the below if you override treetable’s selectionListener.

MethodExpression meth = ef.createMethodExpression(elContext, "#{bindings.treeBinding.treeModel.makeCurrent}",
Object.class, new Class[]{SelectionEvent.class});
meth.invoke(elContext, new Object[]{selectionEvent});

14 Get value from attribute binding in bean

Get bindings from 8.

((oracle.binding.AttributeBinding)bindings.getControlBinding("bindingName")).getInputValue();

15 Get value from list binding in bean

Get bindings from 8.

((JUCtrlListBinding)bindings.getControlBinding("bindingName")) .attributeValue();

16 Implementing custom selection listener

In this use-case we are providing our own selection listener for the TreeTable because we are interested in capturing the selected node’s VO row instance.

public void onTreeSelect(SelectionEvent selectionEvent) {
//Invoking makeCurrent manually since it will no longer be called as we have provided our own.
FacesContext fc = FacesContext.getCurrentInstance();
ELContext elContext = fc.getELContext();
ExpressionFactory ef = fc.getApplication().getExpressionFactory();
MethodExpression meth = ef.createMethodExpression(elContext, "#{bindings.treeBinding.treeModel.makeCurrent}",
Object.class, new Class[]{SelectionEvent.class});
meth.invoke(elContext, new Object[]{selectionEvent}); //Selection done now getting data from model.
RichTreeTable myTree = (RichTreeTable) selectionEvent.getSource(); //changed for Serialization
if (rowKeys != null) {
CollectionModel treeModel = (CollectionModel) myTree.getValue();
JUCtrlHierBinding treeBinding = (JUCtrlHierBinding) treeModel.getWrappedData();
Iterator selection = rowKeys.iterator();
while (selection.hasNext()) {
//Will loop only once since we have allowed single selection.
List key = (List) selection.next();
JUCtrlHierNodeBinding nodeBinding = treeBinding.findNodeByKeyPath(key);
Row row = nodeBinding.getRow(); //Your custom code which will use row.
//row will be the instance of ViewRowImpl
//which backs the currently selected node.
}
}
}

17 Get all rows (nodes) of a Tree or TreeTable

In my use-case I wanted to iterate over all the rows in the TreeTable to find if it has any rows with pending changes. This was to prevent the user from carrying out some actions if there were unsaved changes. There are many ways to achieve this, like check if transaction or view port is dirty, but I did not care about the state of various other components on the page which are outside the tree.

public List<Row> getTreeNodes(CollectionModel treeModel) {
List<Row> rows = new ArrayList<Row>();
JUCtrlHierBinding treeBinding = (JUCtrlHierBinding) treeModel.getWrappedData();
while (!nodeBindings.isEmpty()) {
List<JUCtrlHierNodeBinding> childNodes = nodeBindings.remove().getChildren();
if (childNodes != null) {
for (JUCtrlHierNodeBinding _node : childNodes) {
}
}
}
return rows;
}

Recently I had one requirement where I needed to programmatically load a resource bundle from a managed bean and read the translated strings. I could not find any official documentation on this, and the net is rife with many alternatives which did not work for me. Anyway here is my version which is simple and works well for me.

//import java.util.ResourceBundle;
//import oracle.javatools.resourcebundle.BundleFactory;
ResourceBundle myBundle = BundleFactory.getBundle("fully.qualified.class_name.of.the.bundle");
String msg = getMsg(myBundle, "myKey");

Where the method getMsg() is

public String getMsg(ResourceBundle bundle, String key) {
if (bundle == null) {
return "";
}
try {
return bundle.getString(key);
} catch (MissingResourceException ex) { //Log error here. }
return "";
}

Please make sure that the bundle class is available on the classpath.

Disclaimer: The views expressed on this blog are my own and do not necessarily reflect the views of my employer. This is may or may not be the recommended way. This is provided here merely in the spirit of sharing. You are more than welcome to post a correction or make an addition in the comment section below. I will happily update this post accordingly.

How to Reset the Root Password of MySql in Linux

I am posting this for my future reference. Also it my help passers by.

If you are using CentOS 6 (maybe others too). To reset the root password use the following steps:-

1. sudo service mysqld stop
2. sudo service mysqld startsos
3. mysql -u root
4. Now you will be at mysql prompt. Here type:-
2. FLUSH PRIVILEGES;
3. quit;
5. sudo service mysqld restart

iFlickr PHP Script: Create mRSS feed of interesting pictures on Flickr.com

Did you notice the “Flickr Gallery” on top right corner of the home page of this blog? The picture slide show is provided by Google’s Ajax Slide Show, but this post is not about that. Google’s Slide Show needs a mRSS feed, which it parses to get the list of thumbnails it wants to present. mRSS is just a normal RSS feed particularly tailored for presenting media. It gets its ‘m’ from media. In this case it is picture media. For my blog’s “Flickr Gallery”, I have scheduled a cron job which runs every day at 5.30 am UTC. This way every day my blog gets fresh set of interesting pictures to present. You can get a good example of mRSS feed on my site at http://www.applegrew.com/util/if.xml. Safari and Firefox will use their own styling rules to style this XML. If you want to check it out the way I want it, then view it in Google Chrome.

The Script

This PHP script spits out mRSS feed of the interesting pictures on Flickr.com. This script can be invoked from web or directly from command line. When invoked it connects to Flickr using its API and gets the list and all related infos from there. Flickr likes to call this list – Flickr Interestigness.

How to use this script

This scripts accepts a number of parameters:-

• api_key (Default: NoKeyGiven) – The Flickr API key. Get it from here.
• pg (Default: 1) – Which page to fetch. This is to help you paginate if that makes sense for you.
• per_pg (Default: 10) – How many pictures you want per page. So, if you want 11th to 20th most interesting pictures then set pg to 2 and per_pg to 10.
• thumbnail_type (Default: , i.e. not set) – Possible values are – SquareThumbnail, Small, Medium or *Large. *The picture of the type set here is set as thumbnail picture. If the type specified is not available then no thumbnail is mentioned. If this is not set then all possible picture sizes would be listed as thumbnails in the feed, with the bigger pics at the top.
• min_size (Default: 0, i.e. not set) – If set (i.e. it is not set to zero) then the pic which is equal or just larger than this dimension will be chosen as thumbnail.

$/usr/bin/php iflickr.php --api_key=key_here --min_size=240 > mrss.xml When run from web:- http://example.com/iflickr.php?api_key=key_here&min_size=240 Please note: To be able to run this from web, you need to set$bind_address variable in the script as empty string.

If you are the administrator of a server then you definitely understand the importance of keeping your system updated always.

The following script will send you a mail whenever your server needs an update. The content of the mail will list out all the available updates.

The script

[code lang=”shell”]
#!/bin/bash
# Author: AppleGrew
# Site: www.applegrew.com

yum check-update > /tmp/checkupdate.log
if [[ $? == 100 ]] then mail -s "New YUM updates available" -r admin@example.com$MAILTO < /tmp/checkupdate.log
fi
[/code]

Notes:

• This script has been tested on CentOS 6 but should run on all RedHat based systems.
• In the above script, change admin@example.com to any valid email id which your server is authorized to send. This the email id for the ‘From’ field. For example you can not set this to (say) someone@gmail.com. Since Google did not authorize your server to send emails on behalf of gmail.com.

Installation notes

• Copy this script to some directory in your server and give it execute permission.
• Now schedule a cron job for this script. You can set this job for any user, since this script does not need root privileges.
• In the cronjob make sure to set the MAILTO environment variable. The script uses this variable to determine the recipient of the notification.

Example cronjob

[code lang=”plain”]
MAILTO=yourmail@email.com
0 0 * * * /home/user1/checkupdate.sh
[/code]

The problem

It seems I have developed a fascination for the number 5, so here we go again. Here I would be using Digit Math to prove that all integers  which end with digit 5 are always divisible by 5.

The proof

Case 1:

Take a two digit number $$x\omega 5$$, i.e. $$x$$ has only one digit.

$$x\omega 5$$ is divisible by 5, if $$x\omega 5 \times \frac 1 5$$ (i.e. $$x \omega 5 \times 0.2$$) leave no remainder.

So, $$x\omega 5 \times 2$$ must end with one zero.

\begin{align} &x\omega5 \times 0\omega2\\ &= 0 \omega 2x \omega 10\tag{Using Bimultiplication}\\ &= (2x + 1) \omega 0\tag{1} \end{align}

Since (1) ends with zero so it is proved.

Case 2:

$$x\omega 5$$ is a number where $$x$$ has $$n$$ digits.

Multiplying $$x\omega 5$$ by $$10^{(n-1)}$$ to make both sides of $$\omega$$ equal in number of digits.

So,

\begin{align} &x\omega (5 \times 10^{n-1}) \times 0 \omega (2\times 10^{n-1})\\ &= 0 \omega (2x\times 10^{n-1})\omega (10\times 10^{n-1})\\ &= (2x\times 10^{n-1})\omega 10^n\\ &= (2x\times 10^{n-1} + 1) \omega ]0[^n\tag{Moving extra 1 to left} \end{align}

In the above equation it is easy to see that the result ends with $$10$$ after we divide the $$10^{n-1}$$. Therefore, this is not going to leave any remainder. (Proved)

Case 3:

$$\frac 5 5 = 1$$. (Proved)

Yeah, this is a trivial case, but for the sake of completeness.

Check out other applications of Digit Math

Link to list of other applications of Digit Math.

The problem

Someday, somewhere I came to know that any number which ends with the digit 5 can be easily squared. The trick can be easily demonstrate using an example. Suppose we want to find the square of 25.

Trick is to take the number before 5 (which will be 2 here), add one to it (2 + 1 = 3) and then multiply them together (2 x 3 = 6). Now the final answer would be the product followed by the number 25, i.e. 625 in this case.

Now let’s try it out yo find $$215^2$$.

\begin{align} 215^2 &= (21 \times (21 + 1)) \omega 25\\ &= (21 \times 22) \omega 25\\ &= 462\omega25\\ &= 46225\tag{Answer} \end{align}

This always seemed to work out very well. The problem was, can I trust this trick? Will this always hold true? I didn’t have answers to those questions, until I proved it myself using Digit Math. Good news is that this trick will always hold true.

The proof

Let the number be $$x = a\omega 5$$.  $$a$$ can have any number of digits.

Case 1:

$$a$$ has exactly one digit. So, $$]a[ = ]5[ = 1$$.

\begin{align} \therefore (a\omega 5)^2 &= aa\omega (5a+5a) \omega 5.5\tag{Using Bimultiplication formula}\\ &= a^2 \omega 10a \omega \underline{2}5\\ &= a^2 \omega (a\omega0) \omega \underline{2}5\\ &= a^2 \omega (a\omega2) \omega 5\\ &= (a^2 + a) \omega 2 \omega 5\\ &= \big(a(a+1)\big) \omega 25\tag{Proved} \end{align}

Case 2:

$$a$$ has more than one digits. So, $$]a[ > (]5[ = 1)$$.

But to apply Bimultiplication formula $$a$$ must have the same number of digits in $$5$$, which is obviously not the case here. So, we will use one trick. We will pad $$5$$ with some number of zeroes on the right, so that, $$]a[ = ]5\omega c[$$, where, $$c$$ is all zeroes and $$]c[ = ]a[ – 1$$. So, if $$a = 123 \Rightarrow c = 00$$.

\begin{align} \therefore (a\omega 5)^2 &= \big(a \omega ]5 \omega c[^{]a[}\big)^2\\ &= a^2 \omega \big(a(5\omega c) + a(5\omega c)\big) \omega (5\omega c)^2\tag{Using Bimultiplication}\\ &= a^2 \omega 2a(5\omega c) \omega (5\omega c)^2\\ &= a^2 \omega (10a \omega 2ac) \omega (5\omega c)^2\\ &= a^2 \omega (10a \omega c) \omega (5\omega c)^2\tag{Since, c is all zeroes}\\ &= a^2 \omega (a \omega 0 \omega c) \omega (5\omega c)^2\\ &= a^2 \omega (a \omega 0 \omega c) \omega (25 \omega 10c \omega c^2)\\ &= a^2 \omega (a \omega 0 \omega c) \omega (25 \omega c \omega c)\tag{1} \end{align}

Since each digit group must have $$]a[$$ digits, so let us move one zero from the middle $$c$$ in $$25 \omega c \omega c$$ to the rightmost $$c$$. So, now that group becomes $$25\omega d\omega e$$, where $$]d[ = ]c[ – 1$$ and $$]e[ = ]c[ + 1$$.

\begin{align} \therefore (1) &= a^2 \omega \big( (a \omega 0 \omega c) + (25 \omega d)\big) \omega e\\ &= a^2 \omega \big( a \omega (0+25) \omega (c+d) \big) \omega e\\ &= a^2 \omega ( a \omega 25 \omega d ) \omega e\tag{2} \end{align}

Now,

\begin{align} ]25 \omega d[ &= ]25[ + ]d[\\ &= 2 + (]c[ – 1)\\ &= 2 + \big((]a[ – 1) – 1\big)\\ &= ]a[ \end{align}

So, in the group $$a \omega 25 \omega d$$, $$a$$ is excess.

\begin{align} \therefore (2) &= (a^2 + a) \omega (25 \omega d) \omega e\\ &= a(a+1) \omega 25 \omega d \omega e\\ &= a(a+1) \omega 25 \omega c \omega c\tag{Shifting a zero from e to d}\\ \end{align}

So finally,

\begin{align} &(a\omega 5 \omega c)^2 = a(a+1) \omega 25 \omega c \omega c\\ &\Rightarrow \big((a\omega 5) \times 10^c\big)^2 = \big(a(a+1) \omega 25 \omega c\big) \times 10^c\\ &\Rightarrow (a\omega 5)^2 \times 10^{2c} = \big(a(a+1) \omega 25\big) \times 10^{2c}\\ &\Rightarrow (a\omega 5)^2 = a(a+1) \omega 25\tag{Proved} \end{align}

So, we see that this trick is applicable for all kinds of whole numbers that end with 5.

Check out other applications of Digit Math

Link to list of other applications of Digit Math.