001    package org.sonar.wsclient.gwt.unmarshallers;
002    
003    import com.google.gwt.json.client.JSONArray;
004    import com.google.gwt.json.client.JSONObject;
005    import org.sonar.gwt.JsonUtils;
006    import org.sonar.wsclient.services.TimeMachine;
007    import org.sonar.wsclient.services.TimeMachineCell;
008    import org.sonar.wsclient.services.TimeMachineColumn;
009    
010    public class TimeMachineUnmarshaller extends AbstractUnmarshaller<TimeMachine> {
011    
012      protected TimeMachine parse(JSONObject json) {
013        JSONArray cols = json.get("cols").isArray();
014        JSONArray cells = json.get("cells").isArray();
015        return new TimeMachine(toColumns(cols), toCells(cells));
016      }
017    
018      private TimeMachineColumn[] toColumns(JSONArray cols) {
019        int size = cols.size();
020        TimeMachineColumn[] result = new TimeMachineColumn[size];
021        for (int index = 0; index < JsonUtils.getArraySize(cols); index++) {
022          JSONObject elem = JsonUtils.getArray(cols, index);
023          result[index] = new TimeMachineColumn(index, JsonUtils.getString(elem, "metric"), null, null);
024        }
025        return result;
026      }
027    
028      private TimeMachineCell[] toCells(JSONArray cells) {
029        int size = JsonUtils.getArraySize(cells);
030        TimeMachineCell[] result = new TimeMachineCell[size];
031        for (int i = 0; i < size; i++) {
032          JSONObject cellJson = JsonUtils.getArray(cells, i);
033          JSONArray valuesJson = cellJson.get("v").isArray();
034    
035          Object[] resultValues = new Object[JsonUtils.getArraySize(valuesJson)];
036          for (int indexValue = 0; indexValue < JsonUtils.getArraySize(valuesJson); indexValue++) {
037            Object value = valuesJson.get(indexValue);
038            resultValues[indexValue] = value;
039          }
040          result[i] = new TimeMachineCell(JsonUtils.getDate(cellJson, "d"), resultValues);
041        }
042        return result;
043      }
044    
045    }