653b9e7ae18fd6891f093efe55621942fb3f23ed
[aaf/authz.git] / authz-gui / src / main / java / com / att / authz / gui / pages / NsHistory.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz.gui.pages;
5
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;
12
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;
35
36 import aaf.v2_0.History;
37 import aaf.v2_0.History.Item;
38
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 };
46         
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") {
52                                 @Override
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>() {
56                                                 @Override
57                                                 public void code(AuthGUI gui, AuthzTrans trans, Cache<HTMLGen> cache, HTMLGen hgen)     throws APIException, IOException {
58                                                         String obName = trans.get(name, null);
59                                                         
60                                                         // Use Javascript to make the table title more descriptive
61                                                         hgen.js()
62                                                         .text("var caption = document.querySelector(\".title\");")
63                                                         .text("caption.innerHTML='History for Namespace [ " + obName + " ]';")                                          
64                                                         .done();
65                                                         
66                                                         // Use Javascript to change Link Target to our last visited Detail page
67                                                         String lastPage = NsDetail.HREF + "?name=" + obName;
68                                                         hgen.js()
69                                                                 .text("alterLink('nsdetail', '"+lastPage + "');")                                                       
70                                                                 .done();
71                                                         
72                                                         hgen.br();
73                                                         hgen.leaf("a","href=#advanced_search","onclick=divVisibility('advanced_search');").text("Advanced Search").end()
74                                                                 .divID("advanced_search", "style=display:none");
75                                                         hgen.incr("table");
76                                                                 
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+"');");
82                                                         hgen.end().end();
83                                                         hgen.end();
84                                                         hgen.end();
85                                                                 
86                                                 }
87                                         });
88                                 }
89                         }
90
91                         );
92         }
93
94         private static void addDateRow(HTMLGen hgen, String s) {
95                 hgen
96                         .incr("tr")
97                         .incr("td")
98                         .incr("label", "for=month", "required").text(s+"*").end()
99                         .end()
100                         .incr("td")
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();
106                         } else {
107                                 hgen.incr("option", "value="+(m.ordinal()+1)).text(m.name()).end();
108                         }
109                 }
110                 hgen.end()
111                         .end()
112                         .incr("td")
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()
117                         .end();
118         }
119                 
120
121         
122         
123         /**
124          * Implement the Table Content for History
125          * 
126          *
127          */
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"};
131                 private Slot name;
132                 private Slot dates;
133                 
134                 public Model(AuthzEnv env) {
135                         name = env.slot(NAME+".name");
136                         dates = env.slot(NAME+".dates");
137                 }
138                 
139                 @Override
140                 public String[] headers() {
141                         return headers;
142                 }
143                 
144                 @Override
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);
148                         
149                         if(oName==null) {
150                                 return Cells.EMPTY;
151                         }
152                         
153                         ArrayList<AbsCell[]> rv = new ArrayList<AbsCell[]>();
154                         String msg = null;
155                         final TimeTaken tt = trans.start("AAF Get History for Namespace ["+oName+"]",Env.REMOTE);
156                         try {
157                                 gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
158                                         @Override
159                                         public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
160                                                 if (oDates != null) {
161                                                         client.setQueryParams("yyyymm="+oDates);
162                                                 }
163                                                 Future<History> fh = client.read("/authz/hist/ns/"+oName,gui.historyDF);
164                                                 if (fh.get(AuthGUI.TIMEOUT)) {
165                                                         tt.done();
166                                                         TimeTaken tt2 = trans.start("Load History Data", Env.SUB);
167                                                         try {
168                                                                 List<Item> histItems = fh.value.getItem();
169                                                                 
170                                                                 java.util.Collections.sort(histItems, new Comparator<Item>() {
171                                                                         @Override
172                                                                         public int compare(Item o1, Item o2) {
173                                                                                 return o2.getTimestamp().compare(o1.getTimestamp());
174                                                                         }
175                                                                 });
176                                                                 
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));
181                                                                         
182                                                                         rv.add(new AbsCell[] {
183                                                                                         new TextCell(i.getTimestamp().toGregorianCalendar().getTime().toString()),
184                                                                                         userCell,
185                                                                                         new TextCell(i.getMemo())
186                                                                         });
187                                                                 }
188                                                         } finally {
189                                                                 tt2.done();
190                                                         }
191                                                 } else {
192                                                         if (fh.code()==403) {
193                                                                 rv.add(new AbsCell[] {new TextCell("You may not view History of Namespace [" + oName + "]", "colspan = 3", "class=center")});
194                                                         } else {
195                                                                 rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***", "colspan = 3", "class=center")});
196                                                         }
197                                                 }
198                                                 return null;
199                                         }
200                                 });
201                         } catch (Exception e) {
202                                 trans.error().log(e);
203                         } finally {
204                                 tt.done();
205                         }
206                 return new Cells(rv,msg);
207                 }
208         }
209
210 }