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;
28 import org.slf4j.LoggerFactory;
29 import org.sonar.api.utils.System2;
30 import org.sonar.core.persistence.DbSession;
31 import org.sonar.core.persistence.migration.v45.Migration45Mapper;
32 import org.sonar.core.persistence.migration.v45.Rule;
33 import org.sonar.core.persistence.migration.v45.RuleParameter;
34 import org.sonar.server.db.DbClient;
35 import org.sonar.server.db.migrations.DatabaseMigration;
36 import org.sonar.server.util.ProgressTask;
38 import javax.annotation.Nullable;
40 import java.util.Collection;
41 import java.util.Date;
42 import java.util.List;
43 import java.util.Timer;
44 import java.util.concurrent.atomic.AtomicLong;
47 * See http://jira.codehaus.org/browse/SONAR-5575
49 * Add missing parameters (with no value) on each custom rules
53 public class AddMissingCustomRuleParametersMigration implements DatabaseMigration {
55 private final DbClient db;
56 private final System2 system;
58 private final AtomicLong counter = new AtomicLong(0L);
59 private final ProgressTask progressTask = new ProgressTask(counter, LoggerFactory.getLogger("DbMigration"));
61 public AddMissingCustomRuleParametersMigration(DbClient db, System2 system) {
67 public void execute() {
68 Timer timer = new Timer("Db Migration Progress");
69 timer.schedule(progressTask, ProgressTask.PERIOD_MS, ProgressTask.PERIOD_MS);
71 DbSession session = db.openSession(false);
73 Migration45Mapper mapper = session.getMapper(Migration45Mapper.class);
75 List<RuleParameter> templateRuleParams = mapper.selectAllTemplateRuleParameters();
76 Multimap<Integer, RuleParameter> templateRuleParamsByRuleId = ArrayListMultimap.create();
77 for (RuleParameter templateRuleParam : templateRuleParams) {
78 templateRuleParamsByRuleId.put(templateRuleParam.getRuleId(), templateRuleParam);
81 List<Rule> customRules = mapper.selectAllCustomRules();
82 Multimap<Integer, Integer> customRuleIdsByTemplateRuleId = HashMultimap.create();
83 for (Rule customRule : customRules) {
84 customRuleIdsByTemplateRuleId.put(customRule.getTemplateId(), customRule.getId());
87 List<RuleParameter> customRuleParams = mapper.selectAllCustomRuleParameters();
88 Multimap<Integer, RuleParameter> customRuleParamsByRuleId = ArrayListMultimap.create();
89 for (RuleParameter customRuleParam : customRuleParams) {
90 customRuleParamsByRuleId.put(customRuleParam.getRuleId(), customRuleParam);
93 // For each parameters of template rules, verify that each custom rules has the parameter
94 for (Integer templateRuleId : templateRuleParamsByRuleId.keySet()) {
95 for (RuleParameter templateRuleParam : templateRuleParamsByRuleId.get(templateRuleId)) {
96 // Each custom rule should have this parameter
97 insertCustomRuleParameterIfNotAlreadyExisting(templateRuleParam, templateRuleId, customRuleIdsByTemplateRuleId, customRuleParamsByRuleId, session);
103 // log the total number of process rows
113 private void insertCustomRuleParameterIfNotAlreadyExisting(RuleParameter templateRuleParam, Integer templateRuleId,
114 Multimap<Integer, Integer> customRuleIdsByTemplateRuleId,
115 Multimap<Integer, RuleParameter> customRuleParamsByRuleId,
117 for (Integer customRuleId : customRuleIdsByTemplateRuleId.get(templateRuleId)) {
118 if (!hasParameter(templateRuleParam.getName(), customRuleParamsByRuleId.get(customRuleId))) {
119 // Insert new custom rule parameter
120 session.getMapper(Migration45Mapper.class).insertRuleParameter(new RuleParameter()
121 .setRuleId(customRuleId)
122 .setRuleTemplateId(templateRuleId)
123 .setName(templateRuleParam.getName())
124 .setDescription(templateRuleParam.getDescription())
125 .setType(templateRuleParam.getType())
128 // Update updated at date of custom rule in order to allow E/S indexation
129 session.getMapper(Migration45Mapper.class).updateRuleUpdateAt(customRuleId, new Date(system.now()));
131 counter.getAndIncrement();
136 private boolean hasParameter(final String parameter, Collection<RuleParameter> customRuleParams) {
137 return Iterables.any(customRuleParams, new Predicate<RuleParameter>() {
139 public boolean apply(@Nullable RuleParameter input) {
140 return input != null && input.getName().equals(parameter);