summaryrefslogtreecommitdiffstats
path: root/authz-gui/src/main/java/com/att/authz/gui/Table.java
blob: f15d4bebe24858cf847c2945b77bedc6ede23609 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*******************************************************************************
 * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
 *******************************************************************************/
package com.att.authz.gui;

import static com.att.xgen.html.HTMLGen.TABLE;
import static com.att.xgen.html.HTMLGen.TD;
import static com.att.xgen.html.HTMLGen.TR;

import java.io.IOException;
import java.util.ArrayList;

import com.att.authz.gui.table.AbsCell;
import org.onap.aaf.inno.env.APIException;
import org.onap.aaf.inno.env.Env;
import org.onap.aaf.inno.env.Slot;
import org.onap.aaf.inno.env.Trans;
import org.onap.aaf.inno.env.TransStore;
import com.att.xgen.Cache;
import com.att.xgen.DynamicCode;
import com.att.xgen.Mark;
import com.att.xgen.html.HTMLGen;
import com.att.xgen.html.State;

public class Table<S extends State<Env>, TRANS extends TransStore> extends NamedCode {
	private final Slot ROW_MSG_SLOT, EMPTY_TABLE_SLOT;
	private final String title;
	private final String[] columns;
	private final Rows rows;
	
	public Table(String title, TRANS trans, Data<S,TRANS> data, String ... attrs)  {
		super(true,attrs);
		ROW_MSG_SLOT=trans.slot("TABLE_ROW_MSG");
		EMPTY_TABLE_SLOT=trans.slot("TABLE_EMPTY");
		this.columns = data.headers();
		boolean alt = false;
		for(String s : attrs) {
			if("class=std".equals(s) || "class=stdform".equals(s)) {
				alt=true;
			}
		}
		rows = new Rows(data,alt?1:0);
		this.title = title;
		
		// Derive an ID from title (from no spaces, etc), and prepend to IDAttributes (Protected from NamedCode)
		idattrs = new String[attrs.length+1];
		idattrs[0] = title.replaceAll("\\s","");
		System.arraycopy(attrs, 0, idattrs, 1, attrs.length);
	}

	@Override
	public void code(Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
		Mark table = new Mark();
		Mark tr = new Mark();
		hgen.incr(table,TABLE)
				.leaf("caption", "class=title").text(title).end()
				.incr(tr,TR);
					for(String column : columns) {
						hgen.leaf("th").text(column).end();
					}
				hgen.end(tr);
				
		// Load Rows Dynamically
		cache.dynamic(hgen, rows);
		// End Table
		hgen.end(table); 
			
		// Print Message from Row Gathering, if available
		cache.dynamic(hgen, new DynamicCode<HTMLGen,S,TRANS>() {
			@Override
			public void code(S state, TRANS trans, Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
				String msg;
				if((msg = trans.get(EMPTY_TABLE_SLOT, null))!=null) {
					hgen.incr("style").text("#inner tr,caption,input,p.preamble {display: none;}#inner p.notfound {margin: 0px 0px 0px 20px}").end();
					hgen.incr(HTMLGen.P,"class=notfound").text(msg).end().br();
				} else if((msg=trans.get(ROW_MSG_SLOT,null))!=null) { 
					hgen.p(msg).br();
				}
			}
		});
	}

	public static class Cells {
		public static final Cells EMPTY = new Cells();
		private Cells() {
			cells = new AbsCell[0][0];
			msg = "No Data Found";
		}
		
		public Cells(ArrayList<AbsCell[]> arrayCells, String msg) {
			cells = new AbsCell[arrayCells.size()][];
			arrayCells.toArray(cells);
			this.msg = msg;
		}
		public AbsCell[][] cells;
		public String msg;
	}
	
	public interface Data<S extends State<Env>, TRANS extends Trans> {
		public Cells get(S state,TRANS trans);
		public String[] headers();
	}

	private class Rows extends DynamicCode<HTMLGen,S,TRANS> {
		private Data<S,TRANS> data;
		private int alt;
		
		public Rows(Data<S,TRANS> data, int alt) {
			this.data = data;
			this.alt = alt;
		}
		
		@Override
		public void code(S state, TRANS trans, Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
			Mark tr = new Mark();
			Mark td = new Mark();
			
			int alt = this.alt;
			Cells cells = data.get(state, trans);
			if(cells.cells.length>0) {
				for(AbsCell[] row : cells.cells) {
					switch(alt) {
						case 1:
							alt=2;
						case 0:
							hgen.incr(tr,TR);
							break;
						default:
							alt=1;
							hgen.incr(tr,TR,"class=alt");
					}
					for(AbsCell cell :row) {
						hgen.leaf(td, TD,cell.attrs());
						cell.write(hgen);
						hgen.end(td);
					}
					hgen.end(tr);
				}
				// Pass Msg back to Table code, in order to place after Table Complete
				if(cells.msg!=null) {
					trans.put(ROW_MSG_SLOT,cells.msg);
				}

			} else {
				trans.put(EMPTY_TABLE_SLOT,cells.msg);
			}
		}
	}
}