Skip to content

Commit

Permalink
fixed , docu
Browse files Browse the repository at this point in the history
Issue #150
  • Loading branch information
rsoika committed Jan 2, 2024
1 parent 56cce08 commit d1833b6
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 83 deletions.
64 changes: 35 additions & 29 deletions imixs-adapters-odf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,28 @@ The adapter can replace text fragments in a paragraph of a ODF document. The ada
The following example searches for an attachment with the name `Agreement-????.odf` and replaces the text fragments `#:company.name:#`, `#:company.country:#` and `#:contract_startdate:#` with the corresponding item values provided by the workitem.

```xml
<odf-update name=
"filename">Agreement-<itemvalue>numsequencenumber</itemvalue>.odf</odf-update>
<odf-update name="findreplace">
<find>#:company.name:#</find>
<replace><itemvalue>company.name</itemvalue></replace>
</odf-update>
<odf-update name="findreplace">
<find>#:company\.country:#</find>
<replace><itemvalue>company.country</itemvalue></replace>
</odf-update>
<odf-update name="findreplace">
<find>#:contract\.startdate:#</find>
<replace><itemvalue format="EEE, MMM d, yyyy">contract.start</itemvalue></replace>
<odf-update>
<filename>Agreement-<itemvalue>numsequencenumber</itemvalue>.odf</filename>
<replace>
<key>#:company.name:#</key>
<value><itemvalue>company.name</itemvalue></value>
</replace>
<replace>
<key>#:company\.country:#</key>
<value><itemvalue>company.country</itemvalue></value>
</replace>
<replace>
<key>#:contract\.startdate:#</key>
<value><itemvalue format="EEE, MMM d, yyyy">contract.start</itemvalue></value>
</replace>
</odf-update>
```

### Regular Expressions for Document Name

You can also define the filename as a pattern including regular expressions. See the following example:

<odf-update name="filename">.*<itemvalue>numsequencenumber</itemvalue>\.odf</odf-update>
<filename>.*<itemvalue>numsequencenumber</itemvalue>\.odf</filename>

This expression will match all files ending with the sequence number and the file extension '.odf'

Expand All @@ -45,9 +46,12 @@ This expression will match all files ending with the sequence number and the fil
The find replace feature of the ODFToolkit supports regular expressions to search for content. For example

```xml
<odf-update name="findreplace">
<find>(Order)\w+</find>
<replace><itemvalue>order.number</itemvalue></replace>
<odf-update>
...
<replace>
<key>(Order)\w+</find>
<value><itemvalue>order.number</itemvalue></value>
</replace>
</odf-update>
```

Expand All @@ -58,18 +62,20 @@ will replace all words starting with 'Order' like 'OrderNumber' , or 'OrderName'
To update a SpreadSheet cell (.ods files) you can simply specify the cell position. See the following Example:

