Skip to content

Commit c62e8f1

Browse files
committed
Fix bug #35
1 parent 4b183a6 commit c62e8f1

File tree

10 files changed

+115
-49
lines changed

10 files changed

+115
-49
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This addon provides an action to extract OCR text from images or plain PDFs in A
88
The plugin is licensed under the [LGPL v3.0](http://www.gnu.org/licenses/lgpl-3.0.html).
99

1010
**State**
11-
Current addon release is 2.0.0
11+
Current addon release is 2.1.0
1212

1313
**Compatibility**
1414
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)

simple-ocr-repo/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>es.keensoft</groupId>
66
<artifactId>simple-ocr-repo</artifactId>
7-
<version>2.0</version>
7+
<version>2.1.0</version>
88
<name>simple-ocr-repo Platform Jar Module - SDK 3</name>
99
<description>Platform JAR Module (to be included in the alfresco.war) - SDK 3</description>
1010
<packaging>jar</packaging>

simple-ocr-repo/src/main/java/es/keensoft/alfresco/ocr/OCRExtractAction.java

+44-36
Original file line numberDiff line numberDiff line change
@@ -65,39 +65,47 @@ protected void addParameterDefinitions(List<ParameterDefinition> paramList) {
6565
@Override
6666
protected void executeImpl(Action action, NodeRef actionedUponNodeRef) {
6767

68-
if (!nodeService.hasAspect(actionedUponNodeRef, OCRdModel.ASPECT_OCRD)) {
68+
if (nodeService.hasAspect(actionedUponNodeRef, OCRdModel.ASPECT_OCRD)) {
6969

70-
ContentData contentData = (ContentData) nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_CONTENT);
71-
72-
// Exclude folders and other nodes without content
73-
if (contentData != null) {
74-
75-
Boolean continueOnError = (Boolean) action.getParameterValue(PARAM_CONTINUE_ON_ERROR);
76-
if (continueOnError == null) continueOnError = true;
77-
78-
// # 5 Problem writing OCRed file
79-
// As action.getExecuteAsychronously() returns always FALSE (it's an Alfresco issue):
80-
// 1 - Try first with new Transaction
81-
// 2 - In case of error, try then with the current Transaction
82-
try {
83-
executeInNewTransaction(actionedUponNodeRef, contentData);
84-
} catch (Throwable throwableNewTransaction) {
85-
logger.warn(actionedUponNodeRef + ": " + throwableNewTransaction.getMessage());
86-
try {
87-
// Current transaction
88-
executeImplInternal(actionedUponNodeRef, contentData);
89-
} catch (Throwable throwableCurrentTransaction) {
90-
if (continueOnError) {
91-
logger.warn(actionedUponNodeRef + ": " + throwableNewTransaction.getMessage());
92-
} else {
93-
throw throwableCurrentTransaction;
94-
}
95-
}
96-
}
97-
98-
}
70+
String versionNode = nodeService.getProperty(actionedUponNodeRef, OCRdModel.PROP_APPLIED_VERSION).toString();
71+
String versionOCR = versionService.getCurrentVersion(actionedUponNodeRef).getVersionLabel().toString();
9972

73+
if(versionNode.equals(versionOCR)) {
74+
return ;
75+
}
10076
}
77+
78+
ContentData contentData = (ContentData) nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_CONTENT);
79+
80+
// Exclude folders and other nodes without content
81+
if (contentData != null) {
82+
83+
Boolean continueOnError = (Boolean) action.getParameterValue(PARAM_CONTINUE_ON_ERROR);
84+
if (continueOnError == null) continueOnError = true;
85+
86+
// # 5 Problem writing OCRed file
87+
// As action.getExecuteAsychronously() returns always FALSE (it's an Alfresco issue):
88+
// 1 - Try first with new Transaction
89+
// 2 - In case of error, try then with the current Transaction
90+
try {
91+
executeInNewTransaction(actionedUponNodeRef, contentData);
92+
} catch (Throwable throwableNewTransaction) {
93+
logger.warn(actionedUponNodeRef + ": " + throwableNewTransaction.getMessage());
94+
try {
95+
// Current transaction
96+
executeImplInternal(actionedUponNodeRef, contentData);
97+
} catch (Throwable throwableCurrentTransaction) {
98+
if (continueOnError) {
99+
logger.warn(actionedUponNodeRef + ": " + throwableNewTransaction.getMessage());
100+
} else {
101+
throw throwableCurrentTransaction;
102+
}
103+
}
104+
}
105+
106+
}
107+
108+
101109

102110
}
103111

