All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datanucleus.store.db4o.fieldmanager.ActivationFieldManager Maven / Gradle / Ivy

/**********************************************************************
Copyright (c) 2007 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.datanucleus.store.db4o.fieldmanager;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.datanucleus.api.ApiAdapter;
import org.datanucleus.state.ObjectProviderFactory;
import org.datanucleus.store.ExecutionContext;
import org.datanucleus.store.ObjectProvider;
import org.datanucleus.store.db4o.DB4OStoreManager;
import org.datanucleus.store.db4o.exceptions.ObjectNotActiveException;
import org.datanucleus.store.fieldmanager.AbstractFieldManager;
import org.datanucleus.store.fieldmanager.SingleValueFieldManager;
import org.datanucleus.store.types.sco.SCO;
import org.datanucleus.util.NucleusLogger;
import org.datanucleus.util.StringUtils;

import com.db4o.ObjectContainer;

/**
 * Field Manager responsible for activating a field where the value is placed there
 * by DB4O but needs activating to make it usable by the persistence process.
 * 
 * @version $Revision: 1.1 $
 */
public class ActivationFieldManager extends AbstractFieldManager
{
    /** DB4O ObjectContainer. */
    private ObjectContainer cont;

    /** StateManager of the owning object whose fields are being fetched. */
    private ObjectProvider sm;

    /**
     * Constructor
     */
    public ActivationFieldManager(ObjectContainer cont, ObjectProvider sm)
    {
        this.cont = cont;
        this.sm = sm;
    }

    /**
     * Utility method to process the passed persistable object.
     * @param fieldNumber Number of the field with this value
     * @param pc The PC object
     */
    protected void processPersistable(int fieldNumber, Object pc)
    {
        ExecutionContext ec = sm.getExecutionContext();
        DB4OStoreManager mgr = (DB4OStoreManager)ec.getStoreManager();

        // Activate the object in DB4O
        mgr.activateObject(cont, pc);

        ObjectProvider pcSM = ec.findObjectProvider(pc);
        if (pcSM == null)
        {
            // Object has no StateManager so create one
            Object id = null;
            try
            {
                id = mgr.getObjectIdForObject(ec, pc);
            }
            catch (ObjectNotActiveException onae)
            {
                // Object stored in this field is not active, so mark it as not loaded
                sm.unloadField(sm.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber).getName());
            }

            if (id == null)
            {
                // Object not persistent yet ?
                return;
            }

            // Add StateManager
            // TODO Localise this message
            NucleusLogger.DATASTORE.debug("Field " + sm.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber).getFullFieldName() + 
                " with value " + StringUtils.toJVMIDString(pc) + " and id=" + id + " has no StateManager so attaching one");
            ObjectProviderFactory.newForHollowPreConstructed(ec, id, pc);
        }
    }

    public Object fetchObjectField(int fieldNumber)
    {
        SingleValueFieldManager sfv = new SingleValueFieldManager();
        sm.provideFields(new int[]{fieldNumber}, sfv);
        Object value = sfv.fetchObjectField(fieldNumber);
        ApiAdapter api = sm.getExecutionContext().getApiAdapter();

        if (value != null)
        {
            if (api.isPersistable(value))
            {
                // Process PC fields
                processPersistable(fieldNumber, value);
            }
            else if (value instanceof Collection)
            {
                // Process all elements of the Collection that are PC
                cont.ext().activate(value, 2); // Activate the Collection, elements - TODO Log this
                if (!(value instanceof SCO))
                {
                    // Replace with SCO
                    value = sm.wrapSCOField(fieldNumber, value, false, false, true);
                }
                Collection coll = (Collection)value;
                Iterator iter = coll.iterator();
                while (iter.hasNext())
                {
                    Object element = iter.next();
                    if (api.isPersistable(element))
                    {
                        processPersistable(fieldNumber, element);
                    }
                }
            }
            else if (value instanceof Map)
            {
                // Process all keys, values of the Map that are PC
                cont.ext().activate(value, 2); // Activate the Map, keys/values - TODO Log this
                if (!(value instanceof SCO))
                {
                    // Replace with SCO
                    value = sm.wrapSCOField(fieldNumber, value, false, false, true);
                }
                Map map = (Map)value;

                // Process any keys that are PersistenceCapable
                Set keys = map.keySet();
                Iterator iter = keys.iterator();
                while (iter.hasNext())
                {
                    Object mapKey = iter.next();
                    if (api.isPersistable(mapKey))
                    {
                        processPersistable(fieldNumber, mapKey);
                    }
                }

                // Process any values that are PersistenceCapable
                Collection values = map.values();
                iter = values.iterator();
                while (iter.hasNext())
                {
                    Object mapValue = iter.next();
                    if (api.isPersistable(mapValue))
                    {
                        processPersistable(fieldNumber, mapValue);
                    }
                }
            }
            else if (value instanceof Object[])
            {
                cont.ext().activate(value, 2); // Activate the array, elements - TODO Log this
                Object[] array = (Object[]) value;
                for (int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy