Mass removal of all Tabs (Style Warnings)
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / ApprovalForm.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.Collections;
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.Form;
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.ButtonCell;
42 import org.onap.aaf.auth.gui.table.RadioCell;
43 import org.onap.aaf.auth.gui.table.RefCell;
44 import org.onap.aaf.auth.gui.table.TableData;
45 import org.onap.aaf.auth.gui.table.TextAndRefCell;
46 import org.onap.aaf.auth.gui.table.TextCell;
47 import org.onap.aaf.auth.org.Organization;
48 import org.onap.aaf.auth.org.OrganizationFactory;
49 import org.onap.aaf.auth.org.Organization.Identity;
50 import org.onap.aaf.cadi.CadiException;
51 import org.onap.aaf.cadi.client.Future;
52 import org.onap.aaf.cadi.client.Rcli;
53 import org.onap.aaf.cadi.client.Retryable;
54 import org.onap.aaf.misc.env.APIException;
55 import org.onap.aaf.misc.env.Env;
56 import org.onap.aaf.misc.env.Slot;
57 import org.onap.aaf.misc.env.TimeTaken;
58 import org.onap.aaf.misc.xgen.Cache;
59 import org.onap.aaf.misc.xgen.DynamicCode;
60 import org.onap.aaf.misc.xgen.Mark;
61 import org.onap.aaf.misc.xgen.html.HTMLGen;
62
63 import aaf.v2_0.Approval;
64 import aaf.v2_0.Approvals;
65
66 public class ApprovalForm extends Page {
67     // Package on purpose
68     static final String NAME="Approvals";
69     static final String HREF = "/gui/approve";
70     static final String[] FIELDS = new String[] {"line[]","user"};
71     
72     
73     public ApprovalForm(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
74         super(gui.env,NAME,HREF, FIELDS,
75
76             new BreadCrumbs(breadcrumbs),
77             new NamedCode(false, "filterByUser") {
78                 @Override
79                 public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
80                     cache.dynamic(hgen, new DynamicCode<HTMLGen, AAF_GUI, AuthzTrans>() {
81                         @Override
82                         public void code(final AAF_GUI gui, final AuthzTrans trans,    final Cache<HTMLGen> cache, final HTMLGen hgen)    throws APIException, IOException {
83                             String user = trans.get(trans.env().slot(NAME+".user"),"");
84                             hgen.incr("p", "class=userFilter")
85                                 .text("Filter by User:")
86                                 .tagOnly("input", "type=text", "value="+user, "id=userTextBox")
87                                 .tagOnly("input", "type=button", "onclick=userFilter('"+HREF+"');", "value=Go!")
88                                 .end();
89                                 }
90                     });
91                 }
92             },
93             new Form(true,new Table<AAF_GUI,AuthzTrans>("Approval Requests", gui.env.newTransNoAvg(),new Model(gui.env),"class=stdform"))
94                 .preamble("The following requires your Approval to proceed in the AAF System.</p><p class=subtext>Hover on Identity for Name; click for WebPhone; If Deny is the only option, User is no longer valid."),
95             new NamedCode(false, "selectAlljs") {
96                 @Override
97                 public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
98                     Mark jsStart = new Mark();
99                     hgen.js(jsStart);
100                     hgen.text("function selectAll(radioClass) {");
101                     hgen.text("var radios = document.querySelectorAll(\".\"+radioClass);");
102                     hgen.text("for (i = 0; i < radios.length; i++) {");
103                     hgen.text("radios[i].checked = true;");
104                     hgen.text("}");
105                     hgen.text("}");
106                     hgen.end(jsStart);
107                 }
108             });
109         
110     }
111     
112     /**
113      * Implement the Table Content for Approvals
114      * 
115      * @author Jonathan
116      *
117      */
118     private static class Model extends TableData<AAF_GUI,AuthzTrans> {
119         //TODO come up with a generic way to do ILM Info (people page)
120         private static final String TODO_ILM_INFO = "TODO: ILM Info";
121         private static final String DOMAIN_OF_USER = "@DOMAIN";
122         
123         private static final String[] headers = new String[] {"Identity","Request","Approve","Deny"};
124         private Slot sUser;
125         
126         public Model(AuthzEnv env) {
127             sUser = env.slot(NAME+".user");
128         }
129         
130         @Override
131         public String[] headers() {
132             return headers;
133         }
134         
135         @Override
136         public Cells get(final AuthzTrans trans, final AAF_GUI gui) {
137             final String userParam = trans.get(sUser, null);
138             ArrayList<AbsCell[]> rv = new ArrayList<>();
139             String msg = null;
140             TimeTaken tt = trans.start("AAF Get Approvals for Approver",Env.REMOTE);
141             try {
142                 final List<Approval> pendingApprovals = new ArrayList<>();
143                 final List<Integer> beginIndicesPerApprover = new ArrayList<>();
144                 int numLeft = gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Integer>() {
145                     @Override
146                     public Integer code(Rcli<?> client) throws CadiException, ConnectException, APIException {
147                         Future<Approvals> fa = client.read("/authz/approval/approver/"+trans.user(),gui.getDF(Approvals.class));
148                         int numLeft = 0;
149                         if(fa.get(AAF_GUI.TIMEOUT)) {
150                             
151                             if(fa.value!=null) {
152                                 for (Approval appr : fa.value.getApprovals()) {
153                                     if ("pending".equals(appr.getStatus())) {
154                                         if (userParam!=null && !appr.getUser().equalsIgnoreCase(userParam)) {
155                                                 numLeft++;
156                                                 continue;
157                                         }
158                                         pendingApprovals.add(appr);
159                                     }
160                                 }
161                             }
162                             
163                             String prevApprover = null;
164                             int overallIndex = 0;
165                                 
166                             for (Approval appr : pendingApprovals) {
167                                 String currApprover = appr.getApprover();
168                                 if (!currApprover.equals(prevApprover)) {
169                                     prevApprover = currApprover;
170                                     beginIndicesPerApprover.add(overallIndex);
171                                 }
172                                 overallIndex++;
173                             }
174                         }
175                         return numLeft;
176                     }
177                 });
178                 
179                 if (!pendingApprovals.isEmpty()) {
180                     // Only add select all links if we have approvals
181                     AbsCell[] selectAllRow = new AbsCell[] {
182                             AbsCell.Null,
183                             AbsCell.Null,
184                             new ButtonCell("all", "onclick=selectAll('approve')", "class=selectAllButton"),
185                             new ButtonCell("all", "onclick=selectAll('deny')", "class=selectAllButton")
186                         };
187                     rv.add(selectAllRow);
188                 }
189                         
190                 int line=-1;
191                 
192                 while (!beginIndicesPerApprover.isEmpty()) {
193                     int beginIndex = beginIndicesPerApprover.remove(0);
194                     int endIndex = (beginIndicesPerApprover.isEmpty()?pendingApprovals.size():beginIndicesPerApprover.get(0));
195                     List<Approval> currApproverList = pendingApprovals.subList(beginIndex, endIndex);
196                     
197                     String currApproverFull = currApproverList.get(0).getApprover();
198                     String currApproverShort = currApproverFull.substring(0,currApproverFull.indexOf('@'));
199                     String currApprover = (trans.user().indexOf('@')<0?currApproverShort:currApproverFull);
200                     if (!currApprover.equals(trans.user())) {
201                         AbsCell[] approverHeader;
202                         if (currApproverFull.substring(currApproverFull.indexOf('@')).equals(DOMAIN_OF_USER)) {
203                             approverHeader = new AbsCell[] { 
204                                     new TextAndRefCell("Approvals Delegated to Me by ", currApprover,
205                                             TODO_ILM_INFO + currApproverShort, 
206                                             true,
207                                             new String[] {"colspan=4", "class=head"})
208                             };
209                         } else {
210                             approverHeader = new AbsCell[] { 
211                                     new TextCell("Approvals Delegated to Me by " + currApprover,
212                                             new String[] {"colspan=4", "class=head"})
213                             };
214                         }
215                         rv.add(approverHeader);
216                     }
217                     
218                     // Sort by User Requesting
219                     Collections.sort(currApproverList, new Comparator<Approval>() {
220                         @Override
221                         public int compare(Approval a1, Approval a2) {
222                             return a1.getUser().compareTo(a2.getUser());
223                         }
224                     });
225                     
226                     String prevUser = null;
227                     boolean userOK=true;
228
229                     for (Approval appr : currApproverList) {
230                         if(++line<MAX_LINE) { // limit number displayed at one time.
231                             AbsCell userCell;
232                             String user = appr.getUser();
233                             if(user.equals(prevUser)) {
234                                 userCell = AbsCell.Null; 
235                             } else if (user.endsWith(DOMAIN_OF_USER)){
236                                 userOK=true;
237                                 String title;
238                                 Organization org = OrganizationFactory.obtain(trans.env(), user);
239                                 if(org==null) {
240                                     title="";
241                                 } else {
242                                     Identity au = org.getIdentity(trans, user);
243                                     if(au!=null) {
244                                         if("MECHID".equals(au.type())) {
245                                             Identity managedBy = au.responsibleTo();
246                                             if(managedBy==null) {
247                                                 title ="title=" + au.type();
248                                             } else {
249                                                 title="title=Sponsor is " + managedBy.fullName();                                                
250                                             }
251                                         } else {
252                                             title="title=" + au.fullName();
253                                         }
254                                     } else {
255                                         userOK=false;
256                                         title="title=Not a User at " + org.getName();
257                                     }
258                                 }
259                                 prevUser=user;
260                                 userCell = new RefCell(prevUser,
261                                     TODO_ILM_INFO+user.substring(0, user.length()-DOMAIN_OF_USER.length()),
262                                     true,
263                                     title);
264                             } else {
265                                 userCell = new TextCell(prevUser);
266                             }
267                             AbsCell[] sa = new AbsCell[] {
268                                 userCell,
269                                 new TextCell(appr.getMemo()),
270                                 userOK?new RadioCell("line."+ line,"approve", "approved|"+appr.getTicket()):new TextCell(""),
271                                 new RadioCell("line."+ line,"deny", "denied|"+appr.getTicket())
272                             };
273                             rv.add(sa);
274                         } else {
275                             ++numLeft;
276                         }
277                     }
278                 }
279                 if(numLeft>0) {
280                     msg = "After these, there will be " + numLeft + " approvals left to process";
281                 }
282                 if(rv.isEmpty()) {
283                     if (numLeft>0) {
284                         msg = "No Approvals to process at this time for user " + userParam +". You have " 
285                             + numLeft + " other approvals to process.";
286                     } else {
287                         msg = "No Approvals to process at this time";
288                     }
289                 }
290             } catch (Exception e) {
291                 trans.error().log(e);
292             } finally {
293                 tt.done();
294             }
295         return new Cells(rv,msg);
296         }
297     }
298 }