1
22
23 package com.liferay.portlet.webform.util;
24
25 import com.liferay.counter.service.CounterLocalServiceUtil;
26 import com.liferay.mozilla.javascript.Context;
27 import com.liferay.mozilla.javascript.Scriptable;
28 import com.liferay.mozilla.javascript.ScriptableObject;
29 import com.liferay.portal.PortalException;
30 import com.liferay.portal.SystemException;
31 import com.liferay.portal.kernel.log.Log;
32 import com.liferay.portal.kernel.log.LogFactoryUtil;
33 import com.liferay.portal.kernel.util.StringPool;
34 import com.liferay.portal.kernel.util.Validator;
35 import com.liferay.portlet.expando.NoSuchTableException;
36 import com.liferay.portlet.expando.model.ExpandoColumnConstants;
37 import com.liferay.portlet.expando.model.ExpandoTable;
38 import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
39 import com.liferay.portlet.expando.service.ExpandoRowLocalServiceUtil;
40 import com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil;
41
42 import java.io.BufferedReader;
43 import java.io.IOException;
44 import java.io.StringReader;
45
46 import java.util.ArrayList;
47 import java.util.List;
48 import java.util.Map;
49
50 import javax.portlet.PortletPreferences;
51
52
61 public class WebFormUtil {
62
63 public static ExpandoTable addTable(String tableName)
64 throws PortalException, SystemException {
65
66 try {
67 ExpandoTableLocalServiceUtil.deleteTable(
68 WebFormUtil.class.getName(), tableName);
69 }
70 catch (NoSuchTableException nste) {
71 }
72
73 return ExpandoTableLocalServiceUtil.addTable(
74 WebFormUtil.class.getName(), tableName);
75 }
76
77 public static ExpandoTable checkTable(
78 String tableName, PortletPreferences preferences)
79 throws Exception {
80
81 ExpandoTable expandoTable = null;
82
83 try {
84 expandoTable = ExpandoTableLocalServiceUtil.getTable(
85 WebFormUtil.class.getName(), tableName);
86 }
87 catch (NoSuchTableException nste) {
88 expandoTable = addTable(tableName);
89
90 int i = 1;
91
92 String fieldLabel = preferences.getValue(
93 "fieldLabel" + i, StringPool.BLANK);
94
95 while ((i == 1) || (Validator.isNotNull(fieldLabel))) {
96 ExpandoColumnLocalServiceUtil.addColumn(
97 expandoTable.getTableId(), fieldLabel,
98 ExpandoColumnConstants.STRING);
99
100 i++;
101
102 fieldLabel = preferences.getValue(
103 "fieldLabel" + i, StringPool.BLANK);
104 }
105 }
106
107 return expandoTable;
108 }
109
110 public static String getNewDatabaseTableName(String portletId)
111 throws SystemException {
112
113 long formId = CounterLocalServiceUtil.increment(
114 WebFormUtil.class.getName());
115
116 return portletId + StringPool.UNDERLINE + formId;
117 }
118
119 public static int getTableRowsCount(String tableName)
120 throws SystemException {
121
122 return ExpandoRowLocalServiceUtil.getRowsCount(
123 WebFormUtil.class.getName(), tableName);
124 }
125
126 public static String[] split(String s) {
127 return split(s, StringPool.COMMA);
128 }
129
130 public static String[] split(String s, String delimiter) {
131 if (s == null || delimiter == null) {
132 return new String[0];
133 }
134
135 s = s.trim();
136
137 if (!s.endsWith(delimiter)) {
138 StringBuilder sb = new StringBuilder();
139
140 sb.append(s);
141 sb.append(delimiter);
142
143 s = sb.toString();
144 }
145
146 if (s.equals(delimiter)) {
147 return new String[0];
148 }
149
150 List<String> nodeValues = new ArrayList<String>();
151
152 if (delimiter.equals("\n") || delimiter.equals("\r")) {
153 try {
154 BufferedReader br = new BufferedReader(new StringReader(s));
155
156 String line = null;
157
158 while ((line = br.readLine()) != null) {
159 nodeValues.add(line);
160 }
161
162 br.close();
163 }
164 catch (IOException ioe) {
165 ioe.printStackTrace();
166 }
167 }
168 else {
169 int offset = 0;
170 int pos = s.indexOf(delimiter, offset);
171
172 while (pos != -1) {
173 nodeValues.add(new String(s.substring(offset, pos)));
174
175 offset = pos + delimiter.length();
176 pos = s.indexOf(delimiter, offset);
177 }
178 }
179
180 return nodeValues.toArray(new String[nodeValues.size()]);
181 }
182
183 public static boolean validate(
184 String currentFieldValue, Map<String,String> fieldsMap,
185 String validationScript)
186 throws Exception {
187
188 boolean validationResult = false;
189
190 Context context = Context.enter();
191
192 StringBuilder sb = new StringBuilder();
193
194 sb.append("currentFieldValue = String('" + currentFieldValue + "');\n");
195
196 sb.append("var fieldsMap = {};\n");
197
198 for (String key : fieldsMap.keySet()) {
199 sb.append("fieldsMap['");
200 sb.append(key);
201 sb.append("'] = '");
202 sb.append(fieldsMap.get(key));
203 sb.append("';\n");
204 }
205
206 sb.append("function validation(currentFieldValue, fieldsMap) {\n");
207 sb.append(validationScript);
208 sb.append("};\n");
209 sb.append("internalValidationResult = ");
210 sb.append("validation(currentFieldValue, fieldsMap);");
211
212 String script = sb.toString();
213
214 try {
215 Scriptable scope = context.initStandardObjects();
216
217 Object jsFieldsMap = Context.javaToJS(fieldsMap, scope);
218
219 ScriptableObject.putProperty(scope, "jsFieldsMap", jsFieldsMap);
220
221 context.evaluateString(scope, script, "Validation Script", 1, null);
222
223 Object obj = ScriptableObject.getProperty(
224 scope, "internalValidationResult");
225
226 if (obj instanceof Boolean) {
227 validationResult = ((Boolean)obj).booleanValue();
228 }
229 else {
230 throw new Exception("The script must return a boolean value");
231 }
232 }
233 catch (Exception e) {
234 String msg =
235 "The following script has execution errors:\n" +
236 validationScript + "\n" + e.getMessage();
237
238 _log.error(msg);
239
240 throw new Exception(msg, e);
241 }
242 finally {
243 Context.exit();
244 }
245
246 return validationResult;
247 }
248
249 private static Log _log = LogFactoryUtil.getLog(WebFormUtil.class);
250
251 }