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.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       * This property key points to the user defined folder to output sql files A property with the key is required to build the executable
47       */
48      protected final static String DUMP_FOLDER_KEY = "impex.schemadump.folder";
49  
50      /**
51       * This property key points to the user defined prefix for dumped file names
52       */
53      protected final static String FILENAME_PREFIX_KEY = "impex.schemadump.prefix";
54  
55      /**
56       * This property key maps to a boolean value of whether or not to skip execution
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 }