Expand my Community achievements bar.

SOLVED

Assembling multiple XDPs having fragments - XFA Dynamic assembler failed to stitch a fragment

Avatar

Level 4

HI Folks,

Wanted to check if long standing issue of fragments reference is resolved in AEM 6.5 OSGI JAVA SDK, where form is unable to stitch fragments if they are not referenced with absolute paths or are in Repo.

 

We have Forms and fragments placed in Network instead of Repo and we are unable to perform DDX operations as we are getting 

XFA Dynamic assembler failed to stitch a fragment

we can neither refer using absolute path nor we can place them in repo. 

 

We are in need to combine 2 or more XFA based forms and generate 1 combined interactive form.

 

Any pointers?

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

public String insertFragments(String masterTemplate, Map < Integer, Map > fragmentsToAssemble, Document ddx) {
System.out.println("The master template is " + masterTemplate);
ResourceResolver serviceResolver = getResolver.getFormsServiceResolver();
javax.jcr.Node masterXdp = getResolver.getFormsServiceResolver()
.getResource("/content/dam/wefinancedemo/templates/" + masterTemplate +
"/jcr:content/renditions/original/jcr:content")
.adaptTo(javax.jcr.Node.class);

Map < String, Object > mapOfDocuments = new HashMap < String, Object > ();
try {
InputStream masterXdpIS = masterXdp.getProperty("jcr:data").getBinary().getStream();
Document masterXdpTemplate = new Document(masterXdpIS);
mapOfDocuments.put(masterTemplate, masterXdpTemplate);
for (Map.Entry < Integer, Map > map: fragmentsToAssemble.entrySet()) {
String fragmentName = (String) map.getValue().get("source");
System.out.println("The fragment name is " + fragmentName);
String fragmentPath = "/content/dam/wefinancedemo/fragments/" + fragmentName +
"/jcr:content/renditions/original/jcr:content";
System.out.println(map.getKey() + " = " + map.getValue().get("source"));
javax.jcr.Node fragment = getResolver.getFormsServiceResolver().getResource(fragmentPath)
.adaptTo(javax.jcr.Node.class);
InputStream fragmentis = fragment.getProperty("jcr:data").getBinary().getStream();
Document fragmentToInclude = new Document(fragmentis);
System.out.println("$$$$ Added document " + fragmentName + " to the map");
mapOfDocuments.put(fragmentName, fragmentToInclude);
fragmentName = null;

}
for (Map.Entry < String, Object > map: mapOfDocuments.entrySet()) {
System.out.println("The keys in the custom map are " + map.getKey());

}
AssemblerOptionSpec aoSpec = new AssemblerOptionSpec();
aoSpec.setFailOnError(true);
AssemblerResult ar = null;

ar = assemblerService.invoke(ddx, mapOfDocuments, aoSpec);
Document pdfRendered = ar.getDocuments().get("rendered.pdf");
javax.jcr.Node ocrFiles = (javax.jcr.Node) serviceResolver.getResource("/content/ocrfiles")
.adaptTo(javax.jcr.Node.class);
System.out.println("The jcrNode name is " + ocrFiles.getName());
UUID uuid = UUID.randomUUID();
String uuidString = uuid.toString();
javax.jcr.Node ocrFile = ocrFiles.addNode(uuidString + ".pdf", "nt:file");
System.out.println("The ocrFiles node was created");
javax.jcr.Node resNode = ocrFile.addNode("jcr:content", "nt:resource");
Session session = (Session) serviceResolver.adaptTo(Session.class);
ValueFactory valueFactory = session.getValueFactory();

Binary contentValue = valueFactory.createBinary(pdfRendered.getInputStream());

resNode.setProperty("jcr:data", contentValue);
serviceResolver.commit();
File f1 = new File("c:\\scrapp\\pdfRendered.pdf");
pdfRendered.copyToFile(f1);

System.out.println("Assembled fragments succesfully");

return ocrFile.getPath();

} catch (ValueFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (PathNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RepositoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

View solution in original post

4 Replies

Avatar

Level 4

Thanks Girish, appreciate your follow up. Awaiting code snippet. FYI, i have tried creating multiple ddx but it worked for either flat or if only 1 XDP/PDF source is XFA Interactive form and rest had to be flat.

 

Also FYI- we already have pre-rendered Interactive PDFs which can act as source also instead of XDP. Any example around that will be helpful.

 

 

Thanks

Manu

Avatar

Correct answer by
Employee Advisor

public String insertFragments(String masterTemplate, Map < Integer, Map > fragmentsToAssemble, Document ddx) {
System.out.println("The master template is " + masterTemplate);
ResourceResolver serviceResolver = getResolver.getFormsServiceResolver();
javax.jcr.Node masterXdp = getResolver.getFormsServiceResolver()
.getResource("/content/dam/wefinancedemo/templates/" + masterTemplate +
"/jcr:content/renditions/original/jcr:content")
.adaptTo(javax.jcr.Node.class);

Map < String, Object > mapOfDocuments = new HashMap < String, Object > ();
try {
InputStream masterXdpIS = masterXdp.getProperty("jcr:data").getBinary().getStream();
Document masterXdpTemplate = new Document(masterXdpIS);
mapOfDocuments.put(masterTemplate, masterXdpTemplate);
for (Map.Entry < Integer, Map > map: fragmentsToAssemble.entrySet()) {
String fragmentName = (String) map.getValue().get("source");
System.out.println("The fragment name is " + fragmentName);
String fragmentPath = "/content/dam/wefinancedemo/fragments/" + fragmentName +
"/jcr:content/renditions/original/jcr:content";
System.out.println(map.getKey() + " = " + map.getValue().get("source"));
javax.jcr.Node fragment = getResolver.getFormsServiceResolver().getResource(fragmentPath)
.adaptTo(javax.jcr.Node.class);
InputStream fragmentis = fragment.getProperty("jcr:data").getBinary().getStream();
Document fragmentToInclude = new Document(fragmentis);
System.out.println("$$$$ Added document " + fragmentName + " to the map");
mapOfDocuments.put(fragmentName, fragmentToInclude);
fragmentName = null;

}
for (Map.Entry < String, Object > map: mapOfDocuments.entrySet()) {
System.out.println("The keys in the custom map are " + map.getKey());

}
AssemblerOptionSpec aoSpec = new AssemblerOptionSpec();
aoSpec.setFailOnError(true);
AssemblerResult ar = null;

ar = assemblerService.invoke(ddx, mapOfDocuments, aoSpec);
Document pdfRendered = ar.getDocuments().get("rendered.pdf");
javax.jcr.Node ocrFiles = (javax.jcr.Node) serviceResolver.getResource("/content/ocrfiles")
.adaptTo(javax.jcr.Node.class);
System.out.println("The jcrNode name is " + ocrFiles.getName());
UUID uuid = UUID.randomUUID();
String uuidString = uuid.toString();
javax.jcr.Node ocrFile = ocrFiles.addNode(uuidString + ".pdf", "nt:file");
System.out.println("The ocrFiles node was created");
javax.jcr.Node resNode = ocrFile.addNode("jcr:content", "nt:resource");
Session session = (Session) serviceResolver.adaptTo(Session.class);
ValueFactory valueFactory = session.getValueFactory();

Binary contentValue = valueFactory.createBinary(pdfRendered.getInputStream());

resNode.setProperty("jcr:data", contentValue);
serviceResolver.commit();
File f1 = new File("c:\\scrapp\\pdfRendered.pdf");
pdfRendered.copyToFile(f1);

System.out.println("Assembled fragments succesfully");

return ocrFile.getPath();

} catch (ValueFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (PathNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RepositoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

Avatar

Level 9

Workflow user is my another account through which I have shared the sample Code.

you need xdp to insert another xdp