View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.sql.spring;
17  
18  import java.io.File;
19  import java.util.Properties;
20  
21  import org.kuali.common.util.execute.Executable;
22  import org.kuali.common.util.execute.spring.ExecutableConfig;
23  import org.kuali.common.util.properties.spring.EnvironmentPropertySourceConfig;
24  import org.kuali.common.util.runonce.smart.PropertiesFileRunOnce;
25  import org.kuali.common.util.runonce.smart.RunOnce;
26  import org.kuali.common.util.runonce.smart.RunOnceExecutable;
27  import org.kuali.common.util.spring.SpringExecUtils;
28  import org.kuali.common.util.spring.service.SpringService;
29  import org.kuali.common.util.spring.service.SpringServiceConfig;
30  import org.kuali.rice.core.api.config.property.ConfigContext;
31  import org.springframework.beans.factory.annotation.Autowired;
32  import org.springframework.context.annotation.Bean;
33  import org.springframework.context.annotation.Configuration;
34  import org.springframework.context.annotation.Import;
35  import org.springframework.core.env.PropertySource;
36  
37  /**
38   * Set up an {@code Executable} that resets the database.
39   *
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  @Configuration
43  @Import({ SpringServiceConfig.class, EnvironmentPropertySourceConfig.class })
44  public class SourceSqlRunOnceConfig implements ExecutableConfig {
45  
46      @Autowired
47      SpringService service;
48  
49      @Autowired
50      PropertySource<?> propertySource;
51  
52      private static final String PROJECT_HOME_KEY = "project.home";
53      private static final String RUNONCE_FILENAME = "runonce.properties";
54      private static final String ENCODING = "UTF-8";
55      private static final String PROPERTY_KEY = "project.db.reset";
56  
57      @Override
58      @Bean(initMethod = "execute")
59      public Executable executable() {
60          // This needs to come from ConfigContext instead of EnvironmentService for the scenario where nobody
61          // has wired in a bootstrap PSC in order to help manage the resetting of the database via RunOnce
62          Properties properties = ConfigContext.getCurrentContextConfig().getProperties();
63          String projectHome = properties.getProperty(PROJECT_HOME_KEY);
64  
65          File file = new File(projectHome, RUNONCE_FILENAME);
66          RunOnce runOnce = PropertiesFileRunOnce.builder(file, ENCODING, PROPERTY_KEY).build();
67          Executable executable = SpringExecUtils.getSpringExecutable(service, propertySource, SourceSqlExecConfig.class);
68  
69          return RunOnceExecutable.builder(executable, runOnce).build();
70      }
71  }