001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2009 SonarSource SA 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; 021 022 import org.sonar.api.batch.TimeMachine; 023 import org.sonar.api.batch.TimeMachineQuery; 024 import org.sonar.api.database.DatabaseSession; 025 import org.sonar.api.database.daos.MeasuresDao; 026 import org.sonar.api.database.model.MeasureModel; 027 import org.sonar.api.database.model.Snapshot; 028 import org.sonar.api.measures.Measure; 029 import org.sonar.api.resources.Resource; 030 import org.sonar.batch.indexer.DefaultSonarIndex; 031 032 import java.util.*; 033 import javax.persistence.Query; 034 035 public class DefaultTimeMachine implements TimeMachine { 036 037 private DatabaseSession session; 038 private DefaultSonarIndex index; 039 private MeasuresDao measuresDao; 040 041 public DefaultTimeMachine(DatabaseSession session, DefaultSonarIndex index, MeasuresDao measuresDao) { 042 this.session = session; 043 this.index = index; 044 this.measuresDao = measuresDao; 045 } 046 047 public List<Measure> getMeasures(TimeMachineQuery query) { 048 List<Object[]> objects = execute(query, true); 049 List<Measure> result = new ArrayList<Measure>(); 050 051 for (Object[] object : objects) { 052 MeasureModel model = (MeasureModel) object[0]; 053 Measure measure = model.toMeasure(); 054 measure.setDate((Date) object[1]); 055 result.add(measure); 056 } 057 return result; 058 } 059 060 public List<Object[]> getMeasuresFields(TimeMachineQuery query) { 061 return execute(query, false); 062 } 063 064 protected List execute(TimeMachineQuery query, boolean selectAllFields) { 065 Resource resource = index.getResource(query.getResource()); 066 if (resource == null) { 067 return Collections.emptyList(); 068 } 069 070 StringBuilder sb = new StringBuilder(); 071 Map<String, Object> params = new HashMap<String, Object>(); 072 073 if (selectAllFields) { 074 sb.append("SELECT m, s.createdAt "); 075 } else { 076 sb.append("SELECT s.createdAt, m.metric, m.value "); 077 } 078 sb.append(" FROM " + MeasureModel.class.getSimpleName() + " m, " + Snapshot.class.getSimpleName() + " s WHERE m.snapshotId=s.id AND s.resourceId=:resourceId AND s.status=:status "); 079 params.put("resourceId", resource.getId()); 080 params.put("status", Snapshot.STATUS_PROCESSED); 081 082 sb.append(" AND m.rule IS NULL AND m.rulePriority IS NULL AND m.rulesCategoryId IS NULL "); 083 if (query.getMetrics() != null) { 084 sb.append(" AND m.metric IN (:metrics) "); 085 params.put("metrics", measuresDao.getMetrics(query.getMetrics())); 086 } 087 if (query.isFromCurrentAnalysis()) { 088 sb.append(" AND s.createdAt>=:from "); 089 params.put("from", index.getProject().getAnalysisDate()); 090 091 } else if (query.getFrom() != null) { 092 sb.append(" AND s.createdAt>=:from "); 093 params.put("from", query.getFrom()); 094 } 095 if (query.isToCurrentAnalysis()) { 096 sb.append(" AND s.createdAt<=:to "); 097 params.put("to", index.getProject().getAnalysisDate()); 098 099 } else if (query.getTo() != null) { 100 sb.append(" AND s.createdAt<=:to "); 101 params.put("to", query.getTo()); 102 } 103 if (query.isOnlyLastAnalysis()) { 104 sb.append(" AND s.last=:last "); 105 params.put("last", Boolean.TRUE); 106 } 107 sb.append(" ORDER BY s.createdAt "); 108 109 Query jpaQuery = session.createQuery(sb.toString()); 110 111 for (Map.Entry<String, Object> entry : params.entrySet()) { 112 jpaQuery.setParameter(entry.getKey(), entry.getValue()); 113 } 114 return jpaQuery.getResultList(); 115 } 116 }