1 /*******************************************************************************
2 * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3 *******************************************************************************/
4 package com.att.authz.gui;
6 import static com.att.xgen.html.HTMLGen.TABLE;
7 import static com.att.xgen.html.HTMLGen.TD;
8 import static com.att.xgen.html.HTMLGen.TR;
10 import java.io.IOException;
11 import java.util.ArrayList;
13 import com.att.authz.gui.table.AbsCell;
14 import org.onap.aaf.inno.env.APIException;
15 import org.onap.aaf.inno.env.Env;
16 import org.onap.aaf.inno.env.Slot;
17 import org.onap.aaf.inno.env.Trans;
18 import org.onap.aaf.inno.env.TransStore;
19 import com.att.xgen.Cache;
20 import com.att.xgen.DynamicCode;
21 import com.att.xgen.Mark;
22 import com.att.xgen.html.HTMLGen;
23 import com.att.xgen.html.State;
25 public class Table<S extends State<Env>, TRANS extends TransStore> extends NamedCode {
26 private final Slot ROW_MSG_SLOT, EMPTY_TABLE_SLOT;
27 private final String title;
28 private final String[] columns;
29 private final Rows rows;
31 public Table(String title, TRANS trans, Data<S,TRANS> data, String ... attrs) {
33 ROW_MSG_SLOT=trans.slot("TABLE_ROW_MSG");
34 EMPTY_TABLE_SLOT=trans.slot("TABLE_EMPTY");
35 this.columns = data.headers();
37 for(String s : attrs) {
38 if("class=std".equals(s) || "class=stdform".equals(s)) {
42 rows = new Rows(data,alt?1:0);
45 // Derive an ID from title (from no spaces, etc), and prepend to IDAttributes (Protected from NamedCode)
46 idattrs = new String[attrs.length+1];
47 idattrs[0] = title.replaceAll("\\s","");
48 System.arraycopy(attrs, 0, idattrs, 1, attrs.length);
52 public void code(Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
53 Mark table = new Mark();
55 hgen.incr(table,TABLE)
56 .leaf("caption", "class=title").text(title).end()
58 for(String column : columns) {
59 hgen.leaf("th").text(column).end();
63 // Load Rows Dynamically
64 cache.dynamic(hgen, rows);
68 // Print Message from Row Gathering, if available
69 cache.dynamic(hgen, new DynamicCode<HTMLGen,S,TRANS>() {
71 public void code(S state, TRANS trans, Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
73 if((msg = trans.get(EMPTY_TABLE_SLOT, null))!=null) {
74 hgen.incr("style").text("#inner tr,caption,input,p.preamble {display: none;}#inner p.notfound {margin: 0px 0px 0px 20px}").end();
75 hgen.incr(HTMLGen.P,"class=notfound").text(msg).end().br();
76 } else if((msg=trans.get(ROW_MSG_SLOT,null))!=null) {
83 public static class Cells {
84 public static final Cells EMPTY = new Cells();
86 cells = new AbsCell[0][0];
87 msg = "No Data Found";
90 public Cells(ArrayList<AbsCell[]> arrayCells, String msg) {
91 cells = new AbsCell[arrayCells.size()][];
92 arrayCells.toArray(cells);
95 public AbsCell[][] cells;
99 public interface Data<S extends State<Env>, TRANS extends Trans> {
100 public Cells get(S state,TRANS trans);
101 public String[] headers();
104 private class Rows extends DynamicCode<HTMLGen,S,TRANS> {
105 private Data<S,TRANS> data;
108 public Rows(Data<S,TRANS> data, int alt) {
114 public void code(S state, TRANS trans, Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
115 Mark tr = new Mark();
116 Mark td = new Mark();
119 Cells cells = data.get(state, trans);
120 if(cells.cells.length>0) {
121 for(AbsCell[] row : cells.cells) {
130 hgen.incr(tr,TR,"class=alt");
132 for(AbsCell cell :row) {
133 hgen.leaf(td, TD,cell.attrs());
139 // Pass Msg back to Table code, in order to place after Table Complete
140 if(cells.msg!=null) {
141 trans.put(ROW_MSG_SLOT,cells.msg);
145 trans.put(EMPTY_TABLE_SLOT,cells.msg);