f15d4bebe24858cf847c2945b77bedc6ede23609
[aaf/authz.git] / authz-gui / src / main / java / com / att / authz / gui / Table.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz.gui;
5
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;
9
10 import java.io.IOException;
11 import java.util.ArrayList;
12
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;
24
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;
30         
31         public Table(String title, TRANS trans, Data<S,TRANS> data, String ... attrs)  {
32                 super(true,attrs);
33                 ROW_MSG_SLOT=trans.slot("TABLE_ROW_MSG");
34                 EMPTY_TABLE_SLOT=trans.slot("TABLE_EMPTY");
35                 this.columns = data.headers();
36                 boolean alt = false;
37                 for(String s : attrs) {
38                         if("class=std".equals(s) || "class=stdform".equals(s)) {
39                                 alt=true;
40                         }
41                 }
42                 rows = new Rows(data,alt?1:0);
43                 this.title = title;
44                 
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);
49         }
50
51         @Override
52         public void code(Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
53                 Mark table = new Mark();
54                 Mark tr = new Mark();
55                 hgen.incr(table,TABLE)
56                                 .leaf("caption", "class=title").text(title).end()
57                                 .incr(tr,TR);
58                                         for(String column : columns) {
59                                                 hgen.leaf("th").text(column).end();
60                                         }
61                                 hgen.end(tr);
62                                 
63                 // Load Rows Dynamically
64                 cache.dynamic(hgen, rows);
65                 // End Table
66                 hgen.end(table); 
67                         
68                 // Print Message from Row Gathering, if available
69                 cache.dynamic(hgen, new DynamicCode<HTMLGen,S,TRANS>() {
70                         @Override
71                         public void code(S state, TRANS trans, Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
72                                 String msg;
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) { 
77                                         hgen.p(msg).br();
78                                 }
79                         }
80                 });
81         }
82
83         public static class Cells {
84                 public static final Cells EMPTY = new Cells();
85                 private Cells() {
86                         cells = new AbsCell[0][0];
87                         msg = "No Data Found";
88                 }
89                 
90                 public Cells(ArrayList<AbsCell[]> arrayCells, String msg) {
91                         cells = new AbsCell[arrayCells.size()][];
92                         arrayCells.toArray(cells);
93                         this.msg = msg;
94                 }
95                 public AbsCell[][] cells;
96                 public String msg;
97         }
98         
99         public interface Data<S extends State<Env>, TRANS extends Trans> {
100                 public Cells get(S state,TRANS trans);
101                 public String[] headers();
102         }
103
104         private class Rows extends DynamicCode<HTMLGen,S,TRANS> {
105                 private Data<S,TRANS> data;
106                 private int alt;
107                 
108                 public Rows(Data<S,TRANS> data, int alt) {
109                         this.data = data;
110                         this.alt = alt;
111                 }
112                 
113                 @Override
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();
117                         
118                         int alt = this.alt;
119                         Cells cells = data.get(state, trans);
120                         if(cells.cells.length>0) {
121                                 for(AbsCell[] row : cells.cells) {
122                                         switch(alt) {
123                                                 case 1:
124                                                         alt=2;
125                                                 case 0:
126                                                         hgen.incr(tr,TR);
127                                                         break;
128                                                 default:
129                                                         alt=1;
130                                                         hgen.incr(tr,TR,"class=alt");
131                                         }
132                                         for(AbsCell cell :row) {
133                                                 hgen.leaf(td, TD,cell.attrs());
134                                                 cell.write(hgen);
135                                                 hgen.end(td);
136                                         }
137                                         hgen.end(tr);
138                                 }
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);
142                                 }
143
144                         } else {
145                                 trans.put(EMPTY_TABLE_SLOT,cells.msg);
146                         }
147                 }
148         }
149 }