01: /**********************************************************************
02: Copyright (c) 2006 Erik Bengtson and others. All rights reserved.
03: Licensed under the Apache License, Version 2.0 (the "License");
04: you may not use this file except in compliance with the License.
05: You may obtain a copy of the License at
06:
07: http://www.apache.org/licenses/LICENSE-2.0
08:
09: Unless required by applicable law or agreed to in writing, software
10: distributed under the License is distributed on an "AS IS" BASIS,
11: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: See the License for the specific language governing permissions and
13: limitations under the License.
14:
15:
16: Contributors:
17: ...
18: **********************************************************************/package org.jpox.store.mapping;
19:
20: import org.jpox.ClassLoaderResolver;
21: import org.jpox.ClassNameConstants;
22: import org.jpox.ObjectManager;
23: import org.jpox.store.expression.QueryExpression;
24: import org.jpox.store.expression.ScalarExpression;
25: import org.jpox.store.expression.StringLiteral;
26: import org.jpox.store.mapping.StringMapping;
27:
28: /**
29: * Mapping for a StringBuffer type.
30: *
31: * Note: A java.lang.StringBuffer is a final class and does not allow a
32: * SCO implementation in order of implementing dirty detecting
33: *
34: * @version $Revision: 1.2 $
35: **/
36: public class StringBufferMapping extends StringMapping {
37: protected static StringBuffer mappingSampleValue = new StringBuffer();
38:
39: public Object getSampleValue(ClassLoaderResolver clr) {
40: return mappingSampleValue;
41: }
42:
43: /**
44: * Accessor for the name of the java-type actually used when mapping the particular datastore
45: * field. This java-type must have an entry in the datastore mappings.
46: * @param index requested datastore field index.
47: * @return the name of java-type for the requested datastore field.
48: */
49: public String getJavaTypeForDatastoreMapping(int index) {
50: // All of the types extending this class will be using java-type of String for the datastore
51: return ClassNameConstants.JAVA_LANG_STRING;
52: }
53:
54: public ScalarExpression newLiteral(QueryExpression qs, Object value) {
55: ScalarExpression expr = new StringLiteral(qs, this ,
56: ((StringBuffer) value).toString());
57: return expr;
58: }
59:
60: /**
61: * Delegates to StringMapping the storage with giving a String
62: */
63: public void setObject(ObjectManager om, Object preparedStatement,
64: int[] exprIndex, Object value) {
65: Object v = value;
66: if (v != null) {
67: v = ((StringBuffer) v).toString();
68: }
69: super .setObject(om, preparedStatement, exprIndex, v);
70: }
71:
72: /**
73: * Delegates to StringMapping the retrieval of a String and constructs
74: * a StringBuffer out of it
75: */
76: public Object getObject(ObjectManager om, Object resultSet,
77: int[] exprIndex) {
78: if (exprIndex == null) {
79: return null;
80: }
81: Object value = getDataStoreMapping(0).getObject(resultSet,
82: exprIndex[0]);
83: if (value != null) {
84: return new StringBuffer(value.toString());
85: }
86: return null;
87: }
88:
89: public Class getJavaType() {
90: return StringBuffer.class;
91: }
92: }
|