1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util.metainf.spring;
17
18 import java.io.File;
19 import java.util.ArrayList;
20 import java.util.Comparator;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.kuali.common.util.metainf.model.MetaInfContext;
26 import org.kuali.common.util.metainf.model.MetaInfResource;
27 import org.kuali.common.util.metainf.model.MetaInfResourceFilenameComparator;
28 import org.kuali.common.util.metainf.model.MetaInfResourceLocationComparator;
29 import org.kuali.common.util.metainf.service.MetaInfUtils;
30 import org.kuali.common.util.nullify.NullUtils;
31 import org.kuali.common.util.project.ProjectUtils;
32 import org.kuali.common.util.project.model.Build;
33 import org.kuali.common.util.project.model.Project;
34 import org.kuali.common.util.project.spring.AutowiredProjectConfig;
35 import org.kuali.common.util.spring.SpringUtils;
36 import org.kuali.common.util.spring.env.EnvironmentService;
37 import org.kuali.common.util.spring.service.SpringServiceConfig;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.context.annotation.Bean;
40 import org.springframework.context.annotation.Configuration;
41 import org.springframework.context.annotation.Import;
42
43
44
45
46
47 @Configuration
48 @Import({ AutowiredProjectConfig.class, MetaInfExecutableConfig.class, SpringServiceConfig.class })
49 public class RiceSqlConfig implements MetaInfContextsConfig {
50
51 private static final boolean DEFAULT_GENERATE_RELATIVE_PATHS = true;
52 private static final String RELATIVE_KEY = MetaInfUtils.PROPERTY_PREFIX + ".sql.relative";
53 private static final String PREFIX = "sql";
54 private static final String DEFAULT_VENDORS = "mysql,oracle";
55 private static final String VENDORS_KEY = MetaInfUtils.PROPERTY_PREFIX + ".db.vendors";
56 private static final String IMPEX_ARTIFACT_ID = "rice-impex-master";
57
58 @Autowired
59 EnvironmentService env;
60
61 @Autowired
62 Project project;
63
64 @Autowired
65 Build build;
66
67 @Override
68 @Bean
69 public List<MetaInfContext> metaInfContexts() {
70 List<String> vendors = SpringUtils.getNoneSensitiveListFromCSV(env, VENDORS_KEY, DEFAULT_VENDORS);
71 List<MetaInfContext> contexts = new ArrayList<MetaInfContext>();
72 for (String vendor : vendors) {
73 for (MetaInfGroup group : MetaInfGroup.values()) {
74 MetaInfContext context = getMetaInfContext(group, vendor);
75 contexts.add(context);
76 }
77 }
78 return contexts;
79 }
80
81 protected MetaInfContext getMetaInfContext(MetaInfGroup group, String vendor) {
82 Map<MetaInfGroup, String> defaultIncludes = getDefaultIncludes(project, IMPEX_ARTIFACT_ID, vendor);
83 Map<MetaInfGroup, String> defaultExcludes = getDefaultExcludes(defaultIncludes);
84 boolean relativePaths = env.getBoolean(RELATIVE_KEY, DEFAULT_GENERATE_RELATIVE_PATHS);
85 File outputFile = MetaInfUtils.getOutputFile(project, build, vendor, group);
86 String includesKey = MetaInfConfigUtils.getIncludesKey(group, PREFIX) + "." + vendor;
87 String excludesKey = MetaInfConfigUtils.getExcludesKey(group, PREFIX) + "." + vendor;
88 List<String> includes = SpringUtils.getNoneSensitiveListFromCSV(env, includesKey, defaultIncludes.get(group));
89 List<String> excludes = SpringUtils.getNoneSensitiveListFromCSV(env, excludesKey, defaultExcludes.get(group));
90 File scanDir = build.getOutputDir();
91 String encoding = build.getEncoding();
92 Comparator<MetaInfResource> comparator = getComparator(group);
93 return new MetaInfContext.Builder(outputFile, encoding, scanDir).comparator(comparator).includes(includes).excludes(excludes).relativePaths(relativePaths).build();
94 }
95
96 protected Comparator<MetaInfResource> getComparator(MetaInfGroup group) {
97 if (MetaInfGroup.OTHER.equals(group)) {
98
99
100
101
102
103
104 return new MetaInfResourceFilenameComparator();
105 } else {
106 return new MetaInfResourceLocationComparator();
107 }
108 }
109
110 protected Map<MetaInfGroup, String> getDefaultIncludes(Project project, String impexArtifactId, String vendor) {
111 String resourcePath = ProjectUtils.getResourcePath(project.getGroupId(), project.getArtifactId());
112 Map<MetaInfGroup, String> map = new HashMap<MetaInfGroup, String>();
113 map.put(MetaInfGroup.SCHEMA, resourcePath + "/initial-db/sql/" + vendor + "/" + impexArtifactId + ".sql");
114 map.put(MetaInfGroup.DATA, resourcePath + "/initial-db/sql/" + vendor + "/*.sql");
115 map.put(MetaInfGroup.CONSTRAINTS, resourcePath + "/initial-db/sql/" + vendor + "/" + impexArtifactId + "-constraints.sql");
116 map.put(MetaInfGroup.OTHER, resourcePath + "/upgrades/**/" + vendor + "/**/*.sql");
117 return map;
118 }
119
120 protected Map<MetaInfGroup, String> getDefaultExcludes(Map<MetaInfGroup, String> defaultIncludes) {
121 Map<MetaInfGroup, String> map = new HashMap<MetaInfGroup, String>();
122
123 map.put(MetaInfGroup.SCHEMA, NullUtils.NONE);
124
125 map.put(MetaInfGroup.DATA, defaultIncludes.get(MetaInfGroup.SCHEMA) + "," + defaultIncludes.get(MetaInfGroup.CONSTRAINTS));
126
127 map.put(MetaInfGroup.CONSTRAINTS, NullUtils.NONE);
128
129 map.put(MetaInfGroup.OTHER, NullUtils.NONE);
130 return map;
131 }
132
133 }