1 | /* |
2 | * Copyright 2007 brunella ltd |
3 | * |
4 | * Licensed under the LGPL Version 3 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * |
7 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
8 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
9 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
10 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
11 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
12 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
13 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
14 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
15 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
16 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
17 | * THE POSSIBILITY OF SUCH DAMAGE. |
18 | */ |
19 | package sf.qof.mapping; |
20 | |
21 | import java.lang.reflect.Constructor; |
22 | import java.lang.reflect.Method; |
23 | |
24 | public abstract class AbstractBaseMapping implements ParameterMapping, ResultMapping { |
25 | // SQL mappings |
26 | protected int[] sqlIndexes; |
27 | protected String[] sqlColumns; |
28 | // Java mappings |
29 | protected int index; |
30 | protected Class<?> type; |
31 | protected Class<?> collectionType; |
32 | protected Class<?> mapKeyType; |
33 | protected Class<?> beanType; |
34 | protected Method method; |
35 | protected MappingAdapter adapter; |
36 | protected Integer constructorParameter; |
37 | protected Constructor<?> constructor; |
38 | protected boolean usesArray; |
39 | |
40 | // implements ParameterMapping |
41 | public void setParameters(int index, Class<?> type, Class<?> collectionType, Class<?> beanType, Method getter, |
42 | int[] sqlIndexes, String[] sqlColumns, MappingAdapter adapter, boolean usesArray) { |
43 | this.index = index; |
44 | this.type = type; |
45 | this.collectionType = collectionType; |
46 | this.beanType = beanType; |
47 | this.method = getter; |
48 | this.sqlIndexes = sqlIndexes; |
49 | this.sqlColumns = sqlColumns; |
50 | this.adapter = adapter; |
51 | this.usesArray = usesArray; |
52 | } |
53 | |
54 | // implements ResultMapping |
55 | public void setParameters(Class<?> type, Class<?> collectionType, Class<?> beanType, Method setter, int[] sqlIndexes, |
56 | String[] sqlColumns, MappingAdapter adapter, Class<?> mapKeyType, Integer constructorParameter, Constructor<?> constructor) { |
57 | this.index = -1; |
58 | this.type = type; |
59 | this.collectionType = collectionType; |
60 | this.beanType = beanType; |
61 | this.method = setter; |
62 | this.sqlIndexes = sqlIndexes; |
63 | this.sqlColumns = sqlColumns; |
64 | this.adapter = adapter; |
65 | this.mapKeyType = mapKeyType; |
66 | this.constructorParameter = constructorParameter; |
67 | this.constructor = constructor; |
68 | } |
69 | |
70 | public Class<?> getBeanType() { |
71 | return beanType; |
72 | } |
73 | |
74 | public Class<?> getCollectionType() { |
75 | return collectionType; |
76 | } |
77 | |
78 | public int getIndex() { |
79 | return index; |
80 | } |
81 | |
82 | public Method getGetter() { |
83 | return method; |
84 | } |
85 | |
86 | public Method getSetter() { |
87 | return method; |
88 | } |
89 | |
90 | public String[] getSqlColumns() { |
91 | return sqlColumns; |
92 | } |
93 | |
94 | public int[] getSqlIndexes() { |
95 | return sqlIndexes; |
96 | } |
97 | |
98 | public Class<?> getType() { |
99 | return type; |
100 | } |
101 | |
102 | public MappingAdapter getAdapter() { |
103 | return adapter; |
104 | } |
105 | |
106 | public boolean usesCollection() { |
107 | return collectionType != null; |
108 | } |
109 | |
110 | public boolean usesAtomic() { |
111 | return beanType == null && constructor == null; |
112 | } |
113 | |
114 | public boolean isMapKey() { |
115 | return mapKeyType != null; |
116 | } |
117 | |
118 | public Class<?> getMapKeyType() { |
119 | return mapKeyType; |
120 | } |
121 | |
122 | public Integer getConstructorParameter() { |
123 | return constructorParameter; |
124 | } |
125 | |
126 | public Constructor<?> getConstructor() { |
127 | return constructor; |
128 | } |
129 | |
130 | public boolean usesArray() { |
131 | return usesArray; |
132 | } |
133 | |
134 | private String getSqlColumnsString() { |
135 | if (sqlColumns != null) { |
136 | StringBuffer sb = new StringBuffer(); |
137 | sb.append('['); |
138 | for (int i = 0; i < sqlColumns.length; i++) { |
139 | if (i > 0) { |
140 | sb.append(','); |
141 | } |
142 | sb.append('"').append(sqlColumns[i]).append('"'); |
143 | } |
144 | sb.append(']'); |
145 | return sb.toString(); |
146 | } else { |
147 | return ""; |
148 | } |
149 | } |
150 | |
151 | private String getSqlIndexesString() { |
152 | if (sqlIndexes != null) { |
153 | StringBuffer sb = new StringBuffer(); |
154 | sb.append('['); |
155 | for (int i = 0; i < sqlIndexes.length; i++) { |
156 | if (i > 0) { |
157 | sb.append(','); |
158 | } |
159 | sb.append(sqlIndexes[i]); |
160 | } |
161 | sb.append(']'); |
162 | return sb.toString(); |
163 | } else { |
164 | return ""; |
165 | } |
166 | } |
167 | |
168 | private String stripType(Class<?> type) { |
169 | String typeName = type.getName(); |
170 | typeName = typeName.replace("java.lang.", ""); |
171 | typeName = typeName.replace("java.util.", ""); |
172 | typeName = typeName.replace("sf.qof.mapping.AbstractNumberMapping$", ""); |
173 | typeName = typeName.replace("sf.qof.mapping.AbstractCharacterMapping$", ""); |
174 | typeName = typeName.replace("sf.qof.mapping.AbstractDateTimeMapping$", ""); |
175 | return typeName; |
176 | } |
177 | |
178 | public String parameterMappingInfo() { |
179 | StringBuffer sb = new StringBuffer(); |
180 | sb.append("Parameter: ").append(stripType(this.getClass())).append('\n'); |
181 | sb.append('\t').append(getSqlColumnsString()).append(getSqlIndexesString()); |
182 | sb.append(" = parameter ").append(index).append(' '); |
183 | if (method != null) { |
184 | sb.append(method); |
185 | } else { |
186 | sb.append(stripType(type)); |
187 | } |
188 | if (adapter != null) { |
189 | sb.append("\tuse adapter ").append(adapter.getClass()); |
190 | } |
191 | |
192 | return sb.toString(); |
193 | } |
194 | |
195 | public String resultMappingInfo() { |
196 | StringBuffer sb = new StringBuffer(); |
197 | sb.append("Result: ").append(stripType(this.getClass())).append('\n'); |
198 | sb.append('\t').append(getSqlColumnsString()).append(getSqlIndexesString()); |
199 | sb.append(" => "); |
200 | if (method != null) { |
201 | sb.append(method); |
202 | } else { |
203 | sb.append(stripType(type)); |
204 | } |
205 | if (adapter != null) { |
206 | sb.append("\n\tuse adapter ").append(adapter.getClass()); |
207 | } |
208 | if (constructorParameter != null) { |
209 | sb.append("\tuse constructor ").append(constructorParameter); |
210 | } |
211 | |
212 | return sb.toString(); |
213 | } |
214 | } |