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;
34 import java.util.List;
36 @DependedUpon(DecoratorBarriers.END_OF_TIME_MACHINE)
37 public final class TimeMachineConfigurationPersister implements Decorator {
39 private final TimeMachineConfiguration timeMachineConfiguration;
40 private Snapshot projectSnapshot;
41 private DatabaseSession session;
43 public TimeMachineConfigurationPersister(TimeMachineConfiguration timeMachineConfiguration, Snapshot projectSnapshot, DatabaseSession session) {
44 this.timeMachineConfiguration = timeMachineConfiguration;
45 this.projectSnapshot = projectSnapshot;
46 this.session = session;
49 public void decorate(Resource resource, DecoratorContext context) {
50 if (ResourceUtils.isProject(resource)) {
51 persistConfiguration();
55 void persistConfiguration() {
56 List<PastSnapshot> pastSnapshots = timeMachineConfiguration.getProjectPastSnapshots();
57 for (PastSnapshot pastSnapshot : pastSnapshots) {
58 Snapshot snapshot = session.reattach(Snapshot.class, projectSnapshot.getId());
59 updatePeriodParams(snapshot, pastSnapshot);
60 updatePeriodParams(projectSnapshot, pastSnapshot);
61 session.save(snapshot);
66 public boolean shouldExecuteOnProject(Project project) {
70 private void updatePeriodParams(Snapshot snapshot, PastSnapshot pastSnapshot) {
71 int periodIndex = pastSnapshot.getIndex();
72 snapshot.setPeriodMode(periodIndex, pastSnapshot.getMode());
73 snapshot.setPeriodModeParameter(periodIndex, pastSnapshot.getModeParameter());
74 snapshot.setPeriodDate(periodIndex, pastSnapshot.getTargetDate());