Package de.odysseus.calyxo.forms.impl

Source Code of de.odysseus.calyxo.forms.impl.Input

/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed 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 de.odysseus.calyxo.forms.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.el.Expression;
import javax.servlet.jsp.el.VariableResolver;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import de.odysseus.calyxo.base.Message;
import de.odysseus.calyxo.forms.FormData;
import de.odysseus.calyxo.forms.FormInput;
import de.odysseus.calyxo.forms.FormInputResult;
import de.odysseus.calyxo.forms.FormInputValues;
import de.odysseus.calyxo.forms.conf.FieldConfig;
import de.odysseus.calyxo.forms.conf.InputConfig;

/**
* Instances of this class implement the
* {@link de.odysseus.calyxo.forms.FormInput} interface. An input
* contains several input fields, according to its input configuration
* element.
*
* @author Christoph Beck
*/
public class Input implements FormInput {
  private static Log log = LogFactory.getLog(InputFieldResult.class);
 
  private InputConfig inputConfig;
  private ArrayList fields;
  private Message message;

  /**
   * Constructor
   */
  public Input(Factory factory, InputConfig inputConfig, Locale locale) {
    super();

    this.inputConfig = inputConfig;
    fields = new ArrayList();
    Iterator fieldConfigs = inputConfig.getFieldConfigs();
    while (fieldConfigs.hasNext()) {
      FieldConfig fieldConfig = (FieldConfig)fieldConfigs.next();
      fields.add(new InputField(factory, fieldConfig, locale, inputConfig.isArray()));
    }
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.forms.FormInput#getName()
   */
  public String getName() {
    return inputConfig.getName();
  }

  /**
   * Get {@link InputField} objects
   */
  public Iterator getFields() {
    return fields.iterator();
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.forms.FormInput#format(javax.servlet.http.HttpServletRequest, de.odysseus.calyxo.forms.FormData)
   */
  public Object format(HttpServletRequest request, FormData bean) throws Exception {
    for (int i = 0; i < fields.size(); i++) {
      InputField field = (InputField)fields.get(i);
      String property = field.getFieldConfig().getProperty();
      Object value = bean._getProperty(property);
      if (isArray()) {
        return field.formatArray(request, value);
      } else if (value != null && !value.equals(field.getFieldConfig().getNull())) {
        return field.formatScalar(request, value);
      }
    }
    return ((InputField)fields.get(0)).format(request, bean);
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.forms.FormInput#format(javax.servlet.http.HttpServletRequest, de.odysseus.calyxo.forms.FormData, int)
   */
  public String format(HttpServletRequest request, FormData bean, int index, String notAvailableResult) throws Exception {
    for (int i = 0; i < fields.size(); i++) {
      InputField field = (InputField)fields.get(i);
      String property = field.getFieldConfig().getProperty();
      Object[] values = (Object[])bean._getProperty(property);
      if (values != null && index < values.length) {
        return field.formatScalar(request, values[index]);
      }
    }
    return notAvailableResult;
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.forms.FormInput#validate(javax.servlet.http.HttpServletRequest, de.odysseus.calyxo.forms.FormInputValues)
   */
  public FormInputResult validate(HttpServletRequest request, FormInputValues params, VariableResolver resolver) {
    InputField field = null;
    ValidatorSequenceValue value = null;
    Iterator fields = this.fields.iterator();
    while (fields.hasNext()) {
      field = (InputField)fields.next();
      value = field.execute(request, params);
      if (value.isValid()) {
        break;
      }
    }
    return new InputResult(this, new InputFieldResult(field, value), resolver);
  }

  /**
   * Get message.
   */
  public Message getMessage() {
    if (message == null && inputConfig.getMessageConfig() != null) {
      message = new MessageImpl(inputConfig.getMessageConfig());     
    }
    return message;
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.forms.FormInput#isArray()
   */
  public boolean isArray() {
    return inputConfig.isArray();
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.forms.FormInput#isRelaxed(javax.servlet.jsp.el.VariableResolver)
   */
  public boolean isRelaxed(VariableResolver resolver) {
    Expression expr = inputConfig.getParsedRelaxExpression();
    if (expr != null) {
      Object result = null;
      try {
        result = expr.evaluate(resolver);
      } catch (Exception e) {
        log.error("Evaluation error in '" + inputConfig.getRelax() + "'", e);
        return false;
      }
      if (result instanceof Boolean) {
        return ((Boolean)result).booleanValue();
      }
      log.error("Bad evaluation result in '" + inputConfig.getRelax() + "': " + result);     
    }
    return false;
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.forms.FormInput#isIgnored(javax.servlet.jsp.el.VariableResolver)
   */
  public boolean isIgnored(VariableResolver resolver) {
    Expression expr = inputConfig.getParsedIgnoreExpression();
    if (expr != null) {
      Object result = null;
      try {
        result = expr.evaluate(resolver);
      } catch (Exception e) {
        log.error("Evaluation error in '" + inputConfig.getIgnore() + "'", e);
        return false;
      }
      if (result instanceof Boolean) {
        return ((Boolean)result).booleanValue();
      }
      log.error("Bad evaluation result in '" + inputConfig.getIgnore() + "': " + result);     
    }
    return false;
  }
}
TOP

Related Classes of de.odysseus.calyxo.forms.impl.Input

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.