summaryrefslogtreecommitdiffstats
path: root/auth/auth-gui/src/main/java/org/onap/aaf/auth/gui/pages/PermHistory.java
blob: 4c3bd32e46f8ac3e114a083969c6deb738bdd368 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
 * ============LICENSE_START====================================================
 * org.onap.aaf
 * ===========================================================================
 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
 * ===========================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END====================================================
 *
 */

package org.onap.aaf.auth.gui.pages;


import java.io.IOException;
import java.net.ConnectException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.List;

import org.onap.aaf.auth.env.AuthzEnv;
import org.onap.aaf.auth.env.AuthzTrans;
import org.onap.aaf.auth.gui.AAF_GUI;
import org.onap.aaf.auth.gui.BreadCrumbs;
import org.onap.aaf.auth.gui.NamedCode;
import org.onap.aaf.auth.gui.Page;
import org.onap.aaf.auth.gui.Table;
import org.onap.aaf.auth.gui.Table.Cells;
import org.onap.aaf.auth.gui.table.AbsCell;
import org.onap.aaf.auth.gui.table.TableData;
import org.onap.aaf.auth.gui.table.TextCell;
import org.onap.aaf.cadi.CadiException;
import org.onap.aaf.cadi.client.Future;
import org.onap.aaf.cadi.client.Rcli;
import org.onap.aaf.cadi.client.Retryable;
import org.onap.aaf.misc.env.APIException;
import org.onap.aaf.misc.env.Env;
import org.onap.aaf.misc.env.Slot;
import org.onap.aaf.misc.env.TimeTaken;
import org.onap.aaf.misc.xgen.Cache;
import org.onap.aaf.misc.xgen.DynamicCode;
import org.onap.aaf.misc.xgen.html.HTMLGen;

import aaf.v2_0.History;
import aaf.v2_0.History.Item;


public class PermHistory extends Page {
    static final String NAME="PermHistory";
    static final String HREF = "/gui/permHistory";
    static final String FIELDS[] = {"type","instance","action","dates"};
    
