2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 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.batch.index.ResourceCache;
35 import java.util.List;
37 @DependedUpon(DecoratorBarriers.END_OF_TIME_MACHINE)
38 public final class TimeMachineConfigurationPersister implements Decorator {
40 private final TimeMachineConfiguration timeMachineConfiguration;
41 private ResourceCache resourceCache;
42 private DatabaseSession session;
44 public TimeMachineConfigurationPersister(TimeMachineConfiguration timeMachineConfiguration, ResourceCache resourceCache, DatabaseSession session) {
45 this.timeMachineConfiguration = timeMachineConfiguration;
46 this.resourceCache = resourceCache;
47 this.session = session;
51 public void decorate(Resource resource, DecoratorContext context) {
52 if (ResourceUtils.isProject(resource)) {
53 persistConfiguration(resource);
57 void persistConfiguration(Resource module) {
58 List<PastSnapshot> pastSnapshots = timeMachineConfiguration.getProjectPastSnapshots();
59 Snapshot projectSnapshot = resourceCache.get(module.getEffectiveKey()).snapshot();
60 for (PastSnapshot pastSnapshot : pastSnapshots) {
61 Snapshot snapshot = session.reattach(Snapshot.class, projectSnapshot.getId());
62 updatePeriodParams(snapshot, pastSnapshot);
63 updatePeriodParams(projectSnapshot, pastSnapshot);
64 session.save(snapshot);
70 public boolean shouldExecuteOnProject(Project project) {
74 private void updatePeriodParams(Snapshot snapshot, PastSnapshot pastSnapshot) {
75 int periodIndex = pastSnapshot.getIndex();
76 snapshot.setPeriodMode(periodIndex, pastSnapshot.getMode());
77 snapshot.setPeriodModeParameter(periodIndex, pastSnapshot.getModeParameter());
78 snapshot.setPeriodDate(periodIndex, pastSnapshot.getDate());