001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.jdbc2;
023:
024: import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCEntityBridge2;
025: import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCCMRFieldBridge2;
026: import org.jboss.ejb.plugins.cmp.jdbc2.schema.EntityTable;
027: import org.jboss.ejb.plugins.cmp.jdbc2.schema.Cache;
028: import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCAbstractCMRFieldBridge;
029:
030: import javax.ejb.DuplicateKeyException;
031: import java.sql.SQLException;
032:
033: /**
034: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
035: * @version <tt>$Revision: 57209 $</tt>
036: */
037: public class PersistentContext {
038: private final EntityTable.Row row;
039: private final JDBCCMRFieldBridge2.FieldState[] cmrStates;
040:
041: public PersistentContext(JDBCEntityBridge2 entity,
042: EntityTable.Row row) {
043: this .row = row;
044:
045: JDBCAbstractCMRFieldBridge[] cmrFields = entity.getCMRFields();
046: if (cmrFields != null) {
047: cmrStates = new JDBCCMRFieldBridge2.FieldState[cmrFields.length];
048: } else {
049: cmrStates = null;
050: }
051: }
052:
053: public Object getFieldValue(int rowIndex) {
054: return row.getFieldValue(rowIndex);
055: }
056:
057: public void setFieldValue(int rowIndex, Object value) {
058: row.setFieldValue(rowIndex, value);
059: }
060:
061: public void setPk(Object pk) throws DuplicateKeyException {
062: if (pk == null) {
063: throw new IllegalArgumentException("Primary key is null!");
064: }
065:
066: row.insert(pk);
067: }
068:
069: public boolean isDirty() {
070: return row.isDirty();
071: }
072:
073: public void setDirty() {
074: row.setDirty();
075: }
076:
077: public void setDirtyRelations() {
078: row.setDirtyRelations();
079: }
080:
081: public void remove() {
082: row.delete();
083: }
084:
085: public JDBCCMRFieldBridge2.FieldState getCMRState(int cmrIndex) {
086: return cmrStates[cmrIndex];
087: }
088:
089: public void setCMRState(int cmrIndex,
090: JDBCCMRFieldBridge2.FieldState state) {
091: cmrStates[cmrIndex] = state;
092: }
093:
094: public void loadCachedRelations(int cmrIndex,
095: Cache.CacheLoader loader) {
096: row.loadCachedRelations(cmrIndex, loader);
097: }
098:
099: public void cacheRelations(int cmrIndex, Cache.CacheLoader loader) {
100: row.cacheRelations(cmrIndex, loader);
101: }
102:
103: public void flush() throws SQLException, DuplicateKeyException {
104: row.flush();
105: }
106:
107: public void nullForeignKey(
108: EntityTable.ForeignKeyConstraint constraint) {
109: row.nullForeignKey(constraint);
110: }
111:
112: public void nonNullForeignKey(
113: EntityTable.ForeignKeyConstraint constraint) {
114: row.nonNullForeignKey(constraint);
115: }
116: }
|