/*
 * The contents of this file are subject to the terms 
 * of the Common Development and Distribution License 
 * (the License).  You may not use this file except in
 * compliance with the License.
 * 
 * You can obtain a copy of the license at 
 * https://glassfish.dev.java.net/public/CDDLv1.0.html or
 * glassfish/bootstrap/legal/CDDLv1.0.txt.
 * See the License for the specific language governing 
 * permissions and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL 
 * Header Notice in each file and include the License file 
 * at glassfish/bootstrap/legal/CDDLv1.0.txt.  
 * If applicable, add the following below the CDDL Header, 
 * with the fields enclosed by brackets [] replaced by
 * you own identifying information: 
 * "Portions Copyrighted [year] [name of copyright owner]"
 * 
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 */


package com.sun.enterprise.deployment.runtime;

import java.beans.*;
import java.util.List;
import java.util.ArrayList;
import com.sun.enterprise.deployment.Descriptor;

/**
 * This base class defines common behaviour and data for all runtime
 * descriptors.
 *
 * @author Jerome Dochez
 */
public abstract class RuntimeDescriptor extends Descriptor {
    
    protected PropertyChangeSupport propListeners;

    /** Creates a new instance of RuntimeDescriptor */
    public RuntimeDescriptor(RuntimeDescriptor other) {
	super(other);
	propListeners = new PropertyChangeSupport(this); // not copied  
    }

    /** Creates a new instance of RuntimeDescriptor */
    public RuntimeDescriptor() {
	propListeners = new PropertyChangeSupport(this);    
    }
    
    /** 
     * Add a property listener for this bean
     * @param the property listener
     */
    public void addPropertyChangeListener(PropertyChangeListener l) {
        propListeners.addPropertyChangeListener(l);
    }
    
    /**
     * removes a property listener for this bean
     * @param the property listener to remove
     */
    public void removePropertyChangeListener(PropertyChangeListener l) {
        propListeners.removePropertyChangeListener(l);
    }
    
    /**
     * Add a property listener for a specific property name
     * @param the property name
     * @param the property listener
     */
    public void addPropertyChangeListener(String n, PropertyChangeListener l) {
        propListeners.addPropertyChangeListener(n, l);
    }
    
    /**
     * Remover a property listener for specific property name 
     * @param the property name
     * @param the property listener
     */
    public void removePropertyChangeListener(String n, PropertyChangeListener l) {
        propListeners.removePropertyChangeListener(n, l);
    }
    
    /**
     * Sets a property value
     * @param the property name
     * @param the property value
     */
    public void setValue(String name, Object value) {
        Object oldValue = getExtraAttribute(name);
        addExtraAttribute(name, value);
        propListeners.firePropertyChange(name, oldValue, value);
    }
    
    /**
     * @return a property value
     */
    public Object getValue(String name) { 
        return getExtraAttribute(name);
    }
    
    /**
     * indexed property support
     */
    protected void setValue(String name, int index, Object value) {
	List list = getIndexedProperty(name);
	list.set(index, value);
	setValue(name, list);
    }
    
    protected Object getValue(String name, int index) {
	List list = getIndexedProperty(name);
	return list.get(index);	
    }
    
    protected int addValue(String name, Object value) {
	List list = getIndexedProperty(name);
	list.add(value);
	setValue(name, list);
	return list.indexOf(value);
    }
    
    protected int removeValue(String name, Object value) {
	List list = getIndexedProperty(name);
	int index = list.indexOf(value);
	list.remove(index);
	return index;
    }
    
    protected void removeValue(String name, int index) {
	List list = getIndexedProperty(name);
	list.remove(index);
    }
    
    protected void setValues(String name, Object[] values) {
	List list = getIndexedProperty(name);
	for (int i=0;i<values.length;) {
	    list.add(values[i]);
	}
    }
    
    protected Object[] getValues(String name) {
	List list = (List) getValue(name);
	if (list!=null && list.size()>0) {
	    Class c = list.get(0).getClass();
	    Object array = java.lang.reflect.Array.newInstance(c, list.size());
	    return list.toArray((Object[]) array);
	}
	else 
	    return null;
    }
    
    protected int size(String name) {
	List list = (List) getValue(name);
	if (list!=null) 
	    return list.size();
	else 
	    return 0;
    }
    
    private List getIndexedProperty(String name) {
	Object o = getValue(name);
	if (o==null) {
	    return new ArrayList();
	} else {
	    return (List) o;
	}
    }
    
    // xml attributes support
    public void setAttributeValue(String elementName, String attributeName, Object value) {
	// here we have to play games...
	// the nodes cannot know if the property scalar is 0,1 or n 
	// so we look if the key name is already used (means property* 
	// DTD langua) and find the last one entered
	
	
	int index = 0;
	while (getValue(elementName + "-" + index + "-" + attributeName)!=null) {
	    index++;
	}
	if (index==0) {
	    // we had no value yet
	    setValue(elementName + "-0-" + attributeName, value);
	} else {
	    setValue(elementName + "-" + (index+1) + "-" + attributeName, value);
	}
    }
    
    public String getAttributeValue(String elementName, String attributeName) {
	return getAttributeValue(elementName,0, attributeName);
    }
    
    // attribute stored at the descriptor level are treated like elements
    public void setAttributeValue(String attributeName, String value) {
	setValue(attributeName, value);
    }
    
    public String getAttributeValue(String attributeName) {
	return (String) getValue(attributeName);
    }
    
    // indexed xml attributes support
    public void setAttributeValue(String elementName, int index, String attributeName, Object value) {
	setValue(elementName + "-" + index + "-" + attributeName, value);
    }
    
    public String getAttributeValue(String elementName, int index, String attributeName) {
	return (String) getValue(elementName + "-" + index + "-" + attributeName);
    }    
}
