How to implement condition based file upload in DocLib ?

thumbnail
Rajesh Chaurasia, modified 6 Years ago. Regular Member Posts: 183 Join Date: 8/18/11 Recent Posts
hi All,

I have a use case where i want to upload a documents(s) based on OCR match ie if there is a specific text present in document i am uploading , only then the document should get uploaded.I have been able to do it for a document when there is match , in which case i safely upload the document.But i need to know what should i return in case of no match in which case i am return null , causing incorrect page to show with ui anomalies.I used a service wrapper and overided the folllowing:

@Component(immediate = true, property = {}, service = ServiceWrapper.class)
public class DocumentOcrBasedUpload extends DLFileEntryLocalServiceWrapper

@Override
    public com.liferay.document.library.kernel.model.DLFileEntry addFileEntry(long userId, long groupId,
            long repositoryId, long folderId, String sourceFileName, String mimeType, String title, String description,
            String changeLog, long fileEntryTypeId,
            java.util.Map<String, com.liferay.dynamic.data.mapping.kernel.DDMFormValues> ddmFormValuesMap,
            java.io.File file, java.io.InputStream is, long size,
            com.liferay.portal.kernel.service.ServiceContext serviceContext)
            throws com.liferay.portal.kernel.exception.PortalException {
        
            if(isOcrCompliantUpload(PropsUtil.get(OCR_MATCH_STRING),PropsUtil.get(OCR_WORK_DIR),sourceFileName)) {
                return super.addFileEntry(userId, groupId, repositoryId, folderId, sourceFileName, mimeType, title, description, changeLog, fileEntryTypeId, ddmFormValuesMap, file, is, size, serviceContext);
            }else {
                return null; --> this is erroneous for me , i want to know what should be the return type in this case , it shouldnt be null ?
            }
    }
thumbnail
Olaf Kock, modified 6 Years ago. Liferay Legend Posts: 6441 Join Date: 9/23/08 Recent Posts
Rajesh Chaurasia:


@Override
&nbsp;&nbsp; &nbsp;public com.liferay.document.library.kernel.model.DLFileEntry addFileEntry(long userId, long groupId,
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;long repositoryId, long folderId, String sourceFileName, String mimeType, String title, String description,
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String changeLog, long fileEntryTypeId,
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;java.util.Map<string, com.liferay.dynamic.data.mapping.kernel.ddmformvalues> ddmFormValuesMap,
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;java.io.File file, java.io.InputStream is, long size,
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;com.liferay.portal.kernel.service.ServiceContext serviceContext)
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;throws com.liferay.portal.kernel.exception.PortalException {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(isOcrCompliantUpload(PropsUtil.get(OCR_MATCH_STRING),PropsUtil.get(OCR_WORK_DIR),sourceFileName)) {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return super.addFileEntry(userId, groupId, repositoryId, folderId, sourceFileName, mimeType, title, description, changeLog, fileEntryTypeId, ddmFormValuesMap, file, is, size, serviceContext);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}else {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return null; --&gt; this is erroneous for me , i want to know what should be the return type in this case , it shouldnt be null ?
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;}</string,>


Is this abbreviated? I'm wondering why
isOcrCompliantUpload
doesn't reference the file or inputStream parameter, and what would be OCR'ed in that case.
But if you deny creating that file, and don't create any file in the backend, you can't create and return an "empty" document (as that would create a document, which you don't want). Thus null seems fine to me.

However: Liferay also uploads images for use in illustrating your web content to the Document Library. Make sure that you don't deny their upload. You may want to apply the described limitation to a specific site or folder. Which brings me to an alternative method: You can also implement this functionality as a workflow, with a scripted approval step that would decouple the OCR process from the upload itself, providing the opportunity to manually override if necessary.
thumbnail
Rajesh Chaurasia, modified 6 Years ago. Regular Member Posts: 183 Join Date: 8/18/11 Recent Posts
Hi Olaf
Thanks for your reply ,

isOcrCompliantUpload is a method i added to class that i used to override the wrapper.It checks for ocr text string and returns true or false on return .

​​​​​​​​​​​​​​public boolean isOcrCompliantUpload(String keywordmatchtofailupload,String pathOfDir,String sourceFileName) {
        Ocr.setUp(); // one time setup
        Ocr ocr = new Ocr(); // create a new OCR engine
        ocr.startEngine("eng", Ocr.SPEED_FASTEST); // English
        String sOcrMatch = ocr.recognize(new File[] {new File( pathOfDir+sourceFileName)},Ocr.RECOGNIZE_TYPE_ALL, Ocr.OUTPUT_FORMAT_PLAINTEXT); // PLAINTEXT | XML | PDF | RTF
        System.out.println("sOcrMatch: " + sOcrMatch);
        ocr.stopEngine();
        if(null!=sOcrMatch && sOcrMatch.contains(keywordmatchtofailupload) ) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
thumbnail
Olaf Kock, modified 6 Years ago. Liferay Legend Posts: 6441 Join Date: 9/23/08 Recent Posts
Rajesh Chaurasia:

Hi Olaf
Thanks for your reply ,

isOcrCompliantUpload is a method i added to class that i used to override the wrapper.It checks for ocr text string and returns true or false on return .

​​​​​​​​​​​​​​
I'd rather expect sourceFileName to be just the name of the file originally uploaded, and have nothing to do with the temporary file that you find. The binary content is probably in the file or inputStream parameter.
thumbnail
Rajesh Chaurasia, modified 6 Years ago. Regular Member Posts: 183 Join Date: 8/18/11 Recent Posts
Hi Olaf ,

Will it be fine if instead of null i return an empty DLFileEntry object , or show a error message in ui that document was not uploaded ? In which case if you can let em know how to do that at Service wrapper level.
thumbnail
Olaf Kock, modified 6 Years ago. Liferay Legend Posts: 6441 Join Date: 9/23/08 Recent Posts
Rajesh Chaurasia:

Hi Olaf ,

Will it be fine if instead of null i return an empty DLFileEntry object , or show a error message in ui that document was not uploaded ? In which case if you can let em know how to do that at Service wrapper level.

The ServiceWrapper can't show an error message in the UI, as it's the service, and can be called from anywhere, with or without UI.
You'd have to identify the UI that you'd like to show the message.

An alternative to the null return might be to throw an exception. AFAIK there's no "empty" DLFileEntry object. I'd expect it to have an id and a persistent backend (or to be able to be persisted later).
thumbnail
Rajesh Chaurasia, modified 6 Years ago. Regular Member Posts: 183 Join Date: 8/18/11 Recent Posts
Hi Olaf 

Thanks for the prompt and relevant response.
​​​​​​​