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