e6cf0410d4a6b69abcdf14e03dd750a847305398
[aaf/authz.git] / authz-gui / src / main / java / com / att / authz / gui / pages / ApprovalForm.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz.gui.pages;
5
6 import java.io.IOException;
7 import java.net.ConnectException;
8 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.Comparator;
11 import java.util.List;
12 import com.att.authz.env.AuthzEnv;
13 import com.att.authz.env.AuthzTrans;
14 import com.att.authz.gui.AuthGUI;
15 import com.att.authz.gui.BreadCrumbs;
16 import com.att.authz.gui.Form;
17 import com.att.authz.gui.NamedCode;
18 import com.att.authz.gui.Page;
19 import com.att.authz.gui.Table;
20 import com.att.authz.gui.Table.Cells;
21 import com.att.authz.gui.table.AbsCell;
22 import com.att.authz.gui.table.ButtonCell;
23 import com.att.authz.gui.table.RadioCell;
24 import com.att.authz.gui.table.RefCell;
25 import com.att.authz.gui.table.TextAndRefCell;
26 import com.att.authz.gui.table.TextCell;
27 import com.att.authz.org.Organization;
28 import com.att.authz.org.Organization.Identity;
29 import com.att.authz.org.OrganizationFactory;
30 import org.onap.aaf.cadi.CadiException;
31 import org.onap.aaf.cadi.client.Future;
32 import org.onap.aaf.cadi.client.Rcli;
33 import org.onap.aaf.cadi.client.Retryable;
34 import org.onap.aaf.inno.env.APIException;
35 import org.onap.aaf.inno.env.Env;
36 import org.onap.aaf.inno.env.Slot;
37 import org.onap.aaf.inno.env.TimeTaken;
38 import com.att.xgen.Cache;
39 import com.att.xgen.DynamicCode;
40 import com.att.xgen.Mark;
41 import com.att.xgen.html.HTMLGen;
42
43 import aaf.v2_0.Approval;
44
45 public class ApprovalForm extends Page {
46         // Package on purpose
47         static final String NAME="Approvals";
48         static final String HREF = "/gui/approve";
49         static final String WEBPHONE = "http://webphone.att.com/cgi-bin/webphones.pl?id=";
50         static final String[] FIELDS = new String[] {"line[]","user"};
51         
52         
53         public ApprovalForm(final AuthGUI gui, final Page ... breadcrumbs) throws APIException, IOException {
54                 super(gui.env,NAME,HREF, FIELDS,
55
56                         new BreadCrumbs(breadcrumbs),
57                         new NamedCode(false, "filterByUser") {
58                                 @Override
59                                 public void code(Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
60                                         cache.dynamic(hgen, new DynamicCode<HTMLGen, AuthGUI, AuthzTrans>() {
61                                                 @Override
62                                                 public void code(AuthGUI gui, AuthzTrans trans, Cache<HTMLGen> cache, HTMLGen hgen)     throws APIException, IOException {
63                                                         String user = trans.get(trans.env().slot(NAME+".user"),"");
64                                                         hgen.incr("p", "class=userFilter")
65                                                                 .text("Filter by User:")
66                                                                 .tagOnly("input", "type=text", "value="+user, "id=userTextBox")
67                                                                 .tagOnly("input", "type=button", "onclick=userFilter('"+HREF+"');", "value=Go!")
68                                                                 .end();
69                                                                 }
70                                         });
71                                 }
72                         },
73                         new Form(true,new Table<AuthGUI,AuthzTrans>("Approval Requests", gui.env.newTransNoAvg(),new Model(gui.env()),"class=stdform"))
74                                 .preamble("The following requires your Approval to proceed in the AAF System.</p><p class=subtext>Hover on Identity for Name; click for WebPhone"),
75                         new NamedCode(false, "selectAlljs") {
76                                 @Override
77                                 public void code(Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
78                                         Mark jsStart = new Mark();
79                                         hgen.js(jsStart);
80                                         hgen.text("function selectAll(radioClass) {");
81                                         hgen.text("var radios = document.querySelectorAll(\".\"+radioClass);");
82                                         hgen.text("for (i = 0; i < radios.length; i++) {");
83                                         hgen.text("radios[i].checked = true;");
84                                         hgen.text("}");
85                                         hgen.text("}");
86                                         hgen.end(jsStart);
87                                 }
88                         });
89                 
90         }
91         
92         /**
93          * Implement the Table Content for Approvals
94          * 
95          *
96          */
97         private static class Model implements Table.Data<AuthGUI,AuthzTrans> {
98                 private static final String[] headers = new String[] {"Identity","Request","Approve","Deny"};
99                 private static final Object THE_DOMAIN = null;
100                 private Slot sUser;
101                 
102                 public Model(AuthzEnv env) {
103                         sUser = env.slot(NAME+".user");
104                 }
105                 
106                 @Override
107                 public String[] headers() {
108                         return headers;
109                 }
110                 
111                 @Override
112                 public Cells get(final AuthGUI gui, final AuthzTrans trans) {
113                         final String userParam = trans.get(sUser, null);
114                         ArrayList<AbsCell[]> rv = new ArrayList<AbsCell[]>();
115                         String msg = null;
116                         TimeTaken tt = trans.start("AAF Get Approvals for Approver",Env.REMOTE);
117                         try {
118                                 final List<Approval> pendingApprovals = new ArrayList<Approval>();
119                                 final List<Integer> beginIndicesPerApprover = new ArrayList<Integer>();
120                                 int numLeft = gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Integer>() {
121                                         @Override
122                                         public Integer code(Rcli<?> client) throws CadiException, ConnectException, APIException {
123                                                 Future<aaf.v2_0.Approvals> fa = client.read("/authz/approval/approver/"+trans.user(),gui.approvalsDF);
124                                                 int numLeft = 0;
125                                                 if(fa.get(AuthGUI.TIMEOUT)) {
126                                                         
127                                                         if(fa.value!=null) {
128                                                                 for (Approval appr : fa.value.getApprovals()) {
129                                                                         if (appr.getStatus().equals("pending")) {
130                                                                                 if (userParam!=null) {
131                                                                                         if (!appr.getUser().equalsIgnoreCase(userParam)) {
132                                                                                                 numLeft++;
133                                                                                                 continue;
134                                                                                         }
135                                                                                 }
136                                                                                 pendingApprovals.add(appr);
137                                                                         }
138                                                                 }
139                                                         }
140                                                         
141                                                         String prevApprover = null;
142                                                         int overallIndex = 0;
143                                                                 
144                                                         for (Approval appr : pendingApprovals) {
145                                                                 String currApprover = appr.getApprover();
146                                                                 if (!currApprover.equals(prevApprover)) {
147                                                                         prevApprover = currApprover;
148                                                                         beginIndicesPerApprover.add(overallIndex);
149                                                                 }
150                                                                 overallIndex++;
151                                                         }
152                                                 }
153                                                 return numLeft;
154                                         }
155                                 });
156                                 
157                                 if (pendingApprovals.size() > 0) {
158                                         // Only add select all links if we have approvals
159                                         AbsCell[] selectAllRow = new AbsCell[] {
160                                                         AbsCell.Null,
161                                                         AbsCell.Null,
162                                                         new ButtonCell("all", "onclick=selectAll('approve')", "class=selectAllButton"),
163                                                         new ButtonCell("all", "onclick=selectAll('deny')", "class=selectAllButton")
164                                                 };
165                                         rv.add(selectAllRow);
166                                 }
167                                                 
168                                 int line=-1;
169                                 
170                                 while (beginIndicesPerApprover.size() > 0) {
171                                         int beginIndex = beginIndicesPerApprover.remove(0);
172                                         int endIndex = (beginIndicesPerApprover.isEmpty()?pendingApprovals.size():beginIndicesPerApprover.get(0));
173                                         List<Approval> currApproverList = pendingApprovals.subList(beginIndex, endIndex);
174                                         
175                                         String currApproverFull = currApproverList.get(0).getApprover();
176                                         String currApproverShort = currApproverFull.substring(0,currApproverFull.indexOf('@'));
177                                         String currApprover = (trans.user().indexOf('@')<0?currApproverShort:currApproverFull);
178                                         if (!currApprover.equals(trans.user())) {
179                                                 AbsCell[] approverHeader;
180                                                 if (currApproverFull.substring(currApproverFull.indexOf('@')).equals(THE_DOMAIN)) {
181                                                         approverHeader = new AbsCell[] { 
182                                                                         new TextAndRefCell("Approvals Delegated to Me by ", currApprover,
183                                                                                         WEBPHONE + currApproverShort, 
184                                                                                         new String[] {"colspan=4", "class=head"})
185                                                         };
186                                                 } else {
187                                                         approverHeader = new AbsCell[] { 
188                                                                         new TextCell("Approvals Delegated to Me by " + currApprover,
189                                                                                         new String[] {"colspan=4", "class=head"})
190                                                         };
191                                                 }
192                                                 rv.add(approverHeader);
193                                         }
194                                         
195                                         // Sort by User Requesting
196                                         Collections.sort(currApproverList, new Comparator<Approval>() {
197                                                 @Override
198                                                 public int compare(Approval a1, Approval a2) {
199                                                         return a1.getUser().compareTo(a2.getUser());
200                                                 }
201                                         });
202                                         
203                                         String prevUser = null;
204                                         for (Approval appr : currApproverList) {
205                                                 if(++line<MAX_LINE) { // limit number displayed at one time.
206                                                         AbsCell userCell;
207                                                         String user = appr.getUser();
208                                                         if(user.equals(prevUser)) {
209                                                                 userCell = AbsCell.Null; 
210                                                         } else {
211                                                                 String title;
212                                                                 Organization org = OrganizationFactory.obtain(trans.env(), user);
213                                                                 if(org==null) {
214                                                                         title="";
215                                                                 } else {
216                                                                         Identity au = org.getIdentity(trans, user);
217                                                                         if(au!=null) {
218                                                                                 if(au.type().equals("MECHID")) {
219                                                                                         title="title=Sponsor is " + au.responsibleTo();
220                                                                                 } else {
221                                                                                         title="title=" + au.fullName();
222                                                                                 }
223                                                                         } else {
224                                                                                 title="";
225                                                                         }
226                                                                 }
227                                                                 userCell = new RefCell(prevUser=user, 
228                                                                         "" //TODO add Organization Link ability
229                                                                         ,title);
230                                                         }
231                                                         AbsCell[] sa = new AbsCell[] {
232                                                                 userCell,
233                                                                 new TextCell(appr.getMemo()),
234                                                                 new RadioCell("line"+ line,"approve", "approved|"+appr.getTicket()),
235                                                                 new RadioCell("line"+ line,"deny", "denied|"+appr.getTicket())
236                                                         };
237                                                         rv.add(sa);
238                                                 } else {
239                                                         ++numLeft;
240                                                 }
241                                         }
242                                 }
243                                 if(numLeft>0) {
244                                         msg = "After these, there will be " + numLeft + " approvals left to process";
245                                 }
246                                 if(rv.size()==0) {
247                                         if (numLeft>0) {
248                                                 msg = "No Approvals to process at this time for user " + userParam +". You have " 
249                                                         + numLeft + " other approvals to process.";
250                                         } else {
251                                                 msg = "No Approvals to process at this time";
252                                         }
253                                 }
254                         } catch (Exception e) {
255                                 trans.error().log(e);
256                         } finally {
257                                 tt.done();
258                         }
259                 return new Cells(rv,msg);
260                 }
261         }
262 }