Reuse of extension methods

Hello,

We recently discover your different products and are trying to determine the faisaibility of a feature.
In our data model, we have a lot of string collections (arrays) and we want to format them like this:

“Mr. A, Mr B, Mr C and Mr D”

We tried to implement that using ([Template Syntax - Part 2 of 2 | GroupDocs.Assembly for Java](Extension Methods of Iteration Variables)) and it works like a charm!

But our question is, can we avoid to repeat this kind of logic for each collection that we have ?
We tried to create a static method and referenced the class in “DocumentAssembler.knowTypes”, but we do not receive directly a collection of strings but an object from your model representing the iteration.

Kind regards

@ybenkh

We are investigating this scenario. Your investigation ticket ID is ASSEMBLYJAVA-30.

Hi @Atir_Tahir,

Thanks for your prompt answer.
Do you have a link to be able to follow the ticket ASSEMBLYJAVA-30 ?

Regards

@ybenkh

In order to continue investigation, we further need following details:

  • Sample code/application,
  • Source template/document
  • Data

The use case is pretty simple as said before.

Here is our template (15.1 KB).

I cannot attach a json file, so here it is our data.json:

{
  "contacts": ["Mr A", "Mr B", "Mr C", "Mr D"],
  "reasons": ["LETR", "CP31.2", "LSTUP"],
  "places": ["Geneva", "Lausanne", "Bern"]
}

Then the source code is also as simple as examples provided in your documentation:

final String strDocumentTemplate = "in.docx";
final String strDocumentReport = "out.docx";

final JsonDataSource datasource = new JsonDataSource(new FileInputStream(strDirectoryPath + "datasources/data.json"));
final DataSourceInfo dataSourceInfo = new DataSourceInfo(datasource);

final DocumentAssembler assembler = new DocumentAssembler();
assembler.assembleDocument(
  strDirectoryPath + strDocumentTemplate,
  strDirectoryPath + strDocumentReport,
  dataSourceInfo
);

Please note that we are using GroupDocs Assembly v22.2.

@ybenkh

Thanks for the details. We are further looking into this issue.

@ybenkh

Please use following code and the attached template - in2.zip (12.4 KB).

public class Runner
{
    // com.groupdocs.assembly.system.data.DataRow
    public static String printList(Iterable<DataRow> rows, String fieldName, String commonSeparator, String lastSeparator)
    {
        StringBuilder builder = new StringBuilder();
        int lastCommonSeparatorPosition = -1;

        for (DataRow row : rows)
        {
            if (builder.length() != 0)
            {
                lastCommonSeparatorPosition = builder.length();
                builder.append(commonSeparator);
            }

            builder.append(row.get(fieldName));
        }

        if (lastCommonSeparatorPosition != -1)
        {
            builder.delete(lastCommonSeparatorPosition, lastCommonSeparatorPosition + commonSeparator.length());
            builder.insert(lastCommonSeparatorPosition, lastSeparator);
        }

        return builder.toString();
    }
}

Note that DocumentAssembler should be made aware of Runner before assembling a document as follows:

DocumentAssembler assembler = new DocumentAssembler(); 
assembler.getKnownTypes().add(Runner.class); 
assembler.assembleDocument(...);

Wow great! It fits perfectly to our needs.

Thanks again for your prompt answers.

1 Like

@ybenkh

You are welcome.