/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.webapp.jonasadmin.service.ejb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 * @author Michel-Ange ANTON
 */
public class EjbForm extends ActionForm {

// --------------------------------------------------------- Properties variables

    /**
     * Parameters properties
     */
    private String type = null;
    private String fullType = null;
    private String file = null;
    private String name = null;
    private String objectName = null;
    private String action = null;

    /**
     * Global MBean Ejb properties
     */
    private int cacheSize = 0;
    private int maxCacheSize = 0;
    private int minPoolSize = 0;
    private int poolSize = 0;
    private String displayName = null;
    private String ejbClass = null;
    private String ejbFileName = null;
    private String ejbName = null;
    private String homeClass = null;
    private String jndiName = null;
    private String localClass = null;
    private String localHomeClass = null;
    private String remoteClass = null;
    private boolean dependency = false;
    private boolean databaseServiceActivated = false;
    private boolean resourceServiceActivated = false;
    private boolean dataSource = false;
    private HashMap dataSources = new HashMap();
    private HashMap jdbcRas = new HashMap();
    private boolean jmsServiceActivated = false;
    private boolean joramResourceLoaded = false;
    private boolean jmsConnection = false;
    private ArrayList jmsConnections = new ArrayList();
    private boolean jmsDestination = false;
    private ArrayList jmsDestinations = new ArrayList();
    private Hashtable jmsDestinationsTable = new Hashtable();
    private boolean mailServiceActivated = false;
    private boolean mailSession = false;
    private HashMap mailSessions = new HashMap();
    private boolean mailMime = false;
    private HashMap mailMimes = new HashMap();

// --------------------------------------------------------- Public Methods

    /**
     * Reset all properties to their default values.
     *
     * @param mapping The mapping used to select this instance
     * @param request The servlet request we are processing
     */

    public void reset(ActionMapping mapping, HttpServletRequest request) {
        // Parameters properties
        type = null;
        file = null;
        name = null;
        objectName = null;
        fullType = null;
        action = null;

        // Global MBean Ejb properties
        cacheSize = 0;
        maxCacheSize = 0;
        minPoolSize = 0;
        poolSize = 0;
        displayName = null;
        ejbClass = null;
        ejbFileName = null;
        ejbName = null;
        homeClass = null;
        jndiName = null;
        localClass = null;
        localHomeClass = null;
        remoteClass = null;

        dependency = false;
        databaseServiceActivated = false;
        resourceServiceActivated = false;
        dataSources = new HashMap();
        dataSource = false;
        jdbcRas = new HashMap();
        jmsServiceActivated = false;
        joramResourceLoaded = false;
        jmsConnection = false;
        jmsConnections = new ArrayList();
        jmsDestination = false;
        jmsDestinations = new ArrayList();
        jmsDestinationsTable = new Hashtable();
        mailServiceActivated = false;
        mailSession = false;
        mailSessions = new HashMap();
        mailMime = false;
        mailMimes = new HashMap();
    }

