View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.common.impex.spring;
17  
18  import java.util.List;
19  
20  import org.kuali.common.impex.data.DumpDataExecutable;
21  import org.kuali.common.impex.data.service.DumpDataContext;
22  import org.kuali.common.impex.util.DumpConstants;
23  import org.kuali.common.impex.util.DumpUtils;
24  import org.kuali.common.jdbc.spring.JdbcDataSourceConfig;
25  import org.kuali.common.util.LocationUtils;
26  import org.kuali.common.util.StringFilter;
27  import org.kuali.common.util.spring.SpringUtils;
28  import org.springframework.beans.factory.annotation.Autowired;
29  import org.springframework.context.annotation.Bean;
30  import org.springframework.context.annotation.Configuration;
31  import org.springframework.core.env.Environment;
32  
33  @Configuration
34  public class DumpDataConfig {
35  
36  	// These are often customized
37  	private static final String DIR_KEY = "impex.dump.data.dir";
38  	private static final String STATS_LOCATION_KEY = "impex.dump.data.stats.location";
39  	private static final String INCLUDES_KEY = "impex.dump.data.includes";
40  	private static final String EXCLUDES_KEY = "impex.dump.data.excludes";
41  
42  	// Defaults for these are usually ok
43  	private static final String ENCODING_KEY = "impex.dump.data.encoding";
44  	private static final String THREADS_KEY = "impex.dump.data.threads";
45  	private static final String ROW_INTERVAL_KEY = "impex.dump.data.rowInterval";
46  	private static final String DATA_INTERVAL_KEY = "impex.dump.data.dataInterval";
47  	private static final String SERVICE_KEY = "impex.dump.data.service";
48  	private static final String SKIP_KEY = "impex.dump.data.skip";
49  
50  	@Autowired
51  	Environment env;
52  
53  	@Autowired
54  	JdbcDataSourceConfig dataSourceConfig;
55  
56  	@Bean
57  	public DumpDataExecutable dumpDataExecutable() {
58  
59  		// Extract some context from the Environment
60  		DumpDataContext context = getDumpDataContext();
61  
62  		// Setup an executable for exporting the data
63  		DumpDataExecutable exec = new DumpDataExecutable();
64  		exec.setSkip(SpringUtils.getBoolean(env, SKIP_KEY, DumpDataExecutable.DEFAULT_SKIP_EXECUTION));
65  		exec.setService(SpringUtils.getInstance(env, SERVICE_KEY, DumpDataExecutable.DEFAULT_SERVICE.getClass()));
66  		exec.setContext(context);
67  		return exec;
68  	}
69  
70  	protected DumpDataContext getDumpDataContext() {
71  		DumpDataContext context = new DumpDataContext();
72  		context.setWorkingDir(LocationUtils.getFileQuietly(SpringUtils.getProperty(env, DIR_KEY)));
73  		context.setTableStatisticsLocation(SpringUtils.getProperty(env, STATS_LOCATION_KEY));
74  		context.setDataThreads(SpringUtils.getInteger(env, THREADS_KEY, DumpUtils.DEFAULT_DATA_THREADS));
75  		context.setRowCountInterval(SpringUtils.getInteger(env, ROW_INTERVAL_KEY, DumpUtils.DEFAULT_ROW_INTERVAL));
76  		context.setDataSizeInterval(SpringUtils.getBytesInteger(env, DATA_INTERVAL_KEY, DumpUtils.DEFAULT_DATA_INTERVAL));
77  		context.setDataSource(dataSourceConfig.jdbcDataSource());
78  		context.setEncoding(SpringUtils.getProperty(env, ENCODING_KEY));
79  		context.setTableNameFilter(getTableNameFilter());
80  		return context;
81  	}
82  
83  	protected StringFilter getTableNameFilter() {
84  		List<String> tableIncludes = SpringUtils.getListFromCSV(env, INCLUDES_KEY, DumpConstants.DEFAULT_REGEX_INCLUDE);
85  		List<String> tableExcludes = SpringUtils.getListFromCSV(env, EXCLUDES_KEY, DumpConstants.DEFAULT_REGEX_EXCLUDE);
86  		return StringFilter.getInstance(tableIncludes, tableExcludes);
87  	}
88  
89  }