Tuesday, October 28, 2014

Resetting ADF table sorting

ADF table sort behaves weird in some cases, like once you do a sort on any column and if you revisit the same screen, the table retains the sorting column in memory for all subsequent querries. If at all we forcefully re-execute the VO then also it retains the sorting.

So to remove sorting execute this piece of code :

    public void resetSort()
        {
            
            SortCriteria[] sc = new SortCriteria[0];
            DCBindingContainer iteratorbinding = this.getBindingContainer();
            if(iteratorbinding != null){
            DCIteratorBinding iter =
                iteratorbinding.findIteratorBinding("SampleVO1Iterator");
            
            iter.applySortCriteria(sc);
            

            }
        }

By doing this you do not need to re-execute the VO also.

Friday, May 24, 2013

Adding Multiple LOV's to a single ViewObject attribute

There is a requirement like i need to show different LOV(different LOV i mean LOV deriving data from different sources) defined  on one attribute based on the value selected from another attribute.

Requirement :

 Implementation:

I have a VO called SearchPanelVO in which i have 2 attributes called CriteriaAttr and CriteriaVal.
On CriteriaAttr  attribute i have defined a drop down from a static list as shown below :


So based on the value selected from this static list i need to invoke different LOVs defined on CriteriaVal attribute which means if i select "AIT #" i should show AitNameLOV, if i select "App Manager #" i should show AppManagerContactLOV and similarly AppMgmtContactLOV  and so on.

Open the SearchPanelVO , and in order to switch LOVs I defined new transient attribute LovSwitcher.(shown in snapshot below) The value of this attribute is a groovy expression which when evaluated should give proper LOV's name. Then i defined 5 LOVs on CriteriaVal attribute as shown in snapshot below.



 The groovy expression on the value property of the new transient attribute is like :


On the page add partialtrigger on the CriterVal attribute so that it can refresh based on the value selected in CriterAttr drop down.

Next you need to add a small piece of code which will clear the value selected from any LOV on the CriterVal attribute on changing value in CriteriaAttr attribute. Implement a ValueChangeListener on CriteriaAttr attribute which will clear the value from CriteriaVal attribute.

Done !!!

Saturday, December 1, 2012

Import data from a CSV file into View Object

There is a use case where in you need to import data from a CSV file into a View Object, Here i will explain a use case where in i will be importing the attendance for a particular student.
The CSV file will be in a format as below(first line will be header info and subsequent lines will have attendance data)
SSN,Name,MonHrs,TueHrs,WedHrs,ThuHrs,FriHrs,SatHrs,SunHrs
1234-12-123,Sanjeeb,8,8,8,8,8,8,8 
4567-56-987,XYZ,7,8,5,8,8,7,8

