Skip to content

Instantly share code, notes, and snippets.

@ramannanda9
Created July 5, 2014 12:52
Show Gist options
  • Save ramannanda9/f59c2f51ac7ccf13839e to your computer and use it in GitHub Desktop.
Save ramannanda9/f59c2f51ac7ccf13839e to your computer and use it in GitHub Desktop.
It is used to filter the content in a managed bean
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.NumberConverter;
import javax.faces.model.SelectItem;
import oracle.adf.view.rich.component.rich.data.RichTable;
import oracle.adf.view.rich.event.QueryEvent;
import oracle.adf.view.rich.model.AttributeCriterion;
import oracle.adf.view.rich.model.AttributeDescriptor;
import oracle.adf.view.rich.model.AttributeDescriptor.ComponentType;
import oracle.adf.view.rich.model.ConjunctionCriterion;
import oracle.adf.view.rich.model.Criterion;
import oracle.adf.view.rich.model.FilterableQueryDescriptor;
import oracle.adf.view.rich.model.ListOfValuesModel;
import oracle.adf.view.rich.model.QueryDescriptor;
public class TempBackBean {
List<Person> valuesList=null;
List<Person> entireData=new ArrayList<Person>();
CustomQueryDescriptor qs;
CustomBindingDefHolder customBindingDefHolder;
private RichTable table;
public TempBackBean() {
super();
customBindingDefHolder=new CustomBindingDefHolder();
CustomBindingDefHolder.SavedSearchDef savedSearchDef =
customBindingDefHolder.getDefaultSearch();
qs=new CustomQueryDescriptor(savedSearchDef);
}
public void setValuesList(List<Person> valuesList) {
this.valuesList = valuesList;
}
public List<Person> getValuesList() {
if(valuesList==null){
valuesList=new ArrayList<Person>();
Person person1=new Person();
person1.setFirstName("Hero");
person1.setLastName("Ji");
Person person2=new Person();
person2.setFirstName("ABC");
person2.setLastName("EF");
valuesList.add(person1);
valuesList.add(person2);
entireData=valuesList;
}
return valuesList;
}
public void onQuery(QueryEvent queryEvent) {
// Add event code here...
List<Person> tempList=new ArrayList<Person>();
tempList.addAll(entireData);
// pre-processing code here
boolean invokeQuery = true;
/*
* Method called by the Query Listener. This method checks if
* the DepartmentId parameter contains a valid number and puts
* the DepartmentName into the expected case
*/
FilterableQueryDescriptor fqd =
(FilterableQueryDescriptor)
queryEvent.getDescriptor();
Map map = fqd.getFilterCriteria();
String value=(String)map.get("firstName");
if(value!=null && !value.isEmpty()){
for (int i=0;i<tempList.size();i++)
{
if(!(tempList.get(i).getFirstName().toLowerCase()).startsWith(value.toLowerCase())){
tempList.remove(i);
}
}
}
valuesList=tempList;
// AdfFacesContext.getCurrentInstance().addPartialTarget(this.getTable());
}
public void setQs(CustomQueryDescriptor qs) {
this.qs = qs;
}
public CustomQueryDescriptor getQs() {
return qs;
}
public void setTable(RichTable table) {
this.table = table;
}
public RichTable getTable() {
return table;
}
public void setEntireData(List<Person> entireData) {
this.entireData = entireData;
}
public List<Person> getEntireData() {
return entireData;
}
/**
* Custom Query Descriptor
*/
public class CustomQueryDescriptor extends FilterableQueryDescriptor {
public CustomQueryDescriptor(CustomBindingDefHolder.SavedSearchDef savedSearchDef) {
_savedSearchDef = savedSearchDef;
_conjunctionCriterion =
new CustomConjunctionCriterion(savedSearchDef);
_hiddenCriterions = new ArrayList<Criterion>();
_uiHintMap = new AbstractMap() {
public Object get(Object key) {
if (key.equals(UIHINT_AUTO_EXECUTE))
return _savedSearchDef.isAutoExecute();
else if (key.equals(UIHINT_MODE))
return _savedSearchDef.getMode();
else if (key.equals(UIHINT_NAME))
return _savedSearchDef.getName();
else if (key.equals(UIHINT_SAVE_RESULTS_LAYOUT))
return _savedSearchDef.isSaveResultsLayout();
else if (key.equals(UIHINT_SHOW_IN_LIST))
return _savedSearchDef.isShowInList();
else if (key.equals(UIHINT_IMMUTABLE))
return _savedSearchDef.isReadOnly();
else if (key.equals(UIHINT_RESULTS_COMPONENT_ID))
return _savedSearchDef.getResultsId();
else if (key.equals(UIHINT_DEFAULT))
return _savedSearchDef.isDefault();
else
return null;
}
public Object put(Object key, Object value) {
if (value == null && containsKey(key))
return get(key);
if (key.equals(UIHINT_NAME)) {
_savedSearchDef.setName((String)value);
return _savedSearchDef.getName();
} else if (key.equals(UIHINT_MODE) &&
value instanceof QueryDescriptor.QueryMode) {
_savedSearchDef.setMode((QueryDescriptor.QueryMode)value);
return _savedSearchDef.getMode();
} else if (key.equals(UIHINT_RESULTS_COMPONENT_ID)) {
_savedSearchDef.setResultsId((String)value);
return _savedSearchDef.getResultsId();
} else {
// Rest are boolean entries
boolean b =
(value instanceof String) ? Boolean.parseBoolean((String)value) :
Boolean.TRUE.equals(value);
if (key.equals(UIHINT_AUTO_EXECUTE)) {
_savedSearchDef.setAutoExecute(b);
return _savedSearchDef.isAutoExecute();
} else if (key.equals(UIHINT_SAVE_RESULTS_LAYOUT)) {
_savedSearchDef.setSaveResultsLayout(b);
return _savedSearchDef.isSaveResultsLayout();
} else if (key.equals(UIHINT_SHOW_IN_LIST)) {
_savedSearchDef.setShowInList(b);
return _savedSearchDef.isShowInList();
} else if (key.equals(UIHINT_IMMUTABLE)) {
_savedSearchDef.setReadOnly(b);
return _savedSearchDef.isReadOnly();
} else if (key.equals(UIHINT_DEFAULT)) {
_savedSearchDef.setDefault(b);
return _savedSearchDef.isDefault();
}
}
return null;
}
public Set entrySet() {
return Collections.EMPTY_SET;
}
};
}
public void addCriterion(String name) {
// searchField is marked as removable when user adds new criterion. But once a QueryDescriptor
// is saved (Save.../ or QOP.CREATE), the added Criterion(s) are removed from the original
// QueryDescriptor and in the new QueryDescriptor, the added Criterions can no longer be
// removed, as saving it makes it part of a new savedSearch definition.
// by default, added criterion is marked removable, multiSelect is false and has no dependent
// criterion
_savedSearchDef.addSearchFieldDef(name, null, null, null, false,
/* isMultiSelect*/true,
/* isRemovable*/new boolean[] { false, false });
_conjunctionCriterion =
new CustomConjunctionCriterion(_savedSearchDef);
}
public void changeMode(QueryDescriptor.QueryMode mode) {
// Do nothing
getUIHints().put(QueryDescriptor.UIHINT_MODE, mode);
}
public CustomBindingDefHolder.SavedSearchDef getSavedSearchDef() {
return _savedSearchDef;
}
public boolean isAdvancedMode() {
return (_savedSearchDef.getMode() ==
QueryDescriptor.QueryMode.ADVANCED);
}
public ConjunctionCriterion getConjunctionCriterion() {
return _conjunctionCriterion;
}
public void setConjunctionCriterion(ConjunctionCriterion criterion) {
_conjunctionCriterion = criterion;
}
public String getName() {
return _savedSearchDef.getName();
}
public void removeCriterion(Criterion criterion) {
if (criterion != null && criterion instanceof AttributeCriterion) {
CustomAttributeCriterion attrCriterion =
(CustomAttributeCriterion)criterion;
CustomConjunctionCriterion demoConj =
(CustomConjunctionCriterion)_conjunctionCriterion;
demoConj.removeCriterion(attrCriterion);
} else {
throw new UnsupportedOperationException();
}
}
public Map<String, Object> getUIHints() {
return _uiHintMap;
}
public AttributeCriterion getCurrentCriterion() {
return null;
}
public void setCurrentCriterion(AttributeCriterion attrCriterion) {
}
@Override
public Map<String, Object> getFilterCriteria() {
return _filterCriteria;
}
@Override
public void setFilterCriteria(Map<String, Object> filterCriteria) {
_filterCriteria = filterCriteria;
}
public void addHiddenCriterion(CustomAttributeCriterion criterion) {
_hiddenCriterions.add(criterion);
}
public void removeHiddenCriterion(CustomAttributeCriterion criterion) {
_hiddenCriterions.remove(criterion);
}
Map<String, Object> _filterCriteria;
private CustomBindingDefHolder.SavedSearchDef _savedSearchDef;
private Map<String, Object> _uiHintMap;
private ConjunctionCriterion _conjunctionCriterion;
//List for hidden criterion so that they can be added back unchanged.
private List<Criterion> _hiddenCriterions;
}
private class CustomConjunctionCriterion extends ConjunctionCriterion {
public CustomConjunctionCriterion(CustomBindingDefHolder.SavedSearchDef savedSearchDef) {
List<CustomBindingDefHolder.SearchFieldDef> searchFieldDefList =
savedSearchDef.getSearchFields();
_savedSearchDef = savedSearchDef;
_criterionList = new ArrayList<Criterion>();
_criterionSearchFieldMap =
new HashMap<Criterion, CustomBindingDefHolder.SearchFieldDef>();
int index = 0;
for (CustomBindingDefHolder.SearchFieldDef searchFieldDef :
searchFieldDefList) {
Criterion criterion =
new CustomAttributeCriterion(searchFieldDef);
_criterionList.add(criterion);
_criterionSearchFieldMap.put(criterion, searchFieldDef);
index++;
}
}
public CustomBindingDefHolder.SearchFieldDef getDemoSearchField(Object criterion) {
if (criterion != null)
return _criterionSearchFieldMap.get(criterion);
return null;
}
public ConjunctionCriterion getRoot() {
return this;
}
public Object getKey(Criterion criterion) {
for (int index = 0; index < _criterionList.size(); index++) {
if (_criterionList.get(index).equals(criterion))
return new Integer(index); // Return an Object
}
return null;
}
public Criterion getCriterion(Object key) {
int index = -1;
// Always expect Integer object from framework as Integer is passed in getKey() method
if (key != null && key instanceof Integer) {
index = ((Integer)key).intValue();
}
List<Criterion> criterionList = getCriterionList();
if (index > -1 && index < criterionList.size())
return criterionList.get(index);
return null;
}
// Utility method for searching a criterion by it 'Name'. This only returns criterion which
// are not added by Add Fields button.
public Criterion getCriterionByName(String name) {
for (Criterion criterion : _criterionList) {
CustomAttributeCriterion attr =
(CustomAttributeCriterion)criterion;
if (name.equals(attr.getAttribute().getName()) &&
!attr.getSearchField().isRemovable())
return criterion;
}
return null;
}
public ConjunctionCriterion.Conjunction getConjunction() {
ConjunctionCriterion.Conjunction conjType =
_savedSearchDef.getSelectedConjunction();
if (conjType == null) {
return _savedSearchDef.getDefaultConjunction();
}
return conjType;
}
public List<Criterion> getCriterionList() {
return _criterionList;
}
public void setConjunction(ConjunctionCriterion.Conjunction conjunction) {
_savedSearchDef.setSelectedConjunction(conjunction);
}
private void removeCriterion(CustomAttributeCriterion criterion) {
_savedSearchDef.removeSearchFieldDef(getDemoSearchField(criterion));
_criterionList.remove(criterion);
_criterionSearchFieldMap.remove(criterion);
}
private void addCriterion(CustomAttributeCriterion criterion,
CustomBindingDefHolder.SearchFieldDef searchField) {
_criterionList.add(criterion);
_criterionSearchFieldMap.put(criterion, searchField);
_savedSearchDef.addSearchFieldDef(searchField);
}
CustomBindingDefHolder.SavedSearchDef _savedSearchDef;
Map<Criterion, CustomBindingDefHolder.SearchFieldDef> _criterionSearchFieldMap;
List<Criterion> _criterionList;
}
public class CustomAttributeCriterion extends AttributeCriterion {
public CustomAttributeCriterion(CustomBindingDefHolder.SearchFieldDef searchFieldDef) {
_searchFieldDef = searchFieldDef;
_attrDesc =
new CustomAttributeDescriptor(searchFieldDef.getAttributeDef());
}
public AttributeDescriptor getAttribute() {
return _attrDesc;
}
public AttributeDescriptor.Operator getOperator() {
return ((CustomAttributeDescriptor)_attrDesc).getOperator(_searchFieldDef.getOperator());
}
/**
* The list of operators returned for an AttributeCriterion could be different from those
* returned for an AttributeDescriptor. For e.g., for an LOV attribute that supports multiple
* selection it makes more sense to support "In" and "Not In" operators rather than "Equals /
* Not Equals".
* @return
*/
public Map<String, AttributeDescriptor.Operator> getOperators() {
Map<String, AttributeDescriptor.Operator> optrMap =
new LinkedHashMap<String, AttributeDescriptor.Operator>();
List<CustomBindingDefHolder.OperatorDef> operatorList =
_searchFieldDef.getOperators();
for (CustomBindingDefHolder.OperatorDef operator : operatorList) {
AttributeDescriptor.Operator optr =
((CustomAttributeDescriptor)_attrDesc).getOperator(operator);
optrMap.put(optr.getLabel(), optr);
}
return optrMap;
}
public List<? extends Object> getValues() {
return _searchFieldDef.getValues();
}
public void setValues(List<? extends Object> values) {
int index = 0;
for (Object value : values) {
_searchFieldDef.getValues().set(index, value);
index++;
}
}
/**
* Returns true, if this criterion has dependent criterion. The method will be called to
* determine whether af:query component should be refreshed in case the value of this criterion
* changes.
* @param index value indicating the position of the criterion value field
* @retrun boolean returns true if the criterion has dependent criterion
*/
public boolean hasDependentCriterion(int index) {
return _searchFieldDef.hasDependentField(index);
}
public CustomBindingDefHolder.SearchFieldDef getSearchField() {
return _searchFieldDef;
}
public boolean isRemovable() {
return _searchFieldDef.isRemovable();
}
public void setOperator(AttributeDescriptor.Operator operator) {
_searchFieldDef.setOperator(getDemoOperator(operator));
}
/**
* Returns the ComponentType specific to an AttributeCriterion. This may be different from the
* ComponentType of the AttributeDescriptor, based on the operator chosen or whether
* multi-select is enabled or not. For e.g., for LOV attributes, <br/>
* - when multi-select is enabled, <br/>
* - for operator 'In / Not In', the component type will always be selectManyChoice regardless
* of the default type or dataType. <br/>
* - For all other operators it's an inputText (inputDate or inputNumberSpinbox for Date and
* Number datatypes respectively.)
* - when multi-select is disabled, <br/>
* - for operator Equals/Not Equals, it will be the default type (of the attribute)<br/>
* - for all other operators, an inputText (or inputDate or inputNumberSpinbox)
*
* @param operator the operator for which the component type needs to be determined
* @return a ComponentType
*/
@Override
public AttributeDescriptor.ComponentType getComponentType(AttributeDescriptor.Operator operator) {
CustomAttributeDescriptor demoAttrDesc =
(CustomAttributeDescriptor)getAttribute();
boolean isMultiSelectOper =
demoAttrDesc.hasVariableOperands(operator);
if (demoAttrDesc.isLOV()) {
if (isMultiSelectOper) {
// always return selectOneChoice for multiSelect enabled operators
return ComponentType.selectManyChoice;
} else {
// For certain operators the default component type is used, otherwise the base component
// type is used (based on the datatype)
if (demoAttrDesc.useDefaultComponentType(operator))
return demoAttrDesc.getComponentType();
else {
// based on the type of the attribute
String typeName = demoAttrDesc.getType().getName();
if (typeName.equals("java.lang.Number"))
return ComponentType.inputNumberSpinbox;
else if (typeName.equals("java.util.Date"))
return ComponentType.inputDate;
else
return ComponentType.inputText;
}
}
}
// Return default componentType
return demoAttrDesc.getComponentType();
}
private CustomBindingDefHolder.OperatorDef getDemoOperator(AttributeDescriptor.Operator selectedOper) {
for (CustomBindingDefHolder.OperatorDef optr :
CustomBindingDefHolder.OperatorDef.values()) {
if (selectedOper != null &&
selectedOper.getValue().equals(optr.getSymbol())) {
return optr;
}
}
return null;
}
/**
* For the componentType used by the criterion based on the current operator, return the list of
* model objects to use.
* @return
*/
@Override
public List<? extends Object> getModelList() {
AttributeDescriptor.ComponentType compType =
getComponentType(getOperator());
CustomAttributeDescriptor attrDesc =
(CustomAttributeDescriptor)getAttribute();
if (attrDesc == null) {
return Collections.emptyList();
} else {
Object attrModel = attrDesc.getModel(compType);
if (attrModel != null) {
List<Object> modelList = new ArrayList<Object>();
AttributeDescriptor.Operator operator = getOperator();
int operandCount =
(operator != null) ? operator.getOperandCount() : 1;
// Add a model item to the list for each operand, of the current operator.
for (int i = 0; i < operandCount; i++) {
modelList.add(attrModel);
}
return modelList;
} else
return Collections.emptyList();
}
}
private CustomBindingDefHolder.SearchFieldDef _searchFieldDef = null;
private AttributeDescriptor _attrDesc = null;
}
public final class CustomAttributeDescriptor extends AttributeDescriptor {
public CustomAttributeDescriptor(CustomBindingDefHolder.AttributeDef attrDef) {
_attrDef = attrDef;
}
/**
* Returns the NumberConverter for Salary attribute else returns null.
* @return Converter returns the NumberConverter for Salary attribute else returns null
* @see AttributeDescriptor#getConverter
*/
public Converter getConverter() {
Application application =
FacesContext.getCurrentInstance().getApplication();
String name = getName();
if (name.equals("Salary")) {
Converter converter =
application.createConverter(Number.class);
if (converter != null &&
converter instanceof NumberConverter) {
NumberConverter numberConv = (NumberConverter)converter;
numberConv.setGroupingUsed(false);
numberConv.setPattern("$ ###0,000.00");
numberConv.setType("currency");
numberConv.setCurrencySymbol("$");
return numberConv;
}
}
return null;
}
public String getDescription() {
return _attrDef.getDescription();
}
public String getFormat() {
return _attrDef.getFormat();
}
public String getLabel() {
return _attrDef.getLabel();
}
/**
* Returns true if criterion is indexed and not required.
*
* @return true if criterion is indexed and not required else false.
*/
public boolean isIndexed() {
//only when criterion is indexed but not required it will be considered as indexed
return (_attrDef.isIndexed() && !_attrDef.isMandatory());
}
public boolean isLOV() {
return _attrDef.isLOV();
}
/**
* Based on the component type of the attribute, it returns an appropriate model object expected
* of that component
* @return
*/
public Object getModel() {
AttributeDescriptor.ComponentType compType =
_attrDef.getComponentType();
return getModel(compType);
}
public Object getModel(AttributeDescriptor.ComponentType compType) {
if (compType.equals(AttributeDescriptor.ComponentType.selectOneChoice) ||
compType.equals(AttributeDescriptor.ComponentType.selectManyChoice)) {
if (_attrDef.isLOV()) {
List<SelectItem> selectItems = new ArrayList<SelectItem>();
for (int i = 0; i < _attrDef.getListOfValues().length;
i++) {
Object item = _attrDef.getListOfValues()[i];
SelectItem selItem = new SelectItem();
selItem.setLabel(item.toString());
selItem.setValue(item);
selectItems.add(selItem);
}
return selectItems;
}
} else if (compType.equals(AttributeDescriptor.ComponentType.inputListOfValues) ||
compType.equals(AttributeDescriptor.ComponentType.inputComboboxListOfValues)) {
// TODO
ListOfValuesModel lovModel = null;
return lovModel;
}
return null;
}
public String getName() {
return _attrDef.getName();
}
public Set<AttributeDescriptor.Operator> getSupportedOperators() {
List<CustomBindingDefHolder.OperatorDef> operatorList =
_attrDef.getSupportedOperators();
Set<AttributeDescriptor.Operator> optrSet =
new HashSet<AttributeDescriptor.Operator>();
for (CustomBindingDefHolder.OperatorDef operator : operatorList) {
AttributeDescriptor.Operator optr = new OperatorImpl(operator);
optrSet.add(optr);
}
return optrSet;
}
public Class getType() {
return _attrDef.getType();
}
public AttributeDescriptor.ComponentType getComponentType() {
return _attrDef.getComponentType();
}
public boolean isReadOnly() {
return false;
}
/**
* Returns true if this criterion is a required field or is the only indexed field in the
* criterion list.
*
* @return true if single indexed criterion exist else false
*/
public boolean isRequired() {
if (_attrDef.isIndexed() && _isSingleIndexedCriterion()) {
return true;
}
return _attrDef.isMandatory();
}
public AttributeDescriptor.Operator getOperator(CustomBindingDefHolder.OperatorDef operator) {
if (operator != null)
return new OperatorImpl(operator);
else
return null;
}
public int getLength() {
return _attrDef.getLength();
}
public int getMaximumLength() {
return _attrDef.getMaximumLength();
}
/**
* Determines if the passed in operator can be used with multiple values, like the IN, Not IN
* @param operator
* @return boolean
*/
public boolean hasVariableOperands(AttributeDescriptor.Operator operator) {
if (operator == null)
return false;
OperatorImpl operImpl = (OperatorImpl)operator;
return operImpl.getOperatorDef().hasVariableOperands();
}
/**
* Determines if the operator requires showing the default component type. The default component
* type of the attribute is used for operator Equals / Not Equals.
* @param operator
* @return
*/
public boolean useDefaultComponentType(AttributeDescriptor.Operator operator) {
if (operator != null) {
OperatorImpl operImpl = (OperatorImpl)operator;
return (operImpl.getOperatorDef() == null ||
operImpl.getOperatorDef().equals(CustomBindingDefHolder.OperatorDef.EQUALS) ||
operImpl.getOperatorDef().equals(CustomBindingDefHolder.OperatorDef.NOT_EQUALS));
}
return true;
}
public class OperatorImpl extends AttributeDescriptor.Operator {
public OperatorImpl(CustomBindingDefHolder.OperatorDef operator) {
_operator = operator;
}
public String getLabel() {
return (_operator != null) ? _operator.getLabel() : null;
}
public Object getValue() {
return (_operator != null) ? _operator.getSymbol() : null;
}
// Returns the default operandCount for the operator. Except when the operator is null it
// returns 1 and if operandCount is -1, indicating unlimited operands, it returns 1, as from
// the UI standpoint a selectmanyChoice will be rendered.
public int getOperandCount() {
if (_operator != null) {
int operandCount = _operator.getOperandCount();
if (operandCount != -1)
return operandCount;
}
return 1;
}
@Override
public boolean equals(Object operator) {
if (operator != null && _operator != null) {
return (_operator.getSymbol().equals(operator.toString()));
}
return false;
}
@Override
public String toString() {
return (_operator != null) ? _operator.getSymbol() : null;
}
private CustomBindingDefHolder.OperatorDef getOperatorDef() {
return _operator;
}
CustomBindingDefHolder.OperatorDef _operator;
}
private CustomBindingDefHolder.AttributeDef _attrDef;
}
/**
* Returns true if there exists at least one criterion in the criterion list, that is 'indexed'.
*
* @return true if single indexed criterion exist else false
*/
private boolean _isSingleIndexedCriterion() {
QueryDescriptor qd = getQs();
List<Criterion> criterionList =
qd.getConjunctionCriterion().getCriterionList();
int indexedFieldCount = 0;
for (Criterion criterion : criterionList) {
AttributeCriterion attrCtr = (AttributeCriterion)criterion;
AttributeDescriptor attrDesc = attrCtr.getAttribute();
if (attrDesc.isIndexed()) {
indexedFieldCount++;
if (indexedFieldCount > 1)
return false;
}
}
return (indexedFieldCount == 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment