// Path: ui/src/content/jcr_root/libs/foundation/src/impl/src/main/java/com/day/cq/wcm/foundation/forms/impl/FormsUtil.java
// public class FormsUtil {
// 
//     private FormsUtil() {
//         // no instances
//     }
// 
//     /**
//      * Filter the element name for unallowed characters.
//      * @param nodeName The name of the element
//      * @return Filtered named
//      */
//     public static String filterElementName(String nodeName) {
//         final StringBuilder sb  = new StringBuilder();
//         char lastAdded = 0;
// 
//         for(int i=0; i < nodeName.length(); i++) {
//             final char c = nodeName.charAt(i);
//             char toAdd = c;
// 
//             if (FormsConstants.ALLOWED_NAME_CHARS.indexOf(c) < 0) {
//                 if (lastAdded == FormsConstants.REPLACEMENT_CHAR) {
//                     // do not add several _ in a row
//                     continue;
//                 }
//                 toAdd = FormsConstants.REPLACEMENT_CHAR;
// 
//             } else if(i == 0 && Character.isDigit(c)) {
//                 sb.append(FormsConstants.REPLACEMENT_CHAR);
//             }
// 
//             sb.append(toAdd);
//             lastAdded = toAdd;
//         }
// 
//         if (sb.length()==0) {
//             sb.append(FormsConstants.REPLACEMENT_CHAR);
//         }
// 
//         return sb.toString();
//     }
// }


import com.day.cq.wcm.foundation.forms.impl.FormsUtil;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
/*
 * Copyright 1997-2009 Day Management AG
 * Barfuesserplatz 6, 4001 Basel, Switzerland
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Day Management AG, ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Day.
 */
package com.day.cq.wcm.foundation.forms;




/**
 * A description of a form field as it is later used in the html
 * form.
 * A field component usually maps to a single field description,
 * however compound fields like an address field map to
 * several field descriptions.
 *
 * The field description is used mostly during validation as it
 * contains all necessary information.
 *
 * Field descriptions can be maintained from the {@link FieldHelper}.
 * @since 5.3
 */
public class FieldDescription {

    /** Field name. */
    private String name;

    /** Is this field required? */
    private boolean required = false;

    /** The message which is displayed if a required field is not set. */
    private String requiredMsg;

    /** Optional constraint type */
    private String constraintType;

    /** The message which is displayed if the constraint is not met. */
    private String constraintMsg;

    /** Is this a read only field? */
    private boolean readOnly = false;

    /** Is this a private field? */
    private boolean privateField = false;

    /** Is this a multi value field? */
    private boolean multiValue = false;

    /** The associated resource. */
    private final Resource fieldResource;

    /**
     * Construct a new field description for a field resource.
     * @param rsrc
     */
    public FieldDescription(final Resource rsrc) {
        this.fieldResource = rsrc;
    }

    /**
     * Construct a new field description for a field resource
     * and set the name property.
     * @param rsrc The resource
     * @param name The field name.
     */
    public FieldDescription(final Resource rsrc, final String name) {
        this.fieldResource = rsrc;
        this.name = name;
    }

    public void update(final Resource rsrc) {
        final ValueMap props = ResourceUtil.getValueMap(rsrc);
        this.required = props.get(FormsConstants.ELEMENT_PROPERTY_REQUIRED, Boolean.FALSE);
        this.readOnly = props.get(FormsConstants.ELEMENT_PROPERTY_READ_ONLY, Boolean.FALSE);
        String name = props.get(FormsConstants.ELEMENT_PROPERTY_NAME, "");
        if ( name.length() == 0 ) {
           name = ResourceUtil.getName(rsrc);
// Path: ui/src/content/jcr_root/libs/foundation/src/impl/src/main/java/com/day/cq/wcm/foundation/forms/impl/FormsUtil.java
// public class FormsUtil {
// 
//     private FormsUtil() {
//         // no instances
//     }
// 
//     /**
//      * Filter the element name for unallowed characters.
//      * @param nodeName The name of the element
//      * @return Filtered named
//      */
//     public static String filterElementName(String nodeName) {
//         final StringBuilder sb  = new StringBuilder();
//         char lastAdded = 0;
// 
//         for(int i=0; i < nodeName.length(); i++) {
//             final char c = nodeName.charAt(i);
//             char toAdd = c;
// 
//             if (FormsConstants.ALLOWED_NAME_CHARS.indexOf(c) < 0) {
//                 if (lastAdded == FormsConstants.REPLACEMENT_CHAR) {
//                     // do not add several _ in a row
//                     continue;
//                 }
//                 toAdd = FormsConstants.REPLACEMENT_CHAR;
// 
//             } else if(i == 0 && Character.isDigit(c)) {
//                 sb.append(FormsConstants.REPLACEMENT_CHAR);
//             }
// 
//             sb.append(toAdd);
//             lastAdded = toAdd;
//         }
// 
//         if (sb.length()==0) {
//             sb.append(FormsConstants.REPLACEMENT_CHAR);
//         }
// 
//         return sb.toString();
//     }
// }


// Path: ui/src/content/jcr_root/libs/foundation/src/impl/src/main/java/com/day/cq/wcm/foundation/forms/FieldDescription.java
import com.day.cq.wcm.foundation.forms.impl.FormsUtil;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;

/*
 * Copyright 1997-2009 Day Management AG
 * Barfuesserplatz 6, 4001 Basel, Switzerland
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Day Management AG, ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Day.
 */
package com.day.cq.wcm.foundation.forms;




/**
 * A description of a form field as it is later used in the html
 * form.
 * A field component usually maps to a single field description,
 * however compound fields like an address field map to
 * several field descriptions.
 *
 * The field description is used mostly during validation as it
 * contains all necessary information.
 *
 * Field descriptions can be maintained from the {@link FieldHelper}.
 * @since 5.3
 */
public class FieldDescription {

    /** Field name. */
    private String name;

    /** Is this field required? */
    private boolean required = false;

    /** The message which is displayed if a required field is not set. */
    private String requiredMsg;

    /** Optional constraint type */
    private String constraintType;

    /** The message which is displayed if the constraint is not met. */
    private String constraintMsg;

    /** Is this a read only field? */
    private boolean readOnly = false;

    /** Is this a private field? */
    private boolean privateField = false;

    /** Is this a multi value field? */
    private boolean multiValue = false;

    /** The associated resource. */
    private final Resource fieldResource;

    /**
     * Construct a new field description for a field resource.
     * @param rsrc
     */
    public FieldDescription(final Resource rsrc) {
        this.fieldResource = rsrc;
    }

    /**
     * Construct a new field description for a field resource
     * and set the name property.
     * @param rsrc The resource
     * @param name The field name.
     */
    public FieldDescription(final Resource rsrc, final String name) {
        this.fieldResource = rsrc;
        this.name = name;
    }

    public void update(final Resource rsrc) {
        final ValueMap props = ResourceUtil.getValueMap(rsrc);
        this.required = props.get(FormsConstants.ELEMENT_PROPERTY_REQUIRED, Boolean.FALSE);
        this.readOnly = props.get(FormsConstants.ELEMENT_PROPERTY_READ_ONLY, Boolean.FALSE);
        String name = props.get(FormsConstants.ELEMENT_PROPERTY_NAME, "");
        if ( name.length() == 0 ) {
           name = ResourceUtil.getName(rsrc);
           name = FormsUtil.filterElementName(name);