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