001/**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.sql.spring;
017
018import java.io.File;
019import java.util.Properties;
020
021import com.google.common.collect.Lists;
022import org.kuali.common.util.execute.Executable;
023import org.kuali.common.util.execute.spring.ExecutableConfig;
024import org.kuali.common.util.properties.spring.EnvironmentPropertySourceConfig;
025import org.kuali.common.util.runonce.smart.PropertiesFileRunOnce;
026import org.kuali.common.util.runonce.smart.RunOnce;
027import org.kuali.common.util.runonce.smart.RunOnceExecutable;
028import org.kuali.common.util.spring.SpringExecUtils;
029import org.kuali.common.util.spring.service.SpringService;
030import org.kuali.common.util.spring.service.SpringServiceConfig;
031import org.kuali.rice.core.api.config.property.ConfigContext;
032import org.springframework.beans.factory.annotation.Autowired;
033import org.springframework.context.annotation.Bean;
034import org.springframework.context.annotation.Configuration;
035import org.springframework.context.annotation.Import;
036import org.springframework.core.env.PropertySource;
037
038/**
039 * Set up an {@code Executable} that resets the database.
040 *
041 * @author Kuali Rice Team (rice.collab@kuali.org)
042 */
043@Configuration
044@Import({ SpringServiceConfig.class, EnvironmentPropertySourceConfig.class })
045public class SourceSqlRunOnceConfig implements ExecutableConfig {
046
047    private static final String PROJECT_HOME_KEY = "project.home";
048    private static final String RUNONCE_FILENAME = "runonce.properties";
049    private static final String ENCODING = "UTF-8";
050    private static final String PROPERTY_KEY = "project.db.reset";
051
052    /**
053     * The Spring loader.
054     */
055    @Autowired
056    SpringService service;
057
058    /**
059     * The final list of properties.
060     */
061    @Autowired
062    PropertySource<?> propertySource;
063
064    /**
065     * {@inheritDoc}
066     *
067     * <p>
068     * The {@code project.home} property needs to come from the {@link ConfigContext} instead of the
069     * {@link org.kuali.common.util.spring.env.EnvironmentService} for the scenario where nobody has wired in a
070     * bootstrap PSC in order to help manage the resetting of the database via RunOnce.
071     * </p>
072     */
073    @Override
074    @Bean(initMethod = "execute")
075    public Executable executable() {
076        Properties properties = ConfigContext.getCurrentContextConfig().getProperties();
077        String projectHome = properties.getProperty(PROJECT_HOME_KEY);
078
079        File file = new File(projectHome, RUNONCE_FILENAME);
080        RunOnce runOnce = PropertiesFileRunOnce.builder(file, ENCODING, PROPERTY_KEY).build();
081        Executable executable = SpringExecUtils.getSpringExecutable(service, propertySource, SourceSqlExecConfig.class,
082                Lists.newArrayList("master"));
083
084        return RunOnceExecutable.builder(executable, runOnce).build();
085    }
086
087}