package com.ibm.emb.meb.test.ejb;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.emb.ContentAccessException;
import javax.emb.IllegalOptionException;
import javax.emb.MalformedQueryException;
import javax.emb.MediaEntityLocal;
import javax.emb.MediaEntityLocalHome;
import javax.emb.MediaException;
import javax.emb.MediaFormat;
import javax.emb.MediaFormatRegistry;
import javax.emb.MetaDataEntityLocal;
import javax.emb.MetaDataEntityLocalHome;
import javax.emb.MetaDataSyntaxException;
import javax.emb.MetaDataValidationException;
import javax.emb.UnsupportedQueryLanguageException;
import javax.emb.VersionChainIntegrityException;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.ibm.emb.meb.wrapper.MetaDataEntityWrapperBean;

/**
 * Bean implementation class for Enterprise Bean: MediaEntityLocalTestDriver
 */
public class MetaDataEntityLocalTestDriverBean extends MetaDataEntityWrapperBean implements javax.ejb.SessionBean {

    /**
     *
     */
    private static final long serialVersionUID = 3906363848565864498L;

    private static String MEBHOME_JNDI = "java:comp/env/ejb/emb/MediaEntityLocalHome";

    private static String MDEBHOME_JNDI = "java:comp/env/ejb/emb/MetaDataEntityLocalHome";

    private javax.ejb.SessionContext mySessionCtx;

    private MediaEntityLocalHome meb_home;

    private MetaDataEntityLocalHome mdeb_home;

    private InitialContext ic;

    /**
     * getSessionContext
     */
    public javax.ejb.SessionContext getSessionContext() {
        return mySessionCtx;
    }

    /**
     * setSessionContext
     */
    public void setSessionContext(javax.ejb.SessionContext ctx) {
        mySessionCtx = ctx;
    }

    /**
     * ejbCreate
     */
    public void ejbCreate() throws javax.ejb.CreateException {
    }

    /**
     * ejbActivate
     */
    public void ejbActivate() {
    }

    /**
     * ejbPassivate
     */
    public void ejbPassivate() {
    }

    /**
     * ejbRemove
     */
    public void ejbRemove() {
    }

    private InitialContext getInitialContext() throws NamingException {
        if (ic == null) ic = new InitialContext();
        return ic;
    }

    private MediaEntityLocalHome getMebHome() throws NamingException {
        if (meb_home == null) meb_home = (MediaEntityLocalHome) getInitialContext().lookup(MEBHOME_JNDI);
        return meb_home;
    }

    private MetaDataEntityLocalHome getMdebHome() throws NamingException {
        if (mdeb_home == null) mdeb_home = (MetaDataEntityLocalHome) getInitialContext().lookup(MDEBHOME_JNDI);
        return mdeb_home;
    }

    public void cleanUp() throws NamingException {

    }

    private boolean checkEJBException(EJBException caughtException, Class exceptionToCheck) {
        boolean flag = false;

        EJBException tmp = (EJBException) caughtException;
        while (tmp.getCausedByException() != null) {
            // System.out.println("[EXCEPTION] " +
            // tmp.getCausedByException().getClass().getName());
            if (tmp.getCausedByException().getClass() == exceptionToCheck) {
                // System.out.println("[MATCH FOUND]");
                flag = true;
                break;
            } else if (tmp.getCausedByException() instanceof EJBException) {
                tmp = (EJBException) tmp.getCausedByException();
            } else {
                break;
            }
        }
        return flag;
    }

    public String createMediaSetName(String name) throws NamingException, CreateException, MediaException {
        MediaEntityLocal meb = getMebHome().create();
        if (name != null) meb.setName(name);
        return (String) meb.getPrimaryKey();
    }

    public void bindMediaFormat(String fileExt, MediaFormat format) throws MediaException {
        MediaFormatRegistry reg = MediaFormatRegistry.SINGLETON;
        reg.rebind(fileExt, format);
    }

    public void removeMEBByPK(String pk) throws NamingException, FinderException, RemoveException {
        MediaEntityLocal meb = getMebHome().findByPrimaryKey(pk);
        meb.remove();
    }

    public void removeMDEBByPK(String pk) throws NamingException, FinderException, RemoveException {
        MetaDataEntityLocal mdeb = getMdebHome().findByPrimaryKey(pk);
        mdeb.remove();
    }

