001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.batch.rule.internal;
021
022import java.util.HashMap;
023import java.util.Map;
024import javax.annotation.Nullable;
025import org.apache.commons.lang.StringUtils;
026import org.sonar.api.rule.RuleKey;
027import org.sonar.api.rule.Severity;
028
029/**
030 * @since 4.2
031 */
032public class NewActiveRule {
033  final RuleKey ruleKey;
034  String name;
035  String severity = Severity.defaultSeverity();
036  Map<String, String> params = new HashMap<>();
037  long createdAt;
038  String internalKey;
039  String language;
040  String templateRuleKey;
041  private final ActiveRulesBuilder builder;
042
043  NewActiveRule(ActiveRulesBuilder builder, RuleKey ruleKey) {
044    this.builder = builder;
045    this.ruleKey = ruleKey;
046  }
047
048  public NewActiveRule setName(String name) {
049    this.name = name;
050    return this;
051  }
052
053  public NewActiveRule setSeverity(@Nullable String severity) {
054    this.severity = StringUtils.defaultIfBlank(severity, Severity.defaultSeverity());
055    return this;
056  }
057
058  public NewActiveRule setInternalKey(@Nullable String internalKey) {
059    this.internalKey = internalKey;
060    return this;
061  }
062
063  public NewActiveRule setTemplateRuleKey(@Nullable String templateRuleKey) {
064    this.templateRuleKey = templateRuleKey;
065    return this;
066  }
067
068  public NewActiveRule setLanguage(@Nullable String language) {
069    this.language = language;
070    return this;
071  }
072
073  public NewActiveRule setParam(String key, @Nullable String value) {
074    // possible improvement : check that the param key exists in rule definition
075    if (value == null) {
076      params.remove(key);
077    } else {
078      params.put(key, value);
079    }
080    return this;
081  }
082
083  public Map<String, String> params() {
084    return params;
085  }
086
087  public long getCreatedAt() {
088    return createdAt;
089  }
090
091  public void setCreatedAt(long createdAt) {
092    this.createdAt = createdAt;
093  }
094
095  public ActiveRulesBuilder activate() {
096    builder.activate(this);
097    return builder;
098  }
099}