001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.jdbc.support;
018:
019: import java.util.Iterator;
020: import java.util.LinkedList;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.springframework.dao.DataRetrievalFailureException;
025: import org.springframework.dao.InvalidDataAccessApiUsageException;
026:
027: /**
028: * Default implementation of the {@link KeyHolder} interface, to be used for
029: * holding auto-generated keys (as potentially returned by JDBC insert statements).
030: *
031: * <p>Create an instance of this class for each insert operation, and pass
032: * it to the corresponding {@link org.springframework.jdbc.core.JdbcTemplate}
033: * or {org.springframework.jdbc.object.SqlUpdate} methods.
034: *
035: * @author Thomas Risberg
036: * @author Juergen Hoeller
037: * @since 1.1
038: */
039: public class GeneratedKeyHolder implements KeyHolder {
040:
041: private final List keyList;
042:
043: /**
044: * Create a new GeneratedKeyHolder with a default list.
045: */
046: public GeneratedKeyHolder() {
047: this .keyList = new LinkedList();
048: }
049:
050: /**
051: * Create a new GeneratedKeyHolder with a given list.
052: * @param keyList a list to hold maps of keys
053: */
054: public GeneratedKeyHolder(List keyList) {
055: this .keyList = keyList;
056: }
057:
058: public Number getKey() throws InvalidDataAccessApiUsageException,
059: DataRetrievalFailureException {
060: if (this .keyList.size() == 0) {
061: return null;
062: }
063: if (this .keyList.size() > 1
064: || ((Map) this .keyList.get(0)).size() > 1) {
065: throw new InvalidDataAccessApiUsageException(
066: "The getKey method should only be used when a single key is returned. "
067: + "The current key entry contains multiple keys: "
068: + this .keyList);
069: }
070: Iterator keyIter = ((Map) this .keyList.get(0)).values()
071: .iterator();
072: if (keyIter.hasNext()) {
073: Object key = keyIter.next();
074: if (!(key instanceof Number)) {
075: throw new DataRetrievalFailureException(
076: "The generated key is not of a supported numeric type. "
077: + "Unable to cast ["
078: + (key != null ? key.getClass()
079: .getName() : null) + "] to ["
080: + Number.class.getName() + "]");
081: }
082: return (Number) key;
083: } else {
084: throw new DataRetrievalFailureException(
085: "Unable to retrieve the generated key. "
086: + "Check that the table has an identity column enabled.");
087: }
088: }
089:
090: public Map getKeys() throws InvalidDataAccessApiUsageException {
091: if (this .keyList.size() == 0) {
092: return null;
093: }
094: if (this .keyList.size() > 1)
095: throw new InvalidDataAccessApiUsageException(
096: "The getKeys method should only be used when keys for a single row are returned. "
097: + "The current key list contains keys for multiple rows: "
098: + this .keyList);
099: return (Map) this .keyList.get(0);
100: }
101:
102: public List getKeyList() {
103: return this.keyList;
104: }
105:
106: }
|