001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 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.person_id, m.rule_id, 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) " +
071 " and s.status=:status and s.project_id=(select p.id from projects p where p.kee=:resourceKey and p.qualifier<>:lib)";
072 return session.createNativeQuery(sql)
073 .setParameter("metricIds", metricByIds.keySet())
074 .setParameter("rootSnapshotId", ObjectUtils.defaultIfNull(projectPastSnapshot.getRootId(), projectPastSnapshot.getId()))
075 .setParameter("resourceKey", resourceKey)
076 .setParameter("lib", Qualifiers.LIBRARY)
077 .setParameter("status", Snapshot.STATUS_PROCESSED)
078 .getResultList();
079 }
080
081 public static int getMetricId(Object[] row) {
082 // can be BigDecimal on Oracle
083 return ((Number) row[0]).intValue();
084 }
085
086 public static Integer getCharacteristicId(Object[] row) {
087 // can be BigDecimal on Oracle
088 Number number = (Number) row[1];
089 return number != null ? number.intValue() : null;
090 }
091
092 public static Integer getPersonId(Object[] row) {
093 // can be BigDecimal on Oracle
094 Number number = (Number) row[2];
095 return number != null ? number.intValue() : null;
096 }
097
098 public static Integer getRuleId(Object[] row) {
099 // can be BigDecimal on Oracle
100 Number number = (Number) row[3];
101 return number != null ? number.intValue() : null;
102 }
103
104 public static boolean hasValue(Object[] row) {
105 return row[4] != null;
106 }
107
108 public static double getValue(Object[] row) {
109 return ((Number) row[4]).doubleValue();
110 }
111
112 }