001/**
002 * Copyright 2005-2018 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.xml.spring;
017
018import java.util.Properties;
019
020import com.google.common.collect.Lists;
021import org.kuali.common.util.execute.Executable;
022import org.kuali.common.util.properties.spring.EnvironmentPropertySourceConfig;
023import org.kuali.common.util.runonce.smart.RunOnce;
024import org.kuali.common.util.runonce.smart.RunOnceExecutable;
025import org.kuali.common.util.spring.SpringExecUtils;
026import org.kuali.common.util.spring.event.ApplicationEventListenerConfig;
027import org.kuali.common.util.spring.event.ExecutableApplicationEventListener;
028import org.kuali.common.util.spring.service.SpringService;
029import org.kuali.common.util.spring.service.SpringServiceConfig;
030import org.kuali.rice.core.api.CoreConstants;
031import org.kuali.rice.core.api.config.property.ConfigContext;
032import org.kuali.rice.xml.ingest.ParameterServiceRunOnce;
033import org.springframework.beans.factory.annotation.Autowired;
034import org.springframework.context.annotation.Bean;
035import org.springframework.context.annotation.Configuration;
036import org.springframework.context.annotation.Import;
037import org.springframework.context.event.ContextRefreshedEvent;
038import org.springframework.context.event.SmartApplicationListener;
039import org.springframework.core.Ordered;
040import org.springframework.core.env.PropertySource;
041
042/**
043 * Set up an {@code SmartApplicationListener} that ingests XML when it receives a {@code ContextRefreshedEvent}.
044 * 
045 * @author Kuali Rice Team (rice.collab@kuali.org)
046 */
047@Configuration
048@Import({ SpringServiceConfig.class, EnvironmentPropertySourceConfig.class })
049public class IngestXmlRunOnceConfig implements ApplicationEventListenerConfig {
050
051    private static final String ORDER_KEY = "rice.ingest.order";
052    private static final String NAMESPACE_KEY = "rice.ingest.param.namespace";
053    private static final String COMPONENT_KEY = "rice.ingest.param.component";
054    private static final String NAME_KEY = "rice.ingest.param.name";
055    private static final String DESCRIPTION_KEY = "rice.ingest.param.description";
056    private static final String RUN_ON_MISSING_PARAMETER_KEY = "rice.ingest.runOnMissingParameter";
057
058    private static final String NAMESPACE = "KR-WKFLW";
059    private static final String COMPONENT = "All";
060    private static final String NAME = "INGEST_XML_AT_STARTUP_IND";
061    private static final String DESCRIPTION = "Set this to 'Y' to ingest XML documents at application startup";
062    private static final Boolean RUN_ON_MISSING_PARAMETER = Boolean.FALSE;
063
064    /**
065     * The Spring loader.
066     */
067    @Autowired
068    SpringService service;
069
070    /**
071     * The final list of properties.
072     */
073    @Autowired
074    PropertySource<?> propertySource;
075
076    /**
077     * {@inheritDoc}
078     *
079     * <p>
080     * The {@link org.kuali.rice.coreservice.api.parameter.Parameter} properties need to come from the
081     * {@link ConfigContext} instead of the {@link org.kuali.common.util.spring.env.EnvironmentService} for the scenario
082     * where nobody has wired in a bootstrap PSC in order to help manage the resetting of the database via RunOnce.
083     * </p>
084     */
085        @Override
086        @Bean
087        public SmartApplicationListener applicationEventListener() {
088                Properties properties = ConfigContext.getCurrentContextConfig().getProperties();
089                String applicationId = properties.getProperty(CoreConstants.Config.APPLICATION_ID);
090                String namespace = properties.getProperty(NAMESPACE_KEY, NAMESPACE);
091                String component = properties.getProperty(COMPONENT_KEY, COMPONENT);
092                String name = properties.getProperty(NAME_KEY, NAME);
093                String description = properties.getProperty(DESCRIPTION_KEY, DESCRIPTION);
094                boolean runOnMissingParameter = Boolean.parseBoolean(properties.getProperty(RUN_ON_MISSING_PARAMETER_KEY, RUN_ON_MISSING_PARAMETER.toString()));
095        int order = Integer.parseInt(properties.getProperty(ORDER_KEY, String.valueOf(Ordered.LOWEST_PRECEDENCE)));
096
097                RunOnce runOnce = ParameterServiceRunOnce.builder(applicationId, namespace, component, name)
098                .description(description).runOnMissingParameter(runOnMissingParameter).build();
099                Executable springExecutable = SpringExecUtils.getSpringExecutable(service, propertySource, IngestXmlExecConfig.class,
100                Lists.newArrayList("master"));
101        Executable executable = RunOnceExecutable.builder(springExecutable, runOnce).build();
102
103                return ExecutableApplicationEventListener.builder(executable, ContextRefreshedEvent.class).order(order).build();
104        }
105
106}