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 | |
| 28 | private final String respType; |
| 29 | private final String nodeName; |
| 30 | |
| 31 | /** |
| 32 | * Constructs a table request handler for a specific table view. When |
| 33 | * table requests come in, the handler will generate the appropriate |
| 34 | * table rows, sort them according the the request sort parameters, and |
| 35 | * send back the response to the client. |
| 36 | * |
| 37 | * @param reqType type of the request event |
| 38 | * @param respType type of the response event |
| 39 | * @param nodeName name of JSON node holding row data |
| 40 | */ |
| 41 | public TableRequestHandler(String reqType, String respType, String nodeName) { |
| 42 | super(reqType); |
| 43 | this.respType = respType; |
| 44 | this.nodeName = nodeName; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 49 | TableModel tm = createTableModel(); |
| 50 | populateTable(tm, payload); |
| 51 | |
| 52 | String sortCol = JsonUtils.string(payload, "sortCol", defaultColumnId()); |
| 53 | String sortDir = JsonUtils.string(payload, "sortDir", "asc"); |
| 54 | tm.sort(sortCol, TableModel.sortDir(sortDir)); |
| 55 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 56 | ObjectNode rootNode = MAPPER.createObjectNode(); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 57 | rootNode.set(nodeName, TableUtils.generateArrayNode(tm)); |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 58 | sendMessage(respType, 0, rootNode); |
| 59 | } |
| 60 | |
| 61 | /** |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 62 | * Creates the table model (devoid of data) using {@link #getColumnIds()} |
| 63 | * to initialize it, ready to be populated. |
| 64 | * <p> |
| 65 | * This default implementation returns a table model with default |
| 66 | * formatters and comparators for all columns. |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 67 | * |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 68 | * @return an empty table model |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 69 | */ |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 70 | protected TableModel createTableModel() { |
| 71 | return new TableModel(getColumnIds()); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the default column ID to be used when one is not supplied in |
| 76 | * the payload as the column on which to sort. |
| 77 | * <p> |
| 78 | * This default implementation returns "id". |
| 79 | * |
| 80 | * @return default sort column identifier |
| 81 | */ |
| 82 | protected String defaultColumnId() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 83 | return "id"; |
| 84 | } |
| 85 | |
| 86 | /** |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 87 | * Subclasses should return the array of column IDs with which |
| 88 | * to initialize their table model. |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 89 | * |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 90 | * @return the column IDs |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 91 | */ |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 92 | protected abstract String[] getColumnIds(); |
| 93 | |
| 94 | /** |
| 95 | * Subclasses should populate the table model by adding |
| 96 | * {@link TableModel.Row rows}. |
| 97 | * <pre> |
| 98 | * tm.addRow() |
| 99 | * .cell(COL_ONE, ...) |
| 100 | * .cell(COL_TWO, ...) |
| 101 | * ... ; |
| 102 | * </pre> |
| 103 | * The request payload is provided in case there are request filtering |
| 104 | * parameters (other than sort column and sort direction) that are required |
| 105 | * to generate the appropriate data. |
| 106 | * |
| 107 | * @param tm the table model |
| 108 | * @param payload request payload |
| 109 | */ |
| 110 | protected abstract void populateTable(TableModel tm, ObjectNode payload); |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 111 | } |