blob: b8d48575bf3b3e0ee15c0eea2d17ddbb894a43d9 [file] [log] [blame]
Simon Huntabd16f62015-05-01 13:14:40 -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
19import com.fasterxml.jackson.databind.node.ObjectNode;
Simon Hunt3d1b0652015-05-05 17:27:24 -070020import org.onosproject.ui.JsonUtils;
Simon Huntabd16f62015-05-01 13:14:40 -070021import org.onosproject.ui.RequestHandler;
22
Simon Huntabd16f62015-05-01 13:14:40 -070023/**
24 * Message handler specifically for table views.
25 */
26public 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 Hunt3d1b0652015-05-05 17:27:24 -070049 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 Huntabd16f62015-05-01 13:14:40 -070056 ObjectNode rootNode = MAPPER.createObjectNode();
Simon Hunt3d1b0652015-05-05 17:27:24 -070057 rootNode.set(nodeName, TableUtils.generateArrayNode(tm));
Simon Huntabd16f62015-05-01 13:14:40 -070058 sendMessage(respType, 0, rootNode);
59 }
60
61 /**
Simon Hunt3d1b0652015-05-05 17:27:24 -070062 * 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 Huntabd16f62015-05-01 13:14:40 -070067 *
Simon Hunt3d1b0652015-05-05 17:27:24 -070068 * @return an empty table model
Simon Huntabd16f62015-05-01 13:14:40 -070069 */
Simon Hunt3d1b0652015-05-05 17:27:24 -070070 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 Huntabd16f62015-05-01 13:14:40 -070083 return "id";
84 }
85
86 /**
Simon Hunt3d1b0652015-05-05 17:27:24 -070087 * Subclasses should return the array of column IDs with which
88 * to initialize their table model.
Simon Huntabd16f62015-05-01 13:14:40 -070089 *
Simon Hunt3d1b0652015-05-05 17:27:24 -070090 * @return the column IDs
Simon Huntabd16f62015-05-01 13:14:40 -070091 */
Simon Hunt3d1b0652015-05-05 17:27:24 -070092 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 Huntabd16f62015-05-01 13:14:40 -0700111}