aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ResourceUnmarshaller.java
blob: e322ac458f283aba5cb7dc6ad60dab96875131cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Sonar, open source software quality management tool.
 * Copyright (C) 2008-2011 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * Sonar is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Sonar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sonar; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.wsclient.unmarshallers;

import org.sonar.wsclient.services.Measure;
import org.sonar.wsclient.services.Resource;
import org.sonar.wsclient.services.WSUtils;

import java.util.ArrayList;
import java.util.List;

public class ResourceUnmarshaller extends AbstractUnmarshaller<Resource> {

  @Override
  protected Resource parse(Object json) {
    Resource resource = new Resource();
    parseResourceFields(json, resource);
    parseMeasures(json, resource);
    return resource;
  }

  private void parseResourceFields(Object json, Resource resource) {
    WSUtils utils = WSUtils.getINSTANCE();
    resource.setId(utils.getInteger(json, "id"))
        .setKey(utils.getString(json, "key"))
        .setName(utils.getString(json, "name"))
        .setLongName(utils.getString(json, "lname"))
        .setScope(utils.getString(json, "scope"))
        .setQualifier(utils.getString(json, "qualifier"))
        .setLanguage(utils.getString(json, "lang"))
        .setDescription(utils.getString(json, "description"))
        .setDate(utils.getDateTime(json, "date"))
        .setVersion(utils.getString(json, "version"))
        .setPeriod1Mode(utils.getString(json, "p1"))
        .setPeriod1Param(utils.getString(json, "p1p"))
        .setPeriod1Date(utils.getDateTime(json, "p1d"))
        .setPeriod2Mode(utils.getString(json, "p2"))
        .setPeriod2Param(utils.getString(json, "p2p"))
        .setPeriod2Date(utils.getDateTime(json, "p2d"))
        .setPeriod3Mode(utils.getString(json, "p3"))
        .setPeriod3Param(utils.getString(json, "p3p"))
        .setPeriod3Date(utils.getDateTime(json, "p3d"))
        .setPeriod4Mode(utils.getString(json, "p4"))
        .setPeriod4Param(utils.getString(json, "p4p"))
        .setPeriod4Date(utils.getDateTime(json, "p4d"))
        .setPeriod5Mode(utils.getString(json, "p5"))
        .setPeriod5Param(utils.getString(json, "p5p"))
        .setPeriod5Date(utils.getDateTime(json, "p5d"));
  }

  private void parseMeasures(Object json, Resource resource) {
    WSUtils utils = WSUtils.getINSTANCE();
    Object measuresJson = utils.getField(json, "msr");
    if (measuresJson != null) {
      resource.setMeasures(parseMeasures(measuresJson));
    }
  }

  private List<Measure> parseMeasures(Object measuresJson) {
    WSUtils utils = WSUtils.getINSTANCE();
    List<Measure> projectMeasures = new ArrayList<Measure>();
    int len = utils.getArraySize(measuresJson);
    for (int i = 0; i < len; i++) {
      Object measureJson = utils.getArrayElement(measuresJson, i);
      if (measureJson != null) {
        Measure measure = parseMeasure(measureJson);
        projectMeasures.add(measure);
      }
    }
    return projectMeasures;
  }

  private Measure parseMeasure(Object json) {
    WSUtils utils = WSUtils.getINSTANCE();

    Measure measure = new Measure();
    measure
        .setMetricKey(utils.getString(json, "key"))
        .setMetricName(utils.getString(json, "name"))
        .setValue(utils.getDouble(json, "val"))
        .setFormattedValue(utils.getString(json, "frmt_val"))
        .setTrend(utils.getInteger(json, "trend"))
        .setVar(utils.getInteger(json, "var"))
        .setData(utils.getString(json, "data"))
        .setRuleKey(utils.getString(json, "rule_key"))
        .setRuleName(utils.getString(json, "rule_name"))
        .setRuleCategory(utils.getString(json, "rule_category"))
        .setRuleSeverity(utils.getString(json, "rule_priority"))
        .setCharacteristicKey(utils.getString(json, "ctic_key"))
        .setCharacteristicName(utils.getString(json, "ctic_name"))
        .setVariation1(utils.getDouble(json, "var1"))
        .setVariation2(utils.getDouble(json, "var2"))
        .setVariation3(utils.getDouble(json, "var3"))
        .setVariation4(utils.getDouble(json, "var4"))
        .setVariation5(utils.getDouble(json, "var5"));
    return measure;
  }
}