001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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.rules;
021
022import org.apache.commons.lang.StringUtils;
023import org.apache.commons.lang.builder.ToStringBuilder;
024
025import javax.persistence.*;
026
027@Entity
028@Table(name = "rules_parameters")
029public class RuleParam {
030
031  @Id
032  @Column(name = "id")
033  @GeneratedValue
034  private Integer id;
035
036  @ManyToOne(fetch = FetchType.EAGER)
037  @JoinColumn(name = "rule_id")
038  private Rule rule;
039
040  @Column(name = "name", updatable = true, nullable = false, length = 128)
041  private String key;
042
043  @Column(name = "description", updatable = true, nullable = true, length = 4000)
044  private String description;
045
046  @Column(name = "param_type", updatable = true, nullable = true, length = 512)
047  private String type = "STRING";
048
049  @Column(name = "default_value", updatable = true, nullable = true, length = 4000)
050  private String defaultValue;
051
052  /**
053   * @deprecated since 2.3 use the factory method Rule.setParameter()
054   */
055  @Deprecated
056  public RuleParam() {
057  }
058
059  /**
060   * @deprecated since 2.3 use the factory method setParameter()
061   */
062  @Deprecated
063  public RuleParam(Rule rule, String key, String description, String type) {
064    this.rule = rule;
065    this.key = key;
066    this.description = description;
067    this.type = type;
068  }
069
070  public Integer getId() {
071    return id;
072  }
073
074  public Rule getRule() {
075    return rule;
076  }
077
078  RuleParam setRule(Rule rule) {
079    this.rule = rule;
080    return this;
081  }
082
083  public String getKey() {
084    return key;
085  }
086
087  public RuleParam setKey(String key) {
088    this.key = key;
089    return this;
090  }
091
092  public String getDescription() {
093    return description;
094  }
095
096  public RuleParam setDescription(String s) {
097    this.description = StringUtils.defaultString(s, "");
098    return this;
099  }
100
101  public String getType() {
102    return type;
103  }
104
105  public RuleParam setType(String type) {
106    this.type = type;
107    return this;
108  }
109
110  public String getDefaultValue() {
111    return defaultValue;
112  }
113
114  public Boolean getDefaultValueAsBoolean() {
115    if (defaultValue != null) {
116      return Boolean.parseBoolean(defaultValue);
117    }
118    return null;
119  }
120
121  public Integer getDefaultValueAsInteger() {
122    if (defaultValue != null) {
123      return Integer.parseInt(defaultValue);
124    }
125    return null;
126  }
127
128  public RuleParam setDefaultValue(String s) {
129    this.defaultValue = s;
130    return this;
131  }
132
133  @Override
134  public boolean equals(Object obj) {
135    if (!(obj instanceof RuleParam)) {
136      return false;
137    }
138    if (this == obj) {
139      return true;
140    }
141    RuleParam other = (RuleParam) obj;
142    return other.key.equals(key);
143  }
144
145  @Override
146  public int hashCode() {
147    return key.hashCode();
148  }
149
150  @Override
151  public String toString() {
152    return new ToStringBuilder(this)
153        .append("id", id)
154        .append("key", key)
155        .append("desc", description)
156        .append("type", type)
157        .toString();
158  }
159}