Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -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.node.ObjectNode; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 20 | import org.onosproject.ui.JsonUtils; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 21 | import org.onosproject.ui.RequestHandler; |
| 22 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 23 | /** |
| 24 | * Message handler specifically for table views. |
| 25 | */ |
| 26 | public abstract class TableRequestHandler extends RequestHandler { |
| 27 | |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame^] | 28 | private static final String ANNOTS = "annots"; |
| 29 | private static final String NO_ROWS_MSG_KEY = "no_rows_msg"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 30 | private final String respType; |
| 31 | private final String nodeName; |
| 32 | |
| 33 | /** |
| 34 | * Constructs a table request handler for a specific table view. When |
| 35 | * table requests come in, the handler will generate the appropriate |
| 36 | * table rows, sort them according the the request sort parameters, and |
| 37 | * send back the response to the client. |
| 38 | * |
| 39 | * @param reqType type of the request event |
| 40 | * @param respType type of the response event |
| 41 | * @param nodeName name of JSON node holding row data |
| 42 | */ |
| 43 | public TableRequestHandler(String reqType, String respType, String nodeName) { |
| 44 | super(reqType); |
| 45 | this.respType = respType; |
| 46 | this.nodeName = nodeName; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 51 | TableModel tm = createTableModel(); |
| 52 | populateTable(tm, payload); |
| 53 | |
| 54 | String sortCol = JsonUtils.string(payload, "sortCol", defaultColumnId()); |
| 55 | String sortDir = JsonUtils.string(payload, "sortDir", "asc"); |
| 56 | tm.sort(sortCol, TableModel.sortDir(sortDir)); |
| 57 | |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame^] | 58 | addTableConfigAnnotations(tm); |
| 59 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 60 | ObjectNode rootNode = MAPPER.createObjectNode(); |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame^] | 61 | rootNode.set(nodeName, TableUtils.generateRowArrayNode(tm)); |
| 62 | rootNode.set(ANNOTS, TableUtils.generateAnnotObjectNode(tm)); |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 63 | sendMessage(respType, 0, rootNode); |
| 64 | } |
| 65 | |
| 66 | /** |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 67 | * Creates the table model (devoid of data) using {@link #getColumnIds()} |
| 68 | * to initialize it, ready to be populated. |
| 69 | * <p> |
| 70 | * This default implementation returns a table model with default |
| 71 | * formatters and comparators for all columns. |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 72 | * |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 73 | * @return an empty table model |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 74 | */ |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 75 | protected TableModel createTableModel() { |
| 76 | return new TableModel(getColumnIds()); |
| 77 | } |
| 78 | |
| 79 | /** |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame^] | 80 | * Adds all annotations to table model. |
| 81 | * |
| 82 | * @param tm a table model |
| 83 | */ |
| 84 | protected void addTableConfigAnnotations(TableModel tm) { |
| 85 | tm.addAnnotation(NO_ROWS_MSG_KEY, noRowsMessage()); |
| 86 | } |
| 87 | |
| 88 | /** |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 89 | * Returns the default column ID to be used when one is not supplied in |
| 90 | * the payload as the column on which to sort. |
| 91 | * <p> |
| 92 | * This default implementation returns "id". |
| 93 | * |
| 94 | * @return default sort column identifier |
| 95 | */ |
| 96 | protected String defaultColumnId() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 97 | return "id"; |
| 98 | } |
| 99 | |
| 100 | /** |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 101 | * Subclasses should return the array of column IDs with which |
| 102 | * to initialize their table model. |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 103 | * |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 104 | * @return the column IDs |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 105 | */ |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 106 | protected abstract String[] getColumnIds(); |
| 107 | |
| 108 | /** |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame^] | 109 | * Subclasses should return the message to display in the table when there |
| 110 | * are no rows to display. For example, a host table might return |
| 111 | * "No hosts found". |
| 112 | * |
| 113 | * @return the message |
| 114 | */ |
| 115 | protected abstract String noRowsMessage(); |
| 116 | |
| 117 | /** |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 118 | * Subclasses should populate the table model by adding |
| 119 | * {@link TableModel.Row rows}. |
| 120 | * <pre> |
| 121 | * tm.addRow() |
| 122 | * .cell(COL_ONE, ...) |
| 123 | * .cell(COL_TWO, ...) |
| 124 | * ... ; |
| 125 | * </pre> |
| 126 | * The request payload is provided in case there are request filtering |
| 127 | * parameters (other than sort column and sort direction) that are required |
| 128 | * to generate the appropriate data. |
| 129 | * |
| 130 | * @param tm the table model |
| 131 | * @param payload request payload |
| 132 | */ |
| 133 | protected abstract void populateTable(TableModel tm, ObjectNode payload); |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame^] | 134 | } |