Clover coverage report - JFox Service-Oriented Application Framework - 1.0-RC3 Coverage timestamp: 星期三 二月 15 2006 18:10:22 CST FRAMES NO FRAMES |
file stats: | LOC: | 481 |  | Methods: | 19
| NCLOC: | 266 |  | Classes: | 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
Source file | Conditionals | Statements | Methods | TOTAL
IbatisServiceImpl.java | 0% | 5.5% | 15.8% | 6.6% |

1 |  |
     /**
2 |  |
      * @(#)IbatisServiceImpl.java
3 |  |
      *
4 |  |
      * JFoxSOAF, Service-Oriented Application Framework
5 |  |
      *
6 |  |
      * Copyright(c) JFoxSOAF Team
7 |  |
      *
8 |  |
      * Licensed under the GNU LGPL, Version 2.1 (the "License");
9 |  |
      * you may not use this file except in compliance with the License.
10 |  |
      * You may obtain a copy of the License at
11 |  |
      *
12 |  |
      * http://www.gnu.org/copyleft/lesser.html
13 |  |
      *
14 |  |
      * Unless required by applicable law or agreed to in writing, software
15 |  |
      * distributed under the License is distributed on an "AS IS" BASIS,
16 |  |
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 |  |
      * See the License for the specific language governing permissions and
18 |  |
      * limitations under the License.
19 |  |
      *
20 |  |
      * For more information, please visit:
21 |  |
      * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
22 |  |
      * http://www.huihoo.org/jfox/jfoxsoaf
23 |  |
      */
24 |  |
25 |  |
     package org.huihoo.jfox.soaf.services.persistence;
26 |  |
27 |  |
     import java.io.Reader;
28 |  |
     import java.sql.SQLException;
29 |  |
     import java.util.List;
30 |  |
     import java.util.Map;
31 |  |
32 |  |
     import org.apache.commons.logging.Log;
33 |  |
     import org.apache.commons.logging.LogFactory;
34 |  |
     import org.huihoo.jfox.soaf.exception.DAOException;
35 |  |
     import org.huihoo.jfox.soaf.exception.IbatisConfigurationException;
36 |  |
     import org.picocontainer.Startable;
37 |  |
38 |  |
     import com.ibatis.common.resources.Resources;
39 |  |
     import com.ibatis.common.util.PaginatedList;
40 |  |
     import com.ibatis.dao.client.DaoManager;
41 |  |
     import com.ibatis.dao.client.DaoManagerBuilder;
42 |  |
     import com.ibatis.sqlmap.client.SqlMapClient;
43 |  |
     import com.ibatis.sqlmap.client.SqlMapClientBuilder;
44 |  |
     import com.ibatis.sqlmap.client.SqlMapSession;
45 |  |
     import com.ibatis.sqlmap.client.event.RowHandler;
46 |  |
47 |  |
     /**
48 |  |
      * <p>
49 |  |
      * Ibatis persistence service wrapper.
50 |  |
      * </p>
51 |  |
      *
52 |  |
      * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
53 |  |
      * @version $Revision: 1.7 $ $Date: 2006/02/15 08:45:44 $
54 |  |
      * @version Revision: 1.0
55 |  |
      */