In my page i  have dropped a upload component i.e. <af:inputFile/> which will allow me to select a file from my system and import the data into View object. To achieve the same i have implemented a valueChangeListener method in a managed bean for this component . The code below are self explanatory as i have included comments to understand better.


    public void fileUploaded(ValueChangeEvent valueChangeEvent) {
        //getting the file instance from the event.
        UploadedFile file = (UploadedFile)valueChangeEvent.getNewValue();
         try {
          //calling a private method to parse the CSV file and import the data into table component
           parseFile(file.getInputStream());
          //Refresh the table component programmatically, importTab is the binding of the table component
           AdfFacesContext.getCurrentInstance().addPartialTarget(importTab);
         } catch (IOException e) {
           // TODO : Handle your exception

         }
    }



    private void parseFile(java.io.InputStream file) {
        BufferedReader reader =
            new BufferedReader(new InputStreamReader(file));
        String strLine = "";
        StringTokenizer st = null;
        int lineNumber = 0, tokenNumber = 0;
        Row rw = null;

        CollectionModel _tableModel = (CollectionModel)impTab.getValue();
        //the ADF object that implements the CollectionModel is JUCtrlHierBinding. It
        //is wrapped by the CollectionModel API
        JUCtrlHierBinding _adfTableBinding =
            (JUCtrlHierBinding)_tableModel.getWrappedData();
        //Acess the ADF iterator binding that is used with ADF table binding
        DCIteratorBinding it = _adfTableBinding.getDCIteratorBinding();

        //read comma separated file line by line
        try {
            while ((strLine = reader.readLine()) != null) {
                lineNumber++;
                // create a new row skip the header  (header has linenumber 1)
                if (lineNumber > 1) {
                    rw = it.getNavigatableRowIterator().createRow();
                    rw.setNewRowState(Row.STATUS_INITIALIZED);
                    it.getNavigatableRowIterator().insertRow(rw);
                }

                //break comma separated line using ","
                st = new StringTokenizer(strLine, ",");
                while (st.hasMoreTokens()) {
                    //display csv values
                    tokenNumber++;

                    String theToken = st.nextToken();
                    System.out.println("Line # " + lineNumber + ", Token # " +
                                       tokenNumber + ", Token : " + theToken);
                    if (lineNumber > 1) {
                        // set Attribute Values
                        switch (tokenNumber) {
                        case 1:
                            rw.setAttribute("Ssn", theToken);
                        case 2:
                            rw.setAttribute("Firstname", theToken);
                        case 3:
                                rw.setAttribute("Monhrs", theToken);
                        case 4:
                                rw.setAttribute("Tuehrs", theToken);
                        case 5:
                                rw.setAttribute("Wedhrs", theToken);
                        case 6:
                                rw.setAttribute("Thrhrs", theToken);
                        case 7:
                                rw.setAttribute("Frihrs", theToken);
                        case 8:
                                rw.setAttribute("Sathrs", theToken);
                        case 9:
                                rw.setAttribute("Sunhrs", theToken);
                     
                        }
                    }
                }
                //reset token number
                tokenNumber = 0;
            }
        } catch (IOException e) {
            // TODO add more
            FacesContext fctx = FacesContext.getCurrentInstance();
            fctx.addMessage(impTab.getClientId(fctx),
                            new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                             "Content Error in Uploaded file",
                                             e.getMessage()));
        } catch (Exception e) {
            FacesContext fctx = FacesContext.getCurrentInstance();
            fctx.addMessage(null,
                            new FacesMessage(FacesMessage.SEVERITY_ERROR, "Data Error in Uploaded file",
                                             e.getMessage()));
        }
    }


Hope it helps !!!

Thursday, December 22, 2011

Enable FTP for Mac OS X Lion

I was just wondering where is the FTP option for Mac OS X Lion. It used to be in System Preferences -> Sharing but with OS X Lion, it looks like the FTP option is removed but it is not!

To enable FTP, you need to run the following in the terminal
sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist

To disable,
sudo -s launchctl unload -w /System/Library/LaunchDaemons/ftp.plist

Monday, December 19, 2011

Installing Oracle XE 11g on Ubuntu 11.10

Ubuntu is not supported OS for Oracle XE 11g but still we can make it work by following below post. The main issue is due to memory management used by previous version vs ubuntu 11.10.

Friday, December 16, 2011

Oracle XE 11g Post Installation Issue

After installing Oracle XE 11g database, if you get below error when running below command,

[root@*** ~]# /etc/init.d/oracle-xe status

LSNRCTL for Linux: Version 11.2.0.2.0 - Beta on 19-APR-2011 04:07:41

Copyright (c) 1991, 2010, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC_FOR_XE)))
TNS-12541: TNS:no listener
TNS-12560: TNS:protocol adapter error
 TNS-00511: No listener
  Linux Error: 111: Connection refused
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=*******)(PORT=1521)))
TNS-12541: TNS:no listener
TNS-12560: TNS:protocol adapter error
 TNS-00511: No listener
  Linux Error: 111: Connection refused


then it is an issue with network settings of your OS. The main reason is that the listener.ora was referencing a network settings which was modified after the Oracle XE installation.

The solution is to rename the listener.ora to something different (for example, listener.ora_old) and then restart the database by /etc/init.d/oracle-xe restart