Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.ui.table; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import org.onosproject.ui.JsonUtils; |
| 23 | |
| 24 | /** |
| 25 | * Provides static utility methods for dealing with tables. |
| 26 | */ |
| 27 | public final class TableUtils { |
| 28 | |
| 29 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 30 | |
| 31 | // non-instantiable |
| 32 | private TableUtils() { } |
| 33 | |
| 34 | /** |
| 35 | * Produces a JSON array node from the specified table rows. |
| 36 | * |
| 37 | * @param rows table rows |
| 38 | * @return JSON array |
| 39 | */ |
| 40 | public static ArrayNode generateArrayNode(TableRow[] rows) { |
| 41 | ArrayNode array = MAPPER.createArrayNode(); |
| 42 | for (TableRow r : rows) { |
| 43 | array.add(r.toJsonNode()); |
| 44 | } |
| 45 | return array; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Creates a row comparator for the given request. The ID of the column |
| 50 | * to sort on is the payload's "sortCol" property (defaults to "id"). |
| 51 | * The direction for the sort is the payload's "sortDir" property |
| 52 | * (defaults to "asc"). |
| 53 | * |
| 54 | * @param payload the event payload |
| 55 | * @return a row comparator |
| 56 | */ |
| 57 | public static RowComparator createRowComparator(ObjectNode payload) { |
| 58 | return createRowComparator(payload, "id"); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Creates a row comparator for the given request. The ID of the column to |
| 63 | * sort on is the payload's "sortCol" property (or the specified default). |
| 64 | * The direction for the sort is the payload's "sortDir" property |
| 65 | * (defaults to "asc"). |
| 66 | * |
| 67 | * @param payload the event payload |
| 68 | * @param defColId the default column ID |
| 69 | * @return a row comparator |
| 70 | */ |
| 71 | public static RowComparator createRowComparator(ObjectNode payload, |
| 72 | String defColId) { |
| 73 | String sortCol = JsonUtils.string(payload, "sortCol", defColId); |
| 74 | String sortDir = JsonUtils.string(payload, "sortDir", "asc"); |
| 75 | return new RowComparator(sortCol, RowComparator.direction(sortDir)); |
| 76 | } |
| 77 | } |