PageRenderTime 45ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/components/camel-jira/src/main/java/org/apache/camel/component/jira/consumer/WatchUpdatesConsumer.java

https://github.com/apache/camel
Java | 118 lines | 90 code | 12 blank | 16 comment | 11 complexity | be77888cd10bd2cd520686d715249ac0 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.camel.component.jira.consumer;
  18. import java.lang.reflect.Method;
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Objects;
  24. import java.util.concurrent.atomic.AtomicBoolean;
  25. import java.util.stream.Collectors;
  26. import com.atlassian.jira.rest.client.api.domain.Issue;
  27. import org.apache.camel.Exchange;
  28. import org.apache.camel.Processor;
  29. import org.apache.camel.component.jira.JiraConstants;
  30. import org.apache.camel.component.jira.JiraEndpoint;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;
  33. public class WatchUpdatesConsumer extends AbstractJiraConsumer {
  34. private static final transient Logger LOG = LoggerFactory.getLogger(WatchUpdatesConsumer.class);
  35. HashMap<Long, Issue> watchedIssues;
  36. List<String> watchedFieldsList;
  37. String watchedIssuesKeys;
  38. public WatchUpdatesConsumer(JiraEndpoint endpoint, Processor processor) {
  39. super(endpoint, processor);
  40. this.watchedFieldsList = new ArrayList<>();
  41. this.watchedFieldsList = Arrays.asList(endpoint.getWatchedFields().split(","));
  42. }
  43. @Override
  44. protected void doStart() throws Exception {
  45. super.doStart();
  46. initIssues();
  47. }
  48. private void initIssues() {
  49. watchedIssues = new HashMap<>();
  50. List<Issue> issues = getIssues(getEndpoint().getJql(), 0, 50,
  51. getEndpoint().getMaxResults());
  52. issues.forEach(i -> watchedIssues.put(i.getId(), i));
  53. watchedIssuesKeys = issues.stream()
  54. .map(Issue::getKey)
  55. .collect(Collectors.joining(","));
  56. }
  57. @Override
  58. protected int doPoll() throws Exception {
  59. List<Issue> issues = getIssues(getEndpoint().getJql(), 0, 50,
  60. getEndpoint().getMaxResults());
  61. if (watchedIssues.values().size() != issues.size()) {
  62. init();
  63. }
  64. for (Issue issue : issues) {
  65. checkIfIssueChanged(issue);
  66. }
  67. return 0;
  68. }
  69. private void checkIfIssueChanged(Issue issue) throws Exception {
  70. Issue original = watchedIssues.get(issue.getId());
  71. AtomicBoolean issueChanged = new AtomicBoolean();
  72. if (original != null) {
  73. for (String field : this.watchedFieldsList) {
  74. if (hasFieldChanged(issue, original, field)) {
  75. issueChanged.set(true);
  76. }
  77. }
  78. if (issueChanged.get()) {
  79. watchedIssues.put(issue.getId(), issue);
  80. }
  81. }
  82. }
  83. private boolean hasFieldChanged(Issue changed, Issue original, String fieldName) throws Exception {
  84. Method get = Issue.class.getDeclaredMethod("get" + fieldName);
  85. Object originalField = get.invoke(original);
  86. Object changedField = get.invoke(changed);
  87. if (!Objects.equals(originalField, changedField)) {
  88. if (!getEndpoint().isSendOnlyUpdatedField()) {
  89. processExchange(changed, changed.getKey(), fieldName);
  90. } else {
  91. processExchange(changedField, changed.getKey(), fieldName);
  92. }
  93. return true;
  94. }
  95. return false;
  96. }
  97. private void processExchange(Object body, String issueKey, String changed) throws Exception {
  98. Exchange e = createExchange(true);
  99. e.getIn().setBody(body);
  100. e.getIn().setHeader(JiraConstants.ISSUE_KEY, issueKey);
  101. e.getIn().setHeader(JiraConstants.ISSUE_CHANGED, changed);
  102. e.getIn().setHeader(JiraConstants.ISSUE_WATCHED_ISSUES, watchedIssuesKeys);
  103. LOG.debug(" {}: {} changed to {}", issueKey, changed, body);
  104. getProcessor().process(e);
  105. }
  106. }