1 /*******************************************************************************
2 * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3 *******************************************************************************/
4 package com.att.authz.gui.pages;
7 import java.io.IOException;
8 import java.net.ConnectException;
9 import java.util.ArrayList;
10 import java.util.Calendar;
11 import java.util.Comparator;
12 import java.util.List;
14 import com.att.authz.env.AuthzEnv;
15 import com.att.authz.env.AuthzTrans;
16 import com.att.authz.gui.AuthGUI;
17 import com.att.authz.gui.BreadCrumbs;
18 import com.att.authz.gui.NamedCode;
19 import com.att.authz.gui.Page;
20 import com.att.authz.gui.Table;
21 import com.att.authz.gui.Table.Cells;
22 import com.att.authz.gui.table.AbsCell;
23 import com.att.authz.gui.table.RefCell;
24 import com.att.authz.gui.table.TextCell;
25 import org.onap.aaf.cadi.CadiException;
26 import org.onap.aaf.cadi.client.Future;
27 import org.onap.aaf.cadi.client.Rcli;
28 import org.onap.aaf.cadi.client.Retryable;
29 import org.onap.aaf.inno.env.APIException;
30 import org.onap.aaf.inno.env.Env;
31 import org.onap.aaf.inno.env.Slot;
32 import org.onap.aaf.inno.env.TimeTaken;
33 import com.att.xgen.Cache;
34 import com.att.xgen.DynamicCode;
35 import com.att.xgen.html.HTMLGen;
37 import aaf.v2_0.History;
38 import aaf.v2_0.History.Item;
41 public class RoleHistory extends Page {
42 static final String NAME="RoleHistory";
43 static final String HREF = "/gui/roleHistory";
44 static final String FIELDS[] = {"role","dates"};
45 static final String WEBPHONE = "http://webphone.att.com/cgi-bin/webphones.pl?id=";
46 static enum Month { JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY,
47 AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER };
49 public RoleHistory(final AuthGUI gui, final Page ... breadcrumbs) throws APIException, IOException {
50 super(gui.env,NAME,HREF, FIELDS,
51 new BreadCrumbs(breadcrumbs),
52 new Table<AuthGUI,AuthzTrans>("History", gui.env.newTransNoAvg(),new Model(gui.env()),"class=std"),
53 new NamedCode(true, "content") {
55 public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
56 final Slot role = gui.env.slot(NAME+".role");
57 cache.dynamic(hgen, new DynamicCode<HTMLGen, AuthGUI, AuthzTrans>() {
59 public void code(AuthGUI gui, AuthzTrans trans, Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
60 String obRole = trans.get(role, null);
62 // Use Javascript to make the table title more descriptive
64 .text("var caption = document.querySelector(\".title\");")
65 .text("caption.innerHTML='History for Role [ " + obRole + " ]';")
68 // Use Javascript to change Link Target to our last visited Detail page
69 String lastPage = RoleDetail.HREF + "?role=" + obRole;
71 .text("alterLink('roledetail', '"+lastPage + "');")
75 hgen.leaf("a", "href=#advanced_search","onclick=divVisibility('advanced_search');").text("Advanced Search").end()
76 .divID("advanced_search", "style=display:none");
79 addDateRow(hgen,"Start Date");
80 addDateRow(hgen,"End Date");
81 hgen.incr("tr").incr("td");
82 hgen.tagOnly("input", "type=button","value=Get History",
83 "onclick=datesURL('"+HREF+"?role=" + obRole+"');");
96 private static void addDateRow(HTMLGen hgen, String s) {
100 .incr("label", "for=month", "required").text(s+"*").end()
103 .incr("select", "name=month"+s.substring(0, s.indexOf(' ')), "id=month"+s.substring(0, s.indexOf(' ')), "required")
104 .incr("option", "value=").text("Month").end();
105 for (Month m : Month.values()) {
106 if (Calendar.getInstance().get(Calendar.MONTH) == m.ordinal()) {
107 hgen.incr("option", "selected", "value="+(m.ordinal()+1)).text(m.name()).end();
109 hgen.incr("option", "value="+(m.ordinal()+1)).text(m.name()).end();
115 .tagOnly("input","type=number","id=year"+s.substring(0, s.indexOf(' ')),"required",
116 "value="+Calendar.getInstance().get(Calendar.YEAR), "min=1900",
117 "max="+Calendar.getInstance().get(Calendar.YEAR),
118 "placeholder=Year").end()
124 * Implement the Table Content for History
128 private static class Model implements Table.Data<AuthGUI,AuthzTrans> {
129 private static final String CSP_ATT_COM = "@csp.att.com";
130 private static final String[] headers = new String[] {"Date","User","Memo"};
134 public Model(AuthzEnv env) {
135 role = env.slot(NAME+".role");
136 dates = env.slot(NAME+".dates");
140 public String[] headers() {
145 public Cells get(final AuthGUI gui, final AuthzTrans trans) {
146 final String oName = trans.get(role,null);
147 final String oDates = trans.get(dates,null);
149 Cells rv = Cells.EMPTY;
153 rv = gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Cells>() {
155 public Cells code(Rcli<?> client) throws CadiException, ConnectException, APIException {
156 ArrayList<AbsCell[]> rv = new ArrayList<AbsCell[]>();
157 TimeTaken tt = trans.start("AAF Get History for Namespace ["+oName+"]",Env.REMOTE);
160 if (oDates != null) {
161 client.setQueryParams("yyyymm="+oDates);
163 Future<History> fh = client.read("/authz/hist/role/"+oName,gui.historyDF);
164 if (fh.get(AuthGUI.TIMEOUT)) {
166 tt = trans.start("Load History Data", Env.SUB);
167 List<Item> histItems = fh.value.getItem();
169 java.util.Collections.sort(histItems, new Comparator<Item>() {
171 public int compare(Item o1, Item o2) {
172 return o2.getTimestamp().compare(o1.getTimestamp());
176 for (Item i : histItems) {
177 String user = i.getUser();
178 AbsCell userCell = (user.endsWith(CSP_ATT_COM)?
179 new RefCell(user,WEBPHONE + user.substring(0,user.indexOf('@'))):new TextCell(user));
181 rv.add(new AbsCell[] {
182 new TextCell(i.getTimestamp().toGregorianCalendar().getTime().toString()),
184 new TextCell(i.getMemo())
188 if (fh.code()==403) {
189 rv.add(new AbsCell[] {new TextCell("You may not view History of Permission [" + oName + "]", "colspan = 3", "class=center")});
191 rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***", "colspan = 3", "class=center")});
197 return new Cells(rv,msg);
200 } catch (Exception e) {
201 trans.error().log(e);