removed block of commenetd lines of code
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / Table.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
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  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.gui;
23
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;
27
28 import java.io.IOException;
29 import java.util.ArrayList;
30
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;
43
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
51
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);
54         this.other = other;
55     }
56
57     public Table(String title, TRANS trans, Data<S,TRANS> data, String name, String ... attrs)  {
58         super(true,name);
59         for (String a : attrs) {
60             addAttr(false, a);
61         }
62         ROW_MSG_SLOT=trans.slot("TABLE_ROW_MSG");
63         EMPTY_TABLE_SLOT=trans.slot("TABLE_EMPTY");
64         this.columns = data.headers();
65         boolean alt = false;
66         for (String s : attrs) {
67             if ("class=std".equals(s) || "class=stdform".equals(s)) {
68                 alt=true;
69             }
70         }
71         rows = new Rows(data,alt?1:0);
72         this.title = title;
73         // Derive an ID from title (from no spaces, etc), and prepend to IDAttributes (Protected from NamedCode)
74         addAttr(true,title(trans).replaceAll("\\s",""));
75
76         other = null;
77     }
78
79     @Override
80     public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
81         cache.dynamic(hgen, new DynamicCode<HTMLGen,S,TRANS>() {
82             @Override
83             public void code(S state, TRANS trans, Cache<HTMLGen> cache, HTMLGen xgen) throws APIException, IOException {
84                 rows.data.prefix(state, trans, cache, xgen);
85             }
86         });
87         Mark table = new Mark();
88         Mark tr = new Mark();
89
90         hgen.incr(table,TABLE);
91         if (title==null) {
92             cache.dynamic(hgen, new DynamicCode<HTMLGen,S,TRANS>() {
93                 @Override
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();
96                 }
97             });
98         } else {
99             hgen.leaf("caption", "class=title").text(title).end();
100         }
101         hgen.incr(tr,TR);
102                 for (String column : columns) {
103                     hgen.leaf("th").text(column).end();
104                 }
105             hgen.end(tr);
106
107         // Load Rows Dynamically
108         cache.dynamic(hgen, rows);
109         // End Table
110         hgen.end(table);
111
112         if (other!=null) {
113             other.code(cache,hgen);
114         }
115
116         // Print Message from Row Gathering, if available
117         cache.dynamic(hgen, new DynamicCode<HTMLGen,S,TRANS>() {
118             @Override
119             public void code(S state, TRANS trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
120                 String msg;
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) {
125                     hgen.p(msg).br();
126                 }
127             }
128         });
129         cache.dynamic(hgen, new DynamicCode<HTMLGen,S,TRANS>() {
130             @Override
131             public void code(S state, TRANS trans, Cache<HTMLGen> cache, HTMLGen xgen) throws APIException, IOException {
132                 rows.data.postfix(state, trans, cache, xgen);
133             }
134         });
135
136     }
137
138     protected String title(TRANS trans) {
139         return title;
140     }
141
142     public static class Cells {
143         public static final Cells EMPTY = new Cells();
144         private Cells() {
145             cells = new AbsCell[0][0];
146             msg = "No Data Found";
147         }
148
149         public Cells(ArrayList<AbsCell[]> arrayCells, String msg) {
150             cells = new AbsCell[arrayCells.size()][];
151             arrayCells.toArray(cells);
152             this.msg = msg;
153         }
154         public AbsCell[][] cells;
155         public String msg;
156
157     }
158
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();
165     }
166
167     private class Rows extends DynamicCode<HTMLGen,S,TRANS> {
168         private Data<S,TRANS> data;
169         private int alt;
170
171         public Rows(Data<S,TRANS> data, int alt) {
172             this.data = data;
173             this.alt = alt;
174         }
175
176         @Override
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();
180
181             int alt = this.alt;
182             Cells cells = data.get(trans,state);
183             if (cells.cells.length>0) {
184                 for (AbsCell[] row : cells.cells) {
185                     if (row.length==0) {
186                         hgen.text("</table>")
187                             .hr()
188                             .text("<table>");
189                     } else {
190                         switch(alt) {
191                             case 1:
192                                 alt=2;
193                             case 0:
194                                 hgen.incr(tr,TR);
195                                 break;
196                             default:
197                                 alt=1;
198                                 hgen.incr(tr,TR,"class=alt");
199                         }
200                         for (AbsCell cell :row) {
201                             hgen.leaf(td, TD,cell.attrs());
202                             cell.write(hgen);
203                             hgen.end(td);
204                         }
205                         hgen.end(tr);
206                     }
207                 }
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);
211                 }
212             } else {
213                 trans.put(EMPTY_TABLE_SLOT,cells.msg);
214             }
215         }
216     }
217
218 //    public Table<S,TRANS> setPrefix(DynamicCode<HTMLGen, AuthGUI, AuthzTrans> dynamicCode) {
219 //        prefix = dynamicCode;
220 //        return this;
221 //    }
222 //
223 //    public Table<S,TRANS> setPostfix(DynamicCode<HTMLGen, AuthGUI, AuthzTrans> dynamicCode) {
224 //        postfix = dynamicCode;
225 //        return this;
226 //    }
227
228 }