    public int addChildExceptions(String pk, String childpk) throws NamingException, FinderException, MediaException {
        try {
            addMetaDataChild(pk, childpk);
        } catch (NullPointerException e) {
            return MetaDataEntityLocalTestDriver.NULLPOINTER;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MetaDataEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int addMediaExceptions(String pk, String mebpk) throws NamingException, FinderException, MediaException {
        try {
            addMetaDataMedia(pk, mebpk);
        } catch (NullPointerException e) {
            return MetaDataEntityLocalTestDriver.NULLPOINTER;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MetaDataEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int getMediaByFormatExceptions(String pk, MediaFormat format, boolean searchChildren)
            throws NamingException, FinderException, MediaException {
        try {
            getMetaDataEntityMediaEntities(pk, format, searchChildren);
        } catch (NullPointerException e) {
            return MetaDataEntityLocalTestDriver.NULLPOINTER;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MetaDataEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int getMediaByMimeTypeExceptions(String pk, String mimeType, boolean searchChildren) throws NamingException,
            FinderException, MediaException {
        try {
            getMetaDataEntityMediaEntities(pk, mimeType, searchChildren);
        } catch (NullPointerException e) {
            return MetaDataEntityLocalTestDriver.NULLPOINTER;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MetaDataEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int getXMLExceptions(String pk) throws NamingException, FinderException, MediaException {
        try {
            getMetaDataEntityXML(pk);
        } catch (ContentAccessException e) {
            return MetaDataEntityLocalTestDriver.CONTENTACCESS;
        }
        return 0;
    }

    public int removeChildExceptions(String pk, String childpk) throws FinderException, NamingException, MediaException {
        try {
            removeMetaDataChild(pk, childpk);
        } catch (NullPointerException e) {
            return MetaDataEntityLocalTestDriver.NULLPOINTER;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MetaDataEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int removeMediaExceptions(String pk, String mebpk) throws NamingException, FinderException, MediaException {
        try {
            removeMetaDataMedia(pk, mebpk);
        } catch (NullPointerException e) {
            return MetaDataEntityLocalTestDriver.NULLPOINTER;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MetaDataEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int setNameExceptions(String pk, String name) throws FinderException, NamingException, MediaException {
        try {
            setMetaDataEntityName(pk, name);
        } catch (NullPointerException e) {
            return MetaDataEntityLocalTestDriver.NULLPOINTER;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MetaDataEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int setPreviousVersionExceptions(String pk, String prevpk) throws MediaException, FinderException,
            NamingException {
        try {
            setMetaDataEntityPreviousVersion(pk, prevpk);
        } catch (VersionChainIntegrityException e) {
            return MetaDataEntityLocalTestDriver.VERSIONCHAININTEGRITY;
        }
        return 0;
    }

    public int setXMLExceptions(String pk, String xml, boolean validate) throws MediaException, FinderException,
            NamingException {
        try {
            setMetaDataEntityXML(pk, xml, validate);
        } catch (NullPointerException e) {
            return MetaDataEntityLocalTestDriver.NULLPOINTER;
        } catch (MetaDataValidationException e) {
            return MetaDataEntityLocalTestDriver.METADATAVALIDATION;
        } catch (MetaDataSyntaxException e) {
            return MetaDataEntityLocalTestDriver.METADATASYNTAX;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MetaDataEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int findByPKExceptions(String pk) throws NamingException {
        try {
            findMDEBByPrimaryKey(pk);
        } catch (NullPointerException e) {
            return MetaDataEntityLocalTestDriver.NULLPOINTER;
        } catch (FinderException e) {
            return MetaDataEntityLocalTestDriver.FINDER;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MetaDataEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int queryExceptions(String query, String queryLanguage, java.util.Map options) throws MediaException,
            NamingException {
        try {
            queryMetaDataEntity(query, queryLanguage, options);
        } catch (NullPointerException e) {
            return MetaDataEntityLocalTestDriver.NULLPOINTER;
        } catch (FinderException e) {
            return MetaDataEntityLocalTestDriver.FINDER;
        } catch (MalformedQueryException e) {
            return MetaDataEntityLocalTestDriver.MALFORMEDQUERY;
        } catch (UnsupportedQueryLanguageException e) {
            return MetaDataEntityLocalTestDriver.UNSUPPORTEDQUERYLANG;
        } catch (IllegalOptionException e) {
            return MetaDataEntityLocalTestDriver.ILLEGALOPTION;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MetaDataEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int retrieveSupportedOptionsExceptions(String queryLanguage) throws NamingException, MediaException {
        try {
            retrieveMetaDataEntitySupportedOptions(queryLanguage);
        } catch (NullPointerException e) {
            return MetaDataEntityLocalTestDriver.NULLPOINTER;
        } catch (UnsupportedQueryLanguageException e) {
            return MetaDataEntityLocalTestDriver.UNSUPPORTEDQUERYLANG;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MetaDataEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }
}