2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.gui.pages;
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;
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;
56 import aaf.v2_0.History;
57 import aaf.v2_0.History.Item;
60 public class RoleHistory extends Page {
61 static final String NAME="RoleHistory";
62 static final String HREF = "/gui/roleHistory";
63 static final String FIELDS[] = {"role","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 };
68 public RoleHistory(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") {
74 public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
75 final Slot role = gui.env.slot(NAME+".role");
76 cache.dynamic(hgen, new DynamicCode<HTMLGen, AAF_GUI, AuthzTrans>() {
78 public void code(final AAF_GUI gui, final AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
79 String obRole = trans.get(role, null);
81 // Use Javascript to make the table title more descriptive
83 .text("var caption = document.querySelector(\".title\");")
84 .text("caption.innerHTML='History for Role [ " + obRole + " ]';")
87 // Use Javascript to change Link Target to our last visited Detail page
88 String lastPage = RoleDetail.HREF + "?role=" + obRole;
90 .text("alterLink('roledetail', '"+lastPage + "');")
94 hgen.leaf("a", "href=#advanced_search","onclick=divVisibility('advanced_search');").text("Advanced Search").end()
95 .divID("advanced_search", "style=display:none");
98 addDateRow(hgen,"Start Date");
99 addDateRow(hgen,"End Date");
100 hgen.incr("tr").incr("td");
101 hgen.tagOnly("input", "type=button","value=Get History",
102 "onclick=datesURL('"+HREF+"?role=" + obRole+"');");
115 private static void addDateRow(HTMLGen hgen, String s) {
119 .incr("label", "for=month", "required").text(s+"*").end()
122 .incr("select", "name=month"+s.substring(0, s.indexOf(' ')), "id=month"+s.substring(0, s.indexOf(' ')), "required")
123 .incr("option", "value=").text("Month").end();
124 for (Month m : Month.values()) {
125 if (Calendar.getInstance().get(Calendar.MONTH) == m.ordinal()) {
126 hgen.incr("option", "selected", "value="+(m.ordinal()+1)).text(m.name()).end();
128 hgen.incr("option", "value="+(m.ordinal()+1)).text(m.name()).end();
134 .tagOnly("input","type=number","id=year"+s.substring(0, s.indexOf(' ')),"required",
135 "value="+Calendar.getInstance().get(Calendar.YEAR), "min=1900",
136 "max="+Calendar.getInstance().get(Calendar.YEAR),
137 "placeholder=Year").end()
143 * Implement the Table Content for History
148 private static class Model extends TableData<AAF_GUI,AuthzTrans> {
149 private static final String[] headers = new String[] {"Date","User","Memo"};
153 public Model(AuthzEnv env) {
154 role = env.slot(NAME+".role");
155 dates = env.slot(NAME+".dates");
159 public String[] headers() {
164 public Cells get(final AuthzTrans trans, final AAF_GUI gui) {
165 final String oName = trans.get(role,null);
166 final String oDates = trans.get(dates,null);
168 Cells rv = Cells.EMPTY;
172 rv = gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Cells>() {
174 public Cells code(Rcli<?> client) throws CadiException, ConnectException, APIException {
175 ArrayList<AbsCell[]> rv = new ArrayList<AbsCell[]>();
176 TimeTaken tt = trans.start("AAF Get History for Namespace ["+oName+"]",Env.REMOTE);
179 if (oDates != null) {
180 client.setQueryParams("yyyymm="+oDates);
182 Future<History> fh = client.read("/authz/hist/role/"+oName,gui.getDF(History.class));
183 if (fh.get(AAF_GUI.TIMEOUT)) {
185 tt = trans.start("Load History Data", Env.SUB);
186 List<Item> histItems = fh.value.getItem();
188 java.util.Collections.sort(histItems, new Comparator<Item>() {
190 public int compare(Item o1, Item o2) {
191 return o2.getTimestamp().compare(o1.getTimestamp());
195 for (Item i : histItems) {
196 String user = i.getUser();
197 AbsCell userCell = new TextCell(user);
199 rv.add(new AbsCell[] {
200 new TextCell(i.getTimestamp().toGregorianCalendar().getTime().toString()),
202 new TextCell(i.getMemo())
206 if (fh.code()==403) {
207 rv.add(new AbsCell[] {new TextCell("You may not view History of Permission [" + oName + "]", "colspan = 3", "class=center")});
209 rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***", "colspan = 3", "class=center")});
215 return new Cells(rv,msg);
218 } catch (Exception e) {
219 trans.error().log(e);