| 1 | /* |
| 2 | * Copyright 2006-2013 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.batch.core.repository.dao; |
| 18 | |
| 19 | import java.sql.Types; |
| 20 | |
| 21 | import org.springframework.beans.factory.InitializingBean; |
| 22 | import org.springframework.jdbc.core.JdbcOperations; |
| 23 | import org.springframework.util.Assert; |
| 24 | import org.springframework.util.StringUtils; |
| 25 | |
| 26 | /** |
| 27 | * Encapsulates common functionality needed by JDBC batch metadata DAOs - |
| 28 | * provides jdbcTemplate for subclasses and handles table prefixes. |
| 29 | * |
| 30 | * @author Robert Kasanicky |
| 31 | */ |
| 32 | public abstract class AbstractJdbcBatchMetadataDao implements InitializingBean { |
| 33 | |
| 34 | /** |
| 35 | * Default value for the table prefix property. |
| 36 | */ |
| 37 | public static final String DEFAULT_TABLE_PREFIX = "BATCH_"; |
| 38 | |
| 39 | public static final int DEFAULT_EXIT_MESSAGE_LENGTH = 2500; |
| 40 | |
| 41 | private String tablePrefix = DEFAULT_TABLE_PREFIX; |
| 42 | |
| 43 | private int clobTypeToUse = Types.CLOB; |
| 44 | |
| 45 | private JdbcOperations jdbcTemplate; |
| 46 | |
| 47 | protected String getQuery(String base) { |
| 48 | return StringUtils.replace(base, "%PREFIX%", tablePrefix); |
| 49 | } |
| 50 | |
| 51 | protected String getTablePrefix() { |
| 52 | return tablePrefix; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Public setter for the table prefix property. This will be prefixed to all |
| 57 | * the table names before queries are executed. Defaults to |
| 58 | * {@link #DEFAULT_TABLE_PREFIX}. |
| 59 | * |
| 60 | * @param tablePrefix the tablePrefix to set |
| 61 | */ |
| 62 | public void setTablePrefix(String tablePrefix) { |
| 63 | this.tablePrefix = tablePrefix; |
| 64 | } |
| 65 | |
| 66 | public void setJdbcTemplate(JdbcOperations jdbcTemplate) { |
| 67 | this.jdbcTemplate = jdbcTemplate; |
| 68 | } |
| 69 | |
| 70 | protected JdbcOperations getJdbcTemplate() { |
| 71 | return jdbcTemplate; |
| 72 | } |
| 73 | |
| 74 | public int getClobTypeToUse() { |
| 75 | return clobTypeToUse; |
| 76 | } |
| 77 | |
| 78 | public void setClobTypeToUse(int clobTypeToUse) { |
| 79 | this.clobTypeToUse = clobTypeToUse; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void afterPropertiesSet() throws Exception { |
| 84 | Assert.notNull(jdbcTemplate); |
| 85 | } |
| 86 | |
| 87 | } |