Package org.jpox.jpa

Source Code of org.jpox.jpa.JPOXJPAHelper

/**********************************************************************
Copyright (c) 2006 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:
    ...
**********************************************************************/
package org.jpox.jpa;

import javax.jdo.JDODataStoreException;
import javax.jdo.JDOException;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.JDOOptimisticVerificationException;
import javax.jdo.JDOUserException;
import javax.jdo.spi.PersistenceCapable;

import javax.persistence.EntityNotFoundException;
import javax.persistence.OptimisticLockException;
import javax.persistence.PersistenceException;

import org.jpox.exceptions.JPOXDataStoreException;
import org.jpox.exceptions.JPOXException;
import org.jpox.exceptions.JPOXObjectNotFoundException;
import org.jpox.exceptions.JPOXOptimisticException;
import org.jpox.exceptions.JPOXUserException;
import org.jpox.store.exceptions.ReachableObjectNotCascadedException;

/**
* Helper for persistence operations with JPA1 within JPOX.
*
* @version $Revision: 1.1 $
*/
public class JPOXJPAHelper
{
    // ------------------------------ Object Management --------------------------------

    /**
     * Convenience accessor for whether the object is persistent.
     * @param obj The object
     * @return Whether it is persistent
     */
    public static boolean isPersistent(Object obj)
    {
        // TODO Change this to org.jpox.jpa.Persistable when we swap over
        if (obj instanceof PersistenceCapable)
        {
            return ((PersistenceCapable)obj).jdoIsPersistent();
        }
        return false;
    }

    /**
     * Convenience accessor for whether the object is deleted.
     * @param obj The object
     * @return Whether it is deleted
     */
    public static boolean isDeleted(Object obj)
    {
        // TODO Change this to org.jpox.jpa.Persistable when we swap over
        if (obj instanceof PersistenceCapable)
        {
            return ((PersistenceCapable)obj).jdoIsDeleted();
        }
        return false;
    }

    /**
     * Convenience accessor for whether the object is detached.
     * @param obj The object
     * @return Whether it is persistent
     */
    public static boolean isDetached(Object obj)
    {
        // TODO Change this to org.jpox.jpa.Persistable when we swap over
        if (obj instanceof PersistenceCapable)
        {
            return ((PersistenceCapable)obj).jdoIsDetached();
        }
        return false;
    }

    /**
     * Convenience accessor for whether the object is transactional.
     * @param obj The object
     * @return Whether it is transactional
     */
    public static boolean isTransactional(Object obj)
    {
        // TODO Change this to org.jpox.jpa.Persistable when we swap over
        if (obj instanceof PersistenceCapable)
        {
            return ((PersistenceCapable)obj).jdoIsTransactional();
        }
        return false;
    }

    /**
     * Convenience method to return a string of the state of an object.
     * Will return things like "detached", "persistent", etc
     * @param obj The object
     * @return The state
     */
    public static String getObjectState(Object obj)
    {
        if (obj == null)
        {
            return null;
        }

        if (isDetached(obj))
        {
            return "detached";
        }
        else if (isPersistent(obj) && isTransactional(obj) && !isDeleted(obj))
        {
            return "persistent";
        }
        else if (isPersistent(obj) && isTransactional(obj) && isDeleted(obj))
        {
            return "persistent-deleted";
        }

        return "transient";
    }

    // ------------------------------ Convenience --------------------------------

    /**
     * Convenience method to convert a JPOX exception into a JPA exception.
     * If the incoming exception has a "failed object" then create the new exception with
     * a failed object. Otherwise if the incoming exception has nested exceptions then
     * create this exception with those nested exceptions. Else create this exception with
     * the incoming exception as its nested exception.
     * @param jdoe JDOException
     * @return The JPAException
     */
    public static PersistenceException getJPAExceptionForJDOException(JDOException jdoe)
    {
        if (jdoe instanceof JDODataStoreException)
        {
            // JPA doesnt have "datastore" exceptions so just give a PersistenceException
            if (jdoe.getNestedExceptions() != null)
            {
                return new PersistenceException(jdoe.getMessage(), jdoe.getCause());
            }
            else
            {
                return new PersistenceException(jdoe.getMessage(), jdoe);
            }
        }
        else if (jdoe instanceof JDOObjectNotFoundException)
        {
            return new EntityNotFoundException(jdoe.getMessage());
        }
        else if (jdoe instanceof JDOUserException)
        {
            // JPA doesnt have "user" exceptions so just give a PersistenceException
            if (jdoe.getNestedExceptions() != null)
            {
                return new PersistenceException(jdoe.getMessage(), jdoe.getCause());
            }
            else
            {
                return new PersistenceException(jdoe.getMessage(), jdoe);
            }
        }
        else if (jdoe instanceof JDOOptimisticVerificationException)
        {
            if (jdoe.getNestedExceptions() != null)
            {
                return new OptimisticLockException(jdoe.getMessage(), jdoe.getCause());
            }
            else
            {
                return new OptimisticLockException(jdoe.getMessage(), jdoe);
            }
        }
        else
        {
            // JPA doesnt have "internal" exceptions so just give a PersistenceException
            if (jdoe.getNestedExceptions() != null)
            {
                return new PersistenceException(jdoe.getMessage(), jdoe.getCause());
            }
            else
            {
                return new PersistenceException(jdoe.getMessage(), jdoe);
            }
        }
    }

    /**
     * Convenience method to convert a JPOX exception into a JPA exception.
     * If the incoming exception has a "failed object" then create the new exception with
     * a failed object. Otherwise if the incoming exception has nested exceptions then
     * create this exception with those nested exceptions. Else create this exception with
     * the incoming exception as its nested exception.
     * @param jpe JPOXException
     * @return The JPAException
     */
    public static PersistenceException getJPAExceptionForJPOXException(JPOXException jpe)
    {
        if (jpe instanceof ReachableObjectNotCascadedException)
        {
            // Reachable object not persistent but field doesnt allow cascade-persist
            throw new IllegalStateException(jpe.getMessage());
        }
        else if (jpe instanceof JPOXDataStoreException)
        {
            // JPA doesnt have "datastore" exceptions so just give a PersistenceException
            if (jpe.getNestedExceptions() != null)
            {
                return new PersistenceException(jpe.getMessage(), jpe.getCause());
            }
            else
            {
                return new PersistenceException(jpe.getMessage(), jpe);
            }
        }
        else if (jpe instanceof JPOXObjectNotFoundException)
        {
            return new EntityNotFoundException(jpe.getMessage());
        }
        else if (jpe instanceof JPOXUserException)
        {
            // JPA doesnt have "user" exceptions so just give a PersistenceException
            if (jpe.getNestedExceptions() != null)
            {
                return new PersistenceException(jpe.getMessage(), jpe.getCause());
            }
            else
            {
                return new PersistenceException(jpe.getMessage(), jpe);
            }
        }
        else if (jpe instanceof JPOXOptimisticException)
        {
            if (jpe.getNestedExceptions() != null)
            {
                return new OptimisticLockException(jpe.getMessage(), jpe.getCause());
            }
            else
            {
                return new OptimisticLockException(jpe.getMessage(), jpe);
            }
        }
        else
        {
            // JPA doesnt have "internal" exceptions so just give a PersistenceException
            if (jpe.getNestedExceptions() != null)
            {
                return new PersistenceException(jpe.getMessage(), jpe.getCause());
            }
            else
            {
                return new PersistenceException(jpe.getMessage(), jpe);
            }
        }
    }
}
TOP

Related Classes of org.jpox.jpa.JPOXJPAHelper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.