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