com.day.cq.mcm.core.MCMFormsHelper Maven / Gradle / Ivy
/*
* ***********************************************************************
*
* ADOBE CONFIDENTIAL
* ___________________
*
* Copyright 2012 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
* ************************************************************************
*/
package com.day.cq.mcm.core;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import javax.jcr.*;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.scripting.SlingBindings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.granite.xss.XSSAPI;
public class MCMFormsHelper {
/** The logger. */
private static final Logger log = LoggerFactory.getLogger(MCMFormsHelper.class.getName());
private static final String FOUNDATION_FORM_START = "foundation/components/form/start";
private static final String FOUNDATION_FORM_END = "foundation/components/form/end";
private static final String CTA_EMAIL_ID = "mcm/components/cta-form/emailId";
private MCMFormsHelper(){
//no instances
}
/**
*
* @param formResource
* @return null if neither formEnd nor email id is found within form, else email id node if it exists in the form else formEnd
*/
private static Resource checkEmailExists(Resource formResource)
{
Resource resource = null;
boolean found = false;
try
{
Resource formParent = formResource.getParent();
Iterator iter = formParent.listChildren();
Boolean start = false;
while(iter.hasNext())
{
resource = iter.next();
if(start)
{
if(ResourceUtil.isA(resource, CTA_EMAIL_ID))
{
found = true;
break;//field already created in form
}
if(ResourceUtil.isA(resource, FOUNDATION_FORM_END))
{
found = true;
break;
}
}
if(!start && resource.getName().equals(formResource.getName()))
{
start = true;
}
}
}
catch (Exception e)
{
log.error(e.getMessage());
}
if(found)
return resource;
return null;
}
/**
*
* @param formResource
* @return null if NO new email id node is added else returns the newly added email id node to the form
*/
public static Node addEmail(Resource formResource)
{
try
{
if(!ResourceUtil.isA(formResource, FOUNDATION_FORM_START))
return null;
Resource resource = checkEmailExists(formResource);
if(resource == null)
{
log.error("Form End not found");
return null;
}
else
{
if(ResourceUtil.isA(resource, CTA_EMAIL_ID))
return null;
}
Node orderBefore = resource.adaptTo(Node.class);
Node formParent = orderBefore.getParent();
int suffix = 0;
while(formParent.hasNode("emailid_" + suffix))
suffix++;
String nodeName = formParent.hasNode("emailid") ? "emailid_" + suffix : "emailid";
Node emailId = formParent.addNode(nodeName,"nt:unstructured");
emailId.setProperty("name","email");
emailId.setProperty("sling:resourceType",CTA_EMAIL_ID);
emailId.setProperty("sling:resourceSuperType","foundation/components/form/defaults/field");
emailId.setProperty("required","true");
emailId.setProperty("constraintType","foundation/components/form/constraints/email");
formParent.orderBefore(emailId.getName(), orderBefore.getName());
formParent.getSession().save();
return emailId;
}
catch (Exception e)
{
log.error(e.getMessage());
}
return null;
}
/**
* Prints form title with style provided in "className"
*
* @param fieldId
* @param title
* @param required
* @param hideLabel
* @param className
* @param out
* @throws IOException
*/
public static void printTitle(String fieldId, String title, boolean required, boolean hideLabel, String className, SlingHttpServletRequest request, SlingHttpServletResponse response)
throws IOException {
PrintWriter out = response.getWriter();
out.write(" 0) {
title = xssAPI.encodeForHTML(title);
} else {
title = " ";
}
out.write(">");
String titileClassName = "form_leftcollabel" + (!StringUtils.isEmpty(className) ? " " + className : "");
out.write("");
if (fieldId != null) {
fieldId = xssAPI.encodeForHTMLAttr(fieldId);
out.write("");
} else {
out.write("" + title + "");
}
out.write("");
out.write("");
if (!hideLabel) {
if (required) {
out.write(" *");
} else {
out.write(" ");
}
}
out.write("");
out.write("\n");
}
}