Sitemap

Showing posts with label intradoc. Show all posts
Showing posts with label intradoc. Show all posts

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");

Sunday, April 5, 2015

UCM: Handling SQL Queries in UCM service

In case a query have been defined in a query resource definition:

DataBinder db = new DataBinder();
db.putLocal("searchKey", searchKeyword);
db.putLocal("thName", THESAURUS_NAME);
db.putLocal("level", LEVEL);

ResultSet rs = m_workspace.createResultSet("CBIThesaurusSearch", db);
DataResultSet dataContainer = new DataResultSet();
dataContainer.copy(rs);
for (dataContainer.first(); dataContainer.isRowPresent(); dataContainer.next()) {
    System.out.println(dataContainer.getStringValueByName("THEVALUES"));
}


In case we are defining a query in the Java class itself:

String searchKeyword = m_binder.getLocal("ssUserText").toUpperCase().trim();

String maxCountRecord =
    "SELECT * FROM TABLE WHERE X_KEY = '" + key + "' AND X_ASSET_TYPE IS NOT NULL";

ResultSet resultSet = m_workspace.createResultSetSQL(maxCountRecord);
DataResultSet dataResultSet = new DataResultSet();
dataResultSet.copy(resultSet);
m_binder.addResultSet("RelatedSearchNavigation", dataResultSet);

UCM: Execute a PL/SQL Procedure from a service

This is my query resource definition
 name: (ORACLE.CALLABLE)searchQuery
 queryStr: {call RELEVANCY_SEARCH_PKG.RELEVANCY_SEARCH_COUNT (?,?,?,?)}
 parameters: p_ddocname varchar
             p_search_keyword varchar
             p_asset_type varchar
             p_message out:varchar


This is the Java method:
import intradoc.common.ServiceException;
import intradoc.common.SystemUtils;

import intradoc.data.CallableResults;
import intradoc.data.DataBinder;
import intradoc.data.DataException;
import intradoc.data.ResultSet;
import intradoc.data.ResultSetUtils;

import intradoc.server.Service;

import java.util.Properties;


public class AutoRelevancy extends Service {

    private static final String COMPONENT_DEBUG = "AutoRelevancy";

    public void calculateRelevancy() throws ServiceException, DataException {
        String searchKeyword = m_binder.getLocal("searchKeyword");

        try {
            if (m_binder.getLocal("dDocName") != null && (searchKeyword != null)) {

                String dDocName = m_binder.getLocal("dDocName");

                DataBinder db = new DataBinder();
                db.putLocal("dDocName", dDocName);
                ResultSet docInfoRS = m_workspace.createResultSet("QlatestDocInfoByName", db);

                Properties currentRowProp = ResultSetUtils.getCurrentRowProps(docInfoRS);

                final DataBinder binder = new DataBinder();

                binder.putLocal("p_ddocname", currentRowProp.getProperty("dDocName"));
                binder.putLocal("p_search_keyword", searchKeyword.toUpperCase().trim());
                binder.putLocal("p_asset_type", currentRowProp.getProperty("xasset_type"));

                CallableResults resultSql = this.m_workspace.executeCallable("searchQuery", binder);

                String v_message = resultSql.getString("p_message");
            }
        } catch (DataException d) {
            SystemUtils.trace(COMPONENT_DEBUG, "Exception" + d.getMessage());
        } catch (Exception e) {
            SystemUtils.trace(COMPONENT_DEBUG, "Exception" + e.getMessage());
        } finally {
            m_workspace.releaseConnection();
        }
    }
}

UCM: Limit check-in size and extension of the file

SCENARIO: We want to restrict the user to upload a file whose extension is .exe and whose size is more than 5 MB.

IMPLEMENTATION: To check the file extension, write a filter on postValidateCheckinData event. To check the file size, write a filter on validateStandard event.

postValidateCheckinData
Executed at the end of the action validateCheckinData and after the filter validateCheckinData. This is a good place for last-minute alterations of the metadata prior to a check-in.

validateStandard
Executed during update or check-in. This runs before the core validates the metadata fields, such as dReleaseState, dDocName, dRevClassID, dDocTitle, primaryFile, alternateFile, dCreateDate, dInDate, and dOutDate.

    private void checkFileExtension(DataBinder binder) throws ServiceException {
        String dExtension = binder.getLocal("dExtension");
        trace("dExtension: " + dExtension);
        String ExtensionsToControl = "";
        if (SharedObjects.getEnvironmentValue("ExtensionsToControl") != null) {
            ExtensionsToControl = SharedObjects.getEnvironmentValue("ExtensionsToControl");
        }
        trace("ExtensionsToControl: " + ExtensionsToControl);
        if (ExtensionsToControl.indexOf(dExtension) >= 0) {
            String msg = LocaleUtils.encodeMessage("csExtensionsToControl", null, dExtension);
            throw new ServiceException(msg);
        }
    }

    private void checkFileSize(DataBinder binder) throws ServiceException {
        //trace(binder.m_tempFiles.toString());
        String primaryFilePath = binder.getLocal("primaryFile:path");
        trace("primaryFilePath: " + primaryFilePath);
        File file = new File(primaryFilePath);
        long fileSize = file.length();
        trace("fileSize: " + fileSize);
        if (fileSize >= 5 * 1024 * 1024) {
            throw new ServiceException("This document is too large.");
        }
    }

Create an environment file as well:
ExtensionsToControl=exe,rar

I had also created a string resource file to display the error message in case the file extension matches with the one provided in the environment file:
<@csExtensionsToControl=Files of extension {1} cannot be uploaded.@>


NOTES
1. A better approach is provided in the link below:
http://sonaloraclewebcenter.blogspot.in/2015/04/ucm-validating-file-extension.html

2. If you want to check the mime type of the file and not the extension, use Apache Tika 1.7. 

UCM: Retrieve the vault and weblayout path of a content


public class RetrievePaths extends ServiceHandler {

    public static final String CLASS_NAME = "CreateHDAFile";
    public static final String DDOCNAME = "LTSCHAUDHARYHY000208";

    public void retrievePathFiles() throws ServiceException, DataException, IOException {
        m_binder.clearResultSets();
        m_binder.putLocal("dDocName", DDOCNAME);
        executeService(m_binder, "DOC_INFO_BY_NAME");
        FileStoreProviderHelper utils = this.m_service.m_fileUtils;
        String webViewableFilePath =
            utils.computeRenditionPath(m_binder, "webViewableFile", utils.m_context);
        String primaryFilePath =
            utils.computeRenditionPath(m_binder, "primaryFile", utils.m_context);
    }
}

Thanks to Ryan for this tip. More details in the link below: https://groups.yahoo.com/neo/groups/intradoc_users/conversations/topics/25937

UPDATE: Use the service GET_FS_DOC_INFO to get both the vault and weblayout path of a content. From the localdata, access the keys primaryFile and webViewableFile of the binder.