View Javadoc
1   /**
2    * Copyright 2005-2015 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.krms.impl.repository;
17  
18  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
19  import org.kuali.rice.krad.data.platform.MaxValueIncrementerFactory;
20  import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
21  
22  import javax.sql.DataSource;
23  
24  /**
25   * Class used to help migrate KRMS BOs to use DataFieldMaxValueIncrementer
26   *
27   * @author Kuali Rice Team (rice.collab@kuali.org)
28   */
29  public class RepositoryBoIncrementer {
30  
31      private final String seqName;
32      private DataFieldMaxValueIncrementer boIncrementer;
33  
34      public RepositoryBoIncrementer(String seqName) {
35          this.seqName = seqName;
36      }
37  
38      /**
39       * Set the incrementer, useful for testing.
40       *
41       * @param boIncrementer DataFieldMaxValueIncrementer to use for getNewId()
42       */
43      public void setDataFieldMaxValueIncrementer(DataFieldMaxValueIncrementer boIncrementer) {
44          this.boIncrementer = boIncrementer;
45      }
46  
47      /**
48       * Returns the next available id value.
49       *
50       * @return String the next available id
51       */
52      public String getNewId() {
53          if (boIncrementer == null) {
54              // we don't assign to boIncrementer to preserve existing behavior
55              return MaxValueIncrementerFactory.getIncrementer((DataSource) GlobalResourceLoader.getService(
56                      "krmsDataSource"), seqName).nextStringValue();
57          }
58  
59          return boIncrementer.nextStringValue();
60      }
61  }