```xml
<odf-update name="filename">order_<itemvalue>order.number</itemvalue>.ods</odf-update>
<odf-update name="findreplace">
<find>A5</find>
<replace><itemvalue>company.name</itemvalue></replace>
</odf-update>
<odf-update name="findreplace">
<find>B18</find>
<replace><itemvalue>total</itemvalue></replace>
</odf-update>
<odf-update name="findreplace">
<find>C18</find>
<replace><itemvalue format="dd.MM.yyyy">$lasteventdate</itemvalue></replace>
<odf-update>
<filename>order_<itemvalue>order.number</itemvalue>.ods</filename>
<replace>
<key>A5</key>
<value><itemvalue>company.name</itemvalue></value>
</replace>
<replace>
<key>B18</key>
<value><itemvalue>total</itemvalue></value>
</replace>
<replace>
<key>C18</key>
<value><itemvalue format="dd.MM.yyyy">$lasteventdate</itemvalue></value>
</replace>
</odf-update>

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.regex.Pattern;
Expand All @@ -16,6 +17,7 @@
import org.imixs.workflow.engine.WorkflowService;
import org.imixs.workflow.exceptions.AdapterException;
import org.imixs.workflow.exceptions.PluginException;
import org.imixs.workflow.exceptions.ProcessingErrorException;
import org.imixs.workflow.util.XMLParser;
import org.odftoolkit.odfdom.doc.OdfDocument;
import org.odftoolkit.odfdom.doc.OdfSpreadsheetDocument;
Expand All @@ -37,6 +39,16 @@
*
* <pre>
* {@code
*
<odf-update>
<filename>file1.odf</filename>
<replace>
<key>\[contract\.start\]</key>
<value><itemvalue>contract.start</itemvalue></value>
</replace>
</odf-update>
OLD
<odf-update name=
"filename">PLA Membership Agreement-<itemvalue>numsequencenumber</itemvalue>.odt</odf-update>
<odf-update name="findreplace">
Expand All @@ -56,7 +68,7 @@
* </pre>
*
*
* @version 1.0
* @version 2.0
* @author rsoika
*/
public class ODFDOMFindReplaceAdapter implements SignalAdapter {
Expand Down Expand Up @@ -85,58 +97,85 @@ public class ODFDOMFindReplaceAdapter implements SignalAdapter {
public ItemCollection execute(ItemCollection workitem, ItemCollection event)
throws AdapterException, PluginException {

// read the config
ItemCollection odfConfig = workflowService.evalWorkflowResult(event, "odf-update", workitem, false);
if (odfConfig == null || !odfConfig.hasItem("findreplace")) {
throw new PluginException(ODFDOMFindReplaceAdapter.class.getSimpleName(), CONFIG_ERROR,
"Missing odf configuration");
boolean isDeprecated = false;
ItemCollection evalItemCollection = null;
List<ItemCollection> odfUpdateConfigList = null;

// read the mandatory config
// test for deprecated configuration using the <item> tag....
isDeprecated = isDeprecatedConfiguration(workitem, event);
if (isDeprecated) {
logger.warning(
"WopiTemplateAdapter is using deprecated configuration! Please use <odf-update> instead of <odf-update name='find-replace'> - see documentation for details!");
evalItemCollection = workflowService.evalWorkflowResult(event, "odf-update", workitem,
false);
odfUpdateConfigList = new ArrayList<>();
odfUpdateConfigList.add(evalItemCollection);
} else {
String workflowResult = event.getItemValueString("txtActivityResult");
odfUpdateConfigList = XMLParser.parseTagList(workflowResult, "odf-update");
}
String fileName = odfConfig.getItemValueString("filename");
fileName = workflowService.adaptText(fileName, workitem);

if (!fileName.toLowerCase().endsWith(".odt") && !fileName.toLowerCase().endsWith(".ods")) {
throw new PluginException(ODFDOMFindReplaceAdapter.class.getSimpleName(), CONFIG_ERROR,
"Only .odt, .ods files are supported!");
}
List<String> replaceDevList = odfConfig.getItemValue("findreplace");
String eval = odfConfig.getItemValueString("eval");
if (replaceDevList.size() == 0) {
throw new PluginException(ODFDOMFindReplaceAdapter.class.getSimpleName(), CONFIG_ERROR,
"Wrong odf configuration");
if (odfUpdateConfigList == null || odfUpdateConfigList.size() == 0) {
throw new ProcessingErrorException(ODFDOMFindReplaceAdapter.class.getSimpleName(), CONFIG_ERROR,
"missing odf-update configuraiton in BPMN event!");
}

// First we test if the fileName is unique. If not found we test regular
// expressions...
boolean foundFile = false;
FileData fileData = workitem.getFileData(fileName);
if (fileData != null) {
// file data found - so we can updated it....
foundFile = true;
updateFileData(fileData, workitem, replaceDevList, eval);
} else {
// not found, we can test regular expressions...
List<String> fileNames = workitem.getFileNames();
Pattern pattern = Pattern.compile(fileName);
// get all fileNames....
for (String aFileName : fileNames) {
// test if aFilename matches the pattern or the pattern is null
if (pattern.matcher(aFileName).find()) {
// fetch the file
fileData = workitem.getFileData(aFileName);
if (fileData != null) {
// file data found - so we can updated it....
foundFile = true;
updateFileData(fileData, workitem, replaceDevList, eval);
}
// iterate over all odf-udpate configurations
for (ItemCollection odfUpdateConfig : odfUpdateConfigList) {
String fileName = odfUpdateConfig.getItemValueString("filename");
fileName = workflowService.adaptText(fileName, workitem);
if (!fileName.toLowerCase().endsWith(".odt") && !fileName.toLowerCase().endsWith(".ods")) {
throw new PluginException(ODFDOMFindReplaceAdapter.class.getSimpleName(), CONFIG_ERROR,
"Only .odt, .ods files are supported!");
}

List<String> replaceDevList = null;
// support deprecated config
if (isDeprecated) {
replaceDevList = odfUpdateConfig.getItemValue("findreplace");
} else {
// new tag
replaceDevList = odfUpdateConfig.getItemValue("replace");
}

if (replaceDevList.size() == 0) {
throw new PluginException(ODFDOMFindReplaceAdapter.class.getSimpleName(), CONFIG_ERROR,
"Wrong odf configuration - missing <replace> tag! Verify BPMN event confiuration!");
}

// First we test if the fileName is unique. If not found we test regular
// expressions...
boolean foundFile = false;
FileData fileData = workitem.getFileData(fileName);
if (fileData != null) {
// file data found - so we can updated it....
foundFile = true;
updateFileData(fileData, workitem, replaceDevList);
} else {
// not found, we can test regular expressions...
List<String> fileNames = workitem.getFileNames();
Pattern pattern = Pattern.compile(fileName);
// get all fileNames....
for (String aFileName : fileNames) {
// test if aFilename matches the pattern or the pattern is null
if (pattern.matcher(aFileName).find()) {
// fetch the file
fileData = workitem.getFileData(aFileName);
if (fileData != null) {
// file data found - so we can updated it....
foundFile = true;
updateFileData(fileData, workitem, replaceDevList);
}
}
}
}
}

// did we found at least one file
if (foundFile == false) {
throw new PluginException(ODFDOMFindReplaceAdapter.class.getSimpleName(), CONFIG_ERROR,
"wrong odf configuration - no file found matching '" + fileName + "' !");
// did we found at least one file
if (foundFile == false) {
throw new PluginException(ODFDOMFindReplaceAdapter.class.getSimpleName(), CONFIG_ERROR,
"wrong odf configuration - no file found matching '" + fileName + "' !");
}
}

return workitem;
Expand All @@ -152,13 +191,11 @@ public ItemCollection execute(ItemCollection workitem, ItemCollection event)
*
* @param fileData - the fileData object containing the text or calc file
* @param replaceDevList - list of text markers or cell positions to be replaced
* @param eval - optional list of cell positions to be evaluated after
* update (this is for XSSFWorkbooks only)
* @throws IOException
* @throws PluginException
*
*/
public void updateFileData(FileData fileData, ItemCollection workitem, List<String> replaceDevList, String eval)
public void updateFileData(FileData fileData, ItemCollection workitem, List<String> replaceDevList)
throws PluginException {
byte[] newContent = null;

Expand Down Expand Up @@ -235,18 +272,32 @@ public void updateODFDocument(OdfDocument odfDoc, ItemCollection workitem, List<
ItemCollection entityData = XMLParser.parseItemStructure(entityDev);

if (entityData != null) {
String find = entityData.getItemValueString("find");
find = workflowService.adaptText(find, workitem);
String replace = entityData.getItemValueString("replace");
replace = workflowService.adaptText(replace, workitem);
String key = entityData.getItemValueString("key");
// support deprected config
if (entityData.hasItem("find")) {
key = entityData.getItemValueString("find");
}
String value = entityData.getItemValueString("value");
// support depecated config-key
if (entityData.hasItem("replace")) {
value = entityData.getItemValueString("replace");
}

// trim whitespace!
key = key.trim();
value = value.trim();

// adapt values...
key = workflowService.adaptText(key, workitem);
value = workflowService.adaptText(value, workitem);

if (odfDoc instanceof OdfTextDocument) {
replaceODFTextFragment((OdfTextDocument) odfDoc, find, replace);
replaceODFTextFragment((OdfTextDocument) odfDoc, key, value);
}
if (odfDoc instanceof OdfSpreadsheetDocument) {
// get first table sheet...
OdfTable table = odfDoc.getTableList(true).get(0);
replaceODFCell((OdfSpreadsheetDocument) odfDoc, table, find, replace);
replaceODFCell((OdfSpreadsheetDocument) odfDoc, table, key, value);
}

}
Expand Down Expand Up @@ -282,4 +333,26 @@ private void replaceODFCell(OdfSpreadsheetDocument doc, OdfTable table, String a

}

/**
* This method tests if the BPMN configuration is still using the deprecated tag
*
* <odf-update name="findreplace">....
*
* instead of the new
*
* <odf-update>....
*
* @param event
* @return
* @throws PluginException
*/
private boolean isDeprecatedConfiguration(ItemCollection workitem, ItemCollection event) throws PluginException {

String workflowResult = event.getItemValueString("txtActivityResult");
if (!workflowResult.contains("<odf-update>")) {
return true;
}

return false;
}
}

0 comments on commit d1833b6

Please sign in to comment.