    public PermHistory(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
        super(gui.env,NAME,HREF, FIELDS,
            new BreadCrumbs(breadcrumbs),
            new Table<AAF_GUI,AuthzTrans>("History", gui.env.newTransNoAvg(),new Model(gui.env),"class=std"),
            new NamedCode(true, "content") {
                @Override
                public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
                    final Slot sType = gui.env.slot(NAME+".type");
                    final Slot sInstance = gui.env.slot(NAME+".instance");
                    final Slot sAction = gui.env.slot(NAME+".action");
                    cache.dynamic(hgen, new DynamicCode<HTMLGen, AAF_GUI, AuthzTrans>() {
                        @Override
                        public void code(final AAF_GUI gui, final AuthzTrans trans,    final Cache<HTMLGen> cache, final HTMLGen hgen)    throws APIException, IOException {
                            String type = trans.get(sType, null);
                            String instance = trans.get(sInstance,null);
                            String action = trans.get(sAction,null);
                            
                            // Use Javascript to make the table title more descriptive
                            hgen.js()
                            .text("var caption = document.querySelector(\".title\");")
                            .text("caption.innerHTML='History for Permission [ " + type + " ]';")                        
                            .done();
                            
                            // Use Javascript to change Link Target to our last visited Detail page
                            String lastPage = PermDetail.HREF + "?type=" + type
                                    + "&instance=" + instance
                                    + "&action=" + action;
                            hgen.js()
                                .text("alterLink('permdetail', '"+lastPage + "');")                            
                                .done();
                            
                            hgen.br();
                            hgen.leaf("a", "href=#advanced_search", "onclick=divVisibility('advanced_search');","class=greenbutton").text("Advanced Search").end()
                                .divID("advanced_search", "style=display:none");
                            hgen.incr("table");
                                
                            addDateRow(hgen,"Start Date");
                            addDateRow(hgen,"End Date");
                            hgen.incr("tr").incr("td");
                            hgen.tagOnly("input", "type=button","value=Get History",
                                    "onclick=datesURL('"+HREF+"?type=" + type
                                    + "&instance=" + instance
                                    + "&action=" + action+"');","class=greenbutton");
                            hgen.end().end();
                            hgen.end();
                            hgen.end();
                        }
                    });
                }
            }

            );
        
    }
    
    private static void addDateRow(HTMLGen hgen, String s) {
        hgen
            .incr("tr")
            .incr("td")
            .incr("label", "for=month", "required").text(s+"*").end()
            .end()
            .incr("td")
            .incr("select", "name=month"+s.substring(0, s.indexOf(' ')), "id=month"+s.substring(0, s.indexOf(' ')), "required")
            .incr("option", "value=").text("Month").end();
        for (NsHistory.Month m : NsHistory.Month.values()) {
            if (Calendar.getInstance().get(Calendar.MONTH) == m.ordinal()) {
                hgen.incr("option", "selected", "value="+(m.ordinal()+1)).text(m.name()).end();
            } else {
                hgen.incr("option", "value="+(m.ordinal()+1)).text(m.name()).end();
            }
        }
        hgen.end()
            .end()
            .incr("td")
            .tagOnly("input","type=number","id=year"+s.substring(0, s.indexOf(' ')),"required",
                    "value="+Calendar.getInstance().get(Calendar.YEAR), "min=1900", 
                    "max="+Calendar.getInstance().get(Calendar.YEAR),
                    "placeholder=Year").end()
            .end();
    }
    
    /**
     * Implement the Table Content for History
     * 
     * @author Jeremiah
     *
     */
    private static class Model extends TableData<AAF_GUI,AuthzTrans> {
        private static final String[] headers = new String[] {"Date","User","Memo"};
        private Slot sType;
        private Slot sDates;
        
        public Model(AuthzEnv env) {
            sType = env.slot(NAME+".type");
            sDates = env.slot(NAME+".dates");
        }
        
        @Override
        public String[] headers() {
            return headers;
        }
        
        @Override
        public Cells get(final AuthzTrans trans, final AAF_GUI gui) {
            final String oName = trans.get(sType,null);
            final String oDates = trans.get(sDates,null);
            
            if (oName==null) {
                return Cells.EMPTY;
            }
            
            final ArrayList<AbsCell[]> rv = new ArrayList<>();
            String msg = null;
            try {
                gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
                    @Override
                    public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
                        TimeTaken tt = trans.start("AAF Get History for Permission ["+oName+"]",Env.REMOTE);
                        try {
                            if (oDates != null) {
                                client.setQueryParams("yyyymm="+oDates);
                            }
                            Future<History> fh = client.read(
                                "/authz/hist/perm/"+oName,
                                gui.getDF(History.class)
                                );
                            
                            
                            if (fh.get(AAF_GUI.TIMEOUT)) {
                                tt.done();
                                tt = trans.start("Load History Data", Env.SUB);
                                List<Item> histItems = fh.value.getItem();
                                
                                java.util.Collections.sort(histItems, new Comparator<Item>() {
                                    @Override
                                    public int compare(Item o1, Item o2) {
                                        return o2.getTimestamp().compare(o1.getTimestamp());
                                    }
                                });
                                
                                for (Item i : histItems) {
                                    String user = i.getUser();
                                    AbsCell userCell = new TextCell(user);
                                    String memo = i.getMemo().replace("<script>", "&lt;script&gt;").replace("</script>", "&lt;/script&gt;");
                                    rv.add(new AbsCell[] {
                                            new TextCell(i.getTimestamp().toGregorianCalendar().getTime().toString()),
                                            userCell,
                                            new TextCell(memo)
                                    });
                                }
                                
                            } else {
                                if (fh.code()==403) {
                                    rv.add(new AbsCell[] {new TextCell("You may not view History of Permission [" + oName + "]", "colspan = 3", "class=center")});
                                } else {
                                    rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***", "colspan = 3", "class=center")});
                                }
                            }
                        } finally {
                            tt.done();
                        }

                        return null;
                    }
                });
                
            } catch (Exception e) {
                trans.error().log(e);
            }
        return new Cells(rv,msg);
        }
    }

}