2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END====================================================
22 package org.onap.aaf.auth.gui;
24 import static org.onap.aaf.misc.xgen.html.HTMLGen.TABLE;
25 import static org.onap.aaf.misc.xgen.html.HTMLGen.TD;
26 import static org.onap.aaf.misc.xgen.html.HTMLGen.TR;
28 import java.io.IOException;
29 import java.util.ArrayList;
31 import org.onap.aaf.auth.gui.table.AbsCell;
32 import org.onap.aaf.misc.env.APIException;
33 import org.onap.aaf.misc.env.Env;
34 import org.onap.aaf.misc.env.Slot;
35 import org.onap.aaf.misc.env.Trans;
36 import org.onap.aaf.misc.env.TransStore;
37 import org.onap.aaf.misc.xgen.Cache;
38 import org.onap.aaf.misc.xgen.Code;
39 import org.onap.aaf.misc.xgen.DynamicCode;
40 import org.onap.aaf.misc.xgen.Mark;
41 import org.onap.aaf.misc.xgen.html.HTMLGen;
42 import org.onap.aaf.misc.xgen.html.State;
44 public class Table<S extends State<Env>, TRANS extends TransStore> extends NamedCode {
45 private final Slot ROW_MSG_SLOT, EMPTY_TABLE_SLOT;
46 private final String title;
47 private final String[] columns;
48 private final Rows rows;
49 private Code<HTMLGen> other;
52 public Table(String title, TRANS trans, Data<S,TRANS> data, Code<HTMLGen> other, String name, String ... attrs) {
53 this(title,trans,data,name, attrs);
57 public Table(String title, TRANS trans, Data<S,TRANS> data, String name, String ... attrs) {
59 for (String a : attrs) {
62 ROW_MSG_SLOT=trans.slot("TABLE_ROW_MSG");
63 EMPTY_TABLE_SLOT=trans.slot("TABLE_EMPTY");
64 this.columns = data.headers();
66 for (String s : attrs) {
67 if ("class=std".equals(s) || "class=stdform".equals(s)) {
71 rows = new Rows(data,alt?1:0);
73 // Derive an ID from title (from no spaces, etc), and prepend to IDAttributes (Protected from NamedCode)
74 addAttr(true,title(trans).replaceAll("\\s",""));
80 public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
81 cache.dynamic(hgen, new DynamicCode<HTMLGen,S,TRANS>() {
83 public void code(S state, TRANS trans, Cache<HTMLGen> cache, HTMLGen xgen) throws APIException, IOException {
84 rows.data.prefix(state, trans, cache, xgen);
87 Mark table = new Mark();
90 hgen.incr(table,TABLE);
92 cache.dynamic(hgen, new DynamicCode<HTMLGen,S,TRANS>() {
94 public void code(S state, TRANS trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
95 hgen.leaf("caption", "class=title").text(title(trans)).end();
99 hgen.leaf("caption", "class=title").text(title).end();
102 for (String column : columns) {
103 hgen.leaf("th").text(column).end();
107 // Load Rows Dynamically
108 cache.dynamic(hgen, rows);
113 other.code(cache,hgen);
116 // Print Message from Row Gathering, if available
117 cache.dynamic(hgen, new DynamicCode<HTMLGen,S,TRANS>() {
119 public void code(S state, TRANS trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
121 if ((msg = trans.get(EMPTY_TABLE_SLOT, null))!=null) {
122 hgen.incr("style").text("#inner tr,caption,input,p.preamble {display: none;}#inner p.notfound {margin: 0px 0px 0px 20px}").end();
123 hgen.incr(HTMLGen.P,"class=notfound").text(msg).end().br();
124 } else if ((msg=trans.get(ROW_MSG_SLOT,null))!=null) {
129 cache.dynamic(hgen, new DynamicCode<HTMLGen,S,TRANS>() {
131 public void code(S state, TRANS trans, Cache<HTMLGen> cache, HTMLGen xgen) throws APIException, IOException {
132 rows.data.postfix(state, trans, cache, xgen);
138 protected String title(TRANS trans) {
142 public static class Cells {
143 public static final Cells EMPTY = new Cells();
145 cells = new AbsCell[0][0];
146 msg = "No Data Found";
149 public Cells(ArrayList<AbsCell[]> arrayCells, String msg) {
150 cells = new AbsCell[arrayCells.size()][];
151 arrayCells.toArray(cells);
154 public AbsCell[][] cells;
159 public interface Data<S extends State<Env>, TRANS extends Trans> {
160 // Note: Trans is not first to avoid Method Name Collision
161 public void prefix(S state, TRANS trans, final Cache<HTMLGen> cache, final HTMLGen hgen);
162 public Cells get(TRANS trans,S state);
163 public void postfix(S state, TRANS trans, final Cache<HTMLGen> cache, final HTMLGen hgen);
164 public String[] headers();
167 private class Rows extends DynamicCode<HTMLGen,S,TRANS> {
168 private Data<S,TRANS> data;
171 public Rows(Data<S,TRANS> data, int alt) {
177 public void code(final S state, final TRANS trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
178 Mark tr = new Mark();
179 Mark td = new Mark();
182 Cells cells = data.get(trans,state);
183 if (cells.cells.length>0) {
184 for (AbsCell[] row : cells.cells) {
186 hgen.text("</table>")
198 hgen.incr(tr,TR,"class=alt");
200 for (AbsCell cell :row) {
201 hgen.leaf(td, TD,cell.attrs());
208 // Pass Msg back to Table code, in order to place after Table Complete
209 if (cells.msg!=null) {
210 trans.put(ROW_MSG_SLOT,cells.msg);
213 trans.put(EMPTY_TABLE_SLOT,cells.msg);
218 // public Table<S,TRANS> setPrefix(DynamicCode<HTMLGen, AuthGUI, AuthzTrans> dynamicCode) {
219 // prefix = dynamicCode;
223 // public Table<S,TRANS> setPostfix(DynamicCode<HTMLGen, AuthGUI, AuthzTrans> dynamicCode) {
224 // postfix = dynamicCode;