blob: 14c67435e67a19a0af422fe674da361548d92f70 [file] [log] [blame]
Simon Hunt44aa2f82015-04-30 15:01:35 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.ui.table;
18
Simon Hunt3d1b0652015-05-05 17:27:24 -070019import com.fasterxml.jackson.databind.JsonNode;
Simon Hunt44aa2f82015-04-30 15:01:35 -070020import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.node.ArrayNode;
22import com.fasterxml.jackson.databind.node.ObjectNode;
Simon Hunt44aa2f82015-04-30 15:01:35 -070023
24/**
25 * Provides static utility methods for dealing with tables.
26 */
27public final class TableUtils {
28
29 private static final ObjectMapper MAPPER = new ObjectMapper();
30
31 // non-instantiable
32 private TableUtils() { }
33
34 /**
Jian Li69f66632016-01-15 12:27:42 -080035 * Generates a JSON array node from the rows of the given table model.
Simon Hunt44aa2f82015-04-30 15:01:35 -070036 *
Simon Hunt3d1b0652015-05-05 17:27:24 -070037 * @param tm the table model
Jian Li69f66632016-01-15 12:27:42 -080038 * @return the array node representation of rows
Simon Hunt44aa2f82015-04-30 15:01:35 -070039 */
Jian Li69f66632016-01-15 12:27:42 -080040 public static ArrayNode generateRowArrayNode(TableModel tm) {
Simon Hunt44aa2f82015-04-30 15:01:35 -070041 ArrayNode array = MAPPER.createArrayNode();
Simon Hunt3d1b0652015-05-05 17:27:24 -070042 for (TableModel.Row r : tm.getRows()) {
43 array.add(toJsonNode(r, tm));
Simon Hunt44aa2f82015-04-30 15:01:35 -070044 }
45 return array;
46 }
47
Jian Li69f66632016-01-15 12:27:42 -080048 /**
49 * Generates a JSON object node from the annotations of the given table model.
50 *
51 * @param tm the table model
52 * @return the object node representation of the annotations
53 */
54 public static ObjectNode generateAnnotObjectNode(TableModel tm) {
55 ObjectNode node = MAPPER.createObjectNode();
56 for (TableModel.Annot a : tm.getAnnotations()) {
57 node.put(a.key(), a.valueAsString());
58 }
59 return node;
60 }
61
Simon Hunt3d1b0652015-05-05 17:27:24 -070062 private static JsonNode toJsonNode(TableModel.Row row, TableModel tm) {
63 ObjectNode result = MAPPER.createObjectNode();
64 String[] keys = tm.getColumnIds();
65 String[] cells = row.getAsFormattedStrings();
66 int n = keys.length;
67 for (int i = 0; i < n; i++) {
68 result.put(keys[i], cells[i]);
69 }
70 return result;
Simon Hunt44aa2f82015-04-30 15:01:35 -070071 }
72}