1 /*******************************************************************************
2 * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3 *******************************************************************************/
4 package com.att.authz.gui.pages;
6 import java.io.IOException;
7 import java.net.ConnectException;
8 import java.util.ArrayList;
9 import java.util.Calendar;
10 import java.util.Comparator;
11 import java.util.List;
13 import com.att.authz.env.AuthzEnv;
14 import com.att.authz.env.AuthzTrans;
15 import com.att.authz.gui.AuthGUI;
16 import com.att.authz.gui.BreadCrumbs;
17 import com.att.authz.gui.NamedCode;
18 import com.att.authz.gui.Page;
19 import com.att.authz.gui.Table;
20 import com.att.authz.gui.Table.Cells;
21 import com.att.authz.gui.table.AbsCell;
22 import com.att.authz.gui.table.RefCell;
23 import com.att.authz.gui.table.TextCell;
24 import org.onap.aaf.cadi.CadiException;
25 import org.onap.aaf.cadi.client.Future;
26 import org.onap.aaf.cadi.client.Rcli;
27 import org.onap.aaf.cadi.client.Retryable;
28 import org.onap.aaf.inno.env.APIException;
29 import org.onap.aaf.inno.env.Env;
30 import org.onap.aaf.inno.env.Slot;
31 import org.onap.aaf.inno.env.TimeTaken;
32 import com.att.xgen.Cache;
33 import com.att.xgen.DynamicCode;
34 import com.att.xgen.html.HTMLGen;
36 import aaf.v2_0.History;
37 import aaf.v2_0.History.Item;
39 public class NsHistory extends Page {
40 static final String NAME="NsHistory";
41 static final String HREF = "/gui/nsHistory";
42 static final String FIELDS[] = {"name","dates"};
43 static final String WEBPHONE = "http://webphone.att.com/cgi-bin/webphones.pl?id=";
44 static enum Month { JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY,
45 AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER };
47 public NsHistory(final AuthGUI gui, final Page ... breadcrumbs) throws APIException, IOException {
48 super(gui.env,NAME,HREF, FIELDS,
49 new BreadCrumbs(breadcrumbs),
50 new Table<AuthGUI,AuthzTrans>("History", gui.env.newTransNoAvg(),new Model(gui.env()),"class=std"),
51 new NamedCode(true, "content") {
53 public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
54 final Slot name = gui.env.slot(NAME+".name");
55 cache.dynamic(hgen, new DynamicCode<HTMLGen, AuthGUI, AuthzTrans>() {
57 public void code(AuthGUI gui, AuthzTrans trans, Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
58 String obName = trans.get(name, null);
60 // Use Javascript to make the table title more descriptive
62 .text("var caption = document.querySelector(\".title\");")
63 .text("caption.innerHTML='History for Namespace [ " + obName + " ]';")
66 // Use Javascript to change Link Target to our last visited Detail page
67 String lastPage = NsDetail.HREF + "?name=" + obName;
69 .text("alterLink('nsdetail', '"+lastPage + "');")
73 hgen.leaf("a","href=#advanced_search","onclick=divVisibility('advanced_search');").text("Advanced Search").end()
74 .divID("advanced_search", "style=display:none");
77 addDateRow(hgen,"Start Date");
78 addDateRow(hgen,"End Date");
79 hgen.incr("tr").incr("td");
80 hgen.tagOnly("input", "type=button","value=Get History",
81 "onclick=datesURL('"+HREF+"?name=" + obName+"');");
94 private static void addDateRow(HTMLGen hgen, String s) {
98 .incr("label", "for=month", "required").text(s+"*").end()
101 .incr("select", "name=month"+s.substring(0, s.indexOf(' ')), "id=month"+s.substring(0, s.indexOf(' ')), "required")
102 .incr("option", "value=").text("Month").end();
103 for (Month m : Month.values()) {
104 if (Calendar.getInstance().get(Calendar.MONTH) == m.ordinal()) {
105 hgen.incr("option", "selected", "value="+(m.ordinal()+1)).text(m.name()).end();
107 hgen.incr("option", "value="+(m.ordinal()+1)).text(m.name()).end();
113 .tagOnly("input","type=number","id=year"+s.substring(0, s.indexOf(' ')),"required",
114 "value="+Calendar.getInstance().get(Calendar.YEAR), "min=1900",
115 "max="+Calendar.getInstance().get(Calendar.YEAR),
116 "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 name = env.slot(NAME+".name");
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(name,null);
147 final String oDates = trans.get(dates,null);
153 ArrayList<AbsCell[]> rv = new ArrayList<AbsCell[]>();
155 final TimeTaken tt = trans.start("AAF Get History for Namespace ["+oName+"]",Env.REMOTE);
157 gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
159 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
160 if (oDates != null) {
161 client.setQueryParams("yyyymm="+oDates);
163 Future<History> fh = client.read("/authz/hist/ns/"+oName,gui.historyDF);
164 if (fh.get(AuthGUI.TIMEOUT)) {
166 TimeTaken tt2 = trans.start("Load History Data", Env.SUB);
168 List<Item> histItems = fh.value.getItem();
170 java.util.Collections.sort(histItems, new Comparator<Item>() {
172 public int compare(Item o1, Item o2) {
173 return o2.getTimestamp().compare(o1.getTimestamp());
177 for (Item i : histItems) {
178 String user = i.getUser();
179 AbsCell userCell = (user.endsWith(CSP_ATT_COM)?
180 new RefCell(user,WEBPHONE + user.substring(0,user.indexOf('@'))):new TextCell(user));
182 rv.add(new AbsCell[] {
183 new TextCell(i.getTimestamp().toGregorianCalendar().getTime().toString()),
185 new TextCell(i.getMemo())
192 if (fh.code()==403) {
193 rv.add(new AbsCell[] {new TextCell("You may not view History of Namespace [" + oName + "]", "colspan = 3", "class=center")});
195 rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***", "colspan = 3", "class=center")});
201 } catch (Exception e) {
202 trans.error().log(e);
206 return new Cells(rv,msg);