Accessing XPages Data Source in a Plugin

For a plugin (like the extension library) I try to access the datasource with a given "var" name. Accessing the Datasource object is very easy with the following code:

m_DataSourceName contains the name (var) of the datasource.

public DataSource getDataSource() {
    if (StringUtil.isNotEmpty(m_DataSourceName)) {

        UIViewRoot vrCurrent = getFacesContext().getViewRoot();
        if (vrCurrent instanceof UIViewRootEx) {
            for (DataSource dsCurrent : ((UIViewRootEx) vrCurrent)
                    .getData()) {
                if (m_DataSourceName.equals(dsCurrent.getVar())) {
                    return dsCurrent;
                }
            }
        }
    }
    System.out.println("Datasource name:" + m_DataSourceName);
    return null;
}

I'm getting the datasource back and I can cast this datasource:

private TabularDataModel getTDM(DataSource dsCurrent, FacesContext context) {
        try {
            if (dsCurrent instanceof ModelDataSource) {
                ModelDataSource mds = (ModelDataSource) dsCurrent;
                AbstractDataSource ads = (AbstractDataSource) mds;
                ads.load(context);
                System.out.println(ads.getBeanId());
                if (ads.getBeanId() == null) {

                }
                DataModel tdm = mds.getDataModel();
                if (tdm instanceof TabularDataModel) {
                    TabularDataModel tds = (TabularDataModel) tdm;
                    return tds;
                }
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

And now I wanna access the TDM.getRowCount() and this point I'm getting a nullpointer exception. The datasource contains a notes view. Did I miss anything to initialize the datasource?

Upvotes: 0

Views: 407

Answers (1)

Sven Hasselbach
Sven Hasselbach

Reputation: 10485

Here is a solution for your problem:

This will give you all lines of a view, not the entry count*. F.e. if you have a categorized view with 5 categories and 1 entry for each category, this will result in 10 lines. The entry count is 5.

First, create a dummy class which implements FacesDataIterator

public class DummyDataIterator implements com.ibm.xsp.component.FacesDataIterator{

    public DataModel getDataModel() {
        return null;
    }

    public int getFirst() {
        return 0;
    }

    public int getRowIndex() {
        return 0;
    }

    public int getRows() {
        return 0;
    }

    public void setFirst(int paramInt) {}

    public void setRows(int paramInt) {}
}

And then you have to do the following:

  1. Set the data iterator

    tdm.setDataControl( new DummyDataIterator() );
    
  2. Init the row counter for the first time

    tdm.getRowCount();   
    
  3. Calculate the exact row count with a navigator

    (( com.ibm.xsp.model.domino.viewnavigator.NOIViewNavigatorEx) tdm.getDominoViewDataContainer().getNavigator()).calculateExactCount(tdm.getView());
    

Now your row count is initialized, you can get the result with a normal getRowCount:

System.out.println("Rows: " +  tdm.getRowCount() );

Hope this helps!

*:

tdm.getView().getAllEntries().getCount()

Upvotes: 1

Related Questions