    /**
     * Validate the properties that have been set from this HTTP request,
     * and return an <code>ActionErrors</code> object that encapsulates any
     * validation errors that have been found.  If no errors are found, return
     * <code>null</code> or an <code>ActionErrors</code> object with no
     * recorded error messages.
     *
     * @param mapping The mapping used to select this instance
     * @param request The servlet request we are processing
     * @return List of errors or null
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
      ActionErrors oErrors = new ActionErrors();
        return oErrors;
    }

// --------------------------------------------------------- Properties Methods

    // Parameters properties

    public String getFile() {
        return file;
    }

    public String getFilename() {
        String sFilename = null;
        if (file != null) {
            sFilename = file.replace('\\', '/');
            int iPos = sFilename.lastIndexOf('/');
            if (iPos > -1) {
                sFilename = sFilename.substring(iPos + 1);
            }
        }
        return sFilename;
    }

    public String getName() {
        return name;
    }

    public String getObjectName() {
        return objectName;
    }

    public String getType() {
        return type;
    }

    public void setFile(String file) {
        this.file = file;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setObjectName(String objectName) {
        this.objectName = objectName;
    }

    public void setType(String type) {
        this.type = type;
    }

    // Global MBean Ejb properties
    public String getDisplayName() {
        return displayName;
    }

    public String getEjbClass() {
        return ejbClass;
    }

    public String getEjbName() {
        return ejbName;
    }

    public String getHomeClass() {
        return homeClass;
    }

    public String getJndiName() {
        return jndiName;
    }

    public String getLocalClass() {
        return localClass;
    }

    public String getLocalHomeClass() {
        return localHomeClass;
    }

    public String getRemoteClass() {
        return remoteClass;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public void setEjbClass(String ejbClass) {
        this.ejbClass = ejbClass;
    }

    public void setEjbName(String ejbName) {
        this.ejbName = ejbName;
    }

    public void setHomeClass(String homeClass) {
        this.homeClass = homeClass;
    }

    public void setJndiName(String jndiName) {
        this.jndiName = jndiName;
    }

    public void setLocalClass(String localClass) {
        this.localClass = localClass;
    }

    public void setLocalHomeClass(String localHomeClass) {
        this.localHomeClass = localHomeClass;
    }

    public void setRemoteClass(String remoteClass) {
        this.remoteClass = remoteClass;
    }

    public String getEjbFileName() {
        return ejbFileName;
    }

    public void setEjbFileName(String ejbFileName) {
        this.ejbFileName = ejbFileName;
    }

    public String getFullType() {
        return fullType;
    }

    public void setFullType(String fullType) {
        this.fullType = fullType;
    }

    public HashMap getDataSources() {
        return dataSources;
    }

    public void setDataSources(HashMap dataSources) {
        this.dataSources = dataSources;
    }

    public boolean isDataSource() {
        return dataSource;
    }

    public void setDataSource(boolean dataSource) {
        this.dataSource = dataSource;
    }

    public boolean isDependency() {
        return dependency;
    }

    public void setDependency(boolean dependency) {
        this.dependency = dependency;
    }

    public boolean isJmsConnection() {
        return jmsConnection;
    }

    public void setJmsConnection(boolean jmsConnection) {
        this.jmsConnection = jmsConnection;
    }

    public ArrayList getJmsConnections() {
        return jmsConnections;
    }

    public void setJmsConnections(ArrayList jmsConnections) {
        this.jmsConnections = jmsConnections;
    }

    public boolean isJmsDestination() {
        return jmsDestination;
    }

    public void setJmsDestination(boolean jmsDestination) {
        this.jmsDestination = jmsDestination;
    }

    public ArrayList getJmsDestinations() {
        return jmsDestinations;
    }

    public void setJmsDestinations(ArrayList jmsDestinations) {
        this.jmsDestinations = jmsDestinations;
    }

    public HashMap getMailSessions() {
        return mailSessions;
    }

    public void setMailSessions(HashMap mailSessions) {
        this.mailSessions = mailSessions;
    }

    public HashMap getMailMimes() {
        return mailMimes;
    }

    public void setMailMimes(HashMap mailMimes) {
        this.mailMimes = mailMimes;
    }

    public boolean isMailSession() {
        return mailSession;
    }

    public void setMailSession(boolean mailSession) {
        this.mailSession = mailSession;
    }

    public boolean isMailMime() {
        return mailMime;
    }

    public void setMailMime(boolean mailMime) {
        this.mailMime = mailMime;
    }

    public boolean isDatabaseServiceActivated() {
        return databaseServiceActivated;
    }

    public void setDatabaseServiceActivated(boolean databaseServiceActivated) {
        this.databaseServiceActivated = databaseServiceActivated;
    }

    public boolean isJmsServiceActivated() {
        return jmsServiceActivated;
    }

    public void setJmsServiceActivated(boolean jmsServiceActivated) {
        this.jmsServiceActivated = jmsServiceActivated;
    }

    public boolean isMailServiceActivated() {
        return mailServiceActivated;
    }

    public void setMailServiceActivated(boolean mailServiceActivated) {
        this.mailServiceActivated = mailServiceActivated;
    }

    /**
     * @return Returns the joramResourceLoaded.
     */
    public boolean isJoramResourceLoaded() {
        return joramResourceLoaded;
    }
    /**
     * @param joramResourceLoaded The joramResourceLoaded to set.
     */
    public void setJoramResourceLoaded(boolean joramResourceLoaded) {
        this.joramResourceLoaded = joramResourceLoaded;
    }

