blob: 524b6a833bcc643fe6789691d69d50c63d80ad2d [file] [log] [blame]
/*
* Copyright 2017 The Mifos Initiative.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.mifos.deposit.service.internal.command.handler;
import io.mifos.core.command.annotation.Aggregate;
import io.mifos.core.command.annotation.CommandHandler;
import io.mifos.core.command.annotation.EventEmitter;
import io.mifos.core.lang.ApplicationName;
import io.mifos.core.mariadb.domain.FlywayFactoryBean;
import io.mifos.deposit.api.v1.EventConstants;
import io.mifos.deposit.service.ServiceConstants;
import io.mifos.deposit.service.internal.command.MigrationCommand;
import io.mifos.deposit.service.internal.service.helper.RhythmService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import javax.sql.DataSource;
@Aggregate
public class MigrationAggregate {
private final Logger logger;
private final DataSource dataSource;
private final FlywayFactoryBean flywayFactoryBean;
private final ApplicationName applicationName;
private final RhythmService rhythmService;
@Autowired
public MigrationAggregate(@Qualifier(ServiceConstants.LOGGER_NAME) final Logger logger,
final DataSource dataSource,
final FlywayFactoryBean flywayFactoryBean,
final ApplicationName applicationName,
final RhythmService rhythmService) {
super();
this.logger = logger;
this.dataSource = dataSource;
this.flywayFactoryBean = flywayFactoryBean;
this.applicationName = applicationName;
this.rhythmService = rhythmService;
}
@CommandHandler
@EventEmitter(selectorName = EventConstants.SELECTOR_NAME, selectorValue = EventConstants.INITIALIZE)
@Transactional
public String process(final MigrationCommand migrationCommand) {
this.logger.info("Starting migration for deposit account management version: {}.", applicationName.getVersionString());
this.flywayFactoryBean.create(this.dataSource).migrate();
this.rhythmService.setBeat();
this.logger.info("Migration finished.");
return this.applicationName.getVersionString();
}
}