Mass whitespace changes (Style Warnings)
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / RoleHistory.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.gui.pages;
23
24
25 import java.io.IOException;
26 import java.net.ConnectException;
27 import java.util.ArrayList;
28 import java.util.Calendar;
29 import java.util.Comparator;
30 import java.util.List;
31
32 import org.onap.aaf.auth.env.AuthzEnv;
33 import org.onap.aaf.auth.env.AuthzTrans;
34 import org.onap.aaf.auth.gui.AAF_GUI;
35 import org.onap.aaf.auth.gui.BreadCrumbs;
36 import org.onap.aaf.auth.gui.NamedCode;
37 import org.onap.aaf.auth.gui.Page;
38 import org.onap.aaf.auth.gui.Table;
39 import org.onap.aaf.auth.gui.Table.Cells;
40 import org.onap.aaf.auth.gui.table.AbsCell;
41 import org.onap.aaf.auth.gui.table.TableData;
42 import org.onap.aaf.auth.gui.table.TextCell;
43 import org.onap.aaf.cadi.CadiException;
44 import org.onap.aaf.cadi.client.Future;
45 import org.onap.aaf.cadi.client.Rcli;
46 import org.onap.aaf.cadi.client.Retryable;
47 import org.onap.aaf.misc.env.APIException;
48 import org.onap.aaf.misc.env.Env;
49 import org.onap.aaf.misc.env.Slot;
50 import org.onap.aaf.misc.env.TimeTaken;
51 import org.onap.aaf.misc.xgen.Cache;
52 import org.onap.aaf.misc.xgen.DynamicCode;
53 import org.onap.aaf.misc.xgen.html.HTMLGen;
54
55 import aaf.v2_0.History;
56 import aaf.v2_0.History.Item;
57
58
59 public class RoleHistory extends Page {
60     static final String NAME="RoleHistory";
61     static final String HREF = "/gui/roleHistory";
62     static final String FIELDS[] = {"role","dates"};
63     static final String WEBPHONE = "http://webphone.att.com/cgi-bin/webphones.pl?id=";
64     static enum Month { JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, 
65         AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER };
66     
67     public RoleHistory(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
68         super(gui.env,NAME,HREF, FIELDS,
69             new BreadCrumbs(breadcrumbs),
70             new Table<AAF_GUI,AuthzTrans>("History", gui.env.newTransNoAvg(),new Model(gui.env),"class=std"),
71             new NamedCode(true, "content") {
72                 @Override
73                 public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
74                     final Slot role = gui.env.slot(NAME+".role");
75                     cache.dynamic(hgen, new DynamicCode<HTMLGen, AAF_GUI, AuthzTrans>() {
76                         @Override
77                         public void code(final AAF_GUI gui, final AuthzTrans trans,    final Cache<HTMLGen> cache, final HTMLGen hgen)    throws APIException, IOException {
78                             String obRole = trans.get(role, null);
79                             
80                             // Use Javascript to make the table title more descriptive
81                             hgen.js()
82                             .text("var caption = document.querySelector(\".title\");")
83                             .text("caption.innerHTML='History for Role [ " + obRole + " ]';")                        
84                             .done();
85                             
86                             // Use Javascript to change Link Target to our last visited Detail page
87                             String lastPage = RoleDetail.HREF + "?role=" + obRole;
88                             hgen.js()
89                                 .text("alterLink('roledetail', '"+lastPage + "');")                            
90                                 .done();
91                             
92                             hgen.br();
93                             hgen.leaf("a", "href=#advanced_search","onclick=divVisibility('advanced_search');").text("Advanced Search").end()
94                                 .divID("advanced_search", "style=display:none");
95                             hgen.incr("table");
96                                 
97                             addDateRow(hgen,"Start Date");
98                             addDateRow(hgen,"End Date");
99                             hgen.incr("tr").incr("td");
100                             hgen.tagOnly("input", "type=button","value=Get History",
101                                     "onclick=datesURL('"+HREF+"?role=" + obRole+"');");
102                             hgen.end().end();
103                             hgen.end();
104                             hgen.end();
105                         }
106                     });
107                 }
108             }
109
110             );
111         
112     }
113     
114     private static void addDateRow(HTMLGen hgen, String s) {
115         hgen
116             .incr("tr")
117             .incr("td")
118             .incr("label", "for=month", "required").text(s+"*").end()
119             .end()
120             .incr("td")
121             .incr("select", "name=month"+s.substring(0, s.indexOf(' ')), "id=month"+s.substring(0, s.indexOf(' ')), "required")
122             .incr("option", "value=").text("Month").end();
123         for (Month m : Month.values()) {
124             if (Calendar.getInstance().get(Calendar.MONTH) == m.ordinal()) {
125                 hgen.incr("option", "selected", "value="+(m.ordinal()+1)).text(m.name()).end();
126             } else {
127                 hgen.incr("option", "value="+(m.ordinal()+1)).text(m.name()).end();
128             }
129         }
130         hgen.end()
131             .end()
132             .incr("td")
133             .tagOnly("input","type=number","id=year"+s.substring(0, s.indexOf(' ')),"required",
134                     "value="+Calendar.getInstance().get(Calendar.YEAR), "min=1900", 
135                     "max="+Calendar.getInstance().get(Calendar.YEAR),
136                     "placeholder=Year").end()
137             .end();
138     }
139     
140     
141     /**
142      * Implement the Table Content for History
143      * 
144      * @author Jeremiah
145      *
146      */
147     private static class Model extends TableData<AAF_GUI,AuthzTrans> {
148         private static final String[] headers = new String[] {"Date","User","Memo"};
149         private Slot role;
150         private Slot dates;
151         
152         public Model(AuthzEnv env) {
153             role = env.slot(NAME+".role");
154             dates = env.slot(NAME+".dates");
155         }
156         
157         @Override
158         public String[] headers() {
159             return headers;
160         }
161         
162         @Override
163         public Cells get(final AuthzTrans trans, final AAF_GUI gui) {
164             final String oName = trans.get(role,null);
165             final String oDates = trans.get(dates,null);
166             
167             Cells rv = Cells.EMPTY;
168             if (oName!=null) {
169                 
170                 try {
171                     rv = gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Cells>() {
172                         @Override
173                         public Cells code(Rcli<?> client) throws CadiException, ConnectException, APIException {
174                             ArrayList<AbsCell[]> rv = new ArrayList<>();
175                             TimeTaken tt = trans.start("AAF Get History for Namespace ["+oName+"]",Env.REMOTE);
176                             String msg = null;
177                             try {
178                                 if (oDates != null) {
179                                     client.setQueryParams("yyyymm="+oDates);
180                                 }
181                                 Future<History> fh = client.read("/authz/hist/role/"+oName,gui.getDF(History.class));
182                                 if (fh.get(AAF_GUI.TIMEOUT)) {
183                                     tt.done();
184                                     tt = trans.start("Load History Data", Env.SUB);
185                                     List<Item> histItems = fh.value.getItem();
186                                     
187                                     java.util.Collections.sort(histItems, new Comparator<Item>() {
188                                         @Override
189                                         public int compare(Item o1, Item o2) {
190                                             return o2.getTimestamp().compare(o1.getTimestamp());
191                                         }
192                                     });
193                                     
194                                     for (Item i : histItems) {
195                                         String user = i.getUser();
196                                         AbsCell userCell = new TextCell(user);
197
198                                         String memo = i.getMemo().replace("<script>", "&lt;script&gt;").replace("</script>", "&lt;/script&gt;");
199                                         rv.add(new AbsCell[] {
200                                                 new TextCell(i.getTimestamp().toGregorianCalendar().getTime().toString()),
201                                                 userCell,
202                                                 new TextCell(memo)
203                                         });
204                                     }
205                                 } else {
206                                     if (fh.code()==403) {
207                                         rv.add(new AbsCell[] {new TextCell("You may not view History of Permission [" + oName + "]", "colspan = 3", "class=center")});
208                                     } else {
209                                         rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***", "colspan = 3", "class=center")});
210                                     }
211                                 }
212                             } finally {
213                                 tt.done();
214                             }    
215                             return new Cells(rv,msg);
216                         }
217                     });
218                 } catch (Exception e) {
219                     trans.error().log(e);
220                 }
221             }
222             return rv;
223         }
224     }
225
226 }