@@ -166,19 +174,19 @@ private void executeImplInternal(NodeRef actionedUponNodeRef, ContentData conten
166174
writeOriginalContent.setMimetype(MimetypeMap.MIMETYPE_PDF);
167175
}
168176
writeOriginalContent.putContent(writer.getReader());
169-
170-
// Set OCRd aspect to avoid future re-OCR process
171-
Map<QName, Serializable> aspectProperties = new HashMap<QName, Serializable>();
172-
aspectProperties.put(OCRdModel.PROP_PROCESSED_DATE, new Date());
173-
nodeService.addAspect(actionedUponNodeRef, OCRdModel.ASPECT_OCRD, aspectProperties);
174177

175178
// Manual versioning because of Alfresco insane rules for first version content nodes
176179
versionService.ensureVersioningEnabled(actionedUponNodeRef, null);
177180
Map<String, Serializable> versionProperties = new HashMap<String, Serializable>();
178181
versionProperties.put(Version.PROP_DESCRIPTION, "OCRd");
179182
versionProperties.put(VersionModel.PROP_VERSION_TYPE, VersionType.MINOR);
180183
versionService.createVersion(actionedUponNodeRef, versionProperties);
181-
184+
185+
// Set OCRd aspect to avoid future re-OCR process
186+
Map<QName, Serializable> aspectProperties = new HashMap<QName, Serializable>();
187+
aspectProperties.put(OCRdModel.PROP_PROCESSED_DATE, new Date());
188+
aspectProperties.put(OCRdModel.PROP_APPLIED_VERSION, versionService.getCurrentVersion(actionedUponNodeRef).getVersionLabel());
189+
nodeService.addAspect(actionedUponNodeRef, OCRdModel.ASPECT_OCRD, aspectProperties);
182190
}
183191

