1. /*
  2. * Copyright 2002-2004 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. package org.springframework.jdbc.support.incrementer;
  17. import javax.sql.DataSource;
  18. import org.springframework.beans.factory.InitializingBean;
  19. import org.springframework.dao.DataAccessException;
  20. /**
  21. * Implementation of DataFieldMaxValueIncrementer that delegates
  22. * to a single getNextKey template method that returns a long.
  23. * Uses longs for String values, padding with zeroes if required.
  24. * @author Dmitriy Kopylenko
  25. * @author Juergen Hoeller
  26. * @author Jean-Pierre Pawlak
  27. * @version $Id: AbstractDataFieldMaxValueIncrementer.java,v 1.4 2004/03/18 02:46:11 trisberg Exp $
  28. */
  29. public abstract class AbstractDataFieldMaxValueIncrementer implements DataFieldMaxValueIncrementer, InitializingBean {
  30. private DataSource dataSource;
  31. /** The name of the sequence/table containing the sequence */
  32. private String incrementerName;
  33. /** The length to which a string result should be pre-pended with zeroes */
  34. protected int paddingLength = 0;
  35. /**
  36. * Set the data source to retrieve the value from.
  37. */
  38. public void setDataSource(DataSource dataSource) {
  39. this.dataSource = dataSource;
  40. }
  41. /**
  42. * Return the data source to retrieve the value from.
  43. */
  44. public DataSource getDataSource() {
  45. return this.dataSource;
  46. }
  47. /**
  48. * Set the name of the sequence/table.
  49. */
  50. public void setIncrementerName(String incrementerName) {
  51. this.incrementerName = incrementerName;
  52. }
  53. /**
  54. * Return the name of the sequence/table.
  55. */
  56. public String getIncrementerName() {
  57. return this.incrementerName;
  58. }
  59. /**
  60. * Set the padding length, i.e. the length to which a string result
  61. * should be pre-pended with zeroes.
  62. */
  63. public void setPaddingLength(int paddingLength) {
  64. this.paddingLength = paddingLength;
  65. }
  66. /**
  67. * Return the padding length for String values.
  68. */
  69. public int getPaddingLength() {
  70. return paddingLength;
  71. }
  72. public void afterPropertiesSet() {
  73. if (this.dataSource == null) {
  74. throw new IllegalArgumentException("dataSource is required");
  75. }
  76. if (this.incrementerName == null) {
  77. throw new IllegalArgumentException("incrementerName is required");
  78. }
  79. }
  80. public int nextIntValue() throws DataAccessException {
  81. return (int) getNextKey();
  82. }
  83. public long nextLongValue() throws DataAccessException {
  84. return getNextKey();
  85. }
  86. public String nextStringValue() throws DataAccessException {
  87. String s = Long.toString(getNextKey());
  88. int len = s.length();
  89. if (len < this.paddingLength) {
  90. StringBuffer buf = new StringBuffer(this.paddingLength);
  91. for (int i = 0; i < this.paddingLength - len; i++) {
  92. buf.append('0');
  93. }
  94. buf.append(s);
  95. s = buf.toString();
  96. }
  97. return s;
  98. }
  99. /**
  100. * Determine the next key to use, as a long.
  101. * @return the key to use as a long. It will eventually be converted later
  102. * in another format by the public concrete methods of this class.
  103. */
  104. protected abstract long getNextKey();
  105. }