/components/server-data-access/src/main/java/org/marvelution/jji/data/access/model/upgrade/T13_CopyAutomationAppData.java
Java | 188 lines | 156 code | 17 blank | 15 comment | 8 complexity | 4c234ae19615ae495380028c5b7bc7e3 MD5 | raw file
- /*
- * Copyright (c) 2012-present Marvelution Holding B.V.
- *
- * 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 org.marvelution.jji.data.access.model.upgrade;
- import java.util.*;
- import javax.inject.*;
- import org.marvelution.jji.api.*;
- import org.marvelution.jji.automation.api.execution.*;
- import org.marvelution.jji.data.access.model.*;
- import org.marvelution.jji.upgrade.*;
- import com.atlassian.plugin.spring.scanner.annotation.export.*;
- import com.atlassian.pocketknife.api.querydsl.*;
- import com.atlassian.pocketknife.api.querydsl.stream.*;
- import com.atlassian.pocketknife.api.querydsl.util.*;
- import com.atlassian.sal.api.message.*;
- import com.atlassian.sal.api.upgrade.*;
- import com.querydsl.core.*;
- import com.querydsl.sql.dml.*;
- import org.slf4j.*;
- @Named
- @ExportAsService(PluginUpgradeTask.class)
- public class T13_CopyAutomationAppData
- extends AbstractUpgradeTask
- {
- static final String OLD_NAMESPACE = "AO_2224A0_";
- private static final Logger LOGGER = LoggerFactory.getLogger(T13_CopyAutomationAppData.class);
- private final DatabaseAccessor databaseAccessor;
- private final StreamingQueryFactory queryFactory;
- private final ReloadableRulesEngine rulesEngine;
- @Inject
- public T13_CopyAutomationAppData(
- AddonHelper addonHelper,
- DatabaseAccessor databaseAccessor,
- StreamingQueryFactory queryFactory,
- ReloadableRulesEngine rulesEngine)
- {
- super(addonHelper);
- this.databaseAccessor = databaseAccessor;
- this.queryFactory = queryFactory;
- this.rulesEngine = rulesEngine;
- }
- @Override
- public Collection<Message> doUpgrade()
- {
- return databaseAccessor.runInTransaction(conn -> {
- if (shouldCopyAutomationAppData(conn))
- {
- LOGGER.info("Copying Automation App data to new tables");
- Set<String> ruleIds = migrateRules(conn);
- for (String ruleId : ruleIds)
- {
- migrateRuleResults(conn, ruleId);
- }
- migrateRuleQueue(conn);
- }
- else
- {
- LOGGER.debug("Automation app is not installed, or not configured, skipping data copy.");
- }
- return null;
- }, OnRollback.NOOP);
- }
- private boolean shouldCopyAutomationAppData(DatabaseConnection conn)
- {
- PersistedRuleTable ruleSourceTable = new PersistedRuleTable(OLD_NAMESPACE, "sourceRule");
- try
- {
- Long count = conn.select(ruleSourceTable.id().count()).from(ruleSourceTable).fetchOne();
- if (count != null && count > 0)
- {
- return true;
- }
- else
- {
- LOGGER.debug("{} exists but has no rules stored so nothing to copy.", ruleSourceTable.getTableName());
- }
- }
- catch (Exception e)
- {
- LOGGER.debug("Failed to query the number of configured rules in {}; {}", ruleSourceTable.getTableName(), e.getMessage());
- }
- return false;
- }
- private Set<String> migrateRules(DatabaseConnection conn)
- {
- PersistedRuleTable ruleSourceTable = new PersistedRuleTable(OLD_NAMESPACE, "sourceRule");
- PersistedRuleTable ruleTargetTable = Tables.RULE_TABLE;
- Set<String> ruleIds = new HashSet<>();
- SQLInsertClause insert = conn.insert(ruleTargetTable);
- try (CloseableIterable<Tuple> results = queryFactory.stream(conn, () -> conn.select(ruleSourceTable.all()).from(ruleSourceTable)))
- {
- results.foreach(result -> {
- String ruleId = result.get(ruleSourceTable.id());
- String ruleName = result.get(ruleSourceTable.name());
- LOGGER.info("Migrating rule {} ({})", ruleName, ruleId);
- ruleIds.add(ruleId);
- insert.set(ruleTargetTable.id(), ruleId)
- .set(ruleTargetTable.name(), ruleName)
- .set(ruleTargetTable.enabled(), result.get(ruleSourceTable.enabled()))
- .set(ruleTargetTable.description(), result.get(ruleSourceTable.description()))
- .set(ruleTargetTable.whenJson(), result.get(ruleSourceTable.whenJson()))
- .set(ruleTargetTable.ifJson(), result.get(ruleSourceTable.ifJson()))
- .set(ruleTargetTable.thenJson(), result.get(ruleSourceTable.thenJson()))
- .addBatch();
- });
- }
- if (!insert.isEmpty())
- {
- long count = insert.execute();
- LOGGER.info("Migrated {} rules", count);
- }
- return ruleIds;
- }
- private void migrateRuleResults(
- DatabaseConnection conn,
- String ruleId)
- {
- RuleExecutionResultTable resultSourceTable = new RuleExecutionResultTable(OLD_NAMESPACE, "oldResult");
- RuleExecutionResultTable resultTargetTable = Tables.RULE_EXECUTION_RESULT_TABLE;
- SQLInsertClause insert = conn.insert(resultTargetTable);
- try (CloseableIterable<Tuple> results = queryFactory.stream(conn, () -> conn.select(resultSourceTable.all())
- .from(resultSourceTable)
- .where(resultSourceTable.ruleId().eq(ruleId))))
- {
- results.foreach(result -> insert.set(resultTargetTable.id(), result.get(resultSourceTable.id()))
- .set(resultTargetTable.ruleId(), result.get(resultSourceTable.ruleId()))
- .set(resultTargetTable.executionTimestamp(), result.get(resultSourceTable.executionTimestamp()))
- .set(resultTargetTable.durationInMillis(), result.get(resultSourceTable.durationInMillis()))
- .set(resultTargetTable.eventJson(), result.get(resultSourceTable.eventJson()))
- .set(resultTargetTable.conditionResultsJson(), result.get(resultSourceTable.conditionResultsJson()))
- .set(resultTargetTable.actionResultsJson(), result.get(resultSourceTable.actionResultsJson()))
- .addBatch());
- }
- if (!insert.isEmpty())
- {
- long count = insert.execute();
- LOGGER.info("Migrated {} execution results for rule {}", count, ruleId);
- }
- }
- private void migrateRuleQueue(DatabaseConnection conn)
- {
- RuleExecutionQueueTable queueSourceTable = new RuleExecutionQueueTable(OLD_NAMESPACE, "sourceQueue");
- RuleExecutionQueueTable queueTargetTable = Tables.RULE_EXECUTION_QUEUE_TABLE;
- SQLInsertClause insert = conn.insert(queueTargetTable);
- try (CloseableIterable<Tuple> results = queryFactory.stream(conn, () -> conn.select(queueSourceTable.all()).from(queueSourceTable)))
- {
- results.foreach(result -> insert.set(queueTargetTable.id(), result.get(queueSourceTable.id()))
- .set(queueTargetTable.ruleId(), result.get(queueSourceTable.ruleId()))
- .set(queueTargetTable.eventJson(), result.get(queueSourceTable.eventJson()))
- .addBatch());
- }
- if (!insert.isEmpty())
- {
- long count = insert.execute();
- LOGGER.info("Migrated {} queued executions", count);
- }
- rulesEngine.reloadQueuedExecutions();
- }
- }