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.execute;
17  
18  import java.util.Properties;
19  
20  import org.apache.commons.lang3.StringUtils;
21  import org.kuali.common.util.Version;
22  import org.kuali.common.util.VersionUtils;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  import org.springframework.util.Assert;
26  
27  public class SetSourceDbSchemaNameExecutable implements Executable {
28  
29  	private static final Logger logger = LoggerFactory.getLogger(SetSourceDbSchemaNameExecutable.class);
30  
31  	String sourceDbSchemaNameKey = "jdbc.source.db.username";
32  	String dbBranchQualifierKey = "db.branch.qualifier";
33  	// eg KS_SOURCE_DB
34  	String baseSourceDbSchemaName;
35  	Properties mavenProperties;
36  	String version;
37  	boolean skip;
38  
39  	@Override
40  	public void execute() {
41  		// Might be skipping execution altogether
42  		if (skip) {
43  			logger.info("Skipping execution");
44  			return;
45  		}
46  
47  		// Make sure we are configured correctly
48  		Assert.notNull(mavenProperties, "mavenProperties is null");
49  		Assert.hasText(baseSourceDbSchemaName, "baseSourceDbSchemaName is blank");
50  
51  		// Check to see if the source db name is already set
52  		String existingValue = mavenProperties.getProperty(sourceDbSchemaNameKey);
53  		if (!StringUtils.isBlank(existingValue)) {
54  			logger.info("Existing value - [{}={}]", sourceDbSchemaNameKey, existingValue);
55  			return;
56  		}
57  
58  		// Get the qualifier we will append to KS_SOURCE_DB for this branch
59  		String qualifier = getQualifier(mavenProperties, dbBranchQualifierKey, version);
60  
61  		// This is the full schema name that Impex will use
62  		String sourceDbSchemaName = baseSourceDbSchemaName + qualifier;
63  
64  		// Update the internal Maven properties object with the new value
65  		mavenProperties.setProperty(sourceDbSchemaNameKey, sourceDbSchemaName);
66  		logger.debug("Set property - [{}={}]", sourceDbSchemaNameKey, sourceDbSchemaName);
67  	}
68  
69  	protected String getQualifier(Properties mavenProperties, String explicitQualifierKey, String version) {
70  		// Determine if the qualifier is already set
71  		String qualifier = mavenProperties.getProperty(explicitQualifierKey);
72  		if (!StringUtils.isBlank(qualifier)) {
73  			return "_" + qualifier;
74  		}
75  
76  		// Parse the version string into a version pojo
77  		Version v = VersionUtils.getVersion(version);
78  
79  		// If the version qualifier is blank there is nothing more to do
80  		if (StringUtils.isBlank(v.getQualifier())) {
81  			return qualifier;
82  		}
83  
84  		// Transform and append the qualifier based on the qualifier from the version
85  		qualifier = v.getQualifier();
86  		qualifier = StringUtils.upperCase(qualifier);
87  		qualifier = StringUtils.replace(qualifier, "-", "_");
88  		qualifier = StringUtils.replace(qualifier, ".", "_");
89  		qualifier = "_" + qualifier;
90  		return qualifier;
91  	}
92  
93  	public Properties getMavenProperties() {
94  		return mavenProperties;
95  	}
96  
97  	public void setMavenProperties(Properties mavenProperties) {
98  		this.mavenProperties = mavenProperties;
99  	}
100 
101 	public String getVersion() {
102 		return version;
103 	}
104 
105 	public void setVersion(String version) {
106 		this.version = version;
107 	}
108 
109 	public boolean isSkip() {
110 		return skip;
111 	}
112 
113 	public void setSkip(boolean skip) {
114 		this.skip = skip;
115 	}
116 
117 	public String getBaseSourceDbSchemaName() {
118 		return baseSourceDbSchemaName;
119 	}
120 
121 	public void setBaseSourceDbSchemaName(String sourceDbSchemaName) {
122 		this.baseSourceDbSchemaName = sourceDbSchemaName;
123 	}
124 
125 	public String getSourceDbSchemaNameKey() {
126 		return sourceDbSchemaNameKey;
127 	}
128 
129 	public void setSourceDbSchemaNameKey(String sourceDbSchemaNameKey) {
130 		this.sourceDbSchemaNameKey = sourceDbSchemaNameKey;
131 	}
132 
133 	public String getDbBranchQualifierKey() {
134 		return dbBranchQualifierKey;
135 	}
136 
137 	public void setDbBranchQualifierKey(String dbBranchQualifierKey) {
138 		this.dbBranchQualifierKey = dbBranchQualifierKey;
139 	}
140 
141 }