Skip to content

Commit

Permalink
Fix bug #35
Browse files Browse the repository at this point in the history
  • Loading branch information
tfdsimoes-ks committed Jul 31, 2017
1 parent 4b183a6 commit c62e8f1
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This addon provides an action to extract OCR text from images or plain PDFs in A
The plugin is licensed under the [LGPL v3.0](http://www.gnu.org/licenses/lgpl-3.0.html).

**State**
Current addon release is 2.0.0
Current addon release is 2.1.0

**Compatibility**
The current version has been developed using Alfresco 5.2 and Alfresco SDK 3.0.2, although it should also run in Alfresco 5.1, 5.0 & 4.2 (as it is developed by using Alfresco SDK 3.0)
Expand Down
2 changes: 1 addition & 1 deletion simple-ocr-repo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>es.keensoft</groupId>
<artifactId>simple-ocr-repo</artifactId>
<version>2.0</version>
<version>2.1.0</version>
<name>simple-ocr-repo Platform Jar Module - SDK 3</name>
<description>Platform JAR Module (to be included in the alfresco.war) - SDK 3</description>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,39 +65,47 @@ protected void addParameterDefinitions(List<ParameterDefinition> paramList) {
@Override
protected void executeImpl(Action action, NodeRef actionedUponNodeRef) {

if (!nodeService.hasAspect(actionedUponNodeRef, OCRdModel.ASPECT_OCRD)) {
if (nodeService.hasAspect(actionedUponNodeRef, OCRdModel.ASPECT_OCRD)) {

ContentData contentData = (ContentData) nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_CONTENT);

// Exclude folders and other nodes without content
if (contentData != null) {

Boolean continueOnError = (Boolean) action.getParameterValue(PARAM_CONTINUE_ON_ERROR);
if (continueOnError == null) continueOnError = true;

// # 5 Problem writing OCRed file
// As action.getExecuteAsychronously() returns always FALSE (it's an Alfresco issue):
// 1 - Try first with new Transaction
// 2 - In case of error, try then with the current Transaction
try {
executeInNewTransaction(actionedUponNodeRef, contentData);
} catch (Throwable throwableNewTransaction) {
logger.warn(actionedUponNodeRef + ": " + throwableNewTransaction.getMessage());
try {
// Current transaction
executeImplInternal(actionedUponNodeRef, contentData);
} catch (Throwable throwableCurrentTransaction) {
if (continueOnError) {
logger.warn(actionedUponNodeRef + ": " + throwableNewTransaction.getMessage());
} else {
throw throwableCurrentTransaction;
}
}
}

}
String versionNode = nodeService.getProperty(actionedUponNodeRef, OCRdModel.PROP_APPLIED_VERSION).toString();
String versionOCR = versionService.getCurrentVersion(actionedUponNodeRef).getVersionLabel().toString();

if(versionNode.equals(versionOCR)) {
return ;
}
}

ContentData contentData = (ContentData) nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_CONTENT);

// Exclude folders and other nodes without content
if (contentData != null) {

Boolean continueOnError = (Boolean) action.getParameterValue(PARAM_CONTINUE_ON_ERROR);
if (continueOnError == null) continueOnError = true;

// # 5 Problem writing OCRed file
// As action.getExecuteAsychronously() returns always FALSE (it's an Alfresco issue):
// 1 - Try first with new Transaction
// 2 - In case of error, try then with the current Transaction
try {
executeInNewTransaction(actionedUponNodeRef, contentData);
} catch (Throwable throwableNewTransaction) {
logger.warn(actionedUponNodeRef + ": " + throwableNewTransaction.getMessage());
try {
// Current transaction
executeImplInternal(actionedUponNodeRef, contentData);
} catch (Throwable throwableCurrentTransaction) {
if (continueOnError) {
logger.warn(actionedUponNodeRef + ": " + throwableNewTransaction.getMessage());
} else {
throw throwableCurrentTransaction;
}
}
}

}



}

Expand Down Expand Up @@ -166,19 +174,19 @@ private void executeImplInternal(NodeRef actionedUponNodeRef, ContentData conten
writeOriginalContent.setMimetype(MimetypeMap.MIMETYPE_PDF);
}
writeOriginalContent.putContent(writer.getReader());

// Set OCRd aspect to avoid future re-OCR process
Map<QName, Serializable> aspectProperties = new HashMap<QName, Serializable>();
aspectProperties.put(OCRdModel.PROP_PROCESSED_DATE, new Date());
nodeService.addAspect(actionedUponNodeRef, OCRdModel.ASPECT_OCRD, aspectProperties);

// Manual versioning because of Alfresco insane rules for first version content nodes
versionService.ensureVersioningEnabled(actionedUponNodeRef, null);
Map<String, Serializable> versionProperties = new HashMap<String, Serializable>();
versionProperties.put(Version.PROP_DESCRIPTION, "OCRd");
versionProperties.put(VersionModel.PROP_VERSION_TYPE, VersionType.MINOR);
versionService.createVersion(actionedUponNodeRef, versionProperties);


// Set OCRd aspect to avoid future re-OCR process
Map<QName, Serializable> aspectProperties = new HashMap<QName, Serializable>();
aspectProperties.put(OCRdModel.PROP_PROCESSED_DATE, new Date());
aspectProperties.put(OCRdModel.PROP_APPLIED_VERSION, versionService.getCurrentVersion(actionedUponNodeRef).getVersionLabel());
nodeService.addAspect(actionedUponNodeRef, OCRdModel.ASPECT_OCRD, aspectProperties);
}

