Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: How to implement condition based file upload in DocLib ?
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 ?
}
}
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 ?
}
}
Rajesh Chaurasia:
@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 ? } }</string,>
Is this abbreviated? I'm wondering whydoesn't reference the file or inputStream parameter, and what would be OCR'ed in that case.isOcrCompliantUpload
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.
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;
}
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;
}
Rajesh Chaurasia:
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.
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 .
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.
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.
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).
Hi Olaf
Thanks for the prompt and relevant response.
Thanks for the prompt and relevant response.
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™