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     */
020    package org.sonar.wsclient.issue.internal;
021    
022    import org.sonar.wsclient.issue.Issue;
023    import org.sonar.wsclient.issue.IssueComment;
024    import org.sonar.wsclient.issue.WorkDayDuration;
025    import org.sonar.wsclient.unmarshallers.JsonUtils;
026    
027    import javax.annotation.CheckForNull;
028    
029    import java.util.*;
030    
031    /**
032     * @since 3.6
033     */
034    public class DefaultIssue implements Issue {
035    
036      private final Map json;
037    
038      DefaultIssue(Map json) {
039        this.json = json;
040      }
041    
042      /**
043       * Unique key
044       */
045      public String key() {
046        return JsonUtils.getString(json, "key");
047      }
048    
049      public String componentKey() {
050        return JsonUtils.getString(json, "component");
051      }
052    
053      public String projectKey() {
054        return JsonUtils.getString(json, "project");
055      }
056    
057      public String ruleKey() {
058        return JsonUtils.getString(json, "rule");
059      }
060    
061      public String severity() {
062        return JsonUtils.getString(json, "severity");
063      }
064    
065      @CheckForNull
066      public String message() {
067        return JsonUtils.getString(json, "message");
068      }
069    
070      @CheckForNull
071      public Integer line() {
072        return JsonUtils.getInteger(json, "line");
073      }
074    
075      @CheckForNull
076      public Double effortToFix() {
077        return JsonUtils.getDouble(json, "effortToFix");
078      }
079    
080      @CheckForNull
081      public WorkDayDuration technicalDebt() {
082        Map technicalDebt = (Map) json.get("technicalDebt");
083        if (technicalDebt != null) {
084          return new DefaultWorkDayDuration(technicalDebt);
085        }
086        return null;
087      }
088    
089      public String status() {
090        return JsonUtils.getString(json, "status");
091      }
092    
093      /**
094       * The resolution type. Null if the issue is not resolved.
095       */
096      @CheckForNull
097      public String resolution() {
098        return JsonUtils.getString(json, "resolution");
099      }
100    
101      @CheckForNull
102      public String reporter() {
103        return JsonUtils.getString(json, "reporter");
104      }
105    
106      /**
107       * Login of assignee. Null if issue is not assigned.
108       */
109      @CheckForNull
110      public String assignee() {
111        return JsonUtils.getString(json, "assignee");
112      }
113    
114      /**
115       * SCM account
116       */
117      @CheckForNull
118      public String author() {
119        return JsonUtils.getString(json, "author");
120      }
121    
122      @CheckForNull
123      public String actionPlan() {
124        return JsonUtils.getString(json, "actionPlan");
125      }
126    
127      public Date creationDate() {
128        return JsonUtils.getDateTime(json, "creationDate");
129      }
130    
131      public Date updateDate() {
132        return JsonUtils.getDateTime(json, "updateDate");
133      }
134    
135      @CheckForNull
136      public Date closeDate() {
137        return JsonUtils.getDateTime(json, "closeDate");
138      }
139    
140      @CheckForNull
141      public String attribute(String key) {
142        return attributes().get(key);
143      }
144    
145      public Map<String, String> attributes() {
146        Map<String, String> attr = (Map<String,String>) json.get("attr");
147        if (attr == null) {
148          return Collections.emptyMap();
149        }
150        return attr;
151      }
152    
153      /**
154       * Non-null list of comments
155       */
156      public List<IssueComment> comments() {
157        List<IssueComment> comments = new ArrayList<IssueComment>();
158        List<Map> jsonComments = (List<Map>) json.get("comments");
159        if (jsonComments != null) {
160          for (Map jsonComment : jsonComments) {
161            comments.add(new DefaultIssueComment(jsonComment));
162          }
163        }
164        return comments;
165      }
166    }