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.

SnapshotDao.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.component.db;
  21. import com.google.common.base.Strings;
  22. import com.google.common.collect.Lists;
  23. import java.util.Collection;
  24. import java.util.List;
  25. import javax.annotation.CheckForNull;
  26. import javax.annotation.Nullable;
  27. import org.sonar.api.resources.Scopes;
  28. import org.sonar.db.component.SnapshotDto;
  29. import org.sonar.db.component.SnapshotMapper;
  30. import org.sonar.db.component.SnapshotQuery;
  31. import org.sonar.db.Dao;
  32. import org.sonar.db.DbSession;
  33. import org.sonar.server.exceptions.NotFoundException;
  34. public class SnapshotDao implements Dao {
  35. @CheckForNull
  36. public SnapshotDto selectNullableById(DbSession session, Long id) {
  37. return mapper(session).selectByKey(id);
  38. }
  39. public SnapshotDto selectById(DbSession session, Long key) {
  40. SnapshotDto value = selectNullableById(session, key);
  41. if (value == null) {
  42. throw new NotFoundException(String.format("Key '%s' not found", key));
  43. }
  44. return value;
  45. }
  46. @CheckForNull
  47. public SnapshotDto selectLastSnapshotByComponentId(DbSession session, long componentId) {
  48. return mapper(session).selectLastSnapshot(componentId);
  49. }
  50. public List<SnapshotDto> selectSnapshotsByComponentId(DbSession session, long componentId) {
  51. return mapper(session).selectSnapshotsByQuery(new SnapshotQuery().setComponentId(componentId));
  52. }
  53. public List<SnapshotDto> selectSnapshotsByQuery(DbSession session, SnapshotQuery query) {
  54. return mapper(session).selectSnapshotsByQuery(query);
  55. }
  56. public List<SnapshotDto> selectPreviousVersionSnapshots(DbSession session, long componentId, String lastVersion) {
  57. return mapper(session).selectPreviousVersionSnapshots(componentId, lastVersion);
  58. }
  59. public List<SnapshotDto> selectSnapshotAndChildrenOfProjectScope(DbSession session, long snapshotId) {
  60. return mapper(session).selectSnapshotAndChildrenOfScope(snapshotId, Scopes.PROJECT);
  61. }
  62. public int updateSnapshotAndChildrenLastFlagAndStatus(DbSession session, SnapshotDto snapshot, boolean isLast, String status) {
  63. Long rootId = snapshot.getId();
  64. String path = Strings.nullToEmpty(snapshot.getPath()) + snapshot.getId() + ".%";
  65. Long pathRootId = snapshot.getRootIdOrSelf();
  66. return mapper(session).updateSnapshotAndChildrenLastFlagAndStatus(rootId, pathRootId, path, isLast, status);
  67. }
  68. public int updateSnapshotAndChildrenLastFlag(DbSession session, SnapshotDto snapshot, boolean isLast) {
  69. Long rootId = snapshot.getId();
  70. String path = Strings.nullToEmpty(snapshot.getPath()) + snapshot.getId() + ".%";
  71. Long pathRootId = snapshot.getRootIdOrSelf();
  72. return mapper(session).updateSnapshotAndChildrenLastFlag(rootId, pathRootId, path, isLast);
  73. }
  74. public static boolean isLast(SnapshotDto snapshotTested, @Nullable SnapshotDto previousLastSnapshot) {
  75. return previousLastSnapshot == null || previousLastSnapshot.getCreatedAt() < snapshotTested.getCreatedAt();
  76. }
  77. public SnapshotDto insert(DbSession session, SnapshotDto item) {
  78. mapper(session).insert(item);
  79. return item;
  80. }
  81. public void insert(DbSession session, Collection<SnapshotDto> items) {
  82. for (SnapshotDto item : items) {
  83. insert(session, item);
  84. }
  85. }
  86. public void insert(DbSession session, SnapshotDto item, SnapshotDto... others) {
  87. insert(session, Lists.asList(item, others));
  88. }
  89. private SnapshotMapper mapper(DbSession session) {
  90. return session.getMapper(SnapshotMapper.class);
  91. }
  92. }