Prefill Service in Adaptive Forms | AEM Community Blog Seeding

Avatar

Avatar

kautuk_sahni

Community Manager

Total Posts

6.0K

Likes

1.1K

Correct Reply

1.1K

Avatar

kautuk_sahni

Community Manager

Total Posts

6.0K

Likes

1.1K

Correct Reply

1.1K
kautuk_sahni
Community Manager

22-03-2021

BlogImage.jpg

Prefill Service in Adaptive Forms by AEM Blog | Lhotse Technologies

Abstract

In this blog, I want to share how we can use the Prefill Service feature in adaptive forms.

What is the use of Prefill Service?
It is used to prepopulate the form with given values.

How to create Prefill Service?
For creating prefill service you need to create an osgi component. For example:-
In my form I have two text boxes.

So to prefill these values while Preview, I am making the osgi component. My class name is MyPrefillAdaptiveForm.class



import com.adobe.forms.common.service.*;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

@Component
public class MyPrefillAdaptiveForm implements DataProvider {
private Logger logger = LoggerFactory.getLogger(MyPrefillAdaptiveForm.class);
public String getServiceName() {
return “My Prefill Service Name”;
}
public String getServiceDescription() {
return “My Prefill Service”;
}
public PrefillData getPrefillData(final DataOptions dataOptions) throws FormsException {
return new PrefillData() {
public InputStream getInputStream() {
return getData(dataOptions);
}
public ContentType getContentType() {
return ContentType.XML;
}
};
}
private InputStream getData(DataOptions dataOptions) throws FormsException {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement(“data”);
doc.appendChild(rootElement);
Element name = doc.createElement(“name”);
name.setTextContent(“This is Name Field”);
Element title = doc.createElement(“title”);
title.setTextContent(“This is Title Field”);
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final DOMSource source = new DOMSource(doc);
final StreamResult outputTarget = new StreamResult(outputStream);
TransformerFactory.newInstance().newTransformer().transform(source, outputTarget);
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
return inputStream;
} catch (Exception e) {
logger.error(“Error while creating prefill data”, e);
throw new FormsException(e);
}
}
}

Read Full Blog

Prefill Service in Adaptive Forms

Q&A

Please use this thread to ask the related questions.

AEM AEMEBlogSeeding Experience Manager