184192
private NodeRef createNode(NodeRef parentNodeRef, String name, Map<QName, Serializable> props) {

simple-ocr-repo/src/main/java/es/keensoft/alfresco/ocr/model/OCRdModel.java

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ public interface OCRdModel {
66
static final String URI = "http://www.keensoft.es/model/content/ocr/1.0";
77
static final QName ASPECT_OCRD = QName.createQName(URI, "ocrd");
88
static final QName PROP_PROCESSED_DATE = QName.createQName(URI, "processedDate");
9+
static final QName PROP_APPLIED_VERSION = QName.createQName(URI, "versionApplied");
910
}
1011

simple-ocr-repo/src/main/resources/alfresco/module/simple-ocr-repo/model/ocr-model.xml

+8
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
<description>OCR Model</description>
44
<author>keensoft</author>
55
<version>1.0</version>
6+
67
<imports>
78
<import uri="http://www.alfresco.org/model/dictionary/1.0" prefix="d" />
89
</imports>
10+
911
<namespaces>
1012
<namespace uri="http://www.keensoft.es/model/content/ocr/1.0" prefix="ocr" />
1113
</namespaces>
14+
1215
<aspects>
1316
<aspect name="ocr:ocrd">
1417
<title>OCRd</title>
@@ -17,7 +20,12 @@
1720
<title>OCR process date</title>
1821
<type>d:datetime</type>
1922
</property>
23+
<property name="ocr:versionApplied">
24+
<title>OCR applied version</title>
25+
<type>d:text</type>
26+
</property>
2027
</properties>
2128
</aspect>
2229
</aspects>
30+
2331
</model>

simple-ocr-share/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>es.keensoft</groupId>
66
<artifactId>simple-ocr-share</artifactId>
7-
<version>2.0</version>
7+
<version>2.1.0</version>
88
<name>simple-ocr-share Share Jar Module - SDK 3</name>
99
<description>Share JAR Module (to be included in the share.war) - SDK 3</description>
1010
<packaging>jar</packaging>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package es.keensoft.evaluator;
2+
3+
import org.alfresco.web.evaluator.BaseEvaluator;
4+
import org.json.simple.JSONObject;
5+
6+
public class OCREvaluator extends BaseEvaluator {
7+
8+
@Override
9+
public boolean evaluate(JSONObject json) {
10+
11+
JSONObject node = (JSONObject) json.get("node");
12+
JSONObject properties = (JSONObject) node.get("properties");
13+
14+
if(!properties.containsKey("ocr:versionApplied")) {
15+
return false;
16+
}
17+
18+
String currentVersion = properties.get("cm:versionLabel").toString();
19+
String appliedVersion = properties.get("ocr:versionApplied").toString();
20+
21+
return currentVersion.equals(appliedVersion);
22+
}
23+
24+
}

simple-ocr-share/src/main/resources/META-INF/share-config-custom.xml

+29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
<alfresco-config>
22

3+
<aspects>
4+
<visible>
5+
<aspect name="ocr:ocrd" />
6+
</visible>
7+
8+
<addable>
9+
</addable>
10+
11+
<removeable>
12+
</removeable>
13+
</aspects>
14+
15+
<config evaluator="aspect" condition="ocr:ocrd">
16+
<forms>
17+
<form>
18+
<field-visibility>
19+
<show id="ocr:processedDate" />
20+
<show id="ocr:versionApplied" />
21+
</field-visibility>
22+
23+
<appearance>
24+
<set id="ocr-properties" appearance="bordered-panel" label-id="ocr.property.panel" />
25+
<field id="ocr:processedDate" label-id="ocr.property.processed.date" set="ocr-properties" />
26+
<field id="ocr:versionApplied" label-id="ocr.property.version.applied" set="ocr-properties" />
27+
</appearance>
28+
</form>
29+
</forms>
30+
</config>
31+
332
<config evaluator="string-compare" condition="DocumentLibrary">
433
<indicators>
534
<indicator id="indicator.ocr.done" index="1" icon="icon-ocr-16.png" label="ocr.done">

simple-ocr-share/src/main/resources/alfresco/web-extension/messages/simple-ocr-share.properties

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
# Form field labels
55
# Doc lib Action labels and messages
66
# Doc Lib Action Forms labels
7-
#
87

98
# Labels for custom types and aspects
109
# Used in "Manage Aspects" and "Change Type" dialogs
1110
action.waiting.message = Processing OCR ...
1211
ocr.action.success = Success
1312
ocr.action.error = Error
1413
ocr.done = OCR done
15-
ocr.action.name = OCR
14+
ocr.action.name = OCR
15+
16+
ocr.property.processed.date = Processed date
17+
ocr.property.version.applied = Version applied
18+
ocr.property.panel = OCR properties

simple-ocr-share/src/main/resources/alfresco/web-extension/simple-ocr-share-slingshot-application-context.xml

+1-8
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,5 @@
1414
</property>
1515
</bean>
1616

17-
<bean id="evaluator.doclib.ocr.done" parent="evaluator.doclib.action.hasAspect">
18-
<property name="aspects">
19-
<list>
20-
<value>ocr:ocrd</value>
21-
</list>
22-
</property>
23-
</bean>
24-
17+
<bean id="evaluator.doclib.ocr.done" class="es.keensoft.evaluator.OCREvaluator" />
2518
</beans>

0 commit comments

Comments
 (0)