private NodeRef createNode(NodeRef parentNodeRef, String name, Map<QName, Serializable> props) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public interface OCRdModel {
static final String URI = "http://www.keensoft.es/model/content/ocr/1.0";
static final QName ASPECT_OCRD = QName.createQName(URI, "ocrd");
static final QName PROP_PROCESSED_DATE = QName.createQName(URI, "processedDate");
static final QName PROP_APPLIED_VERSION = QName.createQName(URI, "versionApplied");
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
<description>OCR Model</description>
<author>keensoft</author>
<version>1.0</version>

<imports>
<import uri="http://www.alfresco.org/model/dictionary/1.0" prefix="d" />
</imports>

<namespaces>
<namespace uri="http://www.keensoft.es/model/content/ocr/1.0" prefix="ocr" />
</namespaces>

<aspects>
<aspect name="ocr:ocrd">
<title>OCRd</title>
Expand All @@ -17,7 +20,12 @@
<title>OCR process date</title>
<type>d:datetime</type>
</property>
<property name="ocr:versionApplied">
<title>OCR applied version</title>
<type>d:text</type>
</property>
</properties>
</aspect>
</aspects>

</model>
2 changes: 1 addition & 1 deletion simple-ocr-share/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>es.keensoft</groupId>
<artifactId>simple-ocr-share</artifactId>
<version>2.0</version>
<version>2.1.0</version>
<name>simple-ocr-share Share Jar Module - SDK 3</name>
<description>Share JAR Module (to be included in the share.war) - SDK 3</description>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package es.keensoft.evaluator;

import org.alfresco.web.evaluator.BaseEvaluator;
import org.json.simple.JSONObject;

public class OCREvaluator extends BaseEvaluator {

@Override
public boolean evaluate(JSONObject json) {

JSONObject node = (JSONObject) json.get("node");
JSONObject properties = (JSONObject) node.get("properties");

if(!properties.containsKey("ocr:versionApplied")) {
return false;
}

String currentVersion = properties.get("cm:versionLabel").toString();
String appliedVersion = properties.get("ocr:versionApplied").toString();

return currentVersion.equals(appliedVersion);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
<alfresco-config>

<aspects>
<visible>
<aspect name="ocr:ocrd" />
</visible>

<addable>
</addable>

<removeable>
</removeable>
</aspects>

<config evaluator="aspect" condition="ocr:ocrd">
<forms>
<form>
<field-visibility>
<show id="ocr:processedDate" />
<show id="ocr:versionApplied" />
</field-visibility>

<appearance>
<set id="ocr-properties" appearance="bordered-panel" label-id="ocr.property.panel" />
<field id="ocr:processedDate" label-id="ocr.property.processed.date" set="ocr-properties" />
<field id="ocr:versionApplied" label-id="ocr.property.version.applied" set="ocr-properties" />
</appearance>
</form>
</forms>
</config>

<config evaluator="string-compare" condition="DocumentLibrary">
<indicators>
<indicator id="indicator.ocr.done" index="1" icon="icon-ocr-16.png" label="ocr.done">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
# Form field labels
# Doc lib Action labels and messages
# Doc Lib Action Forms labels
#

# Labels for custom types and aspects
# Used in "Manage Aspects" and "Change Type" dialogs
action.waiting.message = Processing OCR ...
ocr.action.success = Success
ocr.action.error = Error
ocr.done = OCR done
ocr.action.name = OCR
ocr.action.name = OCR

ocr.property.processed.date = Processed date
ocr.property.version.applied = Version applied
ocr.property.panel = OCR properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,5 @@
</property>
</bean>

<bean id="evaluator.doclib.ocr.done" parent="evaluator.doclib.action.hasAspect">
<property name="aspects">
<list>
<value>ocr:ocrd</value>
</list>
</property>
</bean>

<bean id="evaluator.doclib.ocr.done" class="es.keensoft.evaluator.OCREvaluator" />
</beans>

0 comments on commit c62e8f1

Please sign in to comment.