/*
 * 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
 *
 * Initial developer(s): Nicolas Christin
 * Contributor(s): ______________________________________.
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */


package org.objectweb.jonas.newbean;


import org.apache.velocity.VelocityContext;


/**
 * This class defines the set of parameters used by the application.
 */
public class ParameterSet {

    private Parameter beanNameParameter = null;
    private Parameter beanFlavorParameter = null;
    private Parameter sessionTypeParameter = null;
    private Parameter persistenceManagerParameter = null;
    private Parameter isLocalParameter = null;
    private Parameter pkgNameParameter = null;
    private Parameter jarNameParameter = null;
    private Parameter pkClassParameter = null;


    /**
     * Creates a new set of parameters associated to the specified
     * Velocity context.
     */
    public ParameterSet(VelocityContext context) {

        // ----------------------------
        // beanNameParameter definition
        // ----------------------------

        beanNameParameter = new Parameter(context) {

                public String getPrompt() {
                    return "Bean Name";
                }

				public String getArgKeyword()
				{
					return "-beanName";
				}

                public boolean isValid() {
                    return (value.length() > 0);
                }
		
                public void export() {
                    vContext.put("beanName", value);
                }
		
                public Parameter getNextParameter() {
                    return beanFlavorParameter;
                }

            };


        // ------------------------------
        // beanFlavorParameter definition
        // ------------------------------

        beanFlavorParameter = new Parameter(context) {

                private final Integer SESSION_BEAN        = new Integer(0);
                private final Integer ENTITY_BEAN         = new Integer(1);
                private final Integer MESSAGE_DRIVEN_BEAN = new Integer(2);
		
                public String getPrompt() {
                    return "Bean type\n  S   Session bean\n  E   Entity bean\n  MD  Message-Driven bean";
                }

				public String getArgKeyword()
				{
					return "-beanType";
				}

                public void setValue(String input) {
                    value = input.toUpperCase();
                }
    
                public boolean isValid() {
                    return (value.equals("S") 
                            || value.equals("E")
                            || value.equals("MD"));
                }

                public void export() {
                    vContext.put("SESSION_BEAN", SESSION_BEAN);
                    vContext.put("ENTITY_BEAN", ENTITY_BEAN);
                    vContext.put("MESSAGE_DRIVEN_BEAN", MESSAGE_DRIVEN_BEAN);
                    Integer beanFlavor = null;
                    String beanType = null;
                    if (value.equals("S")) {
                        beanFlavor = SESSION_BEAN;
                        beanType = "S";
                    }
                    else if (value.equals("E")) {
                        beanFlavor = ENTITY_BEAN;
                        beanType = "E";
                    }
                    else if (value.equals("MD")) {
                        beanFlavor = MESSAGE_DRIVEN_BEAN;
                        beanType = "MD";
                    }
                    vContext.put("beanFlavor", beanFlavor);
                    vContext.put("beanType", beanType);
                }

                public Parameter getNextParameter() {
                    Integer beanFlavor = (Integer)vContext.get("beanFlavor");
                    if (beanFlavor == SESSION_BEAN) {
                        return sessionTypeParameter;
                    }
                    else if (beanFlavor == ENTITY_BEAN) {
                        return persistenceManagerParameter;
                    }
                    return pkgNameParameter;
                }

            };
	

        // -------------------------------
        // sessionTypeParameter definition
        // -------------------------------

        sessionTypeParameter = new Parameter(context) {

                private final Integer STATELESS = new Integer(0);
                private final Integer STATEFUL  = new Integer(1);

                public String getPrompt() {
                    return "Session type\n  L  Staless\n  F  Stateful";
                }

				public String getArgKeyword()
				{
					return "-sessionType";
				}

                public void setValue(String input) {
                    value = input.toUpperCase();
                }
    
                public boolean isValid() {
                    return (value.equals("L")
                            || value.equals("F"));
                }
		
                public void export() {
                    vContext.put("STATELESS", STATELESS);
                    vContext.put("STATEFUL", STATEFUL);
                    Integer sessionType = null;
                    String beanType = (String)vContext.get("beanType");
                    if (value.equals("L")) {
                        sessionType = STATELESS;
                        beanType += "L";
                    }
                    else if (value.equals("F")) {
                        sessionType = STATEFUL;
                        beanType += "F";
                    }
                    vContext.put("sessionType", sessionType);
                    vContext.put("beanType", beanType);
                }
		
                public Parameter getNextParameter() {
                    return isLocalParameter;
                }

            };


        // --------------------------------------
        // persistenceManagerParameter definition
        // --------------------------------------

        persistenceManagerParameter = new Parameter(context) {

                private final Integer BMP      = new Integer(0);
                private final Integer CMP1     = new Integer(1);
                private final Integer CMP2     = new Integer(2);

                public String getPrompt() {
                    return "Persistance manager\n    B  BMP\n    C  CMP 1.x\n    C2 CMP 2.x";
                }

				public String getArgKeyword()
				{
					return "-persistence";
				}

                public void setValue(String input) {
                    value = input.toUpperCase();
                }
    
                public boolean isValid() {
                    return (value.equals("C")
                            || value.equals("B")
                            || value.equals("C2"));
                }
		
                public void export() {
                    vContext.put("CMP1", CMP1);
                    vContext.put("CMP2", CMP2);
                    vContext.put("BMP", BMP);
                    Integer persistenceManager = null;
		    
                    String beanType = (String)vContext.get("beanType");
                    if (value.equals("C")) {
                        persistenceManager = CMP1;
                        beanType += "C";
                    }
                    else if (value.equals("B")) {
                        persistenceManager = BMP;
                        beanType += "B";
                    }
                    else if (value.equals("C2")) {
                        persistenceManager = CMP2;
                        beanType += "C2";
                    }
                    vContext.put("persistenceManager", persistenceManager);
                    vContext.put("beanType", beanType);
                }
		
                public Parameter getNextParameter() {
                    return isLocalParameter;
                }

            };


        // ---------------------------
        // isLocalParameter definition
        // ---------------------------

        isLocalParameter = new Parameter(context) {

                private final Boolean FALSE  = Boolean.FALSE;
                private final Boolean TRUE   = Boolean.TRUE;

                public String getPrompt() {
                    return "Bean location\n  R  Remote\n  L  Local";
                }

				public String getArgKeyword()
				{
					return "-location";
				}

                public void setValue(String input) {
                    value = input.toUpperCase();
                }
    
                public boolean isValid() {
                    return (value.equals("R")
                            || value.equals("L"));
                }
		
                public void export() {
                    Boolean isLocal = null;
                    String beanType = (String)vContext.get("beanType");
                    if (value.equals("L")) {
                        isLocal = TRUE;
                        beanType += "L";
                    }
                    else if (value.equals("R")) {
                        isLocal = FALSE;
                        beanType += "R";
                    }
                    vContext.put("isLocal", isLocal);
                    vContext.put("beanType", beanType);
                }
		
                public Parameter getNextParameter() {
                    return pkgNameParameter;
                }

            };


        // ---------------------------
        // pkgNameParameter definition
        // ---------------------------

        pkgNameParameter = new Parameter(context) {

                public String getPrompt() {
                    return "Package name";
                }

				public String getArgKeyword()
				{
					return "-pkgName";
				}

                public boolean isValid() {
                    return (value.length() > 0);
                }
		
                public void export() {
                    vContext.put("pkgName", value);
                }
		
                public Parameter getNextParameter() {
                    return jarNameParameter;
                }

            };


        // ---------------------------
        // jarNameParameter definition
        // ---------------------------

        jarNameParameter = new Parameter(context) {

                public String getPrompt() {
                    return "Jar name";
                }

				public String getArgKeyword()
				{
					return "-jarName";
				}

                public void setValue(String input) {
                    if (input.endsWith(".jar")) {
                        value = input.substring(0, input.length() - 4);
                    }
                    else {
                        value = input;
                    }
                }
		
                public boolean isValid() {
                    return (value.length() > 0);
                }
		
                public void export() {
                    vContext.put("jarName", value);
                }
		
                public Parameter getNextParameter() {
                    final Integer ENTITY_BEAN =
                        (Integer)vContext.get("ENTITY_BEAN");
                    Integer beanFlavor = 
                        (Integer)vContext.get("beanFlavor");
                    if (beanFlavor == ENTITY_BEAN)
                        return pkClassParameter;
                    else
                        return null;
                }

            };


        // ---------------------------
        // pkClassParameter definition
        // ---------------------------

        pkClassParameter = new Parameter(context) {

                public String getPrompt() {
                    return "Primary Key class\n  S  String\n  I  Integer\n  O  Object";
                }

				public String getArgKeyword()
				{
					return "-primaryKey";
				}

                public void setValue(String input) {
                    value = input.toUpperCase();
                }
    
                public boolean isValid() {
                    return (value.equals("S")
                            || value.equals("I")
                            || value.equals("O"));
                }
		
                public void export() {
                    String pkClass = null;
                    if (value.equals("S"))
                        pkClass = "java.lang.String";
                    else if (value.equals("I"))
                        pkClass = "java.lang.Integer";
                    else if (value.equals("O"))
                        pkClass = "java.lang.Object";
                    vContext.put("pkClass", pkClass);
                }
		
                public Parameter getNextParameter() {
                    return null;
                }

            };

    }


    /**
     * Walk through ({@link Parameter#walkThrough()}) the whole
     * parameters set.
     */
    public void walkThrough() {
        beanNameParameter.walkThrough();
    }

}
