View Javadoc

1   package org.kuali.common.jdbc.vendor.service;
2   
3   import java.sql.Driver;
4   import java.util.List;
5   import java.util.Properties;
6   
7   import org.kuali.common.jdbc.model.context.ConnectionContext;
8   import org.kuali.common.jdbc.sql.model.AdminSql;
9   import org.kuali.common.jdbc.sql.model.DbaSql;
10  import org.kuali.common.jdbc.vendor.model.DatabaseVendor;
11  import org.kuali.common.jdbc.vendor.model.VendorDefault;
12  import org.kuali.common.jdbc.vendor.model.VendorSql;
13  import org.kuali.common.jdbc.vendor.model.keys.Admin;
14  import org.kuali.common.jdbc.vendor.model.keys.Basic;
15  import org.kuali.common.jdbc.vendor.model.keys.Dba;
16  import org.kuali.common.util.Assert;
17  import org.kuali.common.util.ReflectionUtils;
18  import org.kuali.common.util.spring.env.EnvironmentService;
19  import org.kuali.common.util.spring.env.model.EnvironmentKeySuffix;
20  
21  public class DefaultDatabaseVendorService implements DatabaseVendorService {
22  
23  	public DefaultDatabaseVendorService(EnvironmentService env, VendorDefault vendorDefault) {
24  		Assert.noNulls(env, vendorDefault);
25  		this.env = env;
26  		this.vendorDefault = vendorDefault;
27  	}
28  
29  	private final EnvironmentService env;
30  	private final VendorDefault vendorDefault;
31  
32  	@Override
33  	public DatabaseVendor getDatabaseVendor() {
34  		ConnectionContext dba = getDba();
35  		Class<? extends Driver> driver = getDriver();
36  		Properties sql = getSql();
37  		AdminSql adminSql = getAdminSql(sql);
38  		DbaSql dbaSql = getDbaSql(adminSql, sql);
39  		VendorSql vendorSql = new VendorSql(adminSql, dbaSql, sql);
40  		String url = getUrl(dba);
41  		return new DatabaseVendor(vendorDefault, dba, url, driver, vendorSql);
42  	}
43  
44  	protected String getUrl(ConnectionContext dba) {
45  		String key = vendorDefault.getCode() + "." + Basic.URL.getValue();
46  		String defaultValue = dba.getUrl();
47  		String actualValue = env.getString(key, defaultValue);
48  		return actualValue;
49  	}
50  
51  	protected DbaSql getDbaSql(AdminSql adminSql, Properties sql) {
52  		String before = getDbaBefore(vendorDefault.getCode() + "." + Dba.BEFORE.getValue(), adminSql, sql);
53  		String after = getDbaAfter(vendorDefault.getCode() + "." + Dba.AFTER.getValue(), adminSql, sql);
54  		return new DbaSql(before, after);
55  	}
56  
57  	protected String getDbaBefore(String key, AdminSql adminSql, Properties sql) {
58  		return env.getString(key, adminSql.getValidate() + adminSql.getDrop() + adminSql.getCreate());
59  	}
60  
61  	protected String getDbaAfter(String key, AdminSql adminSql, Properties sql) {
62  		return env.getString(key, adminSql.getValidate());
63  	}
64  
65  	protected AdminSql getAdminSql(Properties sql) {
66  		String validate = sql.getProperty(vendorDefault.getCode() + "." + Admin.VALIDATE.getValue());
67  		String create = sql.getProperty(vendorDefault.getCode() + "." + Admin.CREATE.getValue());
68  		String drop = sql.getProperty(vendorDefault.getCode() + "." + Admin.DROP.getValue());
69  		return new AdminSql(validate, create, drop);
70  	}
71  
72  	protected String getSql(String key, Properties sql) {
73  		String defaultValue = sql.getProperty(key);
74  		return env.getString(key, defaultValue);
75  	}
76  
77  	protected Properties getSql() {
78  		List<EnvironmentKeySuffix> suffixes = getSqlKeySuffixes();
79  		Properties properties = new Properties();
80  		for (EnvironmentKeySuffix suffix : suffixes) {
81  			String key = vendorDefault.getCode() + "." + suffix.getValue();
82  			String sql = env.getString(key);
83  			properties.setProperty(key, sql);
84  		}
85  		return properties;
86  	}
87  
88  	protected Class<? extends Driver> getDriver() {
89  		String driver = env.getString(vendorDefault.getCode() + "." + Basic.DRIVER.getValue(), vendorDefault.getDriver());
90  		return ReflectionUtils.getTypedClass(driver);
91  	}
92  
93  	protected ConnectionContext getDba() {
94  		String dbaUrl = env.getString(vendorDefault.getCode() + "." + Dba.URL.getValue(), vendorDefault.getDba().getUrl());
95  		String dbaUsr = env.getString(vendorDefault.getCode() + "." + Dba.USERNAME.getValue(), vendorDefault.getDba().getCredentials().getUsername());
96  		String dbaPwd = env.getString(vendorDefault.getCode() + "." + Dba.PASSWORD.getValue(), vendorDefault.getDba().getCredentials().getPassword());
97  		return new ConnectionContext(dbaUrl, dbaUsr, dbaPwd);
98  	}
99  
100 	protected List<EnvironmentKeySuffix> getSqlKeySuffixes() {
101 		return Admin.asList();
102 	}
103 
104 	public EnvironmentService getEnv() {
105 		return env;
106 	}
107 
108 	public VendorDefault getVendorDefault() {
109 		return vendorDefault;
110 	}
111 
112 }