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.sql.spring;
017    
018    import java.util.List;
019    
020    import javax.sql.DataSource;
021    
022    import org.kuali.common.jdbc.model.context.JdbcContext;
023    import org.kuali.common.jdbc.reset.DefaultJdbcResetConfig;
024    import org.kuali.common.jdbc.service.spring.DataSourceConfig;
025    import org.kuali.common.jdbc.sql.spring.DbaContextConfig;
026    import org.kuali.common.jdbc.sql.spring.JdbcContextsConfig;
027    import org.kuali.common.jdbc.suppliers.ResourcesSupplierFactory;
028    import org.kuali.common.jdbc.suppliers.SqlSupplier;
029    import org.kuali.common.jdbc.suppliers.spring.SuppliersFactoryConfig;
030    import org.kuali.common.jdbc.vendor.model.DatabaseVendor;
031    import org.kuali.common.util.metainf.service.MetaInfUtils;
032    import org.kuali.common.util.metainf.spring.MetaInfGroup;
033    import org.kuali.common.util.project.ProjectService;
034    import org.kuali.common.util.project.model.Project;
035    import org.kuali.common.util.project.spring.ProjectServiceConfig;
036    import org.kuali.rice.sql.project.SqlProjectConstants;
037    import org.springframework.beans.factory.annotation.Autowired;
038    import org.springframework.context.annotation.Bean;
039    import org.springframework.context.annotation.Configuration;
040    import org.springframework.context.annotation.Import;
041    
042    import com.google.common.collect.ImmutableList;
043    
044    /**
045     * Used by developers (to reset their local db), CI (to validate changes), and by the deploy process to reset the
046     * database for instances of the running application.
047     *
048     * @author Kuali Rice Team (rice.collab@kuali.org)
049     */
050    @Configuration
051    @Import({ DbaContextConfig.class, SuppliersFactoryConfig.class, ProjectServiceConfig.class, DefaultJdbcResetConfig.class })
052    public class SourceSqlConfig implements JdbcContextsConfig {
053    
054            @Autowired
055            DbaContextConfig dba;
056    
057            @Autowired
058            ResourcesSupplierFactory factory;
059    
060            @Autowired
061            ProjectService projectService;
062    
063            @Autowired
064            DatabaseVendor vendor;
065    
066            @Autowired
067            DataSourceConfig dataSources;
068    
069            @Bean
070            public Project riceSqlProject() {
071                    return projectService.getProject(SqlProjectConstants.ID);
072            }
073    
074            @Override
075            @Bean
076            public List<JdbcContext> jdbcContexts() {
077                    JdbcContext before = dba.dbaBeforeContext();
078                    JdbcContext schema = getJdbcContext(MetaInfGroup.SCHEMA, true);
079                    JdbcContext data = getJdbcContext(MetaInfGroup.DATA, true);
080                    JdbcContext constraints = getJdbcContext(MetaInfGroup.CONSTRAINTS, true);
081                    JdbcContext other = getJdbcContext(MetaInfGroup.OTHER, false);
082                    JdbcContext after = dba.dbaAfterContext();
083                    return ImmutableList.of(before, schema, data, constraints, other, after);
084            }
085    
086            protected JdbcContext getJdbcContext(MetaInfGroup group, boolean multithreaded) {
087                    String resourcesLocation = MetaInfUtils.getClasspathResource(riceSqlProject(), vendor.getCode(), group);
088                    List<SqlSupplier> suppliers = factory.getSuppliers(resourcesLocation);
089                    DataSource dataSource = dataSources.dataSource();
090                    String message = "[" + group.name().toLowerCase() + ":" + (multithreaded ? "concurrent" : "sequential") + "]";
091                    return new JdbcContext.Builder(dataSource, suppliers).message(message).multithreaded(multithreaded).build();
092            }
093    }