bbaf419f4a696efe1789775665365146b3ef6989
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / PermHistory.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 PermHistory extends Page {
60     static final String NAME="PermHistory";
61     static final String HREF = "/gui/permHistory";
62     static final String FIELDS[] = {"type","instance","action","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 PermHistory(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 sType = gui.env.slot(NAME+".type");
75                     final Slot sInstance = gui.env.slot(NAME+".instance");
76                     final Slot sAction = gui.env.slot(NAME+".action");
77                     cache.dynamic(hgen, new DynamicCode<HTMLGen, AAF_GUI, AuthzTrans>() {
78                         @Override
79                         public void code(final AAF_GUI gui, final AuthzTrans trans,    final Cache<HTMLGen> cache, final HTMLGen hgen)    throws APIException, IOException {
80                             String type = trans.get(sType, null);
81                             String instance = trans.get(sInstance,null);
82                             String action = trans.get(sAction,null);
83                             
84                             // Use Javascript to make the table title more descriptive
85                             hgen.js()
86                             .text("var caption = document.querySelector(\".title\");")
87                             .text("caption.innerHTML='History for Permission [ " + type + " ]';")                        
88                             .done();
89                             
90                             // Use Javascript to change Link Target to our last visited Detail page
91                             String lastPage = PermDetail.HREF + "?type=" + type
92                                     + "&instance=" + instance
93                                     + "&action=" + action;
94                             hgen.js()
95                                 .text("alterLink('permdetail', '"+lastPage + "');")                            
96                                 .done();
97                             
98                             hgen.br();
99                             hgen.leaf("a", "href=#advanced_search", "onclick=divVisibility('advanced_search');").text("Advanced Search").end()
100                                 .divID("advanced_search", "style=display:none");
101                             hgen.incr("table");
102                                 
103                             addDateRow(hgen,"Start Date");
104                             addDateRow(hgen,"End Date");
105                             hgen.incr("tr").incr("td");
106                             hgen.tagOnly("input", "type=button","value=Get History",
107                                     "onclick=datesURL('"+HREF+"?type=" + type
108                                     + "&instance=" + instance
109                                     + "&action=" + action+"');");
110                             hgen.end().end();
111                             hgen.end();
112                             hgen.end();
113                         }
114                     });
115                 }
116             }
117
118             );
119         
120     }
121     
122     private static void addDateRow(HTMLGen hgen, String s) {
123         hgen
124             .incr("tr")
125             .incr("td")
126             .incr("label", "for=month", "required").text(s+"*").end()
127             .end()
128             .incr("td")
129             .incr("select", "name=month"+s.substring(0, s.indexOf(' ')), "id=month"+s.substring(0, s.indexOf(' ')), "required")
130             .incr("option", "value=").text("Month").end();
131         for (Month m : Month.values()) {
132             if (Calendar.getInstance().get(Calendar.MONTH) == m.ordinal()) {
133                 hgen.incr("option", "selected", "value="+(m.ordinal()+1)).text(m.name()).end();
134             } else {
135                 hgen.incr("option", "value="+(m.ordinal()+1)).text(m.name()).end();
136             }
137         }
138         hgen.end()
139             .end()
140             .incr("td")
141             .tagOnly("input","type=number","id=year"+s.substring(0, s.indexOf(' ')),"required",
142                     "value="+Calendar.getInstance().get(Calendar.YEAR), "min=1900", 
143                     "max="+Calendar.getInstance().get(Calendar.YEAR),
144                     "placeholder=Year").end()
145             .end();
146     }
147     
148     /**
149      * Implement the Table Content for History
150      * 
151      * @author Jeremiah
152      *
153      */
154     private static class Model extends TableData<AAF_GUI,AuthzTrans> {
155         private static final String[] headers = new String[] {"Date","User","Memo"};
156         private Slot sType;
157         private Slot sDates;
158         
159         public Model(AuthzEnv env) {
160             sType = env.slot(NAME+".type");
161             sDates = env.slot(NAME+".dates");
162         }
163         
164         @Override
165         public String[] headers() {
166             return headers;
167         }
168         
169         @Override
170         public Cells get(final AuthzTrans trans, final AAF_GUI gui) {
171             final String oName = trans.get(sType,null);
172             final String oDates = trans.get(sDates,null);
173             
174             if (oName==null) {
175                 return Cells.EMPTY;
176             }
177             
178             final ArrayList<AbsCell[]> rv = new ArrayList<>();
179             String msg = null;
180             try {
181                 gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
182                     @Override
183                     public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
184                         TimeTaken tt = trans.start("AAF Get History for Permission ["+oName+"]",Env.REMOTE);
185                         try {
186                             if (oDates != null) {
187                                 client.setQueryParams("yyyymm="+oDates);
188                             }
189                             Future<History> fh = client.read(
190                                 "/authz/hist/perm/"+oName,
191                                 gui.getDF(History.class)
192                                 );
193                             
194                             
195                             if (fh.get(AAF_GUI.TIMEOUT)) {
196                                 tt.done();
197                                 tt = trans.start("Load History Data", Env.SUB);
198                                 List<Item> histItems = fh.value.getItem();
199                                 
200                                 java.util.Collections.sort(histItems, new Comparator<Item>() {
201                                     @Override
202                                     public int compare(Item o1, Item o2) {
203                                         return o2.getTimestamp().compare(o1.getTimestamp());
204                                     }
205                                 });
206                                 
207                                 for (Item i : histItems) {
208                                     String user = i.getUser();
209                                     AbsCell userCell = new TextCell(user);
210                                     String memo = i.getMemo().replace("<script>", "&lt;script&gt;").replace("</script>", "&lt;/script&gt;");
211                                     rv.add(new AbsCell[] {
212                                             new TextCell(i.getTimestamp().toGregorianCalendar().getTime().toString()),
213                                             userCell,
214                                             new TextCell(memo)
215                                     });
216                                 }
217                                 
218                             } else {
219                                 if (fh.code()==403) {
220                                     rv.add(new AbsCell[] {new TextCell("You may not view History of Permission [" + oName + "]", "colspan = 3", "class=center")});
221                                 } else {
222                                     rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***", "colspan = 3", "class=center")});
223                                 }
224                             }
225                         } finally {
226                             tt.done();
227                         }
228
229                         return null;
230                     }
231                 });
232                 
233             } catch (Exception e) {
234                 trans.error().log(e);
235             }
236         return new Cells(rv,msg);
237         }
238     }
239
240 }