PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/components/server-data-access/src/main/java/org/marvelution/jji/data/access/model/upgrade/T13_CopyAutomationAppData.java

https://bitbucket.org/marvelution/jira-jenkins-integration
Java | 188 lines | 156 code | 17 blank | 15 comment | 8 complexity | 4c234ae19615ae495380028c5b7bc7e3 MD5 | raw file
  1. /*
  2. * Copyright (c) 2012-present Marvelution Holding B.V.
  3. *
  4. * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
  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.marvelution.jji.data.access.model.upgrade;
  17. import java.util.*;
  18. import javax.inject.*;
  19. import org.marvelution.jji.api.*;
  20. import org.marvelution.jji.automation.api.execution.*;
  21. import org.marvelution.jji.data.access.model.*;
  22. import org.marvelution.jji.upgrade.*;
  23. import com.atlassian.plugin.spring.scanner.annotation.export.*;
  24. import com.atlassian.pocketknife.api.querydsl.*;
  25. import com.atlassian.pocketknife.api.querydsl.stream.*;
  26. import com.atlassian.pocketknife.api.querydsl.util.*;
  27. import com.atlassian.sal.api.message.*;
  28. import com.atlassian.sal.api.upgrade.*;
  29. import com.querydsl.core.*;
  30. import com.querydsl.sql.dml.*;
  31. import org.slf4j.*;
  32. @Named
  33. @ExportAsService(PluginUpgradeTask.class)
  34. public class T13_CopyAutomationAppData
  35. extends AbstractUpgradeTask
  36. {
  37. static final String OLD_NAMESPACE = "AO_2224A0_";
  38. private static final Logger LOGGER = LoggerFactory.getLogger(T13_CopyAutomationAppData.class);
  39. private final DatabaseAccessor databaseAccessor;
  40. private final StreamingQueryFactory queryFactory;
  41. private final ReloadableRulesEngine rulesEngine;
  42. @Inject
  43. public T13_CopyAutomationAppData(
  44. AddonHelper addonHelper,
  45. DatabaseAccessor databaseAccessor,
  46. StreamingQueryFactory queryFactory,
  47. ReloadableRulesEngine rulesEngine)
  48. {
  49. super(addonHelper);
  50. this.databaseAccessor = databaseAccessor;
  51. this.queryFactory = queryFactory;
  52. this.rulesEngine = rulesEngine;
  53. }
  54. @Override
  55. public Collection<Message> doUpgrade()
  56. {
  57. return databaseAccessor.runInTransaction(conn -> {
  58. if (shouldCopyAutomationAppData(conn))
  59. {
  60. LOGGER.info("Copying Automation App data to new tables");
  61. Set<String> ruleIds = migrateRules(conn);
  62. for (String ruleId : ruleIds)
  63. {
  64. migrateRuleResults(conn, ruleId);
  65. }
  66. migrateRuleQueue(conn);
  67. }
  68. else
  69. {
  70. LOGGER.debug("Automation app is not installed, or not configured, skipping data copy.");
  71. }
  72. return null;
  73. }, OnRollback.NOOP);
  74. }
  75. private boolean shouldCopyAutomationAppData(DatabaseConnection conn)
  76. {
  77. PersistedRuleTable ruleSourceTable = new PersistedRuleTable(OLD_NAMESPACE, "sourceRule");
  78. try
  79. {
  80. Long count = conn.select(ruleSourceTable.id().count()).from(ruleSourceTable).fetchOne();
  81. if (count != null && count > 0)
  82. {
  83. return true;
  84. }
  85. else
  86. {
  87. LOGGER.debug("{} exists but has no rules stored so nothing to copy.", ruleSourceTable.getTableName());
  88. }
  89. }
  90. catch (Exception e)
  91. {
  92. LOGGER.debug("Failed to query the number of configured rules in {}; {}", ruleSourceTable.getTableName(), e.getMessage());
  93. }
  94. return false;
  95. }
  96. private Set<String> migrateRules(DatabaseConnection conn)
  97. {
  98. PersistedRuleTable ruleSourceTable = new PersistedRuleTable(OLD_NAMESPACE, "sourceRule");
  99. PersistedRuleTable ruleTargetTable = Tables.RULE_TABLE;
  100. Set<String> ruleIds = new HashSet<>();
  101. SQLInsertClause insert = conn.insert(ruleTargetTable);
  102. try (CloseableIterable<Tuple> results = queryFactory.stream(conn, () -> conn.select(ruleSourceTable.all()).from(ruleSourceTable)))
  103. {
  104. results.foreach(result -> {
  105. String ruleId = result.get(ruleSourceTable.id());
  106. String ruleName = result.get(ruleSourceTable.name());
  107. LOGGER.info("Migrating rule {} ({})", ruleName, ruleId);
  108. ruleIds.add(ruleId);
  109. insert.set(ruleTargetTable.id(), ruleId)
  110. .set(ruleTargetTable.name(), ruleName)
  111. .set(ruleTargetTable.enabled(), result.get(ruleSourceTable.enabled()))
  112. .set(ruleTargetTable.description(), result.get(ruleSourceTable.description()))
  113. .set(ruleTargetTable.whenJson(), result.get(ruleSourceTable.whenJson()))
  114. .set(ruleTargetTable.ifJson(), result.get(ruleSourceTable.ifJson()))
  115. .set(ruleTargetTable.thenJson(), result.get(ruleSourceTable.thenJson()))
  116. .addBatch();
  117. });
  118. }
  119. if (!insert.isEmpty())
  120. {
  121. long count = insert.execute();
  122. LOGGER.info("Migrated {} rules", count);
  123. }
  124. return ruleIds;
  125. }
  126. private void migrateRuleResults(
  127. DatabaseConnection conn,
  128. String ruleId)
  129. {
  130. RuleExecutionResultTable resultSourceTable = new RuleExecutionResultTable(OLD_NAMESPACE, "oldResult");
  131. RuleExecutionResultTable resultTargetTable = Tables.RULE_EXECUTION_RESULT_TABLE;
  132. SQLInsertClause insert = conn.insert(resultTargetTable);
  133. try (CloseableIterable<Tuple> results = queryFactory.stream(conn, () -> conn.select(resultSourceTable.all())
  134. .from(resultSourceTable)
  135. .where(resultSourceTable.ruleId().eq(ruleId))))
  136. {
  137. results.foreach(result -> insert.set(resultTargetTable.id(), result.get(resultSourceTable.id()))
  138. .set(resultTargetTable.ruleId(), result.get(resultSourceTable.ruleId()))
  139. .set(resultTargetTable.executionTimestamp(), result.get(resultSourceTable.executionTimestamp()))
  140. .set(resultTargetTable.durationInMillis(), result.get(resultSourceTable.durationInMillis()))
  141. .set(resultTargetTable.eventJson(), result.get(resultSourceTable.eventJson()))
  142. .set(resultTargetTable.conditionResultsJson(), result.get(resultSourceTable.conditionResultsJson()))
  143. .set(resultTargetTable.actionResultsJson(), result.get(resultSourceTable.actionResultsJson()))
  144. .addBatch());
  145. }
  146. if (!insert.isEmpty())
  147. {
  148. long count = insert.execute();
  149. LOGGER.info("Migrated {} execution results for rule {}", count, ruleId);
  150. }
  151. }
  152. private void migrateRuleQueue(DatabaseConnection conn)
  153. {
  154. RuleExecutionQueueTable queueSourceTable = new RuleExecutionQueueTable(OLD_NAMESPACE, "sourceQueue");
  155. RuleExecutionQueueTable queueTargetTable = Tables.RULE_EXECUTION_QUEUE_TABLE;
  156. SQLInsertClause insert = conn.insert(queueTargetTable);
  157. try (CloseableIterable<Tuple> results = queryFactory.stream(conn, () -> conn.select(queueSourceTable.all()).from(queueSourceTable)))
  158. {
  159. results.foreach(result -> insert.set(queueTargetTable.id(), result.get(queueSourceTable.id()))
  160. .set(queueTargetTable.ruleId(), result.get(queueSourceTable.ruleId()))
  161. .set(queueTargetTable.eventJson(), result.get(queueSourceTable.eventJson()))
  162. .addBatch());
  163. }
  164. if (!insert.isEmpty())
  165. {
  166. long count = insert.execute();
  167. LOGGER.info("Migrated {} queued executions", count);
  168. }
  169. rulesEngine.reloadQueuedExecutions();
  170. }
  171. }