/* Generated by Together */

package cobalt.deployment.descriptor.dist;

import java.util.Vector;

import cobalt.deployment.descriptor.EjbBundleDescriptor;
import cobalt.deployment.descriptor.EjbDescriptor;
import cobalt.deployment.descriptor.EjbReferenceDescriptor;
import cobalt.deployment.descriptor.ResourceReferenceDescriptor;

/**
 * Title:        J2EE ejb module descriptor
 * Description:  Represents J2EE-specific ejb packaging component descriptor for standard ejb packaging component
 * Copyright:    Copyright (c) 2001
 * Company:      ETRI
 * @author Yeon Jun Choi
 * @version 1.0
 */

public class J2eeEjbBundleDescriptor {
    public J2eeEjbBundleDescriptor(Vector bundles) {
        this.descriptors = bundles;
        ejbs = new Vector();
        ejbRefs = new Vector();
        resRefs = new Vector();
        organize();
    }
    public Vector getDescriptors(){ return descriptors; }

    public void setDescriptors(Vector descriptors){
        this.descriptors = descriptors;
        organize();
    }

    public Vector getEjbs(){ return ejbs; }

    public void setEjbs(Vector ejbs){ this.ejbs = ejbs; }

    void organize() {
        resRefs.clear();
        ejbRefs.clear();
        for ( int i=0 ; i< descriptors.size() ; i++) {
            EjbDescriptor ejb = (EjbDescriptor)descriptors.elementAt(i);
			ejbs.addElement(new J2eeEjbDescriptor(ejb));
            organizeResource(ejb);
            organizeEjbReference(ejb);
        }
    }

    void organizeResource(EjbDescriptor ejb) {
        Object[] entry = ejb.getResources().toArray();
        for ( int i=0 ; i<entry.length ; i++)
            resRefs.addElement(new J2eeResourceReferenceDescriptor(ejb, (ResourceReferenceDescriptor)entry[i]));
    }

    void organizeEjbReference(EjbDescriptor ejb) {
        Object[] entry = ejb.getEjbReferences().values().toArray();
        for ( int i=0 ; i<entry.length ; i++)
            ejbRefs.addElement(new J2eeEjbReferenceDescriptor(ejb, (EjbReferenceDescriptor)entry[i]));
    }

    public Vector getResRefs(){ return resRefs; }

    public Vector getResRefs(EjbDescriptor desc){
        Vector result = new Vector();
        for ( int i=0 ; i<resRefs.size() ; i++) {
            J2eeResourceReferenceDescriptor res = (J2eeResourceReferenceDescriptor)resRefs.elementAt(i);
            if (res.getEjb() == desc)
                result.addElement(res);
        }
        return result;
    }

    public void setResRefs(Vector resRefs){ this.resRefs = resRefs; }

    public Vector getEjbRefs(){ return ejbRefs; }

    public Vector getEjbRefs(EjbDescriptor desc){
        Vector result = new Vector();
        for ( int i=0 ; i<ejbRefs.size() ; i++) {
            J2eeEjbReferenceDescriptor ejb = (J2eeEjbReferenceDescriptor)ejbRefs.elementAt(i);
            if (ejb.getEjb() == desc)
                result.addElement(ejb);
        }
        return result;
    }

    public void setEjbRefs(Vector ejbRefs){ this.ejbRefs = ejbRefs; }

    private Vector descriptors;
    private Vector ejbs;
    private Vector resRefs;
    private Vector ejbRefs;
}
