aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch/src/main/java/org/sonar/batch/compute/TimeMachineConfigurationPersister.java
blob: 68b5e1a8330c7747b0c422996d31eb5532acca48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * SonarQube, open source software quality management tool.
 * Copyright (C) 2008-2014 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * SonarQube is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * SonarQube is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.batch.compute;

import java.util.List;
import org.sonar.api.batch.Decorator;
import org.sonar.api.batch.DecoratorBarriers;
import org.sonar.api.batch.DecoratorContext;
import org.sonar.api.batch.DependedUpon;
import org.sonar.api.batch.RequiresDB;
import org.sonar.api.database.DatabaseSession;
import org.sonar.api.database.model.Snapshot;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.resources.ResourceUtils;
import org.sonar.batch.components.PastSnapshot;
import org.sonar.batch.components.TimeMachineConfiguration;
import org.sonar.batch.index.BatchComponentCache;

import static org.sonar.api.utils.DateUtils.dateToLong;

@DependedUpon(DecoratorBarriers.END_OF_TIME_MACHINE)
@RequiresDB
public final class TimeMachineConfigurationPersister implements Decorator {

  private final TimeMachineConfiguration timeMachineConfiguration;
  private BatchComponentCache resourceCache;
  private DatabaseSession session;

  public TimeMachineConfigurationPersister(TimeMachineConfiguration timeMachineConfiguration, BatchComponentCache resourceCache, DatabaseSession session) {
    this.timeMachineConfiguration = timeMachineConfiguration;
    this.resourceCache = resourceCache;
    this.session = session;
  }

  @Override
  public void decorate(Resource resource, DecoratorContext context) {
    if (ResourceUtils.isProject(resource)) {
      persistConfiguration(resource);
    }
  }

  void persistConfiguration(Resource module) {
    List<PastSnapshot> pastSnapshots = timeMachineConfiguration.getProjectPastSnapshots();
    Snapshot projectSnapshot = resourceCache.get(module).snapshot();
    for (PastSnapshot pastSnapshot : pastSnapshots) {
      Snapshot snapshot = session.reattach(Snapshot.class, projectSnapshot.getId());
      updatePeriodParams(snapshot, pastSnapshot);
      updatePeriodParams(projectSnapshot, pastSnapshot);
      session.save(snapshot);
    }
    session.commit();
  }

  @Override
  public boolean shouldExecuteOnProject(Project project) {
    return true;
  }

  private static void updatePeriodParams(Snapshot snapshot, PastSnapshot pastSnapshot) {
    int periodIndex = pastSnapshot.getIndex();
    snapshot.setPeriodMode(periodIndex, pastSnapshot.getMode());
    snapshot.setPeriodModeParameter(periodIndex, pastSnapshot.getModeParameter());
    snapshot.setPeriodDateMs(periodIndex, dateToLong(pastSnapshot.getDate()));
  }
}