56 |  |
57 |  |
     public class IbatisServiceImpl implements IbatisService, Startable {
58 |  |
59 |  |
         private final Log logger = LogFactory.getLog(getClass());
60 |  |
61 |  |
         private static final String IBATIS_DAO_RESOURCE = "ibatis-dao.xml";
62 |  |
63 |  |
         private static final String IBATIS_SQLMAP_RESOURCE = "ibatis-sqlmap-config.xml";
64 |  |
65 |  |
         private DaoManager daoManager;
66 |  |
67 |  |
         private SqlMapClient sqlMapClient;
68 |  |
69 |  |
         private SqlMapSession session;
70 |  |
71 |  |
         /**
72 |  |
          * Defalut Constructor.
73 |  |
          */
74 | 10 |
         public IbatisServiceImpl() {
75 |  |
         }
76 |  |
77 |  |
         /**
78 |  |
          * @see org.huihoo.jfox.soaf.services.persistence.IbatisService#getDaoManager()
79 |  |
          */
80 | 1 |
         public DaoManager getDaoManager() {
81 | 1 |
             return this.daoManager;
82 |  |
         }
83 |  |
84 | 0 |
         private SqlMapClient getSqlMapClient() {
85 | 0 |
             return this.sqlMapClient;
86 |  |
         }
87 |  |
88 |  |
         /**
89 |  |
          * Returns a single threaded SqlMapSession implementation for use by one
90 |  |
          * user.
91 |  |
          *
92 |  |
          * @see org.huihoo.jfox.soaf.services.persistence.IbatisService#openSession()
93 |  |
          */
94 | 0 |
         public SqlMapSession openSession() throws SQLException {
95 | 0 |
             if (session != null) {
96 | 0 |
                 session = getSqlMapClient().openSession();
97 | 0 |
                 session.startTransaction();
98 |  |
             }
99 | 0 |
             return session;
100 |  |
         }
101 |  |
102 |  |
         /**
103 |  |
          * @see org.huihoo.jfox.soaf.services.persistence.IbatisService#commit()
104 |  |
          */
105 | 0 |
         public void commit() throws SQLException {
106 | 0 |
             if (session != null) {
107 | 0 |
                 session.commitTransaction();
108 |  |
             }
109 |  |
         }
110 |  |
111 |  |
         /**
112 |  |
          * @see org.huihoo.jfox.soaf.services.persistence.IbatisService#closeSession()
113 |  |
          */
114 | 0 |
         public void closeSession() throws SQLException {
115 | 0 |
             if (session != null) {
116 | 0 |
                 session.endTransaction();
117 | 0 |
                 session.close();
118 | 0 |
                 session = null;
119 |  |
             }
120 |  |
         }
121 |  |
122 |  |
         /**
123 |  |
          * @see org.picocontainer.Startable#start()
124 |  |
          */
125 | 10 |
         public void start() {
126 | 10 |
             try {
127 | 10 |
                 Reader reader = Resources.getResourceAsReader(IBATIS_DAO_RESOURCE);
128 | 10 |
                 Reader sqlMapClientReader = Resources
129 |  |
                         .getResourceAsReader(IBATIS_SQLMAP_RESOURCE);
130 | 10 |
                 daoManager = DaoManagerBuilder.buildDaoManager(reader);
131 | 10 |
                 sqlMapClient = SqlMapClientBuilder
132 |  |
                         .buildSqlMapClient(sqlMapClientReader);
133 |  |
             } catch (Exception e) {
134 | 0 |
                 throw new IbatisConfigurationException(
135 |  |
                         "Failed to initialize Ibatis DaoConfig.  Cause: " + e);
136 |  |
             }
137 | 10 |
             logger.info("Start ibatis service successful.");
138 |  |
         }
139 |  |
140 |  |
         /**
141 |  |
          * @see org.picocontainer.Startable#stop()
142 |  |
          */
143 | 0 |
         public void stop() {
144 |  |
         }
145 |  |
146 |  |
         /**
147 |  |
          * Executes a mapped SQL INSERT statement. Insert is a bit different from
148 |  |
          * other update methods, as it provides facilities for returning the primary
149 |  |
          * key of the newly inserted row (rather than the effected rows). This
150 |  |
          * functionality is of course optional.
151 |  |
          *
152 |  |
          * @param id
153 |  |
          * @param parameterObject
154 |  |
          * @return The primary key of the newly inserted row. This might be
155 |  |
          *         automatically generated by the RDBMS, or selected from a sequence
156 |  |
          *         table or other source.
157 |  |
          * @throws SQLException
158 |  |
          * @throws DAOException
159 |  |
          */
160 | 0 |
         public Object insert(String id, Object parameterObject)
161 |  |
                 throws SQLException, DAOException {
162 | 0 |
             SqlMapSession session;
163 | 0 |
             Object object;
164 | 0 |
             try {
165 | 0 |
                 session = openSession();
166 | 0 |
                 object = session.insert(id, parameterObject);
167 | 0 |
                 commit();
168 |  |
             } catch (Exception e) {
169 | 0 |
                 throw new DAOException("Failed to insert - id [" + id
170 |  |
                         + "], parameterObject [" + parameterObject + "]. Cause: "
171 |  |
                         + e, e);
172 |  |
             } finally {
173 | 0 |
                 session = null;
174 | 0 |
                 closeSession();
175 |  |
             }
176 | 0 |
             return object;
177 |  |
         }
178 |  |
179 |  |
         /**
180 |  |
          * Executes a mapped SQL UPDATE statement. Update can also be used for any
181 |  |
          * other update statement type, such as inserts and deletes. Update returns
182 |  |
          * the number of rows effected.
183 |  |
          *
184 |  |
          * @param id
185 |  |
          * @param parameterObject
186 |  |
          * @return The number of rows effected.
187 |  |
          * @throws SQLException
188 |  |
          * @throws DAOException
189 |  |
          */
190 | 0 |
         public int update(String id, Object parameterObject) throws SQLException,
191 |  |
                 DAOException {
192 | 0 |
             SqlMapSession session;
193 | 0 |
             int rowsNum;
194 | 0 |
             try {
195 | 0 |
                 session = openSession();
196 | 0 |
                 rowsNum = session.update(id, parameterObject);
197 | 0 |
                 commit();
198 |  |
             } catch (Exception e) {
199 | 0 |
                 throw new DAOException("Failed to update - id [" + id
200 |  |
                         + "], parameterObject [" + parameterObject + "]. Cause: "
201 |  |
                         + e, e);
202 |  |
             } finally {
203 | 0 |
                 session = null;
204 | 0 |
                 closeSession();
205 |  |
             }
206 | 0 |
             return rowsNum;
207 |  |
         }
208 |  |
209 |  |
         /**
210 |  |
          * Executes a mapped SQL DELETE statement. Delete returns the number of rows
211 |  |
          * effected.
212 |  |
          *
213 |  |
          * @param id
214 |  |
          * @param parameterObject
215 |  |
          * @return The number of rows effected
216 |  |
          * @throws SQLException
217 |  |
          * @throws DAOException
218 |  |
          */
219 | 0 |
         public int delete(String id, Object parameterObject) throws SQLException,
220 |  |
                 DAOException {
221 | 0 |
             SqlMapSession session;
222 | 0 |
             int rowsNum;
223 | 0 |
             try {
224 | 0 |
                 session = openSession();
225 | 0 |
                 rowsNum = session.delete(id, parameterObject);
226 | 0 |
                 commit();
227 |  |
             } catch (Exception e) {
228 | 0 |
                 throw new DAOException("Failed to delete - id [" + id
229 |  |
                         + "], parameterObject [" + parameterObject + "]. Cause: "
230 |  |
                         + e, e);
231 |  |
             } finally {
232 | 0 |
                 session = null;
233 | 0 |
                 closeSession();
234 |  |
             }
235 | 0 |
             return rowsNum;
236 |  |
         }
237 |  |
238 |  |
         /**
239 |  |
          * Executes a mapped SQL SELECT statement that returns data to populate a
240 |  |
          * single object instance. <p/>The parameter object is generally used to
241 |  |
          * supply the input data for the WHERE clause parameter(s) of the SELECT
242 |  |
          * statement.
243 |  |
          *
244 |  |
          * @param id The name of the statement to execute.
245 |  |
          * @param parameterObject The parameter object (e.g. JavaBean, Map, XML
246 |  |
          *            etc.).
247 |  |
          * @return The single result object populated with the result set data.
248 |  |
          */
249 | 0 |
         public Object queryForObject(String id, Object parameterObject)
250 |  |
                 throws SQLException, DAOException {
251 | 0 |
             SqlMapSession session;
252 | 0 |
             Object object;
253 | 0 |
             try {
254 | 0 |
                 session = openSession();
255 | 0 |
                 object = session.queryForObject(id, parameterObject);
256 | 0 |
                 commit();
257 |  |
             } catch (Exception e) {
258 | 0 |
                 throw new DAOException("Failed to execute queryForObject - id ["
259 |  |
                         + id + "], parameterObject [" + parameterObject
260 |  |
                         + "].  Cause: " + e, e);
261 |  |
             } finally {
262 | 0 |
                 session = null;
263 | 0 |
                 closeSession();
264 |  |
             }
265 | 0 |
             return object;
266 |  |
         }
267 |  |
268 |  |
         /**
269 |  |
          * Executes a mapped SQL SELECT statement that returns data to populate the
270 |  |
          * supplied result object.
271 |  |
          *
272 |  |
          * @param id
273 |  |
          * @param parameterObject
274 |  |
          * @param resultObject
275 |  |
          * @return The single result object as supplied by the resultObject
276 |  |
          *         parameter, populated with the result set data.
277 |  |
          * @throws SQLException
278 |  |
          * @throws DAOException
279 |  |
          */
280 | 0 |
         public Object queryForObject(String id, Object parameterObject,
281 |  |
                 Object resultObject) throws SQLException, DAOException {
282 | 0 |
             SqlMapSession session;
283 | 0 |
             Object object;
284 | 0 |
             try {
285 | 0 |
                 session = openSession();
286 | 0 |
                 object = session.queryForObject(id, parameterObject, resultObject);
287 | 0 |
                 commit();
288 |  |
             } catch (Exception e) {
289 | 0 |
                 throw new DAOException("Failed to execute queryForObject - id ["
290 |  |
                         + id + "], parameterObject [" + parameterObject
291 |  |
                         + "].  Cause: " + e, e);
292 |  |
             } finally {
293 | 0 |
                 session = null;
294 | 0 |
                 closeSession();
295 |  |
             }
296 | 0 |
             return object;
297 |  |
         }
298 |  |
299 |  |
         /**
300 |  |
          * Executes a mapped SQL SELECT statement that returns data to populate a
301 |  |
          * number of result objects.
302 |  |
          *
303 |  |
          * @param id
304 |  |
          * @param parameterObject
305 |  |
          * @return A List of result objects.
306 |  |
          * @throws SQLException
307 |  |
          * @throws DAOException
308 |  |
          */
309 | 0 |
         public List queryForList(String id, Object parameterObject)
310 |  |
                 throws SQLException, DAOException {
311 | 0 |
             SqlMapSession session;
312 | 0 |
             List list;
313 | 0 |
             try {
314 | 0 |
                 session = openSession();
315 | 0 |
                 list = session.queryForList(id, parameterObject);
316 | 0 |
                 commit();
317 |  |
             } catch (Exception e) {
318 | 0 |
                 throw new DAOException("Failed to queryForList - id [" + id
319 |  |
                         + "], parameterObject [" + parameterObject + "].  Cause: "
320 |  |
                         + e, e);
321 |  |
             } finally {
322 | 0 |
                 session = null;
323 | 0 |
                 closeSession();
324 |  |
             }
325 | 0 |
             return list;
326 |  |
         }
327 |  |
328 |  |
         /**
329 |  |
          * Executes a mapped SQL SELECT statement that returns data to populate a
330 |  |
          * number of result objects within a certain range.
331 |  |
          *
332 |  |
          * @param id
333 |  |
          * @param parameterObject
334 |  |
          * @param skip
335 |  |
          * @param max
336 |  |
          * @return A List of result objects.
337 |  |
          * @throws SQLException
338 |  |
          * @throws DAOException
339 |  |
          */
340 | 0 |
         public List queryForList(String id, Object parameterObject, int skip,
341 |  |
                 int max) throws SQLException, DAOException {
342 | 0 |
             SqlMapSession session;
343 | 0 |
             List list;
344 | 0 |
             try {
345 | 0 |
                 session = openSession();
346 | 0 |
                 list = session.queryForList(id, parameterObject, skip, max);
347 | 0 |
                 commit();
348 |  |
             } catch (Exception e) {
349 | 0 |
                 throw new DAOException("Failed to queryForList - id [" + id
350 |  |
                         + "], parameterObject [" + parameterObject + "], skip ["
351 |  |
                         + skip + "], max [" + max + "].  Cause: " + e, e);
352 |  |
             } finally {
353 | 0 |
                 session = null;
354 | 0 |
                 closeSession();
355 |  |
             }
356 | 0 |
             return list;
357 |  |
         }
358 |  |
359 |  |
         /**
360 |  |
          * Executes a mapped SQL SELECT statement that returns a number of result
361 |  |
          * objects that will be handled one at a time by a RowHandler.
362 |  |
          *
363 |  |
          * @param id
364 |  |
          * @param parameterObject
365 |  |
          * @param rowHandler
366 |  |
          * @throws SQLException
367 |  |
          * @throws DAOException
368 |  |
          */
369 | 0 |
         public void queryWithRowHandler(String id, Object parameterObject,
370 |  |
                 RowHandler rowHandler) throws SQLException, DAOException {
371 | 0 |
             SqlMapSession session;
372 | 0 |
             try {
373 | 0 |
                 session = openSession();
374 | 0 |
                 session.queryWithRowHandler(id, parameterObject, rowHandler);
375 | 0 |
                 commit();
376 |  |
             } catch (Exception e) {
377 | 0 |
                 throw new DAOException("Failed to queryForList - id [" + id
378 |  |
                         + "], parameterObject [" + parameterObject
379 |  |
                         + "], rowHandler [ " + rowHandler + "].  Cause: " + e, e);
380 |  |
             } finally {
381 | 0 |
                 session = null;
382 | 0 |
                 closeSession();
383 |  |
             }
384 |  |
         }
385 |  |
386 |  |
         /**
387 |  |
          * Executes a mapped SQL SELECT statement that returns data to populate a
388 |  |
          * number of result objects a page at a time.
389 |  |
          *
390 |  |
          * @param id
391 |  |
          * @param parameterObject
392 |  |
          * @param pageSize
393 |  |
          * @return A PaginatedList of result objects.
394 |  |
          * @throws SQLException
395 |  |
          * @throws DAOException
396 |  |
          */
397 | 0 |
         public PaginatedList queryForPaginatedList(String id,
398 |  |
                 Object parameterObject, int pageSize) throws SQLException,
399 |  |
                 DAOException {
400 | 0 |
             SqlMapSession session;
401 | 0 |
             PaginatedList list;
402 | 0 |
             try {
403 | 0 |
                 session = openSession();
404 | 0 |
                 list = session.queryForPaginatedList(id, parameterObject, pageSize);
405 | 0 |
                 commit();
406 |  |
             } catch (Exception e) {
407 | 0 |
                 throw new DAOException("Failed to queryForPaginatedList - id ["
408 |  |
                         + id + "], parameterObject [" + parameterObject
409 |  |
                         + "], pageSize [" + pageSize + "].  Cause: " + e, e);
410 |  |
             } finally {
411 | 0 |
                 session = null;
412 | 0 |
                 closeSession();
413 |  |
             }
414 | 0 |
             return list;
415 |  |
         }
416 |  |
417 |  |
         /**
418 |  |
          * Executes a mapped SQL SELECT statement that returns data to populate a
419 |  |
          * number of result objects that will be keyed into a Map.
420 |  |
          *
421 |  |
          * @param id
422 |  |
          * @param parameterObject
423 |  |
          * @param keyProp
424 |  |
          * @return A Map keyed by keyProp with values being the result object
425 |  |
          *         instance.
426 |  |
          * @throws SQLException
427 |  |
          * @throws DAOException
428 |  |
          */
429 | 0 |
         public Map queryForMap(String id, Object parameterObject, String keyProp)
430 |  |
                 throws SQLException, DAOException {
431 | 0 |
             SqlMapSession session;
432 | 0 |
             Map map;
433 | 0 |
             try {
434 | 0 |
                 session = openSession();
435 | 0 |
                 map = session.queryForMap(id, parameterObject, keyProp);
436 | 0 |
                 commit();
437 |  |
             } catch (Exception e) {
438 | 0 |
                 throw new DAOException("Failed to queryForMap - id [" + id
439 |  |
                         + "], parameterObject [" + parameterObject + "], keyProp ["
440 |  |
                         + keyProp + "].  Cause: " + e, e);
441 |  |
             } finally {
442 | 0 |
                 session = null;
443 | 0 |
                 closeSession();
444 |  |
             }
445 | 0 |
             return map;
446 |  |
         }
447 |  |
448 |  |
         /**
449 |  |
          * Executes a mapped SQL SELECT statement that returns data to populate a
450 |  |
          * number of result objects from which one property will be keyed into a
451 |  |
          * Map.
452 |  |
          *
453 |  |
          * @param id
454 |  |
          * @param parameterObject
455 |  |
          * @param keyProp
456 |  |
          * @param valueProp
457 |  |
          * @return A Map keyed by keyProp with values of valueProp.
458 |  |
          * @throws SQLException
459 |  |
          * @throws DAOException
460 |  |
          */
461 | 0 |
         public Map queryForMap(String id, Object parameterObject, String keyProp,
462 |  |
                 String valueProp) throws SQLException, DAOException {
463 | 0 |
             SqlMapSession session;
464 | 0 |
             Map map;
465 | 0 |
             try {
466 | 0 |
                 session = openSession();
467 | 0 |
                 map = session.queryForMap(id, parameterObject, keyProp, valueProp);
468 | 0 |
                 commit();
469 |  |
             } catch (Exception e) {
470 | 0 |
                 throw new DAOException("Failed to queryForMap - id [" + id
471 |  |
                         + "], parameterObject [" + parameterObject + "], keyProp ["
472 |  |
                         + keyProp + "], valueProp [" + valueProp + "].  Cause: "
473 |  |
                         + e, e);
474 |  |
             } finally {
475 | 0 |
                 session = null;
476 | 0 |
                 closeSession();
477 |  |
             }
478 | 0 |
             return map;
479 |  |
         }
480 |  |
481 |  |
     }