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 PermHistory extends Page {
42 static final String NAME="PermHistory";
43 static final String HREF = "/gui/permHistory";
44 static final String FIELDS[] = {"type","instance","action","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 PermHistory(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 sType = gui.env.slot(NAME+".type");
57 final Slot sInstance = gui.env.slot(NAME+".instance");
58 final Slot sAction = gui.env.slot(NAME+".action");
59 cache.dynamic(hgen, new DynamicCode<HTMLGen, AuthGUI, AuthzTrans>() {
61 public void code(AuthGUI gui, AuthzTrans trans, Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
62 String type = trans.get(sType, null);
63 String instance = trans.get(sInstance,null);
64 String action = trans.get(sAction,null);
66 // Use Javascript to make the table title more descriptive
68 .text("var caption = document.querySelector(\".title\");")
69 .text("caption.innerHTML='History for Permission [ " + type + " ]';")
72 // Use Javascript to change Link Target to our last visited Detail page
73 String lastPage = PermDetail.HREF + "?type=" + type
74 + "&instance=" + instance
75 + "&action=" + action;
77 .text("alterLink('permdetail', '"+lastPage + "');")
81 hgen.leaf("a", "href=#advanced_search", "onclick=divVisibility('advanced_search');").text("Advanced Search").end()
82 .divID("advanced_search", "style=display:none");
85 addDateRow(hgen,"Start Date");
86 addDateRow(hgen,"End Date");
87 hgen.incr("tr").incr("td");
88 hgen.tagOnly("input", "type=button","value=Get History",
89 "onclick=datesURL('"+HREF+"?type=" + type
90 + "&instance=" + instance
91 + "&action=" + action+"');");
104 private static void addDateRow(HTMLGen hgen, String s) {
108 .incr("label", "for=month", "required").text(s+"*").end()
111 .incr("select", "name=month"+s.substring(0, s.indexOf(' ')), "id=month"+s.substring(0, s.indexOf(' ')), "required")
112 .incr("option", "value=").text("Month").end();
113 for (Month m : Month.values()) {
114 if (Calendar.getInstance().get(Calendar.MONTH) == m.ordinal()) {
115 hgen.incr("option", "selected", "value="+(m.ordinal()+1)).text(m.name()).end();
117 hgen.incr("option", "value="+(m.ordinal()+1)).text(m.name()).end();
123 .tagOnly("input","type=number","id=year"+s.substring(0, s.indexOf(' ')),"required",
124 "value="+Calendar.getInstance().get(Calendar.YEAR), "min=1900",
125 "max="+Calendar.getInstance().get(Calendar.YEAR),
126 "placeholder=Year").end()
131 * Implement the Table Content for History
135 private static class Model implements Table.Data<AuthGUI,AuthzTrans> {
136 private static final String CSP_ATT_COM = "@csp.att.com";
137 private static final String[] headers = new String[] {"Date","User","Memo"};
141 public Model(AuthzEnv env) {
142 sType = env.slot(NAME+".type");
143 sDates = env.slot(NAME+".dates");
147 public String[] headers() {
152 public Cells get(final AuthGUI gui, final AuthzTrans trans) {
153 final String oName = trans.get(sType,null);
154 final String oDates = trans.get(sDates,null);
160 ArrayList<AbsCell[]> rv = new ArrayList<AbsCell[]>();
163 gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
165 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
166 TimeTaken tt = trans.start("AAF Get History for Permission ["+oName+"]",Env.REMOTE);
168 if (oDates != null) {
169 client.setQueryParams("yyyymm="+oDates);
171 Future<History> fh = client.read(
172 "/authz/hist/perm/"+oName,
177 if (fh.get(AuthGUI.TIMEOUT)) {
179 tt = trans.start("Load History Data", Env.SUB);
180 List<Item> histItems = fh.value.getItem();
182 java.util.Collections.sort(histItems, new Comparator<Item>() {
184 public int compare(Item o1, Item o2) {
185 return o2.getTimestamp().compare(o1.getTimestamp());
189 for (Item i : histItems) {
190 String user = i.getUser();
191 AbsCell userCell = (user.endsWith(CSP_ATT_COM)?
192 new RefCell(user,WEBPHONE + user.substring(0,user.indexOf('@'))):new TextCell(user));
194 rv.add(new AbsCell[] {
195 new TextCell(i.getTimestamp().toGregorianCalendar().getTime().toString()),
197 new TextCell(i.getMemo())
202 if (fh.code()==403) {
203 rv.add(new AbsCell[] {new TextCell("You may not view History of Permission [" + oName + "]", "colspan = 3", "class=center")});
205 rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***", "colspan = 3", "class=center")});
216 } catch (Exception e) {
217 trans.error().log(e);
219 return new Cells(rv,msg);