001: /**
002: * Copyright (C) 2001-2005 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.genclass;
018:
019: import org.objectweb.jorm.api.PAccessor;
020: import org.objectweb.jorm.api.PBinding;
021: import org.objectweb.jorm.api.PClassMapping;
022: import org.objectweb.jorm.api.PException;
023: import org.objectweb.jorm.api.PMapper;
024: import org.objectweb.jorm.api.PMappingCallback;
025: import org.objectweb.jorm.api.PMappingStructuresManager;
026: import org.objectweb.jorm.api.PNameIterator;
027: import org.objectweb.jorm.mapper.rdb.genclass.RdbGenClassMapping;
028: import org.objectweb.jorm.metainfo.api.MetaObject;
029: import org.objectweb.jorm.naming.api.PBinder;
030: import org.objectweb.jorm.naming.api.PName;
031: import org.objectweb.jorm.naming.api.PNameCoder;
032: import org.objectweb.jorm.type.api.PType;
033: import org.objectweb.jorm.util.api.Loggable;
034: import org.objectweb.medor.tuple.api.TupleCollection;
035: import org.objectweb.perseus.persistence.api.TransactionalPersistenceManager;
036: import org.objectweb.speedo.api.SpeedoRuntimeException;
037: import org.objectweb.speedo.genclass.api.SpeedoGenClassPO;
038: import org.objectweb.speedo.lib.Personality;
039: import org.objectweb.speedo.mim.api.DetachedLifeCycle;
040: import org.objectweb.speedo.mim.api.PersistentObjectItf;
041: import org.objectweb.speedo.mim.lib.AbstractHomeImpl;
042: import org.objectweb.speedo.pm.api.POManagerFactoryItf;
043: import org.objectweb.speedo.pm.api.POManagerItf;
044: import org.objectweb.util.monolog.api.Logger;
045: import org.objectweb.util.monolog.api.LoggerFactory;
046:
047: import java.util.HashMap;
048: import java.util.Iterator;
049: import java.util.Map;
050: import java.util.Properties;
051:
052: /**
053: * This implementation of HomeItf delegated the PClassMapping roles to
054: * another instance. This implementation is dedicated to GenClass (Collection,
055: * List, Vector, Map, ...).
056: *
057: * @author S.Chassande-Barrioz
058: */
059: public abstract class AbstractGenClassHome extends AbstractHomeImpl {
060: /**
061: * The real PClassMapping instance.
062: */
063: PClassMapping pcm;
064: /**
065: * The path of the genclass (source_class_name#gc_field_name)
066: */
067: String path;
068:
069: public AbstractGenClassHome(Personality p) {
070: super (p);
071: }
072:
073: public void init(PClassMapping _pcm,
074: TransactionalPersistenceManager _tpm,
075: POManagerFactoryItf _pmf, String p) {
076: this .tpm = _tpm;
077: this .pmf = _pmf;
078: this .pcm = _pcm;
079: this .path = p;
080: }
081:
082: public PClassMapping getRealPClassMapping() {
083: return pcm;
084: }
085:
086: public void makePersistent(POManagerItf pm, PersistentObjectItf sp,
087: SpeedoGenClassPO thepo, Map map) {
088: if (!sp.speedoIsActive()) {
089: if (sp instanceof SpeedoGenClassPO) {
090: //Genclass contains genclass instances
091: SpeedoGenClassPO sgcp = (SpeedoGenClassPO) sp;
092: if (sgcp.speedoGetPType() == null) {
093: //Assign the PType to the generic class
094: sgcp.speedoSetPType(thepo.speedoGetPType()
095: .getNestedPType());
096: }
097: sgcp.speedoSetLinkedField(thepo.speedoGetGenClassId());
098: sgcp.speedoSetPNameHints(thepo.getPName());
099: }
100: POManagerItf mypm = pm;
101: if (mypm == null) {
102: mypm = thepo.speedoGetPOManager();
103: if (mypm == null) {
104: throw new SpeedoRuntimeException(
105: "When a persistent object is used (read or write), "
106: + "a PersistenceManager is needed");
107: }
108: }
109: mypm.speedoMakePersistent(sp, map);
110: }
111: }
112:
113: public void makePersistent(POManagerItf pm, Iterator it,
114: SpeedoGenClassPO thepo, Map map) {
115: if (it == null) {
116: return;
117: }
118: while (it.hasNext()) {
119: Object o = it.next();
120: if (o instanceof PersistentObjectItf) {
121: //if detached, attach it
122: if (((PersistentObjectItf) o).speedoGetReferenceState()
123: .getDetachedStatus() != DetachedLifeCycle.DETACHED_NONE) {
124: o = pm.speedoAttachCopy(o, map);
125: } else {
126: //else make it persistent
127: makePersistent(pm, (PersistentObjectItf) o, thepo,
128: map);
129: }
130: } else {
131: // Optimisation: If the first element is not a PersistentObjectItf, that
132: // means that ALL collection elements are not PersistentObjectItf
133: // implementation
134: return;
135: }
136: }
137: }
138:
139: // IMPLEMENTATION OF THE HomeItf INTERFACE //
140: //--------------------------------------------//
141:
142: public boolean isDetachable() {
143: return false;
144: }
145:
146: public byte getVersioningStrategy() {
147: return 0;
148: }
149:
150: public Properties getClassProperties() {
151: return null;
152: }
153:
154: public String getPath() {
155: return path;
156: }
157:
158: public void setPrefetchOnGenClass(boolean prefetch) {
159: super .setPrefetchOnGenClass(prefetch);
160: if (pcm instanceof RdbGenClassMapping) {
161: ((RdbGenClassMapping) pcm).setPrefetchActivated(prefetch);
162: }
163: }
164:
165: // IMPLEMENTATION OF THE PClassMapping INTERFACE //
166: //-----------------------------------------------//
167:
168: public String getProjectName() {
169: return pcm.getProjectName();
170: }
171:
172: public PBinding createPBinding() throws PException {
173: PBinding pb = pcm.createPBinding();
174: pb.init(this );
175: return pb;
176:
177: }
178:
179: public void init(PMappingCallback mapper, MetaObject metaclass)
180: throws PException {
181: pcm.init(mapper, metaclass);
182: }
183:
184: public String getClassName() {
185: return pcm.getClassName();
186: }
187:
188: public PClassMapping getGenClassMapping()
189: throws UnsupportedOperationException {
190: return pcm.getGenClassMapping();
191: }
192:
193: public PClassMapping getGenClassMapping(String fn)
194: throws UnsupportedOperationException {
195: return pcm.getGenClassMapping(fn);
196: }
197:
198: public MetaObject getMetaInfo() {
199: return pcm.getMetaInfo();
200: }
201:
202: public PBinder getPBinder() {
203: return pcm.getPBinder();
204: }
205:
206: public PMapper getPMapper() {
207: return pcm.getPMapper();
208: }
209:
210: public PNameCoder getPNameCoder()
211: throws UnsupportedOperationException {
212: return pcm.getPNameCoder();
213: }
214:
215: public PNameCoder getPNameCoder(String fn)
216: throws UnsupportedOperationException {
217: return pcm.getPNameCoder(fn);
218: }
219:
220: public PNameCoder getClassPNameCoder() {
221: return pcm.getClassPNameCoder();
222: }
223:
224: public PNameIterator getPNameIterator(Object conn,
225: boolean withSubType, boolean prefetching, Object txctx)
226: throws PException {
227: return pcm.getPNameIterator(conn, withSubType, prefetching,
228: txctx);
229: }
230:
231: public Iterator getPNameIterator(Object conn) throws PException {
232: return pcm.getPNameIterator(conn, false, false, null);
233: }
234:
235: public PType getPType() {
236: return pcm.getPType();
237: }
238:
239: public boolean isConform(String mappername) {
240: return pcm.isConform(mappername);
241: }
242:
243: public void setPBinder(PBinder pb) throws PException {
244: pcm.setPBinder(pb);
245: }
246:
247: public void configureRefFields(ReferenceConfigurator rc)
248: throws PException, UnsupportedOperationException {
249: pcm.configureRefFields(rc);
250: }
251:
252: public boolean exist(PBinding pb, Object conn) throws PException {
253: return pcm.exist(pb, conn);
254: }
255:
256: public void read(PBinding pb, Object conn, PAccessor pa)
257: throws PException {
258: pcm.read(pb, conn, pa);
259: }
260:
261: public void read(PBinding pb, Object conn, PAccessor pa,
262: Object txctx) throws PException {
263: pcm.read(pb, conn, pa, txctx);
264: }
265:
266: public void read(PBinding pb, Object conn, PAccessor pa,
267: Object txctx, boolean forUpdate) throws PException {
268: pcm.read(pb, conn, pa, txctx, forUpdate);
269: }
270:
271: public void write(PBinding pb, Object conn, PAccessor pa)
272: throws PException {
273: pcm.write(pb, conn, pa);
274: }
275:
276: // IMPLEMENTATION OF THE Loggable INTERFACE //
277: //------------------------------------------//
278:
279: public Logger getLogger() {
280: return ((Loggable) pcm).getLogger();
281: }
282:
283: public LoggerFactory getLoggerFactory() {
284: return ((Loggable) pcm).getLoggerFactory();
285: }
286:
287: public void setLogger(Logger logger) {
288: ((Loggable) pcm).setLogger(logger);
289: }
290:
291: public void setLoggerFactory(LoggerFactory loggerfactory) {
292: ((Loggable) pcm).setLoggerFactory(loggerfactory);
293: }
294:
295: public PClassMapping[] getSubPCMs() throws PException {
296: return pcm.getSubPCMs();
297: }
298:
299: public HashMap getAssociationTable() {
300: return pcm.getAssociationTable();
301: }
302:
303: public void addAssociation(PClassMapping targetClass, int[] indexes) {
304: pcm.addAssociation(targetClass, indexes);
305: }
306:
307: public int[] getIndexesTable(PClassMapping targetClass) {
308: return pcm.getIndexesTable(targetClass);
309: }
310:
311: public PName resolve(Object conn, PName pname) throws PException {
312: return pcm.resolve(conn, pname);
313: }
314:
315: public boolean match(Object obj, boolean b) throws PException {
316: return pcm.match(obj, b);
317: }
318:
319: public PName getDecodedPName(TupleCollection tc, PName pname,
320: boolean intermediaryTuple) throws PException {
321: return pcm.getDecodedPName(tc, pname, intermediaryTuple);
322: }
323:
324: public void init(PMappingStructuresManager pmsm) throws PException {
325: pcm.init(pmsm);
326: }
327:
328: public void classDefined(PMappingStructuresManager pmsm)
329: throws PException {
330: pcm.classDefined(pmsm);
331: }
332: }
|