001 /**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 package org.kuali.rice.krad.data.platform;
017
018 import org.apache.commons.lang.StringUtils;
019 import org.springframework.beans.factory.FactoryBean;
020 import org.springframework.beans.factory.InitializingBean;
021 import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
022
023 import javax.sql.DataSource;
024
025 /**
026 * A factory bean for putting a {@link DataFieldMaxValueIncrementer} in the Spring context.
027 *
028 * @author Kuali Rice Team (rice.collab@kuali.org)
029 */
030 public class MaxValueIncrementerFactoryBean implements FactoryBean<DataFieldMaxValueIncrementer>, InitializingBean {
031
032 private DataSource dataSource;
033 private String incrementerName;
034
035 /**
036 * {@inheritDoc}
037 */
038 @Override
039 public DataFieldMaxValueIncrementer getObject() throws Exception {
040 return MaxValueIncrementerFactory.getIncrementer(dataSource, incrementerName);
041 }
042
043 /**
044 * {@inheritDoc}
045 */
046 @Override
047 public Class<DataFieldMaxValueIncrementer> getObjectType() {
048 return DataFieldMaxValueIncrementer.class;
049 }
050
051 /**
052 * {@inheritDoc}
053 */
054 @Override
055 public boolean isSingleton() {
056 return false;
057 }
058
059 /**
060 * {@inheritDoc}
061 */
062 @Override
063 public void afterPropertiesSet() throws Exception {
064 if (dataSource == null) {
065 throw new IllegalStateException("Property 'dataSource' is required");
066 }
067
068 if (StringUtils.isBlank(incrementerName)) {
069 throw new IllegalStateException("Property 'incrementerName' is required");
070 }
071 }
072
073 /**
074 * Gets the {@link DataSource} for which to retrieve the incrementer.
075 *
076 * @return the {@link DataSource} for which to retrieve the incrementer.
077 */
078 public DataSource getDataSource() {
079 return dataSource;
080 }
081
082 /**
083 * Setter for the {@link DataSource}.
084 *
085 * @param dataSource the {@link DataSource} for which to retrieve the incrementer.
086 */
087 public void setDataSource(DataSource dataSource) {
088 this.dataSource = dataSource;
089 }
090
091 /**
092 * Gets the name of the incrementer.
093 *
094 * @return the name of the incrementer.
095 */
096 public String getIncrementerName() {
097 return incrementerName;
098 }
099
100 /**
101 * Setter for the incrementer name.
102 *
103 * @param incrementerName the name of the incrementer.
104 */
105 public void setIncrementerName(String incrementerName) {
106 this.incrementerName = incrementerName;
107 }
108 }