853113296c138eee30d892dcbdf2a446a83cf89f
[aaf/authz.git] / authz-gui / src / main / java / com / att / authz / gui / pages / RoleHistory.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz.gui.pages;
5
6
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;
13
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;
36
37 import aaf.v2_0.History;
38 import aaf.v2_0.History.Item;
39
40
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 };
48         
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") {
54                                 @Override
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>() {
58                                                 @Override
59                                                 public void code(AuthGUI gui, AuthzTrans trans, Cache<HTMLGen> cache, HTMLGen hgen)     throws APIException, IOException {
60                                                         String obRole = trans.get(role, null);
61                                                         
62                                                         // Use Javascript to make the table title more descriptive
63                                                         hgen.js()
64                                                         .text("var caption = document.querySelector(\".title\");")
65                                                         .text("caption.innerHTML='History for Role [ " + obRole + " ]';")                                               
66                                                         .done();
67                                                         
68                                                         // Use Javascript to change Link Target to our last visited Detail page
69                                                         String lastPage = RoleDetail.HREF + "?role=" + obRole;
70                                                         hgen.js()
71                                                                 .text("alterLink('roledetail', '"+lastPage + "');")                                                     
72                                                                 .done();
73                                                         
74                                                         hgen.br();
75                                                         hgen.leaf("a", "href=#advanced_search","onclick=divVisibility('advanced_search');").text("Advanced Search").end()
76                                                                 .divID("advanced_search", "style=display:none");
77                                                         hgen.incr("table");
78                                                                 
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+"');");
84                                                         hgen.end().end();
85                                                         hgen.end();
86                                                         hgen.end();
87                                                 }
88                                         });
89                                 }
90                         }
91
92                         );
93                 
94         }
95         
96         private static void addDateRow(HTMLGen hgen, String s) {
97                 hgen
98                         .incr("tr")
99                         .incr("td")
100                         .incr("label", "for=month", "required").text(s+"*").end()
101                         .end()
102                         .incr("td")
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();
108                         } else {
109                                 hgen.incr("option", "value="+(m.ordinal()+1)).text(m.name()).end();
110                         }
111                 }
112                 hgen.end()
113                         .end()
114                         .incr("td")
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()
119                         .end();
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 role;
132                 private Slot dates;
133                 
134                 public Model(AuthzEnv env) {
135                         role = env.slot(NAME+".role");
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(role,null);
147                         final String oDates = trans.get(dates,null);
148                         
149                         Cells rv = Cells.EMPTY;
150                         if(oName!=null) {
151                                 
152                                 try {
153                                         rv = gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Cells>() {
154                                                 @Override
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);
158                                                         String msg = null;
159                                                         try {
160                                                                 if (oDates != null) {
161                                                                         client.setQueryParams("yyyymm="+oDates);
162                                                                 }
163                                                                 Future<History> fh = client.read("/authz/hist/role/"+oName,gui.historyDF);
164                                                                 if (fh.get(AuthGUI.TIMEOUT)) {
165                                                                         tt.done();
166                                                                         tt = trans.start("Load History Data", Env.SUB);
167                                                                         List<Item> histItems = fh.value.getItem();
168                                                                         
169                                                                         java.util.Collections.sort(histItems, new Comparator<Item>() {
170                                                                                 @Override
171                                                                                 public int compare(Item o1, Item o2) {
172                                                                                         return o2.getTimestamp().compare(o1.getTimestamp());
173                                                                                 }
174                                                                         });
175                                                                         
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));
180                                                                                 
181                                                                                 rv.add(new AbsCell[] {
182                                                                                                 new TextCell(i.getTimestamp().toGregorianCalendar().getTime().toString()),
183                                                                                                 userCell,
184                                                                                                 new TextCell(i.getMemo())
185                                                                                 });
186                                                                         }
187                                                                 } else {
188                                                                         if (fh.code()==403) {
189                                                                                 rv.add(new AbsCell[] {new TextCell("You may not view History of Permission [" + oName + "]", "colspan = 3", "class=center")});
190                                                                         } else {
191                                                                                 rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***", "colspan = 3", "class=center")});
192                                                                         }
193                                                                 }
194                                                         } finally {
195                                                                 tt.done();
196                                                         }       
197                                                         return new Cells(rv,msg);
198                                                 }
199                                         });
200                                 } catch (Exception e) {
201                                         trans.error().log(e);
202                                 }
203                         }
204                         return rv;
205                 }
206         }
207
208 }