KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > types > AbstractType


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.access.types;
21
22 import java.sql.CallableStatement JavaDoc;
23 import java.sql.PreparedStatement JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25
26 import org.apache.commons.lang.builder.ToStringBuilder;
27 import org.apache.cayenne.map.DbAttribute;
28 import org.apache.cayenne.validation.BeanValidationFailure;
29 import org.apache.cayenne.validation.ValidationResult;
30
31 /**
32  * A convenience superclass of ExtendedType implementations. Implements
33  * {@link #setJdbcObject(PreparedStatement, Object, int, int, int)}in a generic fashion
34  * by calling "setObject(..)" on PreparedStatement. Some adapters may need to override
35  * this behavior as it doesn't work consistently across all JDBC drivers.
36  *
37  * @author Andrus Adamchik
38  */

39 public abstract class AbstractType implements ExtendedType {
40
41     /**
42      * Helper method for ExtendedType implementors to check for null required values.
43      *
44      * @since 1.2
45      * @deprecated since 3.0 as validation should not be done at the DataNode level.
46      */

47     public static boolean validateNull(
48             Object JavaDoc source,
49             String JavaDoc property,
50             Object JavaDoc value,
51             DbAttribute dbAttribute,
52             ValidationResult validationResult) {
53         if (dbAttribute.isMandatory() && value == null) {
54             validationResult.addFailure(new BeanValidationFailure(source, property, "'"
55                     + property
56                     + "' must be not null"));
57             return false;
58         }
59
60         return true;
61     }
62
63     /**
64      * Calls "PreparedStatement.setObject(..)". Some DbAdapters may need to override this
65      * behavior for at least some of the object types, as it doesn't work consistently
66      * across all JDBC drivers.
67      */

68     public void setJdbcObject(
69             PreparedStatement JavaDoc st,
70             Object JavaDoc val,
71             int pos,
72             int type,
73             int precision) throws Exception JavaDoc {
74
75         if (precision != -1) {
76             st.setObject(pos, val, type, precision);
77         }
78         else {
79             st.setObject(pos, val, type);
80         }
81     }
82
83     public abstract String JavaDoc getClassName();
84
85     public abstract Object JavaDoc materializeObject(CallableStatement JavaDoc rs, int index, int type)
86             throws Exception JavaDoc;
87
88     public abstract Object JavaDoc materializeObject(ResultSet JavaDoc rs, int index, int type)
89             throws Exception JavaDoc;
90
91     /**
92      * Always returns true. Simplifies subclass implementation, as only some of the types
93      * can perform the validation.
94      *
95      * @deprecated since 3.0 as validation should not be done at the DataNode level.
96      */

97     public boolean validateProperty(
98             Object JavaDoc source,
99             String JavaDoc property,
100             Object JavaDoc value,
101             DbAttribute dbAttribute,
102             ValidationResult validationResult) {
103         return true;
104     }
105
106     public String JavaDoc toString() {
107         return new ToStringBuilder(this).append("className", getClassName()).toString();
108     }
109
110 }
111
Popular Tags