001/**
002 * Copyright 2005-2016 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.krad.dao.jdbc;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.ojb.broker.PBKey;
020import org.apache.ojb.broker.PersistenceBroker;
021import org.kuali.rice.core.api.config.ConfigurationException;
022import org.kuali.rice.core.api.config.property.ConfigContext;
023import org.kuali.rice.core.framework.persistence.jdbc.dao.PlatformAwareDaoBaseJdbc;
024import org.kuali.rice.krad.bo.ModuleConfiguration;
025import org.kuali.rice.krad.dao.SequenceAccessorDao;
026import org.kuali.rice.krad.data.platform.MaxValueIncrementerFactory;
027import org.kuali.rice.krad.service.KRADServiceLocator;
028import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
029import org.kuali.rice.krad.service.KualiModuleService;
030import org.kuali.rice.krad.service.ModuleService;
031import org.kuali.rice.krad.util.KRADConstants;
032import org.kuali.rice.krad.util.LegacyUtils;
033import org.springmodules.orm.ojb.OjbFactoryUtils;
034
035import javax.sql.DataSource;
036
037/**
038 * This class uses the KualiDBPlatform to get the next number from a given sequence.
039 */
040@Deprecated
041public class SequenceAccessorDaoJdbc extends PlatformAwareDaoBaseJdbc implements SequenceAccessorDao {
042        private KualiModuleService kualiModuleService;
043
044        private Long nextAvailableSequenceNumber(String sequenceName, Class clazz) {
045        ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
046        if ( moduleService == null )
047                throw new ConfigurationException("moduleService is null");
048                                
049        ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
050        if ( moduleConfig == null )
051                throw new ConfigurationException("moduleConfiguration is null");
052
053        String dataSourceName = moduleConfig.getDataSourceName();
054
055        if (StringUtils.isEmpty(dataSourceName)) {
056            return nextAvailableSequenceNumber(sequenceName);
057        } else {
058            PBKey key = new PBKey(dataSourceName);
059            PersistenceBroker broker = OjbFactoryUtils.getPersistenceBroker(key, false);
060
061            if (broker != null) {
062                return getDbPlatform().getNextValSQL(sequenceName, broker);
063            } else {
064                throw new ConfigurationException("PersistenceBroker is null");
065            }
066        }
067        }
068
069    private Long nextAvailableSequenceNumber(String sequenceName) {
070        DataSource dataSource = (DataSource) ConfigContext.getCurrentContextConfig().getObject(KRADConstants.KRAD_APPLICATION_DATASOURCE);
071        if (dataSource == null) {
072            dataSource = KRADServiceLocator.getKradApplicationDataSource();
073        }
074        return Long.valueOf(MaxValueIncrementerFactory.getIncrementer(dataSource, sequenceName).nextLongValue());
075    }
076        
077        public Long getNextAvailableSequenceNumber(String sequenceName, Class clazz) {
078        if (!LegacyUtils.useLegacy(clazz)) {
079            throw new ConfigurationException("SequenceAccessorService should not be used with new data framework! Use "
080                    + MaxValueIncrementerFactory.class.getName() + " instead.");
081        }
082
083                // There are situations where a module hasn't been configured with
084                // a dataSource.  In these cases, this method would have previously
085                // thrown an error.  Instead, we've opted to factor out the code,
086                // catch any configuration-related exceptions, and if one occurs,
087                // attempt to use the dataSource associated with KNS. -- tbradford
088                
089                try {
090                        return nextAvailableSequenceNumber(sequenceName, clazz);
091                }
092                catch ( ConfigurationException e  ) {
093                // Use kradApplication.datasource to get the dataSource associated with KNS
094                        return nextAvailableSequenceNumber(sequenceName);
095                }
096        }
097        
098    /**
099     * @see org.kuali.rice.krad.dao.SequenceAccessorDao#getNextAvailableSequenceNumber(java.lang.String)
100     */
101    public Long getNextAvailableSequenceNumber(String sequenceName) {
102        // Use kradApplication.datasource to get the dataSource associated with KNS
103        return nextAvailableSequenceNumber(sequenceName);
104    }
105    
106    private KualiModuleService getKualiModuleService() {
107        if ( kualiModuleService == null ) 
108            kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
109        return kualiModuleService;
110    }
111}