2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2013 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * SonarQube is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.plugins.core.timemachine;
22 import org.sonar.api.batch.Decorator;
23 import org.sonar.api.batch.DecoratorBarriers;
24 import org.sonar.api.batch.DecoratorContext;
25 import org.sonar.api.batch.DependedUpon;
26 import org.sonar.api.database.DatabaseSession;
27 import org.sonar.api.database.model.Snapshot;
28 import org.sonar.api.resources.Project;
29 import org.sonar.api.resources.Resource;
30 import org.sonar.api.resources.ResourceUtils;
31 import org.sonar.batch.components.PastSnapshot;
32 import org.sonar.batch.components.TimeMachineConfiguration;
33 import org.sonar.core.DryRunIncompatible;
35 import java.util.List;
38 @DependedUpon(DecoratorBarriers.END_OF_TIME_MACHINE)
39 public final class TimeMachineConfigurationPersister implements Decorator {
41 private TimeMachineConfiguration configuration;
42 private Snapshot projectSnapshot;
43 private DatabaseSession session;
45 public TimeMachineConfigurationPersister(TimeMachineConfiguration configuration, Snapshot projectSnapshot, DatabaseSession session) {
46 this.configuration = configuration;
47 this.projectSnapshot = projectSnapshot;
48 this.session = session;
51 public void decorate(Resource resource, DecoratorContext context) {
52 if (ResourceUtils.isProject(resource)) {
53 persistConfiguration();
57 void persistConfiguration() {
58 List<PastSnapshot> pastSnapshots = configuration.getProjectPastSnapshots();
59 for (PastSnapshot pastSnapshot : pastSnapshots) {
60 Snapshot snapshot = session.reattach(Snapshot.class, projectSnapshot.getId());
61 updatePeriodParams(snapshot, pastSnapshot);
62 updatePeriodParams(projectSnapshot, pastSnapshot);
63 session.save(snapshot);
68 public boolean shouldExecuteOnProject(Project project) {
72 private void updatePeriodParams(Snapshot snapshot, PastSnapshot pastSnapshot) {
73 int periodIndex = pastSnapshot.getIndex();
74 snapshot.setPeriodMode(periodIndex, pastSnapshot.getMode());
75 snapshot.setPeriodModeParameter(periodIndex, pastSnapshot.getModeParameter());
76 snapshot.setPeriodDate(periodIndex, pastSnapshot.getTargetDate());