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