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