Tuesday, 3 May 2016

Programmatic InputComboboxListOfValues - Stuff of Legends!!!

Lolz !!! Apologies for the over-exaggerated title of the post. But, I was so happy to get it working :P

Recently, I had this requirement where I had to create inputComboboxListOfValues on the JSFF, but its values were dependent on certain fields already entered by the user on the form. And the lookup values were present in one single lookup table. If I had just 1 or 2 such use-cases, I would have created separate view objects for them and dragged and dropped them on the form in a declarative fashion. But I had almost 10 such LOVs that needed to be implemented in the similar fashion. So, I ran around searching for a way to bind the inputComboboxListOfValues component with a Java List or a Java array. There is documentation available for it online and I was able to make it work successfully. So here's how I did it:
As per the documentation, you need to extend the following classes and you are free to tweak with it as per your requirement:
  1. AttributeCriterion
  2. AttributeDescriptor
  3. ConjunctionCriterion
  4. CollectionModel
  5. ListOfValuesModel
  6. QueryDescriptor
  7. QueryModel
  8. TableModel
As per the design I followed, you would need to create two more Java Classes, preferably inside the same package:
  1. FileData
  2. LOVUtil
We would be creating an object of the LOVUtil class and calling the constructor, passing the java list to populate the ListOfValuesModel and the collection model. The source code for the constructor looks like this:

    public LOVUtil(List _DIR_DATA) {
        for (int i = 0; i < _DIR_DATA.size(); i++) {
            String item1 = _DIR_DATA.get(i).toString();
            String item7 = new Integer(i).toString();
            FileData _curRow = new FileData(item1, item7);
            _values.add(_curRow);
        }
        _filteredList.addAll(_values);
    }

The way, I have been calling it in my managed bean inside a valueChangeListener is shown below:

LOVUtil creditRatingUtil=new LOVUtil(creditRatingInputList);

In this case creditRatingInputList is a java array list which has been populated by iterating the lookup view object (obviously after executing the view criteria based on the "changed value").

That's it, worked like a charm for me!!!

P.S - I am unable to attach the java files here. So, I would request you guys to post your email ids in the comments section and I would be more than happy to send across the java files to you all :) :)
Link to the files