Package org.jpox.jdo

Source Code of org.jpox.jdo.JPOXJDOHelper

/**********************************************************************
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.jdo;

import java.sql.SQLException;
import java.util.Collection;

import javax.jdo.JDODataStoreException;
import javax.jdo.JDOException;
import javax.jdo.JDOFatalDataStoreException;
import javax.jdo.JDOFatalInternalException;
import javax.jdo.JDOFatalUserException;
import javax.jdo.JDOHelper;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.JDOOptimisticVerificationException;
import javax.jdo.JDOUnsupportedOptionException;
import javax.jdo.JDOUserException;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.spi.PersistenceCapable;
import javax.transaction.xa.XAException;

import org.jpox.ObjectManagerFactoryImpl;
import org.jpox.StateManager;
import org.jpox.exceptions.ClassNotPersistableException;
import org.jpox.exceptions.JPOXDataStoreException;
import org.jpox.exceptions.JPOXException;
import org.jpox.exceptions.JPOXObjectNotFoundException;
import org.jpox.exceptions.JPOXOptimisticException;
import org.jpox.exceptions.JPOXUnsupportedOptionException;
import org.jpox.exceptions.JPOXUserException;
import org.jpox.exceptions.NoPersistenceInformationException;
import org.jpox.jdo.exceptions.ClassNotPersistenceCapableException;
import org.jpox.metadata.ClassMetaData;
import org.jpox.metadata.MetaDataManager;
import org.jpox.state.StateManagerFactory;
import org.jpox.util.Localiser;

/**
* Helper for persistence operations with JPOX.
* Extends JDOHelper so that people can use JPOXHelper if they wish.
*
* @version $Revision: 1.17 $
*/
public class JPOXJDOHelper extends JDOHelper
{
    /** Localisation utility for output messages */
    protected static final Localiser LOCALISER = Localiser.getInstance("org.jpox.Localisation",
        ObjectManagerFactoryImpl.class.getClassLoader());

    // ------------------------------ Class MetaData --------------------------------

    /**
     * Accessor for the JPOX MetaData for the specified class
     * @param pmf PersistenceManager factory
     * @param cls The class
     * @return The MetaData for the class
     */
    public static ClassMetaData getMetaDataForClass(PersistenceManagerFactory pmf, Class cls)
    {
        if (pmf == null || cls == null)
        {
            return null;
        }
        if (!(pmf instanceof AbstractPersistenceManagerFactory))
        {
            return null;
        }

        AbstractPersistenceManagerFactory jpoxPMF = (AbstractPersistenceManagerFactory)pmf;
        MetaDataManager mdmgr = jpoxPMF.getOMFContext().getMetaDataManager();
        return (ClassMetaData)mdmgr.getMetaDataForClass(cls, jpoxPMF.getOMFContext().getClassLoaderResolver(null));
    }

    /**
     * Accessor for the names of the classes that have MetaData for this PMF.
     * @param pmf The PMF
     * @return The class names
     */
    public static String[] getClassesWithMetaData(PersistenceManagerFactory pmf)
    {
        if (pmf == null || !(pmf instanceof AbstractPersistenceManagerFactory))
        {
            return null;
        }

        AbstractPersistenceManagerFactory jpoxPMF = (AbstractPersistenceManagerFactory)pmf;
        Collection classes = jpoxPMF.getOMFContext().getMetaDataManager().getClassesWithMetaData();
        return (String[])classes.toArray(new String[classes.size()]);
    }

    // ------------------------------ Object Lifecycle --------------------------------

    /**
     * Convenience method to return a string of the state of an object.
     * Will return things like "detached-dirty", "persistent-clean", etc
     * TODO Remove this when JDOHelper has getObjectState() method returning ObjectState enum.
     * @param obj The object
     * @return The state
     */
    public static String getObjectStateAsString(Object obj)
    {
        if (obj == null)
        {
            return null;
        }

        if (isDetached(obj) && isDirty(obj))
        {
            return "detached-dirty";
        }
        else if (isDetached(obj) && !isDirty(obj))
        {
            return "detached-clean";
        }
        else if (isPersistent(obj) && isDirty(obj) && !isNew(obj) && isTransactional(obj) && !isDeleted(obj))
        {
            return "persistent-dirty";
        }
        else if (isPersistent(obj) && !isNew(obj) && !isDirty(obj) && isTransactional(obj) && !isDeleted(obj))
        {
            return "persistent-clean";
        }
        else if (isPersistent(obj) && isNew(obj) && isDirty(obj) && isTransactional(obj) && !isDeleted(obj))
        {
            return "persistent-new";
        }
        else if (isPersistent(obj) && isNew(obj) && isDirty(obj) && isTransactional(obj) && isDeleted(obj))
        {
            return "persistent-new-deleted";
        }
        else if (isPersistent(obj) && !isNew(obj) && isDirty(obj) && isTransactional(obj) && isDeleted(obj))
        {
            return "persistent-deleted";
        }
        else if (isPersistent(obj) && !isNew(obj) && !isDirty(obj) && !isDeleted(obj) && !isTransactional(obj))
        {
            return "hollow / persistent-nontransactional";
        }
        else if (isPersistent(obj) && !isNew(obj) && isDirty(obj) && !isDeleted(obj) && !isTransactional(obj))
        {
            return "persistent-nontransactional-dirty";
        }
        else if (!isPersistent(obj) && !isNew(obj) && isDirty(obj) && isTransactional(obj) && !isDeleted(obj))
        {
            return "transient-dirty";
        }
        else if (!isPersistent(obj) && !isNew(obj) && !isDirty(obj) && isTransactional(obj) && !isDeleted(obj))
        {
            return "transient-clean";
        }
        else if (!isPersistent(obj) && !isNew(obj) && !isDirty(obj) && !isTransactional(obj) && !isDeleted(obj))
        {
            return "transient-clean";
        }

        return "transient";
    }

