001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar 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 * Sonar 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 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.batch.components; 021 022 import com.google.common.collect.Maps; 023 import org.apache.commons.lang.ObjectUtils; 024 import org.sonar.api.BatchExtension; 025 import org.sonar.api.database.DatabaseSession; 026 import org.sonar.api.database.model.Snapshot; 027 import org.sonar.api.measures.Metric; 028 import org.sonar.api.measures.MetricFinder; 029 import org.sonar.api.resources.Qualifiers; 030 import org.sonar.api.resources.Resource; 031 032 import java.util.Collection; 033 import java.util.Collections; 034 import java.util.List; 035 import java.util.Map; 036 037 public class PastMeasuresLoader implements BatchExtension { 038 039 private Map<Integer, Metric> metricByIds; 040 private DatabaseSession session; 041 042 public PastMeasuresLoader(DatabaseSession session, MetricFinder metricFinder) { 043 this(session, metricFinder.findAll()); 044 } 045 046 PastMeasuresLoader(DatabaseSession session, Collection<Metric> metrics) { 047 this.session = session; 048 this.metricByIds = Maps.newHashMap(); 049 for (Metric metric : metrics) { 050 if (metric.isNumericType()) { 051 metricByIds.put(metric.getId(), metric); 052 } 053 } 054 } 055 056 public Collection<Metric> getMetrics() { 057 return metricByIds.values(); 058 } 059 060 public List<Object[]> getPastMeasures(Resource resource, PastSnapshot projectPastSnapshot) { 061 if (projectPastSnapshot != null && projectPastSnapshot.getProjectSnapshot()!=null) { 062 return getPastMeasures(resource.getEffectiveKey(), projectPastSnapshot.getProjectSnapshot()); 063 } 064 return Collections.emptyList(); 065 } 066 067 public List<Object[]> getPastMeasures(String resourceKey, Snapshot projectPastSnapshot) { 068 String sql = "select m.metric_id, m.characteristic_id, m.rule_id, m.rule_priority, m.value from project_measures m, snapshots s" + 069 " where m.snapshot_id=s.id and m.metric_id in (:metricIds) " + 070 " and (s.root_snapshot_id=:rootSnapshotId or s.id=:rootSnapshotId) and s.status=:status and s.project_id=(select p.id from projects p where p.kee=:resourceKey and p.qualifier<>:lib)"; 071 return session.createNativeQuery(sql) 072 .setParameter("metricIds", metricByIds.keySet()) 073 .setParameter("rootSnapshotId", ObjectUtils.defaultIfNull(projectPastSnapshot.getRootId(), projectPastSnapshot.getId())) 074 .setParameter("resourceKey", resourceKey) 075 .setParameter("lib", Qualifiers.LIBRARY) 076 .setParameter("status", Snapshot.STATUS_PROCESSED) 077 .getResultList(); 078 } 079 080 public static int getMetricId(Object[] row) { 081 // can be BigDecimal on Oracle 082 return ((Number)row[0]).intValue(); 083 } 084 085 public static Integer getCharacteristicId(Object[] row) { 086 // can be BigDecimal on Oracle 087 Number number = (Number) row[1]; 088 return number!=null ? number.intValue() : null; 089 } 090 091 public static Integer getRuleId(Object[] row) { 092 // can be BigDecimal on Oracle 093 Number number = (Number) row[2]; 094 return number!=null ? number.intValue() : null; 095 } 096 097 public static Integer getSeverityId(Object[] row) { 098 // can be BigDecimal on Oracle 099 Number number = (Number) row[3]; 100 return number!=null ? number.intValue() : null; 101 } 102 103 public static boolean hasValue(Object[] row) { 104 return row[4] != null; 105 } 106 107 public static double getValue(Object[] row) { 108 return ((Number) row[4]).doubleValue(); 109 } 110 }