001: /**********************************************************************
002: Copyright (c) 2003 Erik Bengtson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: 2003 Andy Jefferson - coding standards
017: 2006 Michael Brown - updated to cater for alternative types in the PK fields
018: ...
019: **********************************************************************/package org.jpox.store.mapping;
020:
021: import javax.jdo.spi.PersistenceCapable;
022: import javax.jdo.spi.PersistenceCapable.ObjectIdFieldConsumer;
023:
024: import org.jpox.ObjectManager;
025: import org.jpox.util.Localiser;
026:
027: /**
028: * Parameter setter class to deal with application identity.
029: * Parameters must be in the same order as the fields.
030: *
031: * @version $Revision: 1.8 $
032: */
033: class AppIDObjectIdFieldConsumer implements ObjectIdFieldConsumer {
034: /** Localisation of messages. */
035: protected static final Localiser LOCALISER = Localiser
036: .getInstance("org.jpox.store.Localisation");
037:
038: /** Parameter positions in the PreparedStatement. */
039: private int[] params;
040:
041: /** Next parameter index. Increased each call. */
042: private int nextParam;
043:
044: /** The Object Manager */
045: private ObjectManager om;
046:
047: /** The PreparedStatement to populate. */
048: private Object statement;
049:
050: /** The mappings for the object id */
051: private JavaTypeMapping[] javaTypeMappings;
052:
053: /** Number of the mapping being processed. */
054: private int mappingNum = 0;
055:
056: /**
057: * Constructor.
058: * @param param Parameter positions
059: * @param om ObjectManager
060: * @param statement PreparedStatement
061: * @param javaTypeMappings Java mappings for the PC object
062: */
063: public AppIDObjectIdFieldConsumer(int[] param, ObjectManager om,
064: Object statement, JavaTypeMapping[] javaTypeMappings) {
065: this .params = param;
066: nextParam = 0;
067: this .om = om;
068: this .statement = statement;
069:
070: // Save all mappings in the same order, allowing for PersistenceCapableMapping having sub mappings
071: int numMappings = 0;
072: for (int i = 0; i < javaTypeMappings.length; i++) {
073: if (javaTypeMappings[i] instanceof PersistenceCapableMapping) {
074: numMappings += ((PersistenceCapableMapping) javaTypeMappings[i])
075: .getJavaTypeMapping().length;
076: } else {
077: numMappings++;
078: }
079: }
080: this .javaTypeMappings = new JavaTypeMapping[numMappings];
081: int mappingNum = 0;
082: for (int i = 0; i < javaTypeMappings.length; i++) {
083: if (javaTypeMappings[i] instanceof PersistenceCapableMapping) {
084: PersistenceCapableMapping m = (PersistenceCapableMapping) javaTypeMappings[i];
085: JavaTypeMapping[] subMappings = m.javaTypeMappings;
086: for (int j = 0; j < subMappings.length; j++) {
087: this .javaTypeMappings[mappingNum++] = subMappings[j];
088: }
089: } else {
090: this .javaTypeMappings[mappingNum++] = javaTypeMappings[i];
091: }
092: }
093: }
094:
095: /**
096: * Convenience method to return the statement param position(s) for a field.
097: * @param mapping The mapping
098: * @return The param positions
099: */
100: private int[] getParamsForField(JavaTypeMapping mapping) {
101: if (javaTypeMappings.length == 1) {
102: return params;
103: } else {
104: int numCols = mapping.getNumberOfDatastoreFields();
105: int[] fieldParams = new int[numCols];
106: for (int i = 0; i < numCols; i++) {
107: fieldParams[i] = params[nextParam++];
108: }
109: return fieldParams;
110: }
111: }
112:
113: /**
114: * Method to store a boolean in a field.
115: * @param fieldNumber Number of the field
116: * @param value The value to use
117: */
118: public void storeBooleanField(int fieldNumber, boolean value) {
119: JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
120: mapping.setBoolean(om, statement, getParamsForField(mapping),
121: value);
122: }
123:
124: /**
125: * Method to store a byte in a field.
126: * @param fieldNumber Number of the field
127: * @param value The value to use
128: */
129: public void storeByteField(int fieldNumber, byte value) {
130: JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
131: mapping.setByte(om, statement, getParamsForField(mapping),
132: value);
133: }
134:
135: /**
136: * Method to store a character in a field.
137: * @param fieldNumber Number of the field
138: * @param value The value to use
139: */
140: public void storeCharField(int fieldNumber, char value) {
141: JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
142: mapping.setChar(om, statement, getParamsForField(mapping),
143: value);
144: }
145:
146: /**
147: * Method to store a double in a field.
148: * @param fieldNumber Number of the field
149: * @param value The value to use
150: */
151: public void storeDoubleField(int fieldNumber, double value) {
152: JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
153: mapping.setDouble(om, statement, getParamsForField(mapping),
154: value);
155: }
156:
157: /**
158: * Method to store a float in a field.
159: * @param fieldNumber Number of the field
160: * @param value The value to use
161: */
162: public void storeFloatField(int fieldNumber, float value) {
163: JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
164: mapping.setFloat(om, statement, getParamsForField(mapping),
165: value);
166: }
167:
168: /**
169: * Method to store an integer in a field.
170: * @param fieldNumber Number of the field
171: * @param value The value to use
172: */
173: public void storeIntField(int fieldNumber, int value) {
174: JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
175: mapping
176: .setInt(om, statement, getParamsForField(mapping),
177: value);
178: }
179:
180: /**
181: * Method to store a long in a field.
182: * @param fieldNumber Number of the field
183: * @param value The value to use
184: */
185: public void storeLongField(int fieldNumber, long value) {
186: JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
187: mapping.setLong(om, statement, getParamsForField(mapping),
188: value);
189: }
190:
191: /**
192: * Method to store a short in a field.
193: * @param fieldNumber Number of the field
194: * @param value The value to use
195: */
196: public void storeShortField(int fieldNumber, short value) {
197: JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
198: mapping.setShort(om, statement, getParamsForField(mapping),
199: value);
200: }
201:
202: /**
203: * Method to store a String in a field.
204: * @param fieldNumber Number of the field
205: * @param value The value to use
206: */
207: public void storeStringField(int fieldNumber, String value) {
208: JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
209: mapping.setString(om, statement, getParamsForField(mapping),
210: value);
211: }
212:
213: /**
214: * Method to store an object in a field.
215: * @param fieldNumber Number of the field
216: * @param value The value to use
217: */
218: public void storeObjectField(int fieldNumber, Object value) {
219: if (om.getApiAdapter().isPersistable(value)) {
220: PersistenceCapable pc = (PersistenceCapable) value;
221: pc.jdoCopyKeyFieldsFromObjectId(this , pc.jdoGetObjectId());
222: } else {
223: JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
224: mapping.setObject(om, statement,
225: getParamsForField(mapping), value);
226: }
227: }
228: }
|