    /**
     * Method to return the names of all fields that are currently dirty in the
     * passed detached object.
     * TODO Try to remove the need for the PM. We currently use it to generate the temporary StateManager
     * @param obj The PersistenceCapable (detached)
     * @param pm PersistenceManager to use
     * @return Names of the fields that are dirty
     * @throws JPOXUserException Thrown if the object is not detached
     */
    public static String[] getDetachedObjectDirtyFields(Object obj, PersistenceManager pm)
    {
        if (obj == null)
        {
            return null;
        }
        if (!isDetached(obj))
        {
            throw new JPOXUserException(LOCALISER.msg("010008"));
        }

        // Create a StateManager to give us a means of extracting the detached info
        PersistenceCapable pc = (PersistenceCapable)obj;
        org.jpox.ObjectManager thePM = ((AbstractPersistenceManager)pm).getObjectManager();
        StateManager sm = StateManagerFactory.newStateManagerForDetached(thePM, pc, getObjectId(pc), null);
        pc.jdoReplaceStateManager((javax.jdo.spi.StateManager) sm); // Assign this StateManager to our detached object
        sm.retrieveDetachState(sm);
        String[] dirtyFieldNames = sm.getDirtyFieldNames();
        pc.jdoReplaceStateManager(null); // Remove the StateManager from our detached object

        return dirtyFieldNames;
    }

    /**
     * Method to return the names of all fields that are currently loaded in the
     * passed detached object.
     * TODO Try to remove the need for the PM. We currently use it to generate the temporary StateManager
     * @param obj The PersistenceCapable (detached)
     * @param pm PersistenceManager to use
     * @return Names of the fields that are loaded
     * @throws JPOXUserException Thrown if the object is not detached
     */
    public static String[] getDetachedObjectLoadedFields(Object obj, PersistenceManager pm)
    {
        if (obj == null)
        {
            return null;
        }
        if (!isDetached(obj))
        {
            throw new JPOXUserException(LOCALISER.msg("010008"));
        }

        // Create a StateManager to give us a means of extracting the detached info
        PersistenceCapable pc = (PersistenceCapable)obj;
        org.jpox.ObjectManager thePM = ((AbstractPersistenceManager)pm).getObjectManager();
        StateManager sm = StateManagerFactory.newStateManagerForDetached(thePM, pc, getObjectId(pc), null);
        pc.jdoReplaceStateManager((javax.jdo.spi.StateManager) sm); // Assign this StateManager to our detached object
        sm.retrieveDetachState(sm);
        String[] loadedFieldNames = sm.getLoadedFieldNames();
        pc.jdoReplaceStateManager(null); // Remove the StateManager from our detached object

        return loadedFieldNames;
    }

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

