001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.batch.issue;
021
022 import org.sonar.api.BatchComponent;
023 import org.sonar.api.database.model.Snapshot;
024 import org.sonar.api.issue.internal.DefaultIssue;
025 import org.sonar.api.rules.RuleFinder;
026 import org.sonar.batch.ProjectTree;
027 import org.sonar.batch.index.SnapshotCache;
028 import org.sonar.core.issue.db.IssueStorage;
029 import org.sonar.core.persistence.MyBatis;
030 import org.sonar.core.resource.ResourceDao;
031 import org.sonar.core.resource.ResourceDto;
032 import org.sonar.core.resource.ResourceQuery;
033
034 public class ScanIssueStorage extends IssueStorage implements BatchComponent {
035
036 private final SnapshotCache snapshotCache;
037 private final ResourceDao resourceDao;
038 private final ProjectTree projectTree;
039
040 public ScanIssueStorage(MyBatis mybatis, RuleFinder ruleFinder, SnapshotCache snapshotCache, ResourceDao resourceDao, ProjectTree projectTree) {
041 super(mybatis, ruleFinder);
042 this.snapshotCache = snapshotCache;
043 this.resourceDao = resourceDao;
044 this.projectTree = projectTree;
045 }
046
047 @Override
048 protected long componentId(DefaultIssue issue) {
049 Snapshot snapshot = snapshotCache.get(issue.componentKey());
050 if (snapshot != null) {
051 return snapshot.getResourceId();
052 }
053
054 // Load from db when component does not exist in cache (deleted file for example)
055 ResourceDto resourceDto = resourceDao.getResource(ResourceQuery.create().setKey(issue.componentKey()));
056 if (resourceDto == null) {
057 throw new IllegalStateException("Unknown component: " + issue.componentKey());
058 }
059 return resourceDto.getId();
060 }
061
062 @Override
063 protected long projectId(DefaultIssue issue) {
064 return projectTree.getRootProject().getId();
065 }
066
067 }