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.Arrays;
26  import java.util.Collection;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.primefaces.model.TreeNode;
32  import org.primefaces.util.Constants;
33  
34  public class GChartModelBuilder {
35      private final List<GChartModelRow> rows = new ArrayList<>(0);
36      private GChartType gChartType;
37      private final Map<String, Object> options = new HashMap<>(0);
38      private final List<Object> columns = new ArrayList<>(0);
39  
40      public GChartModelBuilder setChartType(final GChartType chartType) {
41          if (gChartType != null) {
42              throw new IllegalStateException("GChart Type already set");
43          }
44          gChartType = chartType;
45  
46          return this;
47      }
48  
49      public GChartModelBuilder importTreeNode(final TreeNode root) {
50          final String label = String.valueOf(root.getData());
51          final String parentLabel = root.getParent() != null ? String.valueOf(root.getParent().getData()) : Constants.EMPTY_STRING;
52  
53          this.addRow(label, parentLabel);
54  
55          final List<TreeNode> nodes = root.getChildren();
56          for (final TreeNode node : nodes) {
57              importTreeNode(node);
58          }
59  
60          return this;
61      }
62  
63      public GChartModelBuilder addColumns(final Object... columns) {
64          this.columns.addAll(Arrays.asList(columns));
65          return this;
66      }
67  
68      public GChartModelBuilder addColumns(final Collection<Object> columns) {
69          this.columns.addAll(columns);
70          return this;
71      }
72  
73      public GChartModelBuilder addRow(final String label, final Object... objects) {
74          rows.add(new DefaultGChartModelRow(label, Arrays.asList(objects)));
75          return this;
76      }
77  
78      public GChartModelBuilder addRow(final String label, final Collection<Object> objects) {
79          rows.add(new DefaultGChartModelRow(label, objects));
80          return this;
81      }
82  
83      public GChartModelBuilder addOption(final String name, final Object value) {
84          options.put(name, value);
85          return this;
86      }
87  
88      public GChartModel build() {
89          return new DefaultGChartModel(rows, gChartType, options, columns);
90      }
91  }