KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > support > GeneratedKeyHolder


1 /*
2  * Copyright 2002-2006 the original author or authors.
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
17 package org.springframework.jdbc.support;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.LinkedList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.springframework.dao.DataRetrievalFailureException;
25 import org.springframework.dao.InvalidDataAccessApiUsageException;
26
27 /**
28  * An implementation of the KeyHolder interface, to be used for holding
29  * auto-generated keys as potentially returned by JDBC insert statements.
30  *
31  * <p>Create an instance of this class for each insert operation, and pass
32  * it to the corresponding JdbcTemplate or SqlUpdate methods.
33  *
34  * @author Thomas Risberg
35  * @since 1.1
36  * @see org.springframework.jdbc.core.JdbcTemplate
37  * @see org.springframework.jdbc.object.SqlUpdate
38  */

39 public class GeneratedKeyHolder implements KeyHolder {
40
41     private final List JavaDoc keyList;
42
43
44     /**
45      * Create a new GeneratedKeyHolder with a default list.
46      */

47     public GeneratedKeyHolder() {
48         this.keyList = new LinkedList JavaDoc();
49     }
50
51     /**
52      * Create a new GeneratedKeyHolder with a given list.
53      * @param keyList a list to hold maps of keys
54      */

55     public GeneratedKeyHolder(List JavaDoc keyList) {
56         this.keyList = keyList;
57     }
58
59
60     public Number JavaDoc getKey() throws InvalidDataAccessApiUsageException, DataRetrievalFailureException {
61         if (this.keyList.size() == 0) {
62             return null;
63         }
64         if (this.keyList.size() > 1 || ((Map JavaDoc) this.keyList.get(0)).size() > 1) {
65             throw new InvalidDataAccessApiUsageException(
66                     "The getKey method should only be used when a single key is returned. " +
67                     "The current key entry contains multiple keys: " + this.keyList);
68         }
69         Iterator JavaDoc keyIter = ((Map JavaDoc) this.keyList.get(0)).values().iterator();
70         if (keyIter.hasNext()) {
71             Object JavaDoc key = keyIter.next();
72             if (!(key instanceof Number JavaDoc)) {
73                 throw new DataRetrievalFailureException(
74                         "The generated key is not of a supported numeric type. " +
75                         "Unable to cast [" + key.getClass().getName() + "] to [" + Number JavaDoc.class.getName() + "]");
76             }
77             return (Number JavaDoc) key;
78         }
79         else {
80             throw new DataRetrievalFailureException("Unable to retrieve the generated key. " +
81                     "Check that the table has an identity column enabled.");
82         }
83     }
84
85     public Map JavaDoc getKeys() throws InvalidDataAccessApiUsageException {
86         if (this.keyList.size() == 0) {
87             return null;
88         }
89         if (this.keyList.size() > 1)
90             throw new InvalidDataAccessApiUsageException(
91                     "The getKeys method should only be used when keys for a single row are returned. " +
92                     "The current key list contains keys for multiple rows: " + this.keyList);
93         return (Map JavaDoc) this.keyList.get(0);
94     }
95
96     public List JavaDoc getKeyList() {
97         return keyList;
98     }
99
100 }
101
Popular Tags