    /**
     * @return Returns the action.
     */
    public String getAction() {
        return action;
    }
    /**
     * @param action The action to set.
     */
    public void setAction(String action) {
        this.action = action;
    }



    /**
     * Helper method to check that it is a required number and
     * is a valid integer within the given range. (min, max).
     *
     * @param  field  The field name in the form for which this error occured.
     * @param  numText  The string representation of the number.
     * @param rangeCheck  Boolean value set to true of reange check should be performed.
     *
     * @param  min  The lower limit of the range
     * @param  max  The upper limit of the range
     * error.webapp.setting.sessionTimeout.required
     */
    protected void numberCheck(ActionErrors p_Errors, String field, String numText, boolean rangeCheck
        , int min, int max) {
        // Check for 'is required'
        if ((numText == null) || (numText.length() < 1)) {
            p_Errors.add(field, new ActionMessage("error.ejb.type.session." + field + ".required"));
        } else {

            // check for 'must be a number' in the 'valid range'
            try {
                int num = Integer.parseInt(numText);
                // perform range check only if required
                if (rangeCheck) {
                    if ((num < min) || (num > max)) {
                        p_Errors.add(field
                            , new ActionMessage("error.ejb.type.session." + field + ".range"));
                    }
                }
            } catch (NumberFormatException e) {
                p_Errors.add(field, new ActionMessage("error.ejb.type.session." + field + ".format"));
            }
        }
    }

    /**
     * @return Returns the jmsDestinationsTable.
     */
    public Hashtable getJmsDestinationsTable() {
        return jmsDestinationsTable;
    }

    /**
     * @param jmsDestinationsTable The jmsDestinationsTable to set.
     */
    public void setJmsDestinationsTable(Hashtable jmsDestinationsTable) {
        this.jmsDestinationsTable = jmsDestinationsTable;
    }

    /**
     * @return Returns the cacheSize.
     */
    public int getCacheSize() {
        return cacheSize;
    }

    /**
     * @param cacheSize The cacheSize to set.
     */
    public void setCacheSize(int cacheSize) {
        this.cacheSize = cacheSize;
    }

    /**
     * @return Returns the maxCacheSize.
     */
    public int getMaxCacheSize() {
        return maxCacheSize;
    }

    /**
     * @param maxCacheSize The maxCacheSize to set.
     */
    public void setMaxCacheSize(int maxCacheSize) {
        this.maxCacheSize = maxCacheSize;
    }

    /**
     * @return Returns the minPoolSize.
     */
    public int getMinPoolSize() {
        return minPoolSize;
    }

    /**
     * @param minPoolSize The minPoolSize to set.
     */
    public void setMinPoolSize(int minPoolSize) {
        this.minPoolSize = minPoolSize;
    }

    /**
     * @return Returns the poolSize.
     */
    public int getPoolSize() {
        return poolSize;
    }

    /**
     * @param poolSize The poolSize to set.
     */
    public void setPoolSize(int poolSize) {
        this.poolSize = poolSize;
    }

	public boolean isResourceServiceActivated() {
		return resourceServiceActivated;
	}

	public void setResourceServiceActivated(boolean resourceServiceActivated) {
		this.resourceServiceActivated = resourceServiceActivated;
	}

	public HashMap getJdbcRas() {
		return jdbcRas;
	}

	public void setJdbcRas(HashMap jdbcRas) {
		this.jdbcRas = jdbcRas;
	}

}
