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