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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.emb.ContentAccessException;
import javax.emb.ContentTooLargeException;
import javax.emb.ContentUnmutableException;
import javax.emb.ConversionException;
import javax.emb.FormatFeatureException;
import javax.emb.FormatNotFoundException;
import javax.emb.FormatSyntaxException;
import javax.emb.LinkTranslationException;
import javax.emb.ListenerVetoException;
import javax.emb.LocationUnmutableException;
import javax.emb.MalformedLocationException;
import javax.emb.Media;
import javax.emb.MediaConverterSpec;
import javax.emb.MediaEntityLocal;
import javax.emb.MediaEntityLocalHome;
import javax.emb.MediaException;
import javax.emb.MediaFormat;
import javax.emb.MediaFormatException;
import javax.emb.MediaFormatRegistry;
import javax.emb.MediaListener;
import javax.emb.MetaDataEntityLocal;
import javax.emb.MetaDataEntityLocalHome;
import javax.emb.NoServerFoundException;
import javax.emb.ProtocolConstraints;
import javax.emb.VersionChainIntegrityException;
import javax.naming.InitialContext;
import javax.naming.NamingException;

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

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

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

    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 void bindMediaFormat(String fileExt, MediaFormat format) throws MediaException {
        MediaFormatRegistry reg = MediaFormatRegistry.SINGLETON;
        reg.rebind(fileExt, format);
    }

    public void unbindMediaFormat(String fileExt) throws MediaException {
        MediaFormatRegistry reg = MediaFormatRegistry.SINGLETON;
        reg.unbind(fileExt);
    }

    public MediaFormat lookUpMediaFormat(String fileExt) throws MediaException {
        MediaFormatRegistry reg = MediaFormatRegistry.SINGLETON;
        return reg.lookup(fileExt);
    }

    public int removeMEBExceptions(String pk) throws NamingException, FinderException, RemoveException,
            ListenerVetoException {
        MediaEntityLocal meb = getMebHome().findByPrimaryKey(pk);
        try {
            meb.remove();
        } catch (EJBException e) {
            if (checkEJBException(e, ListenerVetoException.class))
                return MediaEntityLocalTestDriver.LISTENERVETO;
            else
                throw e;
        }
        return 0;
    }

    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 setContentExceptions(String pk, File file) throws FileNotFoundException, NamingException,
            FinderException, MediaException {

        try {
            setMediaEntityContent(pk, file);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (ContentAccessException e) {
            return MediaEntityLocalTestDriver.CONTENTACCESS;
        } catch (FormatSyntaxException e) {
            return MediaEntityLocalTestDriver.FORMATSYNTAX;
        } catch (ContentUnmutableException e) {
            return MediaEntityLocalTestDriver.CONTENTUNMUTABLE;
        } catch (ContentTooLargeException e) {
            return MediaEntityLocalTestDriver.CONTENTTOOLARGE;
        } catch (FormatNotFoundException e) {
            return MediaEntityLocalTestDriver.FORMATNOTFOUND;
        } catch (ListenerVetoException e) {
            return MediaEntityLocalTestDriver.LISTENERVETO;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else if (checkEJBException(e, ListenerVetoException.class)) {
                return MediaEntityLocalTestDriver.LISTENERVETO;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int setContentExceptions(String pk, byte[] content) throws NamingException, FinderException, MediaException {

        try {
            setMediaEntityContent(pk, content);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (ContentAccessException e) {
            return MediaEntityLocalTestDriver.CONTENTACCESS;
        } catch (FormatSyntaxException e) {
            return MediaEntityLocalTestDriver.FORMATSYNTAX;
        } catch (ContentUnmutableException e) {
            return MediaEntityLocalTestDriver.CONTENTUNMUTABLE;
        } catch (ContentTooLargeException e) {
            return MediaEntityLocalTestDriver.CONTENTTOOLARGE;
        } catch (FormatNotFoundException e) {
            return MediaEntityLocalTestDriver.FORMATNOTFOUND;
        } catch (ListenerVetoException e) {
            return MediaEntityLocalTestDriver.LISTENERVETO;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else if (checkEJBException(e, ListenerVetoException.class)) {
                return MediaEntityLocalTestDriver.LISTENERVETO;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int addListenerExceptions(String pk, MediaListener listener) throws MediaException, NamingException,
            FinderException {
        try {
            addMediaEntityListener(pk, listener);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int addMetaDataExceptions(String pk, String mdeb) throws MediaException, NamingException, FinderException {
        try {
            addMediaEntityMetaData(pk, mdeb);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int convertExceptions(String pk, MediaConverterSpec[] specs) throws MediaException, NamingException,
            FinderException {
        try {
            convertMediaEntity(pk, specs);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (ContentAccessException e) {
            return MediaEntityLocalTestDriver.CONTENTACCESS;
        } catch (ConversionException e) {
            return MediaEntityLocalTestDriver.CONVERSION;
        } catch (MediaFormatException e) {
            return MediaEntityLocalTestDriver.MEDIAFORMAT;
        } catch (ListenerVetoException e) {
            return MediaEntityLocalTestDriver.LISTENERVETO;
        } catch (ContentUnmutableException e) {
            return MediaEntityLocalTestDriver.CONTENTUNMUTABLE;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int exportMediaExceptions(String pk, URL targetLocation) throws MediaException, NamingException,
            FinderException {
        try {
            exportMediaEntity(pk, targetLocation);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (ContentAccessException e) {
            return MediaEntityLocalTestDriver.CONTENTACCESS;
        } catch (MalformedLocationException e) {
            return MediaEntityLocalTestDriver.MALFORMEDLOCATION;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int getContentExceptions(String pk) throws MediaException, NamingException, FinderException {
        try {
            getMediaEntityContent(pk);
        } catch (ContentAccessException e) {
            return MediaEntityLocalTestDriver.CONTENTACCESS;
        } catch (ContentTooLargeException e) {
            return MediaEntityLocalTestDriver.CONTENTTOOLARGE;
        }
        return 0;
    }

    public int getFormatExceptions(String pk) throws MediaException, NamingException, FinderException {
        try {
            getMediaEntityFormat(pk);
        } catch (FormatNotFoundException e) {
            return MediaEntityLocalTestDriver.FORMATNOTFOUND;
        }
        return 0;
    }

    public int getHeaderExceptions(String pk) throws MediaException, NamingException, FinderException {
        try {
            getMediaEntityHeader(pk);
        } catch (FormatNotFoundException e) {
            return MediaEntityLocalTestDriver.FORMATNOTFOUND;
        } catch (FormatSyntaxException e) {
            return MediaEntityLocalTestDriver.FORMATSYNTAX;
        } catch (FormatFeatureException e) {
            return MediaEntityLocalTestDriver.FORMATFEATURE;
        }
        return 0;
    }

    public int getMimeTypeExceptions(String pk) throws MediaException, NamingException, FinderException {
        try {
            getMediaEntityMimeType(pk);
        } catch (FormatNotFoundException e) {
            return MediaEntityLocalTestDriver.FORMATNOTFOUND;
        }
        return 0;
    }

    public int getProxyExceptions(String pk) throws MediaException, NamingException, FinderException {
        try {
            getMediaEntityProxy(pk);
        } catch (ContentAccessException e) {
            return MediaEntityLocalTestDriver.CONTENTACCESS;
        } catch (MediaFormatException e) {
            return MediaEntityLocalTestDriver.MEDIAFORMAT;
        }
        return 0;
    }

    public int getSizeExceptions(String pk) throws MediaException, NamingException, FinderException {
        try {
            getMediaEntitySize(pk);
        } catch (ContentAccessException e) {
            return MediaEntityLocalTestDriver.CONTENTACCESS;
        }
        return 0;
    }

    public int importMediaExceptions(String pk, URL targetLocation, String name) throws MediaException,
            NamingException, FinderException {
        try {
            importMediaEntityContent(pk, targetLocation, name);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (ContentAccessException e) {
            return MediaEntityLocalTestDriver.CONTENTACCESS;
        } catch (ContentTooLargeException e) {
            return MediaEntityLocalTestDriver.CONTENTTOOLARGE;
        } catch (MalformedLocationException e) {
            return MediaEntityLocalTestDriver.MALFORMEDLOCATION;
        } catch (ListenerVetoException e) {
            return MediaEntityLocalTestDriver.LISTENERVETO;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        } catch (CreateException ce) {
            return 0;
        }
        return 0;
    }

    public boolean readCompareContent(String pk, long position, byte[] buffer, File origFileContent)
            throws IOException, NamingException, FinderException, MediaException {
        int amountCopied = this.readMediaEntityContent(pk, position, buffer);

        FileInputStream file1024ByteInputStream = new FileInputStream(origFileContent);
        byte[] fileArray = new byte[amountCopied];
        file1024ByteInputStream.skip(position);

        int amountRead = file1024ByteInputStream.read(fileArray, 0, amountCopied);
        if (amountRead != amountCopied) {
            System.out.println("amountRead: " + amountRead + " amountCopied: " + amountCopied);
            throw new ContentAccessException("error when extracting content from original file");
        }
        byte[] actualBuffer = null;
        if (amountRead < buffer.length) {
            System.out.println("amountRead: " + amountRead);
            actualBuffer = new byte[amountRead];
            for (int i = 0; i < amountRead; i++) {
                actualBuffer[i] = buffer[i];
            }
        } else {
            actualBuffer = buffer;
        }
        file1024ByteInputStream.close();
        if (java.util.Arrays.equals(fileArray, actualBuffer))
            return true;
        else {
            System.out.println("content does not match");
            return false;
        }
    }

    public int readContentExceptions(String pk, long position, byte[] buffer) throws MediaException, NamingException,
            FinderException {
        try {
            readMediaEntityContent(pk, position, buffer);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (ContentAccessException e) {
            return MediaEntityLocalTestDriver.CONTENTACCESS;
        } catch (IndexOutOfBoundsException e) {
            return MediaEntityLocalTestDriver.INDEXOUTOFBOUNDS;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else if (checkEJBException(e, IndexOutOfBoundsException.class)) {
                return MediaEntityLocalTestDriver.INDEXOUTOFBOUNDS;
            } else if (checkEJBException(e, NegativeArraySizeException.class)) {
                return MediaEntityLocalTestDriver.NEGATIVEARRAYSIZE;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public boolean readCompareContent(String pk, long position, byte[] buffer, int offset, int length,
            byte[] origContent, boolean fillBuffer) throws MediaException, NamingException, FinderException {
        int totalCopied = 0;
        int amountCopied = 0;

        amountCopied = this.readMediaEntityContent(pk, position, buffer, offset, length);
        totalCopied = totalCopied + amountCopied;
        // if fillBuffer is true and the buffer did not get filled all the way,
        // loop until filled
        while (fillBuffer && (totalCopied != length)) {
            amountCopied = this.readMediaEntityContent(pk, position + totalCopied, buffer, offset + totalCopied, length
                    - totalCopied);
            totalCopied = totalCopied + amountCopied;
        }

        // compare against original content
        byte[] expBuffer = new byte[buffer.length];
        System.arraycopy(origContent, (int) position, expBuffer, offset, amountCopied);

        if (Arrays.equals(expBuffer, buffer))
            return true;
        else {
            System.out.println("expected byte arrray and retrieved byte array are not equal");
            return false;
        }
    }

    public int readContentExceptions(String pk, long position, byte[] buffer, int offset, int length)
            throws MediaException, NamingException, FinderException {
        try {
            readMediaEntityContent(pk, position, buffer, offset, length);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (ContentAccessException e) {
            return MediaEntityLocalTestDriver.CONTENTACCESS;
        } catch (IndexOutOfBoundsException e) {
            return MediaEntityLocalTestDriver.INDEXOUTOFBOUNDS;
        } catch (NegativeArraySizeException e) {
            return MediaEntityLocalTestDriver.NEGATIVEARRAYSIZE;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else if (checkEJBException(e, IndexOutOfBoundsException.class)) {
                return MediaEntityLocalTestDriver.INDEXOUTOFBOUNDS;
            } else if (checkEJBException(e, NegativeArraySizeException.class)) {
                return MediaEntityLocalTestDriver.NEGATIVEARRAYSIZE;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int removeListenerExceptions(String pk, MediaListener listener) throws MediaException, NamingException,
            FinderException {
        try {
            removeMediaEntityListener(pk, listener);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int removeMetaDataExceptions(String pk, String mdeb) throws MediaException, NamingException, FinderException {
        try {
            removeMediaEntityMetaData(pk, mdeb);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int setChildrenExceptions(String pk, String[] children) throws MediaException, NamingException,
            FinderException {
        try {
            setMediaEntityChildren(pk, children);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (IllegalArgumentException e) {
            return MediaEntityLocalTestDriver.ILLEGALARGUMENT;
        } catch (ContentUnmutableException e) {
            return MediaEntityLocalTestDriver.CONTENTUNMUTABLE;
        } catch (ContentAccessException e) {
            return MediaEntityLocalTestDriver.CONTENTACCESS;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else if (checkEJBException(e, IllegalArgumentException.class)) {
                return MediaEntityLocalTestDriver.ILLEGALARGUMENT;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int setDescriptionExceptions(String pk, String description) throws MediaException, NamingException,
            FinderException {
        try {
            setMediaEntityDescription(pk, description);
        } catch (ListenerVetoException e) {
            return MediaEntityLocalTestDriver.LISTENERVETO;
        }
        return 0;
    }

    public int setLocationExceptions(String pk, URL location) throws MediaException, NamingException, FinderException {
        try {
            setMediaEntityLocation(pk, location);
        } catch (ContentAccessException e) {
            return MediaEntityLocalTestDriver.CONTENTACCESS;
        } catch (LocationUnmutableException e) {
            return MediaEntityLocalTestDriver.LOCATIONUNMUTABLE;
        } catch (ListenerVetoException e) {
            return MediaEntityLocalTestDriver.LISTENERVETO;
        }
        return 0;
    }

    public int setMimeTypeExceptions(String pk, String mimeType) throws MediaException, NamingException,
            FinderException {
        try {
            setMediaEntityMimeType(pk, mimeType);
        } catch (ListenerVetoException e) {
            return MediaEntityLocalTestDriver.LISTENERVETO;
        }
        return 0;
    }

    public int setNameExceptions(String pk, String name) throws MediaException, NamingException, FinderException {
        try {
            setMediaEntityName(pk, name);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (IllegalArgumentException e) {
            return MediaEntityLocalTestDriver.ILLEGALARGUMENT;
        } catch (FormatNotFoundException e) {
            return MediaEntityLocalTestDriver.FORMATNOTFOUND;
        } catch (ListenerVetoException e) {
            return MediaEntityLocalTestDriver.LISTENERVETO;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else if (checkEJBException(e, IllegalArgumentException.class)) {
                return MediaEntityLocalTestDriver.ILLEGALARGUMENT;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int setPreviousVersionExceptions(String pk, String pvpk) throws MediaException, NamingException,
            FinderException {
        try {
            setMediaEntityPreviousVersion(pk, pvpk);
        } catch (VersionChainIntegrityException e) {
            return MediaEntityLocalTestDriver.VERSIONCHAININTEGRITY;
        }
        return 0;
    }

    public int exportHomeMediaExceptions(String[] pks, URL targetDir) throws MediaException, NamingException,
            FinderException {
        try {
            exportHomeMediaEntityBeans(pks, targetDir);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (ContentAccessException e) {
            return MediaEntityLocalTestDriver.CONTENTACCESS;
        } catch (MediaFormatException e) {
            return MediaEntityLocalTestDriver.MEDIAFORMAT;
        } catch (MalformedLocationException e) {
            return MediaEntityLocalTestDriver.MALFORMEDLOCATION;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }

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

    public int importHomeMediaExceptions(URL[] locations, String[] names) throws CreateException, MediaException,
            NamingException, FinderException {
        try {
            importHomeMediaEntityBeans(locations, names);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (ContentAccessException e) {
            return MediaEntityLocalTestDriver.CONTENTACCESS;
        } catch (ContentTooLargeException e) {
            return MediaEntityLocalTestDriver.CONTENTTOOLARGE;
        } catch (MediaFormatException e) {
            return MediaEntityLocalTestDriver.MEDIAFORMAT;
        } catch (MalformedLocationException e) {
            return MediaEntityLocalTestDriver.MALFORMEDLOCATION;
        } catch (IllegalArgumentException e) {
            return MediaEntityLocalTestDriver.ILLEGALARGUMENT;
        } catch (ListenerVetoException e) {
            return MediaEntityLocalTestDriver.LISTENERVETO;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else if (checkEJBException(e, IllegalArgumentException.class)) {
                return MediaEntityLocalTestDriver.ILLEGALARGUMENT;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int publishContentExceptions(Media content, byte transferType, ProtocolConstraints constraints)
            throws MediaException, NamingException, FinderException {
        try {
            publishMediaContent(content, transferType, constraints);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (ContentAccessException e) {
            return MediaEntityLocalTestDriver.CONTENTACCESS;
        } catch (MediaFormatException e) {
            return MediaEntityLocalTestDriver.MEDIAFORMAT;
        } catch (NoServerFoundException e) {
            return MediaEntityLocalTestDriver.NOSERVERFOUND;
        } catch (IllegalArgumentException e) {
            return MediaEntityLocalTestDriver.ILLEGALARGUMENT;
        } catch (ConversionException e) {
            return MediaEntityLocalTestDriver.CONVERSION;
        } catch (LinkTranslationException e) {
            return MediaEntityLocalTestDriver.LINKTRANSLATION;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else if (checkEJBException(e, IllegalArgumentException.class)) {
                return MediaEntityLocalTestDriver.ILLEGALARGUMENT;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public URL publishMediaContent(String pk, byte protocol, ProtocolConstraints constraints) throws MediaException,
            NamingException, FinderException {
        if (pk == null)
            throw new NullPointerException(
                    "MEB Primary Key provided is null, please specify a non-null primary key for this operation");
        MediaEntityLocal meb = getMebHome().findByPrimaryKey(pk);
        URL url = getMebHome().publishContent(meb, protocol, constraints);
        return url;
    }

    public int publishContentExceptions(String pk, byte transferType, ProtocolConstraints constraints)
            throws MediaException, NamingException, FinderException {

        if (pk == null)
            throw new NullPointerException(
                    "MEB Primary Key provided is null, please specify a non-null primary key for this operation");
        MediaEntityLocal meb = getMebHome().findByPrimaryKey(pk);
        try {
            publishMediaContent(meb, transferType, constraints);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (ContentAccessException e) {
            return MediaEntityLocalTestDriver.CONTENTACCESS;
        } catch (MediaFormatException e) {
            return MediaEntityLocalTestDriver.MEDIAFORMAT;
        } catch (NoServerFoundException e) {
            return MediaEntityLocalTestDriver.NOSERVERFOUND;
        } catch (IllegalArgumentException e) {
            return MediaEntityLocalTestDriver.ILLEGALARGUMENT;
        } catch (ConversionException e) {
            return MediaEntityLocalTestDriver.CONVERSION;
        } catch (LinkTranslationException e) {
            return MediaEntityLocalTestDriver.LINKTRANSLATION;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else if (checkEJBException(e, IllegalArgumentException.class)) {
                return MediaEntityLocalTestDriver.ILLEGALARGUMENT;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int publishMediaExceptions(String[] playlist, byte transferType, ProtocolConstraints constraints)
            throws MediaException, NamingException, FinderException {
        try {
            publishMediaEntityBeans(playlist, transferType, constraints);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (ContentAccessException e) {
            return MediaEntityLocalTestDriver.CONTENTACCESS;
        } catch (MediaFormatException e) {
            return MediaEntityLocalTestDriver.MEDIAFORMAT;
        } catch (NoServerFoundException e) {
            return MediaEntityLocalTestDriver.NOSERVERFOUND;
        } catch (IllegalArgumentException e) {
            return MediaEntityLocalTestDriver.ILLEGALARGUMENT;
        } catch (ConversionException e) {
            return MediaEntityLocalTestDriver.CONVERSION;
        } catch (LinkTranslationException e) {
            return MediaEntityLocalTestDriver.LINKTRANSLATION;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else if (checkEJBException(e, IllegalArgumentException.class)) {
                return MediaEntityLocalTestDriver.ILLEGALARGUMENT;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int listenerAboutToChangeMEB(MediaListener listener, String pk, String property) throws FinderException,
            NamingException, MediaException {
        if (listener == null) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        }
        MediaEntityLocal meb = null;
        if (pk != null) meb = getMebHome().findByPrimaryKey(pk);
        try {
            listener.handleAboutToChangeMediaEntity(meb, property);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int listenerMediaEntityChanged(MediaListener listener, String pk, String property) throws FinderException,
            NamingException, MediaException {
        if (listener == null) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        }
        MediaEntityLocal meb = null;
        if (pk != null) meb = getMebHome().findByPrimaryKey(pk);
        try {
            listener.handleMediaEntityChanged(meb, property);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }

    public int listenerAboutToRemoveMEB(MediaListener listener, String pk) throws FinderException, NamingException,
            MediaException {
        if (listener == null) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        }
        MediaEntityLocal meb = null;
        if (pk != null) meb = getMebHome().findByPrimaryKey(pk);
        try {
            listener.handleAboutToRemoveMediaEntity(meb);
        } catch (NullPointerException e) {
            return MediaEntityLocalTestDriver.NULLPOINTER;
        } catch (EJBException e) {
            if (checkEJBException(e, NullPointerException.class)) {
                return MediaEntityLocalTestDriver.NULLPOINTER;
            } else {
                throw e;
            }
        }
        return 0;
    }
}