View Javadoc
1   /**
2    * Copyright 2005-2016 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 com.google.common.collect.Lists;
22  import org.kuali.common.util.execute.Executable;
23  import org.kuali.common.util.execute.spring.ExecutableConfig;
24  import org.kuali.common.util.properties.spring.EnvironmentPropertySourceConfig;
25  import org.kuali.common.util.runonce.smart.PropertiesFileRunOnce;
26  import org.kuali.common.util.runonce.smart.RunOnce;
27  import org.kuali.common.util.runonce.smart.RunOnceExecutable;
28  import org.kuali.common.util.spring.SpringExecUtils;
29  import org.kuali.common.util.spring.service.SpringService;
30  import org.kuali.common.util.spring.service.SpringServiceConfig;
31  import org.kuali.rice.core.api.config.property.ConfigContext;
32  import org.springframework.beans.factory.annotation.Autowired;
33  import org.springframework.context.annotation.Bean;
34  import org.springframework.context.annotation.Configuration;
35  import org.springframework.context.annotation.Import;
36  import org.springframework.core.env.PropertySource;
37  
38  /**
39   * Set up an {@code Executable} that resets the database.
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  @Configuration
44  @Import({ SpringServiceConfig.class, EnvironmentPropertySourceConfig.class })
45  public class SourceSqlRunOnceConfig implements ExecutableConfig {
46  
47      private static final String PROJECT_HOME_KEY = "project.home";
48      private static final String RUNONCE_FILENAME = "runonce.properties";
49      private static final String ENCODING = "UTF-8";
50      private static final String PROPERTY_KEY = "project.db.reset";
51  
52      /**
53       * The Spring loader.
54       */
55      @Autowired
56      SpringService service;
57  
58      /**
59       * The final list of properties.
60       */
61      @Autowired
62      PropertySource<?> propertySource;
63  
64      /**
65       * {@inheritDoc}
66       *
67       * <p>
68       * The {@code project.home} property needs to come from the {@link ConfigContext} instead of the
69       * {@link org.kuali.common.util.spring.env.EnvironmentService} for the scenario where nobody has wired in a
70       * bootstrap PSC in order to help manage the resetting of the database via RunOnce.
71       * </p>
72       */
73      @Override
74      @Bean(initMethod = "execute")
75      public Executable executable() {
76          Properties properties = ConfigContext.getCurrentContextConfig().getProperties();
77          String projectHome = properties.getProperty(PROJECT_HOME_KEY);
78  
79          File file = new File(projectHome, RUNONCE_FILENAME);
80          RunOnce runOnce = PropertiesFileRunOnce.builder(file, ENCODING, PROPERTY_KEY).build();
81          Executable executable = SpringExecUtils.getSpringExecutable(service, propertySource, SourceSqlExecConfig.class,
82                  Lists.newArrayList("master"));
83  
84          return RunOnceExecutable.builder(executable, runOnce).build();
85      }
86  
87  }