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;
50 // private DynamicCode<HTMLGen, AuthGUI, AuthzTrans> prefix,postfix;
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 // prefix=postfix=null;
60 for (String a : attrs) {
63 ROW_MSG_SLOT=trans.slot("TABLE_ROW_MSG");
64 EMPTY_TABLE_SLOT=trans.slot("TABLE_EMPTY");
65 this.columns = data.headers();
67 for (String s : attrs) {
68 if ("class=std".equals(s) || "class=stdform".equals(s)) {
72 rows = new Rows(data,alt?1:0);
74 // Derive an ID from title (from no spaces, etc), and prepend to IDAttributes (Protected from NamedCode)
75 addAttr(true,title(trans).replaceAll("\\s",""));
81 public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
82 cache.dynamic(hgen, new DynamicCode<HTMLGen,S,TRANS>() {
84 public void code(S state, TRANS trans, Cache<HTMLGen> cache, HTMLGen xgen) throws APIException, IOException {
85 rows.data.prefix(state, trans, cache, xgen);
88 Mark table = new Mark();
91 hgen.incr(table,TABLE);
93 cache.dynamic(hgen, new DynamicCode<HTMLGen,S,TRANS>() {
95 public void code(S state, TRANS trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
96 hgen.leaf("caption", "class=title").text(title(trans)).end();
100 hgen.leaf("caption", "class=title").text(title).end();
103 for (String column : columns) {
104 hgen.leaf("th").text(column).end();
108 // Load Rows Dynamically
109 cache.dynamic(hgen, rows);
114 other.code(cache,hgen);
117 // Print Message from Row Gathering, if available
118 cache.dynamic(hgen, new DynamicCode<HTMLGen,S,TRANS>() {
120 public void code(S state, TRANS trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
122 if ((msg = trans.get(EMPTY_TABLE_SLOT, null))!=null) {
123 hgen.incr("style").text("#inner tr,caption,input,p.preamble {display: none;}#inner p.notfound {margin: 0px 0px 0px 20px}").end();
124 hgen.incr(HTMLGen.P,"class=notfound").text(msg).end().br();
125 } else if ((msg=trans.get(ROW_MSG_SLOT,null))!=null) {
130 cache.dynamic(hgen, new DynamicCode<HTMLGen,S,TRANS>() {
132 public void code(S state, TRANS trans, Cache<HTMLGen> cache, HTMLGen xgen) throws APIException, IOException {
133 rows.data.postfix(state, trans, cache, xgen);
139 protected String title(TRANS trans) {
143 public static class Cells {
144 public static final Cells EMPTY = new Cells();
146 cells = new AbsCell[0][0];
147 msg = "No Data Found";
150 public Cells(ArrayList<AbsCell[]> arrayCells, String msg) {
151 cells = new AbsCell[arrayCells.size()][];
152 arrayCells.toArray(cells);
155 public AbsCell[][] cells;
160 public interface Data<S extends State<Env>, TRANS extends Trans> {
161 // Note: Trans is not first to avoid Method Name Collision
162 public void prefix(S state, TRANS trans, final Cache<HTMLGen> cache, final HTMLGen hgen);
163 public Cells get(TRANS trans,S state);
164 public void postfix(S state, TRANS trans, final Cache<HTMLGen> cache, final HTMLGen hgen);
165 public String[] headers();
168 private class Rows extends DynamicCode<HTMLGen,S,TRANS> {
169 private Data<S,TRANS> data;
172 public Rows(Data<S,TRANS> data, int alt) {
178 public void code(final S state, final TRANS trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
179 Mark tr = new Mark();
180 Mark td = new Mark();
183 Cells cells = data.get(trans,state);
184 if (cells.cells.length>0) {
185 for (AbsCell[] row : cells.cells) {
187 hgen.text("</table>")
199 hgen.incr(tr,TR,"class=alt");
201 for (AbsCell cell :row) {
202 hgen.leaf(td, TD,cell.attrs());
209 // Pass Msg back to Table code, in order to place after Table Complete
210 if (cells.msg!=null) {
211 trans.put(ROW_MSG_SLOT,cells.msg);
214 trans.put(EMPTY_TABLE_SLOT,cells.msg);
219 // public Table<S,TRANS> setPrefix(DynamicCode<HTMLGen, AuthGUI, AuthzTrans> dynamicCode) {
220 // prefix = dynamicCode;
224 // public Table<S,TRANS> setPostfix(DynamicCode<HTMLGen, AuthGUI, AuthzTrans> dynamicCode) {
225 // postfix = dynamicCode;