View Javadoc
1   /**
2    * Copyright 2010-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.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   * TODO Should not be a class called {@code RiceSqlConfig} down here in kuali-util. Create a rice-util and move this there? Main issue preventing this from living in the rice-xml
45   * module itself is that it gets tricky having software used very early in the build lifecycle reside in the same project that makes use of it.
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  			// The upgrades folder for Rice has a nested directory structure - [server|client]:[bootstrap|demo|test].
99  			// At the moment, the sorting of SQL located inside the "upgrades" folder for Rice ignores the directory structure and just sorts by filename.
100 			// The idea is that the "initial-db" folder inside Rice will soon have a structure similar to the "upgrades" folder.
101 			// This should enable "additive" dataset generation instead of "subtractive".
102 			// Once the "initial-db" structure is in place, this specialized comparator should be removed.
103 			// All SQL resources would then be sorted by the fully qualified location.
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 		// The schema includes is specific to one exact file, no need to exclude anything
123 		map.put(MetaInfGroup.SCHEMA, NullUtils.NONE);
124 		// Exclude the schema + constraints SQL
125 		map.put(MetaInfGroup.DATA, defaultIncludes.get(MetaInfGroup.SCHEMA) + "," + defaultIncludes.get(MetaInfGroup.CONSTRAINTS));
126 		// The constraint includes is specific to one exact file, no need to exclude anything
127 		map.put(MetaInfGroup.CONSTRAINTS, NullUtils.NONE);
128 		// No need to exclude any of the "upgrades" SQL
129 		map.put(MetaInfGroup.OTHER, NullUtils.NONE);
130 		return map;
131 	}
132 
133 }