KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > mapping > parameter > BasicParameterMap


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.ibatis.sqlmap.engine.mapping.parameter;
17
18 import com.ibatis.sqlmap.engine.cache.CacheKey;
19 import com.ibatis.sqlmap.engine.exchange.DataExchange;
20 import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
21 import com.ibatis.sqlmap.engine.scope.ErrorContext;
22 import com.ibatis.sqlmap.engine.scope.RequestScope;
23 import com.ibatis.sqlmap.engine.type.CustomTypeHandler;
24 import com.ibatis.sqlmap.engine.type.JdbcTypeRegistry;
25 import com.ibatis.sqlmap.engine.type.TypeHandler;
26
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.sql.Types JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 public class BasicParameterMap implements ParameterMap {
35
36   private String JavaDoc id;
37   private Class JavaDoc parameterClass;
38
39   private ParameterMapping[] parameterMappings;
40   private DataExchange dataExchange;
41
42   private String JavaDoc resource;
43
44   private Map JavaDoc parameterMappingIndex = new HashMap JavaDoc();
45
46   private SqlMapExecutorDelegate delegate;
47
48   public BasicParameterMap(SqlMapExecutorDelegate delegate) {
49     this.delegate = delegate;
50   }
51
52   public SqlMapExecutorDelegate getDelegate() {
53     return delegate;
54   }
55
56   public String JavaDoc getId() {
57     return id;
58   }
59
60   public void setId(String JavaDoc id) {
61     this.id = id;
62   }
63
64   public Class JavaDoc getParameterClass() {
65     return parameterClass;
66   }
67
68   public void setParameterClass(Class JavaDoc parameterClass) {
69     this.parameterClass = parameterClass;
70   }
71
72   public DataExchange getDataExchange() {
73     return dataExchange;
74   }
75
76   public void setDataExchange(DataExchange dataExchange) {
77     this.dataExchange = dataExchange;
78   }
79
80   public ParameterMapping[] getParameterMappings() {
81     return parameterMappings;
82   }
83
84   public void setParameterMappingList(List JavaDoc parameterMappingList) {
85     this.parameterMappings = (BasicParameterMapping[]) parameterMappingList.toArray(new BasicParameterMapping[parameterMappingList.size()]);
86     for (int i = 0; i < parameterMappings.length; i++) {
87       parameterMappingIndex.put(parameterMappings[i].getPropertyName(), new Integer JavaDoc(i));
88     }
89     Map JavaDoc props = new HashMap JavaDoc();
90     props.put("map", this);
91
92     dataExchange = delegate.getDataExchangeFactory().getDataExchangeForClass(parameterClass);
93     dataExchange.initialize(props);
94   }
95
96   public int getParameterIndex(String JavaDoc propertyName) {
97     Integer JavaDoc idx = null;
98     idx = (Integer JavaDoc) parameterMappingIndex.get(propertyName);
99     return idx == null ? -1 : idx.intValue();
100   }
101
102   public int getParameterCount() {
103     return this.parameterMappings.length;
104   }
105
106   /**
107    * @param ps
108    * @param parameters
109    * @throws java.sql.SQLException
110    */

111   public void setParameters(RequestScope request, PreparedStatement JavaDoc ps, Object JavaDoc[] parameters)
112       throws SQLException JavaDoc {
113
114     ErrorContext errorContext = request.getErrorContext();
115     errorContext.setActivity("applying a parameter map");
116     errorContext.setObjectId(this.getId());
117     errorContext.setResource(this.getResource());
118     errorContext.setMoreInfo("Check the parameter map.");
119
120     if (parameterMappings != null) {
121       for (int i = 0; i < parameterMappings.length; i++) {
122         BasicParameterMapping mapping = (BasicParameterMapping) parameterMappings[i];
123         errorContext.setMoreInfo(mapping.getErrorString());
124         if (mapping.isInputAllowed()) {
125           setParameter(ps, mapping, parameters, i);
126         }
127       }
128     }
129   }
130
131   public Object JavaDoc[] getParameterObjectValues(RequestScope request, Object JavaDoc parameterObject) {
132     return dataExchange.getData(request, this, parameterObject);
133   }
134
135   public CacheKey getCacheKey(RequestScope request, Object JavaDoc parameterObject) {
136     return dataExchange.getCacheKey(request, this, parameterObject);
137   }
138
139   public void refreshParameterObjectValues(RequestScope request, Object JavaDoc parameterObject, Object JavaDoc[] values) {
140     dataExchange.setData(request, this, parameterObject, values);
141   }
142
143   public String JavaDoc getResource() {
144     return resource;
145   }
146
147   public void setResource(String JavaDoc resource) {
148     this.resource = resource;
149   }
150
151   protected void setParameter(PreparedStatement JavaDoc ps, BasicParameterMapping mapping, Object JavaDoc[] parameters, int i) throws SQLException JavaDoc {
152     Object JavaDoc value = parameters[i];
153     // Apply Null Value
154
String JavaDoc nullValueString = mapping.getNullValue();
155     if (nullValueString != null) {
156       TypeHandler handler = mapping.getTypeHandler();
157       if (handler.equals(value, nullValueString)) {
158         value = null;
159       }
160     }
161
162     // Set Parameter
163
TypeHandler typeHandler = mapping.getTypeHandler();
164     if (value != null) {
165       typeHandler.setParameter(ps, i + 1, value, mapping.getJdbcTypeName());
166     } else if (typeHandler instanceof CustomTypeHandler) {
167       typeHandler.setParameter(ps, i + 1, value, mapping.getJdbcTypeName());
168     } else {
169       int jdbcType = mapping.getJdbcType();
170       if (jdbcType != JdbcTypeRegistry.UNKNOWN_TYPE) {
171         ps.setNull(i + 1, jdbcType);
172       } else {
173         ps.setNull(i + 1, Types.OTHER);
174       }
175     }
176   }
177
178 }
179
Popular Tags