blob: eb2dff7808f158758f1ae041cdd276ee961e0d86 [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 /**
Simon Hunt3d1b0652015-05-05 17:27:24 -070035 * Generates a JSON array node from a table model.
Simon Hunt44aa2f82015-04-30 15:01:35 -070036 *
Simon Hunt3d1b0652015-05-05 17:27:24 -070037 * @param tm the table model
38 * @return the array node representation
Simon Hunt44aa2f82015-04-30 15:01:35 -070039 */
Simon Hunt3d1b0652015-05-05 17:27:24 -070040 public static ArrayNode generateArrayNode(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
Simon Hunt3d1b0652015-05-05 17:27:24 -070048 private static JsonNode toJsonNode(TableModel.Row row, TableModel tm) {
49 ObjectNode result = MAPPER.createObjectNode();
50 String[] keys = tm.getColumnIds();
51 String[] cells = row.getAsFormattedStrings();
52 int n = keys.length;
53 for (int i = 0; i < n; i++) {
54 result.put(keys[i], cells[i]);
55 }
56 return result;
Simon Hunt44aa2f82015-04-30 15:01:35 -070057 }
58}