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