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.server.db.migrations.v451;
22 import com.google.common.base.Predicate;
23 import com.google.common.collect.ArrayListMultimap;
24 import com.google.common.collect.HashMultimap;
25 import com.google.common.collect.Iterables;
26 import com.google.common.collect.Multimap;
27 import org.sonar.api.utils.System2;
28 import org.sonar.core.persistence.DbSession;
29 import org.sonar.core.persistence.migration.v45.Migration45Mapper;
30 import org.sonar.core.persistence.migration.v45.Rule;
31 import org.sonar.core.persistence.migration.v45.RuleParameter;
32 import org.sonar.server.db.DbClient;
33 import org.sonar.server.db.migrations.DatabaseMigration;
34 import org.sonar.server.db.migrations.MassUpdate;
36 import javax.annotation.Nullable;
38 import java.util.Collection;
39 import java.util.Date;
40 import java.util.List;
41 import java.util.Timer;
42 import java.util.concurrent.atomic.AtomicLong;
45 * See http://jira.codehaus.org/browse/SONAR-5575
47 * Add missing parameters (with no value) on each custom rules
51 public class AddMissingCustomRuleParametersMigration implements DatabaseMigration {
53 private final DbClient db;
54 private final System2 system;
56 private final AtomicLong counter = new AtomicLong(0L);
57 private final MassUpdate.ProgressTask progressTask = new MassUpdate.ProgressTask(counter);
59 public AddMissingCustomRuleParametersMigration(DbClient db, System2 system) {
65 public void execute() {
66 Timer timer = new Timer("Db Migration Progress");
67 timer.schedule(progressTask, MassUpdate.ProgressTask.PERIOD_MS, MassUpdate.ProgressTask.PERIOD_MS);
69 DbSession session = db.openSession(false);
71 Migration45Mapper mapper = session.getMapper(Migration45Mapper.class);
73 List<RuleParameter> templateRuleParams = mapper.selectAllTemplateRuleParameters();
74 Multimap<Integer, RuleParameter> templateRuleParamsByRuleId = ArrayListMultimap.create();
75 for (RuleParameter templateRuleParam : templateRuleParams) {
76 templateRuleParamsByRuleId.put(templateRuleParam.getRuleId(), templateRuleParam);
79 List<Rule> customRules = mapper.selectAllCustomRules();
80 Multimap<Integer, Integer> customRuleIdsByTemplateRuleId = HashMultimap.create();
81 for (Rule customRule : customRules) {
82 customRuleIdsByTemplateRuleId.put(customRule.getTemplateId(), customRule.getId());
85 List<RuleParameter> customRuleParams = mapper.selectAllCustomRuleParameters();
86 Multimap<Integer, RuleParameter> customRuleParamsByRuleId = ArrayListMultimap.create();
87 for (RuleParameter customRuleParam : customRuleParams) {
88 customRuleParamsByRuleId.put(customRuleParam.getRuleId(), customRuleParam);
91 // For each parameters of template rules, verify that each custom rules has the parameter
92 for (Integer templateRuleId : templateRuleParamsByRuleId.keySet()) {
93 for (RuleParameter templateRuleParam : templateRuleParamsByRuleId.get(templateRuleId)) {
94 // Each custom rule should have this parameter
95 insertCustomRuleParameterIfNotAlreadyExisting(templateRuleParam, templateRuleId, customRuleIdsByTemplateRuleId, customRuleParamsByRuleId, session);
101 // log the total number of process rows
111 private void insertCustomRuleParameterIfNotAlreadyExisting(RuleParameter templateRuleParam, Integer templateRuleId,
112 Multimap<Integer, Integer> customRuleIdsByTemplateRuleId,
113 Multimap<Integer, RuleParameter> customRuleParamsByRuleId,
115 for (Integer customRuleId : customRuleIdsByTemplateRuleId.get(templateRuleId)) {
116 if (!hasParameter(templateRuleParam.getName(), customRuleParamsByRuleId.get(customRuleId))) {
117 // Insert new custom rule parameter
118 session.getMapper(Migration45Mapper.class).insertRuleParameter(new RuleParameter()
119 .setRuleId(customRuleId)
120 .setRuleTemplateId(templateRuleId)
121 .setName(templateRuleParam.getName())
122 .setDescription(templateRuleParam.getDescription())
123 .setType(templateRuleParam.getType())
126 // Update updated at date of custom rule in order to allow E/S indexation
127 session.getMapper(Migration45Mapper.class).updateRuleUpdateAt(customRuleId, new Date(system.now()));
129 counter.getAndIncrement();
134 private boolean hasParameter(final String parameter, Collection<RuleParameter> customRuleParams) {
135 return Iterables.any(customRuleParams, new Predicate<RuleParameter>() {
137 public boolean apply(@Nullable RuleParameter input) {
138 return input != null && input.getName().equals(parameter);