/**
* Copyright (C) 2001-2005 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.objectweb.speedo.genclass;
import org.objectweb.jorm.api.PAccessor;
import org.objectweb.jorm.api.PBinding;
import org.objectweb.jorm.api.PClassMapping;
import org.objectweb.jorm.api.PException;
import org.objectweb.jorm.api.PMapper;
import org.objectweb.jorm.api.PMappingCallback;
import org.objectweb.jorm.api.PMappingStructuresManager;
import org.objectweb.jorm.api.PNameIterator;
import org.objectweb.jorm.mapper.rdb.genclass.RdbGenClassMapping;
import org.objectweb.jorm.metainfo.api.MetaObject;
import org.objectweb.jorm.naming.api.PBinder;
import org.objectweb.jorm.naming.api.PName;
import org.objectweb.jorm.naming.api.PNameCoder;
import org.objectweb.jorm.type.api.PType;
import org.objectweb.jorm.util.api.Loggable;
import org.objectweb.medor.tuple.api.TupleCollection;
import org.objectweb.perseus.persistence.api.TransactionalPersistenceManager;
import org.objectweb.speedo.api.SpeedoRuntimeException;
import org.objectweb.speedo.genclass.api.SpeedoGenClassPO;
import org.objectweb.speedo.lib.Personality;
import org.objectweb.speedo.mim.api.DetachedLifeCycle;
import org.objectweb.speedo.mim.api.PersistentObjectItf;
import org.objectweb.speedo.mim.lib.AbstractHomeImpl;
import org.objectweb.speedo.pm.api.POManagerFactoryItf;
import org.objectweb.speedo.pm.api.POManagerItf;
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.LoggerFactory;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
/**
* This implementation of HomeItf delegated the PClassMapping roles to
* another instance. This implementation is dedicated to GenClass (Collection,
* List, Vector, Map, ...).
*
* @author S.Chassande-Barrioz
*/
public abstract class AbstractGenClassHome extends AbstractHomeImpl {
/**
* The real PClassMapping instance.
*/
PClassMapping pcm;
/**
* The path of the genclass (source_class_name#gc_field_name)
*/
String path;
public AbstractGenClassHome(Personality p) {
super(p);
}
public void init(PClassMapping _pcm,
TransactionalPersistenceManager _tpm,
POManagerFactoryItf _pmf,
String p) {
this.tpm = _tpm;
this.pmf = _pmf;
this.pcm = _pcm;
this.path = p;
}
public PClassMapping getRealPClassMapping() {
return pcm;
}
public void makePersistent(POManagerItf pm, PersistentObjectItf sp, SpeedoGenClassPO thepo, Map map) {
if (!sp.speedoIsActive()) {
if (sp instanceof SpeedoGenClassPO) {
//Genclass contains genclass instances
SpeedoGenClassPO sgcp = (SpeedoGenClassPO) sp;
if (sgcp.speedoGetPType() == null) {
//Assign the PType to the generic class
sgcp.speedoSetPType(thepo.speedoGetPType().getNestedPType());
}
sgcp.speedoSetLinkedField(thepo.speedoGetGenClassId());
sgcp.speedoSetPNameHints(thepo.getPName());
}
POManagerItf mypm = pm;
if (mypm == null) {
mypm = thepo.speedoGetPOManager();
if (mypm == null) {
throw new SpeedoRuntimeException(
"When a persistent object is used (read or write), " +
"a PersistenceManager is needed");
}
}
mypm.speedoMakePersistent(sp, map);
}
}
public void makePersistent(POManagerItf pm, Iterator it, SpeedoGenClassPO thepo, Map map) {
if (it == null) {
return;
}
while(it.hasNext()) {
Object o = it.next();
if (o instanceof PersistentObjectItf) {
//if detached, attach it
if (((PersistentObjectItf) o).speedoGetReferenceState().getDetachedStatus()
!= DetachedLifeCycle.DETACHED_NONE) {
o = pm.speedoAttachCopy(o, map);
} else {
//else make it persistent
makePersistent(pm, (PersistentObjectItf) o, thepo, map);
}
} else {
// Optimisation: If the first element is not a PersistentObjectItf, that
// means that ALL collection elements are not PersistentObjectItf
// implementation
return;
}
}
}
// IMPLEMENTATION OF THE HomeItf INTERFACE //
//--------------------------------------------//
public boolean isDetachable() {
return false;
}
public byte getVersioningStrategy() {
return 0;
}
public Properties getClassProperties() {
return null;
}
public String getPath() {
return path;
}
public void setPrefetchOnGenClass(boolean prefetch) {
super.setPrefetchOnGenClass(prefetch);
if (pcm instanceof RdbGenClassMapping) {
((RdbGenClassMapping) pcm).setPrefetchActivated(prefetch);
}
}
// IMPLEMENTATION OF THE PClassMapping INTERFACE //
//-----------------------------------------------//
public String getProjectName() {
return pcm.getProjectName();
}
public PBinding createPBinding() throws PException {
PBinding pb = pcm.createPBinding();
pb.init(this);
return pb;
}
public void init(PMappingCallback mapper, MetaObject metaclass)
throws PException {
pcm.init(mapper, metaclass);
}
public String getClassName() {
return pcm.getClassName();
}
public PClassMapping getGenClassMapping()
throws UnsupportedOperationException {
return pcm.getGenClassMapping();
}
public PClassMapping getGenClassMapping(String fn)
throws UnsupportedOperationException {
return pcm.getGenClassMapping(fn);
}
public MetaObject getMetaInfo() {
return pcm.getMetaInfo();
}
public PBinder getPBinder() {
return pcm.getPBinder();
}
public PMapper getPMapper() {
return pcm.getPMapper();
}
public PNameCoder getPNameCoder() throws UnsupportedOperationException {
return pcm.getPNameCoder();
}
public PNameCoder getPNameCoder(String fn)
throws UnsupportedOperationException {
return pcm.getPNameCoder(fn);
}
public PNameCoder getClassPNameCoder() {
return pcm.getClassPNameCoder();
}
public PNameIterator getPNameIterator(Object conn, boolean withSubType,
boolean prefetching, Object txctx) throws PException {
return pcm.getPNameIterator(conn, withSubType, prefetching, txctx);
}
public Iterator getPNameIterator(Object conn) throws PException {
return pcm.getPNameIterator(conn, false, false, null);
}
public PType getPType() {
return pcm.getPType();
}
public boolean isConform(String mappername) {
return pcm.isConform(mappername);
}
public void setPBinder(PBinder pb) throws PException {
pcm.setPBinder(pb);
}
public void configureRefFields(ReferenceConfigurator rc) throws PException,
UnsupportedOperationException {
pcm.configureRefFields(rc);
}
public boolean exist(PBinding pb, Object conn) throws PException {
return pcm.exist(pb, conn);
}
public void read(PBinding pb, Object conn, PAccessor pa) throws PException {
pcm.read(pb, conn, pa);
}
public void read(PBinding pb, Object conn, PAccessor pa, Object txctx)
throws PException {
pcm.read(pb, conn, pa, txctx);
}
public void read(PBinding pb, Object conn, PAccessor pa, Object txctx, boolean forUpdate)
throws PException {
pcm.read(pb, conn, pa, txctx, forUpdate);
}
public void write(PBinding pb, Object conn, PAccessor pa) throws PException {
pcm.write(pb, conn, pa);
}
// IMPLEMENTATION OF THE Loggable INTERFACE //
//------------------------------------------//
public Logger getLogger() {
return ((Loggable) pcm).getLogger();
}
public LoggerFactory getLoggerFactory() {
return ((Loggable) pcm).getLoggerFactory();
}
public void setLogger(Logger logger) {
((Loggable) pcm).setLogger(logger);
}
public void setLoggerFactory(LoggerFactory loggerfactory) {
((Loggable) pcm).setLoggerFactory(loggerfactory);
}
public PClassMapping[] getSubPCMs() throws PException {
return pcm.getSubPCMs();
}
public HashMap getAssociationTable() {
return pcm.getAssociationTable();
}
public void addAssociation(PClassMapping targetClass, int[] indexes) {
pcm.addAssociation(targetClass, indexes);
}
public int[] getIndexesTable(PClassMapping targetClass) {
return pcm.getIndexesTable(targetClass);
}
public PName resolve(Object conn, PName pname) throws PException{
return pcm.resolve(conn, pname);
}
public boolean match(Object obj, boolean b) throws PException{
return pcm.match(obj, b);
}
public PName getDecodedPName(TupleCollection tc, PName pname, boolean intermediaryTuple) throws PException {
return pcm.getDecodedPName(tc, pname, intermediaryTuple);
}
public void init(PMappingStructuresManager pmsm) throws PException {
pcm.init(pmsm);
}
public void classDefined(PMappingStructuresManager pmsm) throws PException {
pcm.classDefined(pmsm);
}
}