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 org.apache.commons.lang.time.DateUtils;
023 import org.sonar.api.BatchExtension;
024 import org.sonar.api.CoreProperties;
025 import org.sonar.api.database.DatabaseSession;
026 import org.sonar.api.database.model.Snapshot;
027 import org.sonar.api.resources.Qualifiers;
028
029 import java.util.Date;
030 import java.util.List;
031
032 public class PastSnapshotFinderByDays implements BatchExtension {
033
034 private DatabaseSession session;
035
036 public PastSnapshotFinderByDays(DatabaseSession session) {
037 this.session = session;
038 }
039
040 PastSnapshot findFromDays(Snapshot projectSnapshot, int days) {
041 Date targetDate = DateUtils.addDays(projectSnapshot.getCreatedAt(), -days);
042 String hql = "from " + Snapshot.class.getSimpleName() + " where resourceId=:resourceId AND status=:status AND createdAt<:date AND qualifier<>:lib order by createdAt asc";
043 List<Snapshot> snapshots = session.createQuery(hql)
044 .setParameter("date", projectSnapshot.getCreatedAt())
045 .setParameter("resourceId", projectSnapshot.getResourceId())
046 .setParameter("status", Snapshot.STATUS_PROCESSED)
047 .setParameter("lib", Qualifiers.LIBRARY)
048 .getResultList();
049
050 Snapshot snapshot = getNearestToTarget(snapshots, targetDate);
051 return new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_DAYS, targetDate, snapshot).setModeParameter(String.valueOf(days));
052 }
053
054 static Snapshot getNearestToTarget(List<Snapshot> snapshots, Date currentDate, int distanceInDays) {
055 Date targetDate = DateUtils.addDays(currentDate, -distanceInDays);
056 return getNearestToTarget(snapshots, targetDate);
057 }
058
059 static Snapshot getNearestToTarget(List<Snapshot> snapshots, Date targetDate) {
060 long bestDistance = Long.MAX_VALUE;
061 Snapshot nearest = null;
062 for (Snapshot snapshot : snapshots) {
063 long distance = distance(snapshot.getCreatedAt(), targetDate);
064 if (distance <= bestDistance) {
065 bestDistance = distance;
066 nearest = snapshot;
067 }
068 }
069 return nearest;
070 }
071
072 static long distance(Date d1, Date d2) {
073 return Math.abs(d1.getTime() - d2.getTime());
074 }
075 }