You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SwitchSnapshotStep.java 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.computation.step;
  21. import java.util.List;
  22. import org.sonar.db.component.SnapshotDto;
  23. import org.sonar.db.DbSession;
  24. import org.sonar.db.MyBatis;
  25. import org.sonar.server.component.db.SnapshotDao;
  26. import org.sonar.server.computation.component.Component;
  27. import org.sonar.server.computation.component.DbIdsRepository;
  28. import org.sonar.server.computation.component.TreeRootHolder;
  29. import org.sonar.server.db.DbClient;
  30. import static org.sonar.server.component.db.SnapshotDao.isLast;
  31. public class SwitchSnapshotStep implements ComputationStep {
  32. private final DbClient dbClient;
  33. private final TreeRootHolder treeRootHolder;
  34. private final DbIdsRepository dbIdsRepository;
  35. public SwitchSnapshotStep(DbClient dbClient, TreeRootHolder treeRootHolder, DbIdsRepository dbIdsRepository) {
  36. this.dbClient = dbClient;
  37. this.treeRootHolder = treeRootHolder;
  38. this.dbIdsRepository = dbIdsRepository;
  39. }
  40. @Override
  41. public void execute() {
  42. DbSession session = dbClient.openSession(true);
  43. try {
  44. Component project = treeRootHolder.getRoot();
  45. long snapshotId = dbIdsRepository.getSnapshotId(project);
  46. disablePreviousSnapshot(session, snapshotId);
  47. enableCurrentSnapshot(session, snapshotId);
  48. } finally {
  49. MyBatis.closeQuietly(session);
  50. }
  51. }
  52. @Override
  53. public String getDescription() {
  54. return "Switch last snapshot flag";
  55. }
  56. private void disablePreviousSnapshot(DbSession session, long reportSnapshotId) {
  57. List<SnapshotDto> snapshots = dbClient.snapshotDao().selectSnapshotAndChildrenOfProjectScope(session, reportSnapshotId);
  58. for (SnapshotDto snapshot : snapshots) {
  59. SnapshotDto previousLastSnapshot = dbClient.snapshotDao().selectLastSnapshotByComponentId(session, snapshot.getComponentId());
  60. if (previousLastSnapshot != null) {
  61. dbClient.snapshotDao().updateSnapshotAndChildrenLastFlag(session, previousLastSnapshot, false);
  62. session.commit();
  63. }
  64. }
  65. }
  66. private void enableCurrentSnapshot(DbSession session, long reportSnapshotId) {
  67. SnapshotDao dao = dbClient.snapshotDao();
  68. SnapshotDto snapshot = dao.selectById(session, reportSnapshotId);
  69. SnapshotDto previousLastSnapshot = dao.selectLastSnapshotByComponentId(session, snapshot.getComponentId());
  70. boolean isLast = isLast(snapshot, previousLastSnapshot);
  71. dao.updateSnapshotAndChildrenLastFlagAndStatus(session, snapshot, isLast, SnapshotDto.STATUS_PROCESSED);
  72. session.commit();
  73. }
  74. }