KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > bridge > JDBCCMP2xAutoUpdatedFieldBridge


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * 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
25 import org.jboss.deployment.DeploymentException;
26 import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager;
27 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCCMPFieldMetaData;
28 import org.jboss.ejb.EntityEnterpriseContext;
29
30 import java.sql.PreparedStatement JavaDoc;
31
32 /**
33  * The base class for all automatically updated fields such as audit and version.
34  *
35  * @author <a HREF="mailto:alex@jboss.org">Alex Loubyansky</a>
36  * @version $Revision: 37459 $
37  */

38 public abstract class JDBCCMP2xAutoUpdatedFieldBridge extends JDBCCMP2xFieldBridge
39 {
40    // Constructors
41

42    public JDBCCMP2xAutoUpdatedFieldBridge(JDBCStoreManager manager,
43                                           JDBCCMPFieldMetaData metadata)
44       throws DeploymentException
45    {
46       super(manager, metadata);
47       defaultFlags |= JDBCEntityBridge.ADD_TO_SET_ON_UPDATE;
48    }
49
50    public JDBCCMP2xAutoUpdatedFieldBridge(JDBCCMP2xFieldBridge cmpField)
51       throws DeploymentException
52    {
53       super(
54          (JDBCStoreManager) cmpField.getManager(),
55          cmpField.getFieldName(),
56          cmpField.getFieldType(),
57          cmpField.getJDBCType(),
58          cmpField.isReadOnly(), // should always be false?
59
cmpField.getReadTimeOut(),
60          cmpField.getPrimaryKeyClass(),
61          cmpField.getPrimaryKeyField(),
62          cmpField,
63          null, // it should not be a foreign key
64
cmpField.getColumnName()
65       );
66       defaultFlags |= JDBCEntityBridge.ADD_TO_SET_ON_UPDATE; // it should be redundant
67
cmpField.addDefaultFlag(JDBCEntityBridge.ADD_TO_SET_ON_UPDATE);
68    }
69
70    public void initInstance(EntityEnterpriseContext ctx)
71    {
72       setFirstVersion(ctx);
73    }
74
75    public int setInstanceParameters(PreparedStatement JavaDoc ps,
76                                     int parameterIndex,
77                                     EntityEnterpriseContext ctx)
78    {
79       Object JavaDoc value;
80       if(ctx.isValid())
81       {
82          // update
83
// generate new value unless it is already provided by the user
84
value = isDirty(ctx) ? getInstanceValue(ctx) : updateVersion(ctx);
85       }
86       else
87       {
88          // create
89
value = getInstanceValue(ctx);
90       }
91       return setArgumentParameters(ps, parameterIndex, value);
92    }
93
94    public abstract void setFirstVersion(EntityEnterpriseContext ctx);
95    public abstract Object JavaDoc updateVersion(EntityEnterpriseContext ctx);
96 }
97
Popular Tags