package org.apache.struts.config;

import java.io.Serializable;
import java.util.HashMap;
import org.apache.struts.action.DynaActionForm;

public class FormBeanConfig implements Serializable {
  protected boolean configured = false;
  
  protected HashMap formProperties = new HashMap();
  
  protected boolean dynamic = false;
  
  public boolean getDynamic() {
    return this.dynamic;
  }
  
  public void setDynamic(boolean dynamic) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
  }
  
  protected ModuleConfig moduleConfig = null;
  
  public ModuleConfig getModuleConfig() {
    return this.moduleConfig;
  }
  
  public void setModuleConfig(ModuleConfig moduleConfig) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
    this.moduleConfig = moduleConfig;
  }
  
  protected String name = null;
  
  public String getName() {
    return this.name;
  }
  
  public void setName(String name) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
    this.name = name;
  }
  
  protected String type = null;
  
  public String getType() {
    return this.type;
  }
  
  public void setType(String type) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
    this.type = type;
    Class dynaBeanClass = DynaActionForm.class;
    Class formBeanClass = formBeanClass();
    if (formBeanClass != null) {
      if (dynaBeanClass.isAssignableFrom(formBeanClass)) {
        this.dynamic = true;
      } else {
        this.dynamic = false;
      } 
    } else {
      this.dynamic = false;
    } 
  }
  
  public void addFormPropertyConfig(FormPropertyConfig config) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
    if (this.formProperties.containsKey(config.getName()))
      throw new IllegalArgumentException("Property " + config.getName() + " already defined"); 
    this.formProperties.put(config.getName(), config);
  }
  
  public FormPropertyConfig findFormPropertyConfig(String name) {
    return (FormPropertyConfig)this.formProperties.get(name);
  }
  
  public FormPropertyConfig[] findFormPropertyConfigs() {
    FormPropertyConfig[] results = new FormPropertyConfig[this.formProperties.size()];
    return (FormPropertyConfig[])this.formProperties.values().toArray((Object[])results);
  }
  
  public void freeze() {
    this.configured = true;
    FormPropertyConfig[] fpconfigs = findFormPropertyConfigs();
    for (int i = 0; i < fpconfigs.length; i++)
      fpconfigs[i].freeze(); 
  }
  
  public void removeFormPropertyConfig(FormPropertyConfig config) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
    this.formProperties.remove(config.getName());
  }
  
  public String toString() {
    StringBuffer sb = new StringBuffer("FormBeanConfig[");
    sb.append("name=");
    sb.append(this.name);
    sb.append(",type=");
    sb.append(this.type);
    sb.append("]");
    return sb.toString();
  }
  
  protected Class formBeanClass() {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader == null)
      classLoader = getClass().getClassLoader(); 
    try {
      return classLoader.loadClass(getType());
    } catch (Exception e) {
      return null;
    } 
  }
}
