View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.component.gchart.model;
23  
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.primefaces.extensions.util.json.GsonConverter;
30  
31  class DefaultGChartModel implements GChartModel {
32  
33      private static final long serialVersionUID = -4757917806522708660L;
34  
35      private final List<GChartModelRow> rows;
36      private final GChartType gChartType;
37      private final transient Map<String, Object> options;
38      private final transient List<Object> columns;
39  
40      public DefaultGChartModel(final List<GChartModelRow> rows, final GChartType gChartType,
41                  final Map<String, Object> options, final List<Object> columns) {
42          super();
43          this.rows = rows;
44          this.gChartType = gChartType;
45          this.options = options;
46          this.columns = columns;
47      }
48  
49      @Override
50      public Map<String, Object> getOptions() {
51          return options;
52      }
53  
54      @Override
55      public Collection<Object> getColumns() {
56          return columns;
57      }
58  
59      @Override
60      public GChartType getChartType() {
61          return gChartType;
62      }
63  
64      @Override
65      public Collection<GChartModelRow> getRows() {
66          return rows;
67      }
68  
69      @Override
70      public String toJson() {
71          final com.google.gson.JsonObject root = new com.google.gson.JsonObject();
72  
73          root.addProperty("type", getChartType().getChartName());
74          root.add("options", GsonConverter.getGson().toJsonTree(getOptions()));
75          root.add("data", extractData());
76  
77          return GsonConverter.getGson().toJson(root);
78      }
79  
80      protected com.google.gson.JsonElement extractData() {
81          final Collection<Collection<Object>> dataTable = new ArrayList<>(0);
82  
83          dataTable.add(getColumns());
84  
85          for (final GChartModelRow row : getRows()) {
86              final Collection<Object> dataRow = new ArrayList<>(0);
87              dataRow.add(row.getLabel());
88              dataRow.addAll(row.getValues());
89  
90              dataTable.add(dataRow);
91          }
92  
93          return GsonConverter.getGson().toJsonTree(dataTable);
94      }
95  
96  }