    /**
     * Convenience method to convert a JPOX exception into a JDO 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 JDOException
     */
    public static JDOException getJDOExceptionForJPOXException(JPOXException jpe)
    {
        // Specific exceptions first
        if (jpe instanceof ClassNotPersistableException)
        {
            return new ClassNotPersistenceCapableException(jpe.getMessage(), jpe);
        }
        else if (jpe instanceof NoPersistenceInformationException)
        {
            return new org.jpox.jdo.exceptions.NoPersistenceInformationException(jpe.getMessage(), jpe);
        }
        else if (jpe instanceof JPOXUnsupportedOptionException)
        {
            return new JDOUnsupportedOptionException(jpe.getMessage(), jpe);
        }
        else if (jpe instanceof JPOXDataStoreException)
        {
            if (jpe.isFatal())
            {
                //sadly JDOFatalDataStoreException dont allow nested exceptions and failed objects together
                if (jpe.getFailedObject() != null)
                {
                    return new JDOFatalDataStoreException(jpe.getMessage(), jpe.getFailedObject());
                }
                else if (jpe.getNestedExceptions() != null)
                {
                    return new JDOFatalDataStoreException(jpe.getMessage(), jpe.getNestedExceptions());
                }
                else
                {
                    return new JDOFatalDataStoreException(jpe.getMessage(), jpe);
                }
            }
            else
            {
                if (jpe.getNestedExceptions() != null)
                {
                    if (jpe.getFailedObject() != null)
                    {
                        return new JDODataStoreException(jpe.getMessage(), jpe.getNestedExceptions(), jpe.getFailedObject());
                    }
                    return new JDODataStoreException(jpe.getMessage(), jpe.getNestedExceptions());
                }
                else if (jpe.getFailedObject() != null)
                {
                    return new JDODataStoreException(jpe.getMessage(), jpe.getFailedObject());
                }
                else
                {
                    return new JDODataStoreException(jpe.getMessage(), jpe);
                }
            }
        }
        else if (jpe instanceof JPOXObjectNotFoundException)
        {           
            //sadly JDOObjectNotFoundException dont allow nested exceptions and failed objects together
            if (jpe.getFailedObject() != null)
            {
                return new JDOObjectNotFoundException(jpe.getMessage(), jpe.getFailedObject());
            }
            else if (jpe.getNestedExceptions() != null)
            {
                return new JDOObjectNotFoundException(jpe.getMessage(), jpe.getNestedExceptions());
            }
            else
            {
                return new JDOFatalDataStoreException(jpe.getMessage(), jpe);
            }
        }
        else if (jpe instanceof JPOXUserException)
        {
            if (jpe.isFatal())
            {
                if (jpe.getNestedExceptions() != null)
                {
                    if (jpe.getFailedObject() != null)
                    {
                        return new JDOFatalUserException(jpe.getMessage(), jpe.getNestedExceptions(), jpe.getFailedObject());
                    }
                    return new JDOFatalUserException(jpe.getMessage(), jpe.getNestedExceptions());
                }
                else if (jpe.getFailedObject() != null)
                {
                    return new JDOFatalUserException(jpe.getMessage(), jpe.getFailedObject());
                }
                else
                {
                    return new JDOFatalUserException(jpe.getMessage(), jpe);
                }
            }
            else
            {
                if (jpe.getNestedExceptions() != null)
                {
                    if (jpe.getFailedObject() != null)
                    {
                        return new JDOUserException(jpe.getMessage(), jpe.getNestedExceptions(), jpe.getFailedObject());
                    }
                    return new JDOUserException(jpe.getMessage(), jpe.getNestedExceptions());
                }
                else if (jpe.getFailedObject() != null)
                {
                    return new JDOUserException(jpe.getMessage(), jpe.getFailedObject());
                }
                else
                {
                    return new JDOUserException(jpe.getMessage(), jpe);
                }
            }
        }
        else if (jpe instanceof JPOXOptimisticException)
        {
            //sadly JDOOptimisticVerificationException dont allow nested exceptions and failed objects together
            if (jpe.getFailedObject() != null)
            {
                return new JDOOptimisticVerificationException(jpe.getMessage(), jpe.getFailedObject());
            }
            else if (jpe.getNestedExceptions() != null)
            {
                return new JDOOptimisticVerificationException(jpe.getMessage(), jpe.getNestedExceptions());
            }
            else
            {
                return new JDOOptimisticVerificationException(jpe.getMessage(), jpe);
            }
        }
        else if (jpe instanceof org.jpox.transaction.HeuristicRollbackException
                && jpe.getNestedExceptions().length==1
                && jpe.getNestedExceptions()[0].getCause() instanceof SQLException)
        {
            // if there was a single failure in the transaction, we want to properly propagate the
            // single nested SQLException that was the cause of the failure, so application code
            // can decide on what to do with it
            return new JDODataStoreException(jpe.getMessage(), ((XAException)jpe.getNestedExceptions()[0]).getCause());
        }
        else
        {
            if (jpe.isFatal())
            {
                if (jpe.getNestedExceptions() != null)
                {
                    return new JDOFatalInternalException(jpe.getMessage(), jpe.getNestedExceptions());
                }
                else
                {
                    return new JDOFatalInternalException(jpe.getMessage(), jpe);
                }
            }
            else if (jpe.getNestedExceptions() != null)
            {
                return new JDOException(jpe.getMessage(), jpe.getNestedExceptions());
            }
            else
            {
                return new JDOException(jpe.getMessage(), jpe);
            }
        }
    }
}
TOP

Related Classes of org.jpox.jdo.JPOXJDOHelper

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.