View Javadoc

1   package org.kuali.common.util.metainf.spring;
2   
3   import java.io.File;
4   import java.util.ArrayList;
5   import java.util.HashMap;
6   import java.util.List;
7   import java.util.Map;
8   
9   import org.kuali.common.util.metainf.model.MetaInfContext;
10  import org.kuali.common.util.metainf.service.MetaInfUtils;
11  import org.kuali.common.util.nullify.NullUtils;
12  import org.kuali.common.util.project.model.Build;
13  import org.kuali.common.util.project.model.Project;
14  import org.kuali.common.util.project.spring.AutowiredProjectConfig;
15  import org.kuali.common.util.spring.SpringUtils;
16  import org.kuali.common.util.spring.env.EnvironmentService;
17  import org.kuali.common.util.spring.service.SpringServiceConfig;
18  import org.springframework.beans.factory.annotation.Autowired;
19  import org.springframework.context.annotation.Bean;
20  import org.springframework.context.annotation.Configuration;
21  import org.springframework.context.annotation.Import;
22  
23  @Configuration
24  @Import({ AutowiredProjectConfig.class, MetaInfExecutableConfig.class, SpringServiceConfig.class })
25  public class SqlConfig implements MetaInfContextsConfig {
26  
27  	private static final boolean DEFAULT_GENERATE_RELATIVE_PATHS = true;
28  	private static final String RELATIVE_KEY = MetaInfUtils.PROPERTY_PREFIX + ".sql.relative";
29  	private static final String DB_VENDOR_KEY = "db.vendor";
30  	private static final String PREFIX = "sql";
31  
32  	@Autowired
33  	EnvironmentService env;
34  
35  	@Autowired
36  	Project project;
37  
38  	@Autowired
39  	Build build;
40  
41  	@Override
42  	@Bean
43  	public List<MetaInfContext> metaInfContexts() {
44  		Map<MetaInfGroup, String> defaultIncludes = getDefaultIncludes();
45  		List<MetaInfContext> contexts = new ArrayList<MetaInfContext>();
46  		for (MetaInfGroup group : MetaInfGroup.values()) {
47  			MetaInfContext context = getMetaInfContext(group, defaultIncludes);
48  			contexts.add(context);
49  		}
50  		return contexts;
51  	}
52  
53  	protected MetaInfContext getMetaInfContext(MetaInfGroup group, Map<MetaInfGroup, String> defaultIncludes) {
54  		String databaseVendor = env.getString(DB_VENDOR_KEY);
55  		boolean relativePaths = env.getBoolean(RELATIVE_KEY, DEFAULT_GENERATE_RELATIVE_PATHS);
56  		File outputFile = MetaInfUtils.getOutputFile(project, build, databaseVendor, group);
57  		String includesKey = MetaInfConfigUtils.getIncludesKey(group, PREFIX);
58  		String excludesKey = MetaInfConfigUtils.getExcludesKey(group, PREFIX);
59  		List<String> includes = SpringUtils.getNoneSensitiveListFromCSV(env, includesKey, defaultIncludes.get(group));
60  		List<String> excludes = SpringUtils.getNoneSensitiveListFromCSV(env, excludesKey, NullUtils.NONE);
61  		File scanDir = build.getOutputDir();
62  		String encoding = build.getEncoding();
63  		return new MetaInfContext.Builder(outputFile, encoding, scanDir).includes(includes).excludes(excludes).relativePaths(relativePaths).build();
64  	}
65  
66  	protected Map<MetaInfGroup, String> getDefaultIncludes() {
67  		Map<MetaInfGroup, String> map = new HashMap<MetaInfGroup, String>();
68  		map.put(MetaInfGroup.SCHEMA, "**/initial-db/**/*create-schema.sql");
69  		map.put(MetaInfGroup.DATA, "**/initial-db/**/data/*.sql");
70  		map.put(MetaInfGroup.CONSTRAINTS, "**/initial-db/**/*constraints.sql");
71  		map.put(MetaInfGroup.OTHER, "**/upgrades/**/*.sql");
72  		return map;
73  	}
74  }