View Javadoc

1   package org.kuali.common.jdbc;
2   
3   import java.util.ArrayList;
4   import java.util.Collections;
5   import java.util.List;
6   import java.util.Properties;
7   
8   import org.apache.commons.lang3.StringUtils;
9   import org.kuali.common.jdbc.context.DatabaseResetContext;
10  import org.kuali.common.util.CollectionUtils;
11  import org.kuali.common.util.LocationUtils;
12  import org.kuali.common.util.PropertyUtils;
13  import org.kuali.common.util.SimpleFormatter;
14  import org.slf4j.Logger;
15  import org.slf4j.LoggerFactory;
16  
17  public class DefaultDatabaseService implements DatabaseService {
18  	private static final Logger logger = LoggerFactory.getLogger(DefaultDatabaseService.class);
19  
20  	@Override
21  	public void reset(DatabaseResetContext context) {
22  		long start = System.currentTimeMillis();
23  		logger.info("---------------- Reset Database ----------------");
24  		logger.info("Vendor - {}", context.getDatabaseProcessContext().getVendor());
25  		logger.info("URL - {}", context.getDatabaseProcessContext().getUrl());
26  		logger.info("User - {}", context.getDatabaseProcessContext().getUsername());
27  		logger.debug("Password - {}", context.getDatabaseProcessContext().getPassword());
28  		logger.info("DBA URL - {}", context.getDatabaseProcessContext().getDbaUrl());
29  		logger.info("DBA User - {}", context.getDatabaseProcessContext().getDbaUsername());
30  		logger.debug("DBA Password - {}", context.getDatabaseProcessContext().getDbaPassword());
31  		JdbcMetaData metadata = context.getService().getJdbcMetaData(context.getDbaJdbcContext().getDataSource());
32  		logger.info("Product Name - {}", metadata.getDatabaseProductName());
33  		logger.info("Product Version - {}", metadata.getDatabaseProductVersion());
34  		logger.info("Driver - {}", context.getDatabaseProcessContext().getDriver());
35  		logger.info("Driver Name - {}", metadata.getDriverName());
36  		logger.info("Driver Version - {}", metadata.getDriverVersion());
37  		logger.info("SQL Encoding - {}", context.getEncoding());
38  		logger.info("------------------------------------------------");
39  		SqlMetaDataList metaData = new SqlMetaDataList();
40  		add(metaData, doDba(context));
41  		add(metaData, doSQL(context, "schema", context.getSchemaPropertyPrefix()));
42  		context.getNormalJdbcContext().setShowProgressMin(5000);
43  		add(metaData, doSQL(context, "data load", context.getDataPropertyPrefix()));
44  		context.getNormalJdbcContext().setShowProgressMin(50);
45  		add(metaData, doSQL(context, "constraints", context.getConstraintPropertyPrefix()));
46  		metaData.setExecutionTime(System.currentTimeMillis() - start);
47  		logExecution("reset database", metaData, context.getFormatter());
48  	}
49  
50  	protected void add(SqlMetaDataList one, SqlMetaDataList two) {
51  		one.setCount(one.getCount() + two.getCount());
52  		one.addAll(two);
53  	}
54  
55  	protected SqlMetaDataList doDba(DatabaseResetContext context) {
56  		logger.info("Executing DBA SQL");
57  		SqlMetaDataList metadata = context.getService().executeSqlStrings(context.getDbaJdbcContext(), Collections.singletonList(context.getDbaSql()));
58  		logExecution("dba", metadata, context.getFormatter());
59  		return metadata;
60  	}
61  
62  	protected SqlMetaDataList doSQL(DatabaseResetContext context, String type, String prefix) {
63  		List<String> locations = getLocations(context.getProperties(), prefix, context.getLocationListPattern());
64  		logger.info("Executing " + type + " SQL");
65  		SqlMetaDataList metadata = context.getService().executeSql(context.getNormalJdbcContext(), locations, context.getEncoding());
66  		logExecution(type, metadata, context.getFormatter());
67  		return metadata;
68  	}
69  
70  	protected List<String> getLocations(Properties properties, String prefix, String locationListPattern) {
71  		List<String> keys = PropertyUtils.getStartsWithKeys(properties, prefix);
72  		List<String> locations = new ArrayList<String>();
73  		for (String key : keys) {
74  			String value = properties.getProperty(key);
75  			if (isLocationList(key, locationListPattern)) {
76  				List<String> list = LocationUtils.getLocations(value);
77  				locations.addAll(list);
78  			} else {
79  				locations.add(value);
80  			}
81  		}
82  		return locations;
83  	}
84  
85  	protected boolean isLocationList(String key, String pattern) {
86  		return StringUtils.contains(key, pattern);
87  	}
88  
89  	protected void logExecution(String executionType, SqlMetaDataList metadata, SimpleFormatter formatter) {
90  		List<Object> args = new ArrayList<Object>();
91  		args.add(formatter.getCount(metadata.getCount()));
92  		args.add(formatter.getCount(metadata.size()));
93  		args.add(formatter.getTime(metadata.getExecutionTime()));
94  		logger.info("Total " + executionType + " SQL statements: {}  SQL sources: {}  Total time: {}", CollectionUtils.toArray(args));
95  	}
96  
97  }