f360b0d534055ab3660f2f32435ca0253620819f
[aaf/authz.git] / authz-gui / src / main / java / com / att / authz / gui / pages / PermHistory.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 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 };
48         
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") {
54                                 @Override
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>() {
60                                                 @Override
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);
65                                                         
66                                                         // Use Javascript to make the table title more descriptive
67                                                         hgen.js()
68                                                         .text("var caption = document.querySelector(\".title\");")
69                                                         .text("caption.innerHTML='History for Permission [ " + type + " ]';")                                           
70                                                         .done();
71                                                         
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;
76                                                         hgen.js()
77                                                                 .text("alterLink('permdetail', '"+lastPage + "');")                                                     
78                                                                 .done();
79                                                         
80                                                         hgen.br();
81                                                         hgen.leaf("a", "href=#advanced_search", "onclick=divVisibility('advanced_search');").text("Advanced Search").end()
82                                                                 .divID("advanced_search", "style=display:none");
83                                                         hgen.incr("table");
84                                                                 
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+"');");
92                                                         hgen.end().end();
93                                                         hgen.end();
94                                                         hgen.end();
95                                                 }
96                                         });
97                                 }
98                         }
99
100                         );
101                 
102         }
103         
104         private static void addDateRow(HTMLGen hgen, String s) {
105                 hgen
106                         .incr("tr")
107                         .incr("td")
108                         .incr("label", "for=month", "required").text(s+"*").end()
109                         .end()
110                         .incr("td")
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();
116                         } else {
117                                 hgen.incr("option", "value="+(m.ordinal()+1)).text(m.name()).end();
118                         }
119                 }
120                 hgen.end()
121                         .end()
122                         .incr("td")
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()
127                         .end();
128         }
129         
130         /**
131          * Implement the Table Content for History
132          * 
133          *
134          */
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"};
138                 private Slot sType;
139                 private Slot sDates;
140                 
141                 public Model(AuthzEnv env) {
142                         sType = env.slot(NAME+".type");
143                         sDates = env.slot(NAME+".dates");
144                 }
145                 
146                 @Override
147                 public String[] headers() {
148                         return headers;
149                 }
150                 
151                 @Override
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);
155                         
156                         if(oName==null) {
157                                 return Cells.EMPTY;
158                         }
159                         
160                         ArrayList<AbsCell[]> rv = new ArrayList<AbsCell[]>();
161                         String msg = null;
162                         try {
163                                 gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
164                                         @Override
165                                         public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
166                                                 TimeTaken tt = trans.start("AAF Get History for Permission ["+oName+"]",Env.REMOTE);
167                                                 try {
168                                                         if (oDates != null) {
169                                                                 client.setQueryParams("yyyymm="+oDates);
170                                                         }
171                                                         Future<History> fh = client.read(
172                                                                 "/authz/hist/perm/"+oName,
173                                                                 gui.historyDF
174                                                                 );
175                                                         
176                                                         
177                                                         if (fh.get(AuthGUI.TIMEOUT)) {
178                                                                 tt.done();
179                                                                 tt = trans.start("Load History Data", Env.SUB);
180                                                                 List<Item> histItems = fh.value.getItem();
181                                                                 
182                                                                 java.util.Collections.sort(histItems, new Comparator<Item>() {
183                                                                         @Override
184                                                                         public int compare(Item o1, Item o2) {
185                                                                                 return o2.getTimestamp().compare(o1.getTimestamp());
186                                                                         }
187                                                                 });
188                                                                 
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));
193                                                                         
194                                                                         rv.add(new AbsCell[] {
195                                                                                         new TextCell(i.getTimestamp().toGregorianCalendar().getTime().toString()),
196                                                                                         userCell,
197                                                                                         new TextCell(i.getMemo())
198                                                                         });
199                                                                 }
200                                                                 
201                                                         } else {
202                                                                 if (fh.code()==403) {
203                                                                         rv.add(new AbsCell[] {new TextCell("You may not view History of Permission [" + oName + "]", "colspan = 3", "class=center")});
204                                                                 } else {
205                                                                         rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***", "colspan = 3", "class=center")});
206                                                                 }
207                                                         }
208                                                 } finally {
209                                                         tt.done();
210                                                 }
211
212                                                 return null;
213                                         }
214                                 });
215                                 
216                         } catch (Exception e) {
217                                 trans.error().log(e);
218                         }
219                 return new Cells(rv,msg);
220                 }
221         }
222
223 }