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