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.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;
55 import aaf.v2_0.History;
56 import aaf.v2_0.History.Item;
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 };
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") {
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>() {
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);
84 // Use Javascript to make the table title more descriptive
86 .text("var caption = document.querySelector(\".title\");")
87 .text("caption.innerHTML='History for Permission [ " + type + " ]';")
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;
95 .text("alterLink('permdetail', '"+lastPage + "');")
99 hgen.leaf("a", "href=#advanced_search", "onclick=divVisibility('advanced_search');").text("Advanced Search").end()
100 .divID("advanced_search", "style=display:none");
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+"');");
122 private static void addDateRow(HTMLGen hgen, String s) {
126 .incr("label", "for=month", "required").text(s+"*").end()
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();
135 hgen.incr("option", "value="+(m.ordinal()+1)).text(m.name()).end();
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()
149 * Implement the Table Content for History
154 private static class Model extends TableData<AAF_GUI,AuthzTrans> {
155 private static final String[] headers = new String[] {"Date","User","Memo"};
159 public Model(AuthzEnv env) {
160 sType = env.slot(NAME+".type");
161 sDates = env.slot(NAME+".dates");
165 public String[] headers() {
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);
178 final ArrayList<AbsCell[]> rv = new ArrayList<>();
181 gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
183 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
184 TimeTaken tt = trans.start("AAF Get History for Permission ["+oName+"]",Env.REMOTE);
186 if (oDates != null) {
187 client.setQueryParams("yyyymm="+oDates);
189 Future<History> fh = client.read(
190 "/authz/hist/perm/"+oName,
191 gui.getDF(History.class)
195 if (fh.get(AAF_GUI.TIMEOUT)) {
197 tt = trans.start("Load History Data", Env.SUB);
198 List<Item> histItems = fh.value.getItem();
200 java.util.Collections.sort(histItems, new Comparator<Item>() {
202 public int compare(Item o1, Item o2) {
203 return o2.getTimestamp().compare(o1.getTimestamp());
207 for (Item i : histItems) {
208 String user = i.getUser();
209 AbsCell userCell = new TextCell(user);
210 String memo = i.getMemo().replace("<script>", "<script>").replace("</script>", "</script>");
211 rv.add(new AbsCell[] {
212 new TextCell(i.getTimestamp().toGregorianCalendar().getTime().toString()),
219 if (fh.code()==403) {
220 rv.add(new AbsCell[] {new TextCell("You may not view History of Permission [" + oName + "]", "colspan = 3", "class=center")});
222 rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***", "colspan = 3", "class=center")});
233 } catch (Exception e) {
234 trans.error().log(e);
236 return new Cells(rv,msg);