View Javadoc
1   /**
2    * Copyright 2005-2014 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.sql.spring;
17  
18  import java.util.List;
19  
20  import javax.sql.DataSource;
21  
22  import org.kuali.common.jdbc.model.context.JdbcContext;
23  import org.kuali.common.jdbc.reset.DefaultJdbcResetConfig;
24  import org.kuali.common.jdbc.service.spring.DataSourceConfig;
25  import org.kuali.common.jdbc.sql.spring.DbaContextConfig;
26  import org.kuali.common.jdbc.sql.spring.JdbcContextsConfig;
27  import org.kuali.common.jdbc.suppliers.ResourcesSupplierFactory;
28  import org.kuali.common.jdbc.suppliers.SqlSupplier;
29  import org.kuali.common.jdbc.suppliers.spring.SuppliersFactoryConfig;
30  import org.kuali.common.jdbc.vendor.model.DatabaseVendor;
31  import org.kuali.common.util.metainf.service.MetaInfUtils;
32  import org.kuali.common.util.metainf.spring.MetaInfGroup;
33  import org.kuali.common.util.project.ProjectService;
34  import org.kuali.common.util.project.model.Project;
35  import org.kuali.common.util.project.spring.ProjectServiceConfig;
36  import org.kuali.rice.sql.project.SqlProjectConstants;
37  import org.springframework.beans.factory.annotation.Autowired;
38  import org.springframework.context.annotation.Bean;
39  import org.springframework.context.annotation.Configuration;
40  import org.springframework.context.annotation.Import;
41  
42  import com.google.common.collect.ImmutableList;
43  
44  /**
45   * Used by developers (to reset their local db), CI (to validate changes), and by the deploy process to reset the
46   * database for instances of the running application.
47   *
48   * @author Kuali Rice Team (rice.collab@kuali.org)
49   */
50  @Configuration
51  @Import({ DbaContextConfig.class, SuppliersFactoryConfig.class, ProjectServiceConfig.class, DefaultJdbcResetConfig.class })
52  public class SourceSqlConfig implements JdbcContextsConfig {
53  
54  	@Autowired
55  	DbaContextConfig dba;
56  
57  	@Autowired
58  	ResourcesSupplierFactory factory;
59  
60  	@Autowired
61  	ProjectService projectService;
62  
63  	@Autowired
64  	DatabaseVendor vendor;
65  
66  	@Autowired
67  	DataSourceConfig dataSources;
68  
69  	@Bean
70  	public Project riceSqlProject() {
71  		return projectService.getProject(SqlProjectConstants.ID);
72  	}
73  
74  	@Override
75  	@Bean
76  	public List<JdbcContext> jdbcContexts() {
77  		JdbcContext before = dba.dbaBeforeContext();
78  		JdbcContext schema = getJdbcContext(MetaInfGroup.SCHEMA, true);
79  		JdbcContext data = getJdbcContext(MetaInfGroup.DATA, true);
80  		JdbcContext constraints = getJdbcContext(MetaInfGroup.CONSTRAINTS, true);
81  		JdbcContext other = getJdbcContext(MetaInfGroup.OTHER, false);
82  		JdbcContext after = dba.dbaAfterContext();
83  		return ImmutableList.of(before, schema, data, constraints, other, after);
84  	}
85  
86  	protected JdbcContext getJdbcContext(MetaInfGroup group, boolean multithreaded) {
87  		String resourcesLocation = MetaInfUtils.getClasspathResource(riceSqlProject(), vendor.getCode(), group);
88  		List<SqlSupplier> suppliers = factory.getSuppliers(resourcesLocation);
89  		DataSource dataSource = dataSources.dataSource();
90  		String message = "[" + group.name().toLowerCase() + ":" + (multithreaded ? "concurrent" : "sequential") + "]";
91  		return new JdbcContext.Builder(dataSource, suppliers).message(message).multithreaded(multithreaded).build();
92  	}
93  }