001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
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.wsclient.services;
021    
022    import java.util.Collections;
023    import java.util.Date;
024    import java.util.List;
025    
026    public class Resource extends Model {
027    
028      /* SCOPES */
029      public static final String SCOPE_SET = "PRJ";
030      public static final String SCOPE_SPACE = "DIR";
031      public static final String SCOPE_ENTITY = "FIL";
032      
033      /* QUALIFIERS */
034      public static final String QUALIFIER_VIEW = "VW";
035      public static final String QUALIFIER_SUBVIEW = "SVW";
036      public static final String QUALIFIER_LIB = "LIB";
037      public static final String QUALIFIER_PROJECT = "TRK";
038      public static final String QUALIFIER_MODULE = "BRC";
039      public static final String QUALIFIER_PACKAGE = "PAC";
040      public static final String QUALIFIER_DIRECTORY = "DIR";
041      public static final String QUALIFIER_FILE = "FIL";
042      public static final String QUALIFIER_CLASS = "CLA";
043      public static final String QUALIFIER_UNIT_TEST_CLASS = "UTS";
044    
045      /* LANGUAGES */
046      public static final String LANGUAGE_JAVA = "java";
047    
048      private Integer id;
049      private String key;
050      private String name;
051      private String longName;
052      private String scope;
053      private String qualifier;
054      private String language;
055      private String version;
056      private Integer copy;
057      private String description;
058      private Date date;
059      private List<Measure> measures;
060    
061      public Integer getId() {
062        return id;
063      }
064    
065      public Resource setId(Integer id) {
066        this.id = id;
067        return this;
068      }
069    
070      public String getKey() {
071        return key;
072      }
073    
074      public Resource setKey(String key) {
075        this.key = key;
076        return this;
077      }
078    
079      public String getDescription() {
080        return description;
081      }
082    
083      public Resource setDescription(String description) {
084        this.description = description;
085        return this;
086      }
087    
088      public String getName() {
089        return name;
090      }
091    
092      public String getName(boolean longFormatIfDefined) {
093        if (longFormatIfDefined && longName != null && !"".equals(longName)) {
094          return longName;
095        }
096        return name;
097      }
098    
099      public String getLongName() {
100        return longName;
101      }
102    
103      public Resource setLongName(String longName) {
104        this.longName = longName;
105        return this;
106      }
107    
108      public Resource setName(String s) {
109        this.name = s;
110        return this;
111      }
112    
113      public String getScope() {
114        return scope;
115      }
116    
117      public Resource setScope(String scope) {
118        this.scope = scope;
119        return this;
120      }
121    
122      public String getQualifier() {
123        return qualifier;
124      }
125    
126      public Resource setQualifier(String qualifier) {
127        this.qualifier = qualifier;
128        return this;
129      }
130    
131      public String getLanguage() {
132        return language;
133      }
134    
135      public Resource setLanguage(String language) {
136        this.language = language;
137        return this;
138      }
139    
140      public String getVersion() {
141        return version;
142      }
143    
144      public Resource setVersion(String version) {
145        this.version = version;
146        return this;
147      }
148    
149      public Integer getCopy() {
150        return copy;
151      }
152    
153      public Resource setCopy(Integer copy) {
154        this.copy = copy;
155        return this;
156      }
157    
158      public Date getDate() {
159        return date;
160      }
161    
162      public Resource setDate(Date d) {
163        this.date = d;
164        return this;
165      }
166    
167      public List<Measure> getMeasures() {
168        if (measures == null) {
169          return Collections.emptyList();
170        }
171        return measures;
172      }
173    
174      public Measure getMeasure(String metricKey) {
175        for (Measure measure : getMeasures()) {
176          if (metricKey.equals(measure.getMetricKey())) {
177            return measure;
178          }
179        }
180        return null;
181      }
182    
183      public Double getMeasureValue(String metricKey) {
184        Measure measure = getMeasure(metricKey);
185        if (measure != null) {
186          return measure.getValue();
187        }
188        return null;
189      }
190    
191      public Integer getMeasureIntValue(String metricKey) {
192        Double d = getMeasureValue(metricKey);
193        if (d != null) {
194          return d.intValue();
195        }
196        return null;
197      }
198    
199      public String getMeasureFormattedValue(String metricKey, String defaultValue) {
200        Measure measure = getMeasure(metricKey);
201        if (measure != null) {
202          return measure.getFormattedValue(defaultValue);
203        }
204        return defaultValue;
205      }
206    
207      public void setMeasures(List<Measure> measures) {
208        this.measures = measures;
209      }
210    
211      @Override
212      public String toString() {
213        return new StringBuilder()
214            .append("[id=")
215            .append(id)
216            .append(",key=")
217            .append(key)
218            .append("]")
219            .toString();
220      }
221    }