Il passaggio successivo consiste nel creare un servlet che esegua il rendering del modulo adattivo con i dati salvati e i relativi allegati.
Il seguente codice servlet viene eseguito dopo la verifica del codice OTP. I dati del modulo adattivo e la mappa dei relativi allegati associata all’ID applicazione univoco vengono recuperati dal database. L’oggetto della richiesta viene compilato con i dati del modulo adattivo salvati e la mappa degli allegati. La richiesta viene quindi inoltrata per eseguire il rendering del modulo "storeafwithattachments" precompilato con i dati originali e i relativi allegati.
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());
}
}
}
Crea libreria client per richiamare il servlet per archiviare i dati del modulo