MCMFormsHelper.java
5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.xss.XSSAPI
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.Session
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.scripting.SlingBindings
* org.apache.sling.api.scripting.SlingScriptHelper
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.mcm.core;
import com.adobe.granite.xss.XSSAPI;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.Session;
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.apache.sling.api.scripting.SlingScriptHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MCMFormsHelper {
private static final Logger log = LoggerFactory.getLogger((String)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() {
}
private static Resource checkEmailExists(Resource formResource) {
Resource resource;
boolean found;
resource = null;
found = false;
try {
Resource formParent = formResource.getParent();
Iterator iter = formParent.listChildren();
Boolean start = false;
while (iter.hasNext()) {
resource = (Resource)iter.next();
if (start.booleanValue()) {
if (ResourceUtil.isA((Resource)resource, (String)"mcm/components/cta-form/emailId")) {
found = true;
break;
}
if (ResourceUtil.isA((Resource)resource, (String)"foundation/components/form/end")) {
found = true;
break;
}
}
if (start.booleanValue() || !resource.getName().equals(formResource.getName())) continue;
start = true;
}
}
catch (Exception e) {
log.error(e.getMessage());
}
if (found) {
return resource;
}
return null;
}
public static Node addEmail(Resource formResource) {
try {
if (!ResourceUtil.isA((Resource)formResource, (String)"foundation/components/form/start")) {
return null;
}
Resource resource = MCMFormsHelper.checkEmailExists(formResource);
if (resource == null) {
log.error("Form End not found");
return null;
}
if (ResourceUtil.isA((Resource)resource, (String)"mcm/components/cta-form/emailId")) {
return null;
}
Node orderBefore = (Node)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", "mcm/components/cta-form/emailId");
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;
}
}
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("<div class=\"form_leftcol\"");
if (hideLabel) {
out.write(" style=\"display: none;\"");
}
SlingBindings bindings = (SlingBindings)request.getAttribute(SlingBindings.class.getName());
XSSAPI xssAPI = ((XSSAPI)bindings.getSling().getService(XSSAPI.class)).getRequestSpecificAPI(request);
title = title != null && title.length() > 0 ? xssAPI.encodeForHTML(title) : " ";
out.write(">");
String titileClassName = "form_leftcollabel" + (!StringUtils.isEmpty((String)className) ? new StringBuilder().append(" ").append(className).toString() : "");
out.write("<div class=\"" + titileClassName + "\">");
if (fieldId != null) {
fieldId = xssAPI.encodeForHTMLAttr(fieldId);
out.write("<label for=\"" + fieldId + "\">" + title + "</label>");
} else {
out.write("<span>" + title + "</span>");
}
out.write("</div>");
out.write("<div class=\"form_leftcolmark\">");
if (!hideLabel) {
if (required) {
out.write(" *");
} else {
out.write(" ");
}
}
out.write("</div>");
out.write("</div>\n");
}
}