001/**
002 * Copyright 2005-2017 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 */
016package org.kuali.rice.krms.impl.repository;
017
018import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
019import org.kuali.rice.krad.data.platform.MaxValueIncrementerFactory;
020import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
021
022import javax.sql.DataSource;
023
024/**
025 * Class used to help migrate KRMS BOs to use DataFieldMaxValueIncrementer
026 *
027 * @author Kuali Rice Team (rice.collab@kuali.org)
028 */
029public class RepositoryBoIncrementer {
030
031    private final String seqName;
032    private DataFieldMaxValueIncrementer boIncrementer;
033
034    public RepositoryBoIncrementer(String seqName) {
035        this.seqName = seqName;
036    }
037
038    /**
039     * Set the incrementer, useful for testing.
040     *
041     * @param boIncrementer DataFieldMaxValueIncrementer to use for getNewId()
042     */
043    public void setDataFieldMaxValueIncrementer(DataFieldMaxValueIncrementer boIncrementer) {
044        this.boIncrementer = boIncrementer;
045    }
046
047    /**
048     * Returns the next available id value.
049     *
050     * @return String the next available id
051     */
052    public String getNewId() {
053        if (boIncrementer == null) {
054            // we don't assign to boIncrementer to preserve existing behavior
055            return MaxValueIncrementerFactory.getIncrementer((DataSource) GlobalResourceLoader.getService(
056                    "krmsDataSource"), seqName).nextStringValue();
057        }
058
059        return boIncrementer.nextStringValue();
060    }
061}