01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.ejb.plugins.cmp.jdbc.bridge;
23:
24: import org.jboss.deployment.DeploymentException;
25: import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager;
26: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCCMPFieldMetaData;
27: import org.jboss.ejb.EntityEnterpriseContext;
28:
29: import java.sql.PreparedStatement;
30:
31: /**
32: * The base class for all automatically updated fields such as audit and version.
33: *
34: * @author <a href="mailto:alex@jboss.org">Alex Loubyansky</a>
35: * @version $Revision: 57209 $
36: */
37: public abstract class JDBCCMP2xAutoUpdatedFieldBridge extends
38: JDBCCMP2xFieldBridge {
39: // Constructors
40:
41: public JDBCCMP2xAutoUpdatedFieldBridge(JDBCStoreManager manager,
42: JDBCCMPFieldMetaData metadata) throws DeploymentException {
43: super (manager, metadata);
44: defaultFlags |= JDBCEntityBridge.ADD_TO_SET_ON_UPDATE;
45: }
46:
47: public JDBCCMP2xAutoUpdatedFieldBridge(JDBCCMP2xFieldBridge cmpField)
48: throws DeploymentException {
49: super ((JDBCStoreManager) cmpField.getManager(), cmpField
50: .getFieldName(), cmpField.getFieldType(), cmpField
51: .getJDBCType(), cmpField.isReadOnly(), // should always be false?
52: cmpField.getReadTimeOut(), cmpField
53: .getPrimaryKeyClass(), cmpField
54: .getPrimaryKeyField(), cmpField, null, // it should not be a foreign key
55: cmpField.getColumnName());
56: defaultFlags |= JDBCEntityBridge.ADD_TO_SET_ON_UPDATE; // it should be redundant
57: cmpField.addDefaultFlag(JDBCEntityBridge.ADD_TO_SET_ON_UPDATE);
58: }
59:
60: public void initInstance(EntityEnterpriseContext ctx) {
61: setFirstVersion(ctx);
62: }
63:
64: public int setInstanceParameters(PreparedStatement ps,
65: int parameterIndex, EntityEnterpriseContext ctx) {
66: Object value;
67: if (ctx.isValid()) {
68: // update
69: // generate new value unless it is already provided by the user
70: value = isDirty(ctx) ? getInstanceValue(ctx)
71: : updateVersion(ctx);
72: } else {
73: // create
74: value = getInstanceValue(ctx);
75: }
76: return setArgumentParameters(ps, parameterIndex, value);
77: }
78:
79: public abstract void setFirstVersion(EntityEnterpriseContext ctx);
80:
81: public abstract Object updateVersion(EntityEnterpriseContext ctx);
82: }
|