Next step is to create a servlet that will render the adaptive form with the saved data and its attachments.
The following servlet code is executed after the OTP code has been verified. The adaptive form data and its file attachments map associated with the unique application id are fetched from the database. The request object is populated with the saved adaptive form data and the file attachments map. The request is then forwarded to render the “storeafwithattachments” form pre-populated with the original data and its attachments.
package store.and.fetch.core.servlets;
import java.io.IOException;
import java.io.StringReader;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.json.JSONException;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.day.cq.wcm.api.WCMMode;
import store.and.fetch.core.*;
@Component(service = {
Servlet.class
}, property = {
"sling.servlet.methods=post",
"sling.servlet.paths=/bin/renderaf"
})
public class RenderForm extends SlingAllMethodsServlet {
/**
*
*/
private static final Logger log = LoggerFactory.getLogger(RenderForm.class);
private static final long serialVersionUID = 1 L;
@Reference
AemFormsAndDB aemFormsAndDB;
public org.w3c.dom.Document w3cDocumentFromStrng(String xmlString) {
try {
log.debug("Inside w3cDocumentFromString");
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlString));
return db.parse(is);
} catch (Exception e) {
log.debug(e.getMessage());
}
return null;
}
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) {
log.debug("*****In my Render Form Servlet*****");
String submittedData = request.getParameter("jcr:data");
String applicationNo = "/afData/afUnboundData/data/ApplicationNumber";
org.w3c.dom.Document submittedXml = w3cDocumentFromStrng(submittedData);
XPath xPath = javax.xml.xpath.XPathFactory.newInstance().newXPath();
try {
org.w3c.dom.Node applicationNode = (org.w3c.dom.Node) xPath.compile(applicationNo).evaluate(submittedXml, javax.xml.xpath.XPathConstants.NODE);
log.debug("The application number we got was " + applicationNode.getTextContent());
org.json.JSONObject afDataObject = aemFormsAndDB.getAFFormDataWithAttachments(applicationNode.getTextContent());
log.debug("The data I got was " + afDataObject.toString());
request.setAttribute("data", afDataObject.get("afData"));
org.json.JSONObject customMap = new org.json.JSONObject();
customMap.put("fileAttachmentMap", afDataObject.get("afAttachments"));
request.setAttribute("customContextProperty", customMap.toString());
ServletContext sc = getServletContext();
RequestDispatcher dispatcher = sc.getRequestDispatcher("/content/forms/af/storeafwithattachments.html");
WCMMode.DISABLED.toRequest(request);
dispatcher.forward(request, response);
} catch (JSONException | ServletException | IOException | XPathExpressionException e) {
log.debug("The error message is " + e.getMessage());
}
}
}