Sonar Fixes, Formatting
[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.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
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
64     public PermHistory(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 sType = gui.env.slot(NAME+".type");
72                     final Slot sInstance = gui.env.slot(NAME+".instance");
73                     final Slot sAction = gui.env.slot(NAME+".action");
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 type = trans.get(sType, null);
78                             String instance = trans.get(sInstance,null);
79                             String action = trans.get(sAction,null);
80
81                             // Use Javascript to make the table title more descriptive
82                             hgen.js()
83                             .text("var caption = document.querySelector(\".title\");")
84                             .text("caption.innerHTML='History for Permission [ " + type + " ]';")
85                             .done();
86
87                             // Use Javascript to change Link Target to our last visited Detail page
88                             String lastPage = PermDetail.HREF + "?type=" + type
89                                     + "&instance=" + instance
90                                     + "&action=" + action;
91                             hgen.js()
92                                 .text("alterLink('permdetail', '"+lastPage + "');")
93                                 .done();
94
95                             hgen.br();
96                             hgen.leaf("a", "href=#advanced_search", "onclick=divVisibility('advanced_search');","class=greenbutton").text("Advanced Search").end()
97                                 .divID("advanced_search", "style=display:none");
98                             hgen.incr("table");
99
100                             addDateRow(hgen,"Start Date");
101                             addDateRow(hgen,"End Date");
102                             hgen.incr("tr").incr("td");
103                             hgen.tagOnly("input", "type=button","value=Get History",
104                                     "onclick=datesURL('"+HREF+"?type=" + type
105                                     + "&instance=" + instance
106                                     + "&action=" + action+"');","class=greenbutton");
107                             hgen.end().end();
108                             hgen.end();
109                             hgen.end();
110                         }
111                     });
112                 }
113             }
114
115             );
116
117     }
118
119     private static void addDateRow(HTMLGen hgen, String s) {
120         hgen
121             .incr("tr")
122             .incr("td")
123             .incr("label", "for=month", "required").text(s+"*").end()
124             .end()
125             .incr("td")
126             .incr("select", "name=month"+s.substring(0, s.indexOf(' ')), "id=month"+s.substring(0, s.indexOf(' ')), "required")
127             .incr("option", "value=").text("Month").end();
128         for (NsHistory.Month m : NsHistory.Month.values()) {
129             if (Calendar.getInstance().get(Calendar.MONTH) == m.ordinal()) {
130                 hgen.incr("option", "selected", "value="+(m.ordinal()+1)).text(m.name()).end();
131             } else {
132                 hgen.incr("option", "value="+(m.ordinal()+1)).text(m.name()).end();
133             }
134         }
135         hgen.end()
136             .end()
137             .incr("td")
138             .tagOnly("input","type=number","id=year"+s.substring(0, s.indexOf(' ')),"required",
139                     "value="+Calendar.getInstance().get(Calendar.YEAR), "min=1900",
140                     "max="+Calendar.getInstance().get(Calendar.YEAR),
141                     "placeholder=Year").end()
142             .end();
143     }
144
145     /**
146      * Implement the Table Content for History
147      *
148      * @author Jeremiah
149      *
150      */
151     private static class Model extends TableData<AAF_GUI,AuthzTrans> {
152         private static final String[] headers = new String[] {"Date","User","Memo"};
153         private Slot sType;
154         private Slot sDates;
155
156         public Model(AuthzEnv env) {
157             sType = env.slot(NAME+".type");
158             sDates = env.slot(NAME+".dates");
159         }
160
161         @Override
162         public String[] headers() {
163             return headers;
164         }
165
166         @Override
167         public Cells get(final AuthzTrans trans, final AAF_GUI gui) {
168             final String oName = trans.get(sType,null);
169             final String oDates = trans.get(sDates,null);
170
171             if (oName==null) {
172                 return Cells.EMPTY;
173             }
174
175             final ArrayList<AbsCell[]> rv = new ArrayList<>();
176             String msg = null;
177             try {
178                 gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
179                     @Override
180                     public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
181                         TimeTaken tt = trans.start("AAF Get History for Permission ["+oName+"]",Env.REMOTE);
182                         try {
183                             if (oDates != null) {
184                                 client.setQueryParams("yyyymm="+oDates);
185                             }
186                             Future<History> fh = client.read(
187                                 "/authz/hist/perm/"+oName,
188                                 gui.getDF(History.class)
189                                 );
190
191
192                             if (fh.get(AAF_GUI.TIMEOUT)) {
193                                 tt.done();
194                                 tt = trans.start("Load History Data", Env.SUB);
195                                 List<Item> histItems = fh.value.getItem();
196
197                                 java.util.Collections.sort(histItems, new Comparator<Item>() {
198                                     @Override
199                                     public int compare(Item o1, Item o2) {
200                                         return o2.getTimestamp().compare(o1.getTimestamp());
201                                     }
202                                 });
203
204                                 for (Item i : histItems) {
205                                     String user = i.getUser();
206                                     AbsCell userCell = new TextCell(user);
207                                     String memo = i.getMemo().replace("<script>", "&lt;script&gt;").replace("</script>", "&lt;/script&gt;");
208                                     rv.add(new AbsCell[] {
209                                             new TextCell(i.getTimestamp().toGregorianCalendar().getTime().toString()),
210                                             userCell,
211                                             new TextCell(memo)
212                                     });
213                                 }
214
215                             } else {
216                                 if (fh.code()==403) {
217                                     rv.add(new AbsCell[] {new TextCell("You may not view History of Permission [" + oName + "]", "colspan = 3", "class=center")});
218                                 } else {
219                                     rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***", "colspan = 3", "class=center")});
220                                 }
221                             }
222                         } finally {
223                             tt.done();
224                         }
225
226                         return null;
227                     }
228                 });
229
230             } catch (Exception e) {
231                 trans.error().log(e);
232             }
233         return new Cells(rv,msg);
234         }
235     }
236
237 }