Simon Hunt | 84f4c2a | 2015-09-23 17:52:45 -0700 | [diff] [blame] | 1 | #set( $symbol_pound = '#' ) |
| 2 | #set( $symbol_dollar = '$' ) |
| 3 | #set( $symbol_escape = '\' ) |
| 4 | /* |
Jian Li | c6c76dc | 2016-01-26 14:22:51 -0800 | [diff] [blame] | 5 | * Copyright ${year} Open Networking Laboratory |
Simon Hunt | 84f4c2a | 2015-09-23 17:52:45 -0700 | [diff] [blame] | 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | package ${package}; |
| 20 | |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import com.google.common.collect.ImmutableSet; |
| 23 | import org.onosproject.ui.RequestHandler; |
| 24 | import org.onosproject.ui.UiMessageHandler; |
| 25 | import org.onosproject.ui.table.TableModel; |
| 26 | import org.onosproject.ui.table.TableRequestHandler; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import java.lang.Override; |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.Collection; |
| 33 | import java.util.List; |
| 34 | |
| 35 | /** |
| 36 | * Skeletal ONOS UI Table-View message handler. |
| 37 | */ |
Thomas Vachuska | 2b0fc46 | 2015-09-28 12:04:06 -0700 | [diff] [blame] | 38 | public class AppUiTableMessageHandler extends UiMessageHandler { |
Simon Hunt | 84f4c2a | 2015-09-23 17:52:45 -0700 | [diff] [blame] | 39 | |
| 40 | private static final String SAMPLE_TABLE_DATA_REQ = "sampleTableDataRequest"; |
| 41 | private static final String SAMPLE_TABLE_DATA_RESP = "sampleTableDataResponse"; |
| 42 | private static final String SAMPLE_TABLES = "sampleTables"; |
| 43 | |
| 44 | private static final String SAMPLE_TABLE_DETAIL_REQ = "sampleTableDetailsRequest"; |
| 45 | private static final String SAMPLE_TABLE_DETAIL_RESP = "sampleTableDetailsResponse"; |
| 46 | private static final String DETAILS = "details"; |
| 47 | |
Jian Li | 3dbc888 | 2016-01-15 17:07:18 -0800 | [diff] [blame] | 48 | private static final String NO_ROWS_MESSAGE = "No items found"; |
| 49 | |
Simon Hunt | 84f4c2a | 2015-09-23 17:52:45 -0700 | [diff] [blame] | 50 | private static final String ID = "id"; |
| 51 | private static final String LABEL = "label"; |
| 52 | private static final String CODE = "code"; |
| 53 | private static final String COMMENT = "comment"; |
| 54 | private static final String RESULT = "result"; |
| 55 | |
| 56 | private static final String[] COLUMN_IDS = { ID, LABEL, CODE }; |
| 57 | |
| 58 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 59 | |
| 60 | |
| 61 | @Override |
| 62 | protected Collection<RequestHandler> createRequestHandlers() { |
| 63 | return ImmutableSet.of( |
| 64 | new SampleTableDataRequestHandler(), |
| 65 | new SampleTableDetailRequestHandler() |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | // handler for sample table requests |
| 70 | private final class SampleTableDataRequestHandler extends TableRequestHandler { |
| 71 | |
| 72 | private SampleTableDataRequestHandler() { |
| 73 | super(SAMPLE_TABLE_DATA_REQ, SAMPLE_TABLE_DATA_RESP, SAMPLE_TABLES); |
| 74 | } |
| 75 | |
| 76 | // if necessary, override defaultColumnId() -- if it isn't "id" |
| 77 | |
| 78 | @Override |
| 79 | protected String[] getColumnIds() { |
| 80 | return COLUMN_IDS; |
| 81 | } |
| 82 | |
| 83 | // if required, override createTableModel() to set column formatters / comparators |
| 84 | |
| 85 | @Override |
Jian Li | 3dbc888 | 2016-01-15 17:07:18 -0800 | [diff] [blame] | 86 | protected String noRowsMessage(ObjectNode payload) { |
| 87 | return NO_ROWS_MESSAGE; |
| 88 | } |
| 89 | |
| 90 | @Override |
Simon Hunt | 84f4c2a | 2015-09-23 17:52:45 -0700 | [diff] [blame] | 91 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 92 | // === NOTE: the table model supplied here will have been created |
| 93 | // via a call to createTableModel(). To assign non-default |
| 94 | // cell formatters or comparators to the table model, override |
| 95 | // createTableModel() and set them there. |
| 96 | |
| 97 | // === retrieve table row items from some service... |
| 98 | // SomeService ss = get(SomeService.class); |
| 99 | // List<Item> items = ss.getItems() |
| 100 | |
| 101 | // fake data for demonstration purposes... |
| 102 | List<Item> items = getItems(); |
| 103 | for (Item item: items) { |
| 104 | populateRow(tm.addRow(), item); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private void populateRow(TableModel.Row row, Item item) { |
| 109 | row.cell(ID, item.id()) |
| 110 | .cell(LABEL, item.label()) |
| 111 | .cell(CODE, item.code()); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | |
| 116 | // handler for sample item details requests |
| 117 | private final class SampleTableDetailRequestHandler extends RequestHandler { |
| 118 | |
| 119 | private SampleTableDetailRequestHandler() { |
| 120 | super(SAMPLE_TABLE_DETAIL_REQ); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void process(long sid, ObjectNode payload) { |
| 125 | String id = string(payload, ID, "(none)"); |
| 126 | |
| 127 | // SomeService ss = get(SomeService.class); |
| 128 | // Item item = ss.getItemDetails(id) |
| 129 | |
| 130 | // fake data for demonstration purposes... |
| 131 | Item item = getItem(id); |
| 132 | |
Simon Hunt | 1035d6c | 2015-10-01 18:23:15 -0700 | [diff] [blame] | 133 | ObjectNode rootNode = objectNode(); |
| 134 | ObjectNode data = objectNode(); |
Simon Hunt | 84f4c2a | 2015-09-23 17:52:45 -0700 | [diff] [blame] | 135 | rootNode.set(DETAILS, data); |
| 136 | |
| 137 | if (item == null) { |
| 138 | rootNode.put(RESULT, "Item with id '" + id + "' not found"); |
| 139 | log.warn("attempted to get item detail for id '{}'", id); |
| 140 | |
| 141 | } else { |
| 142 | rootNode.put(RESULT, "Found item with id '" + id + "'"); |
| 143 | |
| 144 | data.put(ID, item.id()); |
| 145 | data.put(LABEL, item.label()); |
| 146 | data.put(CODE, item.code()); |
| 147 | data.put(COMMENT, "Some arbitrary comment"); |
| 148 | } |
| 149 | |
| 150 | sendMessage(SAMPLE_TABLE_DETAIL_RESP, 0, rootNode); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | |
| 155 | // =================================================================== |
| 156 | // NOTE: The code below this line is to create fake data for this |
| 157 | // sample code. Normally you would use existing services to |
| 158 | // provide real data. |
| 159 | |
| 160 | // Lookup a single item. |
| 161 | private static Item getItem(String id) { |
| 162 | // We realize this code is really inefficient, but |
| 163 | // it suffices for our purposes of demonstration... |
| 164 | for (Item item : getItems()) { |
| 165 | if (item.id().equals(id)) { |
| 166 | return item; |
| 167 | } |
| 168 | } |
| 169 | return null; |
| 170 | } |
| 171 | |
| 172 | // Produce a list of items. |
| 173 | private static List<Item> getItems() { |
| 174 | List<Item> items = new ArrayList<>(); |
| 175 | items.add(new Item("item-1", "foo", 42)); |
| 176 | items.add(new Item("item-2", "bar", 99)); |
| 177 | items.add(new Item("item-3", "baz", 65)); |
| 178 | return items; |
| 179 | } |
| 180 | |
| 181 | // Simple model class to provide sample data |
| 182 | private static class Item { |
| 183 | private final String id; |
| 184 | private final String label; |
| 185 | private final int code; |
| 186 | |
| 187 | Item(String id, String label, int code) { |
| 188 | this.id = id; |
| 189 | this.label = label; |
| 190 | this.code = code; |
| 191 | } |
| 192 | |
| 193 | String id() { return id; } |
| 194 | String label() { return label; } |
| 195 | int code() { return code; } |
| 196 | } |
| 197 | } |