Friday, February 27, 2015

Collection of ADF Programmatic Functions

In this post I write some functions you can use those functions in your ADF application.

Get Viewobject attribute datatype programmatic

This function accept 2 parameters (iterator name, attribute name) and will return the data type of this attribute

private String getDataType(String iteratorName, String attributeName)
{
BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry();
DCIteratorBinding iterBind = (DCIteratorBinding) bindings.get(iteratorName);
ViewObject vo=iterBind.getViewObject();
AttributeDefImpl attrDef = (AttributeDefImpl)vo.findAttributeDef(attributeName);
String datatype=attrDef.getJavaType().getName();
return datatype;
}
view raw data type hosted with ❤ by GitHub

Get Viewobject attribute Database Column Name

This function accept 2 parameters (iterator name, attribute name) and will return the database column name which map to the viewobject attribute

private String getDatabaseColumnName(String Iterator, String attributeName)
{
BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry();
DCIteratorBinding iter = (DCIteratorBinding) bindings.get(Iterator);
ViewObject vo = iter.getViewObject();
AttributeDefImpl attrDef = (AttributeDefImpl)vo.findAttributeDef(attributeName);
return attrDef.getColumnName();
}
view raw database column hosted with ❤ by GitHub


Clear Table Filters

If you want to clear table filters programmatically sent table component to this function

public void clearTableFilters(RichTable tableComponent)
{
FilterableQueryDescriptor q = (FilterableQueryDescriptor) tableComponent.getFilterModel();
Map<String, Object> m = q.getFilterCriteria();
if (m != null)
{
m.clear();
}
}


Reset af:query fields

If you want to reset af:query sent query component to this function

public static void resetQueryFields(RichQuery queryComponent)
{
QueryModel queryModel = queryComponent.getModel();
QueryDescriptor queryDescriptor = queryComponent.getValue();
queryModel.reset(queryDescriptor);
}
view raw reset query hosted with ❤ by GitHub


Invoke af:query Search Action programmatically

1- Bind the af:query in the back bean like:

<af:query binding="#{mybean.richQuery}" ... >
view raw query.xml hosted with ❤ by GitHub


2- Add af:commandButton and in it's action write this code

public String runQuerySearchAction()
{
QueryDescriptor queryDescriptor = getRichQuery().getValue();
QueryEvent queryEvent = new QueryEvent(getRichQuery(), queryDescriptor);
queryEvent.setPhaseId(PhaseId.INVOKE_APPLICATION);
getRichQuery().queueEvent(queryEvent);
return null;
}
view raw search.java hosted with ❤ by GitHub


Show error message on adf component

Sometime you need to remove required property from af:inputText because it may cause some problems and in the same time you need to display error message on the inputText component so you can use the following function just send the error message and the adf component

public static void showFieldErrorMessage(String msg, UIComponent component)
{
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage message = new FacesMessage(msg);
message.setSeverity(FacesMessage.SEVERITY_ERROR);
context.addMessage(component.getClientId(context), message);
}
view raw error msg hosted with ❤ by GitHub

Sort Table Column Programmatically

If you want to sort column programmatic you can use the following function just send table component and attribute which you would like to sort and the boolean parameter (true for ascending sort and false for descending sort)

public void sortTable(RichTable tableComponent, String attributeName, boolean ascending)
{
List<SortCriterion> sortList = new ArrayList<SortCriterion>();
SortCriterion sc = new SortCriterion(attributeName, ascending);
sortList.add(sc);
tableComponent.setSortCriteria(sortList);
}
view raw sort hosted with ❤ by GitHub


Reset Table Sort

After sorting in column and need to reset the sorting you can use the following function just send table component and the iterator name (which the table point to) 

public void resetTableSort(RichTable tableComponent, String iteratorName)
{
BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry();
DCIteratorBinding iter = (DCIteratorBinding) bindings.get(iteratorName);
tableComponent.queueEvent(new SortEvent(tableComponent, new ArrayList<SortCriterion>()));
Key currentRow = null;
Row row = iter.getCurrentRow();
if (row != null)
{
currentRow = row.getKey();
}
SortCriteria[] sc = new SortCriteria[0];
iter.applySortCriteria(sc);
iter.executeQuery();
if (currentRow != null)
{
iter.setCurrentRowWithKey(currentRow.toStringFormat(true));
}
}
view raw reset sort hosted with ❤ by GitHub

Call database sequence

When you want to get next value from database sequence you can use the following function just send database sequence name and it will return the next value of the sequence
public static oracle.jbo.domain.Number getSequenceNextValue(String sequenceName)
{
SequenceImpl seq = new SequenceImpl(sequenceName, getDBTransaction());
return seq.getSequenceNumber();
}
view raw db sequence hosted with ❤ by GitHub



Set Focus to component

If you want to set focus to component( as set cursor to inputText) just send component id to this function (if inputText id =it1 and you use page template with id=pt1 so the id which you have to send to this function is pt1:it1)

public static void setFocus(String componentId)
{
FacesContext facesContext = FacesContext.getCurrentInstance();
ExtendedRenderKitService service = Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);
service.addScript(facesContext, "comp = AdfPage.PAGE.findComponent('"+componentId+"'); comp.focus();");
}
view raw setFocus.java hosted with ❤ by GitHub
Expand Table DetailStamp Programmatically

public String expandAction() // button or link action
{
RichTable table = getMyTable(); // bind table component in backbean
RowKeySet rks = table.getDisclosedRowKeys();
DCIteratorBinding tableIter = (DCIteratorBinding) BindingContext.getCurrent().getCurrentBindingsEntry().get("MyView1Iterator"); // Table itertor name (get it from pageDef.)
ViewObject vo = tableIter.getViewObject();
Row r = vo.getCurrentRow();
List keyList = Collections.singletonList(r.getKey());
rks.add(keyList);
AdfFacesContext adfFacesContext = null;
adfFacesContext = AdfFacesContext.getCurrentInstance();
adfFacesContext.addPartialTarget(table.getParent());
return null;
}
view raw expand.java hosted with ❤ by GitHub