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