org.datanucleus.store.db4o.DB4OUtils 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;
import org.datanucleus.identity.OIDFactory;
import org.datanucleus.metadata.AbstractClassMetaData;
import org.datanucleus.metadata.IdentityType;
import org.datanucleus.state.ObjectProviderFactory;
import org.datanucleus.store.ExecutionContext;
import org.datanucleus.store.ObjectProvider;
import org.datanucleus.store.db4o.fieldmanager.AssignStateManagerFieldManager;
import com.db4o.ObjectContainer;
/**
* Utilities for DB4O (http://www.db4o.com).
*/
public class DB4OUtils
{
/**
* Convenience method to take an object returned by DB4O (from a query for example), and prepare it for
* passing to the user. Makes sure there is a StateManager connected, with associated fields marked as loaded.
* @param obj The object (from DB4O)
* @param ec execution context
* @param cont ObjectContainer that returned the object
* @param cmd ClassMetaData for the object
* @param mgr DB4OManager
* @return The StateManager for this object
*/
public static ObjectProvider prepareDB4OObjectForUse(Object obj, ExecutionContext ec, ObjectContainer cont,
AbstractClassMetaData cmd, DB4OStoreManager mgr)
{
if (!ec.getApiAdapter().isPersistable(obj))
{
return null;
}
ObjectProvider sm = ec.findObjectProvider(obj);
if (sm == null)
{
// Find the identity
Object id = null;
if (cmd.getIdentityType() == IdentityType.DATASTORE)
{
long db4oId = cont.ext().getID(obj);
id = OIDFactory.getInstance(ec.getNucleusContext(), db4oId);
}
else
{
id = ec.getApiAdapter().getNewApplicationIdentityObjectId(obj, cmd);
}
// Object not managed so give it a StateManager before returning it
sm = ObjectProviderFactory.newForPersistentClean(ec, id, obj);
sm.provideFields(cmd.getAllMemberPositions(), new AssignStateManagerFieldManager(cont, sm));
}
else
{
// Object returned already available with StateManager so make sure it is active for use
if (!cont.ext().isActive(obj))
{
mgr.activateObject(cont, obj);
}
}
// Wrap all unwrapped SCO fields of this instance so we can pick up any changes
sm.replaceAllLoadedSCOFieldsWithWrappers();
return sm;
}
}