Author: bdelacretaz
Date: Tue Dec 11 02:16:06 2007
New Revision: 603193
URL: http://svn.apache.org/viewvc?rev=603193&view=rev
Log:
SLING-130, @ValueFrom, mapping from form field names to JCR properties in forms
Added:
incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/ValueFromTest.java
(with props)
Modified:
incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
Modified:
incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java?rev=603193&r1=603192&r2=603193&view=diff
==============================================================================
---
incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
(original)
+++
incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
Tue Dec 11 02:16:06 2007
@@ -47,7 +47,6 @@
private static final Logger log =
LoggerFactory.getLogger(MicrojaxPostServlet.class);
private final MicrojaxPropertyValueSetter propertyValueSetter = new
MicrojaxPropertyValueSetter();
- private int createNodeCounter;
private final NodeNameGenerator nodeNameGenerator = new
NodeNameGenerator();
/** Prefix for parameter names which control this POST
@@ -82,6 +81,9 @@
* (or supplied) redirect URL
*/
public static final String RP_DISPLAY_EXTENSION = RP_PREFIX +
"displayExtension";
+
+ /** SLING-130, suffix that maps form field names to different JCR property
names */
+ public static final String VALUE_FROM_SUFFIX = "@ValueFrom";
@Override
protected void doPost(SlingHttpServletRequest request,
SlingHttpServletResponse response)
@@ -263,14 +265,38 @@
throws RepositoryException {
for(Map.Entry<String, RequestParameter[]> e :
request.getRequestParameterMap().entrySet()) {
- String name = e.getKey();
+ final String paramName = e.getKey();
+ String propertyName = paramName;
if(savePrefix!=null) {
- if(!name.startsWith(savePrefix)) {
+ if(!paramName.startsWith(savePrefix)) {
+ continue;
+ }
+ propertyName = paramName.substring(savePrefix.length());
+ }
+
+ // SLING-130: VALUE_FROM_SUFFIX means take the value of this
+ // property from a different field
+ RequestParameter[] values = e.getValue();
+ final int vfIndex = propertyName.indexOf(VALUE_FROM_SUFFIX);
+ if(vfIndex >= 0) {
+ // @ValueFrom example:
+ // <input name="./[EMAIL PROTECTED]" type="hidden"
value="fulltext" />
+ // causes the JCR Text property to be set to the value of the
fulltext form field.
+ propertyName = propertyName.substring(0, vfIndex);
+ final RequestParameter[] rp =
request.getRequestParameterMap().get(paramName);
+ if(rp == null || rp.length > 1) {
+ // @ValueFrom params must have exactly one value, else
ignored
+ continue;
+ }
+ String mappedName = rp[0].getString();
+ values = request.getRequestParameterMap().get(mappedName);
+ if(values==null) {
+ // no value for "fulltext" in our example, ignore parameter
continue;
}
- name = name.substring(savePrefix.length());
}
- setProperty(n,request,name,e.getValue(),createdNodes);
+
+ setProperty(n,request,propertyName,values,createdNodes);
}
}
Added:
incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/ValueFromTest.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/ValueFromTest.java?rev=603193&view=auto
==============================================================================
---
incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/ValueFromTest.java
(added)
+++
incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/ValueFromTest.java
Tue Dec 11 02:16:06 2007
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.microsling.integration;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+/** Test the @ValueFrom field name suffix, SLING-130 */
+public class ValueFromTest extends MicroslingHttpTestBase {
+
+ private String postUrl;
+ private String testText;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ // set test values
+ testText = "This is a test " + System.currentTimeMillis();
+
+ // create the test node, under a path that's specific to this class to
allow collisions
+ postUrl = HTTP_BASE_URL + "/" + getClass().getSimpleName() + "/" +
System.currentTimeMillis() + "/*";
+ final Map<String,String> props = new HashMap<String,String>();
+ props.put("text", testText);
+ }
+
+ public void testWithoutValueFrom() throws IOException {
+ final Map<String,String> props = new HashMap<String,String>();
+ props.put("./text", testText);
+ final String jsonUrl = testClient.createNode(postUrl, props) + ".json";
+ final String json = getContent(jsonUrl, CONTENT_TYPE_JSON);
+ assertJavascript(testText, json, "out.println(data.text)");
+
+ }
+
+ public void testWithValueFrom() throws IOException {
+ final Map<String,String> props = new HashMap<String,String>();
+ props.put("./[EMAIL PROTECTED]", "fulltext");
+ props.put("fulltext", testText);
+ final String jsonUrl = testClient.createNode(postUrl, props) + ".json";
+ final String json = getContent(jsonUrl, CONTENT_TYPE_JSON);
+ assertJavascript("string", json, "out.println(typeof data.text)");
+ assertJavascript(testText, json, "out.println(data.text)");
+ assertJavascript("undefined", json, "out.println(typeof
data.fulltext)");
+ }
+
+ public void testWithValueFromAndMissingField() throws IOException {
+ final Map<String,String> props = new HashMap<String,String>();
+ props.put("./[EMAIL PROTECTED]", "fulltext");
+ // no fulltext field on purpose, field must be ignored
+
+ final String jsonUrl = testClient.createNode(postUrl, props) + ".json";
+ final String json = getContent(jsonUrl, CONTENT_TYPE_JSON);
+ assertJavascript("undefined", json, "out.println(typeof data.text)");
+ assertJavascript("undefined", json, "out.println(typeof data['[EMAIL
PROTECTED]'])");
+ }
+
+
+
+ }
\ No newline at end of file
Propchange:
incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/ValueFromTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/ValueFromTest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL