View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.krad.data.platform;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.springframework.beans.factory.FactoryBean;
20  import org.springframework.beans.factory.InitializingBean;
21  import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
22  
23  import javax.sql.DataSource;
24  
25  /**
26   * A factory bean for putting a {@link DataFieldMaxValueIncrementer} in the Spring context.
27   *
28   * @author Kuali Rice Team (rice.collab@kuali.org)
29   */
30  public class MaxValueIncrementerFactoryBean implements FactoryBean<DataFieldMaxValueIncrementer>, InitializingBean {
31  
32      private DataSource dataSource;
33      private String incrementerName;
34  
35      /**
36       * {@inheritDoc}
37       */
38      @Override
39      public DataFieldMaxValueIncrementer getObject() throws Exception {
40          return MaxValueIncrementerFactory.getIncrementer(dataSource, incrementerName);
41      }
42  
43      /**
44       * {@inheritDoc}
45       */
46      @Override
47      public Class<DataFieldMaxValueIncrementer> getObjectType() {
48          return DataFieldMaxValueIncrementer.class;
49      }
50  
51      /**
52       * {@inheritDoc}
53       */
54      @Override
55      public boolean isSingleton() {
56          return false;
57      }
58  
59      /**
60       * {@inheritDoc}
61       */
62      @Override
63      public void afterPropertiesSet() throws Exception {
64          if (dataSource == null) {
65              throw new IllegalStateException("Property 'dataSource' is required");
66          }
67  
68          if (StringUtils.isBlank(incrementerName)) {
69              throw new IllegalStateException("Property 'incrementerName' is required");
70          }
71      }
72  
73      /**
74       * Gets the {@link DataSource} for which to retrieve the incrementer.
75       *
76       * @return the {@link DataSource} for which to retrieve the incrementer.
77       */
78      public DataSource getDataSource() {
79          return dataSource;
80      }
81  
82      /**
83       * Setter for the {@link DataSource}.
84       *
85       * @param dataSource the {@link DataSource} for which to retrieve the incrementer.
86       */
87      public void setDataSource(DataSource dataSource) {
88          this.dataSource = dataSource;
89      }
90  
91      /**
92       * Gets the name of the incrementer.
93       *
94       * @return the name of the incrementer.
95       */
96      public String getIncrementerName() {
97          return incrementerName;
98      }
99  
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 }