Sitemap

Saturday, April 11, 2015

UCM: Create a resultset and store it in HDA file

SCENARIO: I will create a custom resultset and save it in the form of HDA file in a particular location. I can later access the table and retrieve the data. I hope the code is self-explainable.


public class CreateHDAFile extends Service {

    public static final String HDAFILENAME = "hdafile.hda";
    public static final String CUSTOMRESULTSET = "CustomResultSet";
    public static final String CLASS_NAME = "CreateHDAFile";
    public static final String DIRECTORY = DirectoryLocator.getAppDataDirectory() + "test/";

    public void createHDAFile() throws ServiceException, DataException {
        String query = "SELECT r.did, r.ddocname, R.DDOCTITLE FROM revisions r, docmeta d WHERE r.did = d.did";
        ResultSet resultSet = m_workspace.createResultSetSQL(query);
        DataResultSet dataResultSet = new DataResultSet();
        dataResultSet.copy(resultSet);
        m_binder.addResultSet("InputDataHDAFile", dataResultSet);
        saveDataResultSet(m_binder);
    }

    public static void saveDataResultSet(DataBinder data) throws ServiceException {
        trace(DIRECTORY);
        FileUtils.checkOrCreateDirectoryEx(DIRECTORY, 0, true);
        FileUtils.reserveDirectory(DIRECTORY);
        try {
            ResourceUtils.serializeDataBinder(DIRECTORY, HDAFILENAME, data, true, true);
        } finally {
            FileUtils.releaseDirectory(DIRECTORY);
        }
        DataResultSet drset = (DataResultSet) data.getResultSet("InputDataHDAFile");
        SharedObjects.putTable(CUSTOMRESULTSET, drset);
    }
}

These are the contents of the HDA file:

@ResultSet InputDataHDAFile
3
dID 3 38
dDocName 6 30
dDocTitle 6 255
1
LTSCHAUDHARYHY000001
Test
201
HELLO
HELLO
202
SMILEY
SMILEY
@end

Now to access the contents of the HDA file:

DataBinder binder = new DataBinder();
binder = ResourceUtils.readDataBinder(DIRECTORY, HDAFILENAME);
DataResultSet savedMap = (DataResultSet) binder.getResultSet("InputDataHDAFile");

No comments:

Post a Comment