1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.impex.spring;
17
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21
22 import org.kuali.common.impex.model.Schema;
23 import org.kuali.common.impex.schema.ProduceSchemaExecutable;
24 import org.kuali.common.impex.schema.SchemaSqlProducer;
25 import org.kuali.common.util.spring.SpringUtils;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.context.annotation.Bean;
28 import org.springframework.context.annotation.Configuration;
29 import org.springframework.context.annotation.Import;
30 import org.springframework.core.env.Environment;
31
32 @Configuration
33 @Import({ XmlSchemaConfig.class, SchemaSqlProducerConfig.class })
34 public class ProduceSchemaConfig {
35
36 @Autowired
37 Environment env;
38
39 @Autowired
40 Schema schema;
41
42 @Autowired
43 SchemaSqlProducer schemaProducer;
44
45
46
47
48 protected final static String DUMP_FOLDER_KEY = "impex.schemadump.folder";
49
50
51
52
53 protected final static String FILENAME_PREFIX_KEY = "impex.schemadump.prefix";
54
55
56
57
58 protected final static String SKIP_EXECUTION_KEY = "impex.schemadump.skip";
59
60 protected final static String DEFAULT_FILENAME_PREFIX = "dump-";
61
62 protected final static String TABLE_FILE = "tables.sql";
63 protected final static String FOREIGN_KEY_FILE = "foreignKeys.sql";
64 protected final static String SEQUENCE_FILE = "sequences.sql";
65 protected final static String VIEW_FILE = "views.sql";
66
67 @Bean(initMethod = "execute")
68 public ProduceSchemaExecutable executable() {
69 ProduceSchemaExecutable exec = new ProduceSchemaExecutable();
70 exec.setFileNamesToSqls(fileNamesToSqls());
71 exec.setSkip(skipExecution());
72
73 return exec;
74 }
75
76 @Bean
77 public boolean skipExecution() {
78 return SpringUtils.getBoolean(env, SKIP_EXECUTION_KEY, ProduceSchemaExecutable.DEFAULT_SKIP_EXECUTION);
79 }
80
81 @Bean
82 public Map<String, List<String>> fileNamesToSqls() {
83
84 Map<String, List<String>> fileNamesToSqls = new HashMap<String, List<String>>();
85
86 fileNamesToSqls.put(getFileName(TABLE_FILE), schemaProducer.getTablesSql(schema.getTables()));
87 fileNamesToSqls.put(getFileName(FOREIGN_KEY_FILE), schemaProducer.getForeignKeySql(schema.getForeignKeys()));
88 fileNamesToSqls.put(getFileName(SEQUENCE_FILE), schemaProducer.getSequencesSql(schema.getSequences()));
89 fileNamesToSqls.put(getFileName(VIEW_FILE), schemaProducer.getViewsSql(schema.getViews()));
90
91 return fileNamesToSqls;
92 }
93
94 protected String getFileName(String fileNameSuffix) {
95 StringBuilder sb = new StringBuilder();
96
97 String dumpFolder = SpringUtils.getProperty(env, DUMP_FOLDER_KEY);
98 String fileNamePrefix = SpringUtils.getProperty(env, FILENAME_PREFIX_KEY, DEFAULT_FILENAME_PREFIX);
99
100 sb.append(dumpFolder).append(fileNamePrefix).append(fileNameSuffix);
101
102 return sb.toString();
103 }
104 }