1 /*******************************************************************************
2 * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3 *******************************************************************************/
4 package com.att.authz.gui.pages;
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;
43 import aaf.v2_0.Approval;
45 public class ApprovalForm extends Page {
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"};
53 public ApprovalForm(final AuthGUI gui, final Page ... breadcrumbs) throws APIException, IOException {
54 super(gui.env,NAME,HREF, FIELDS,
56 new BreadCrumbs(breadcrumbs),
57 new NamedCode(false, "filterByUser") {
59 public void code(Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
60 cache.dynamic(hgen, new DynamicCode<HTMLGen, AuthGUI, AuthzTrans>() {
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!")
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") {
77 public void code(Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
78 Mark jsStart = new Mark();
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;");
93 * Implement the Table Content for Approvals
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;
102 public Model(AuthzEnv env) {
103 sUser = env.slot(NAME+".user");
107 public String[] headers() {
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[]>();
116 TimeTaken tt = trans.start("AAF Get Approvals for Approver",Env.REMOTE);
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>() {
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);
125 if(fa.get(AuthGUI.TIMEOUT)) {
128 for (Approval appr : fa.value.getApprovals()) {
129 if (appr.getStatus().equals("pending")) {
130 if (userParam!=null) {
131 if (!appr.getUser().equalsIgnoreCase(userParam)) {
136 pendingApprovals.add(appr);
141 String prevApprover = null;
142 int overallIndex = 0;
144 for (Approval appr : pendingApprovals) {
145 String currApprover = appr.getApprover();
146 if (!currApprover.equals(prevApprover)) {
147 prevApprover = currApprover;
148 beginIndicesPerApprover.add(overallIndex);
157 if (pendingApprovals.size() > 0) {
158 // Only add select all links if we have approvals
159 AbsCell[] selectAllRow = new AbsCell[] {
162 new ButtonCell("all", "onclick=selectAll('approve')", "class=selectAllButton"),
163 new ButtonCell("all", "onclick=selectAll('deny')", "class=selectAllButton")
165 rv.add(selectAllRow);
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);
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"})
187 approverHeader = new AbsCell[] {
188 new TextCell("Approvals Delegated to Me by " + currApprover,
189 new String[] {"colspan=4", "class=head"})
192 rv.add(approverHeader);
195 // Sort by User Requesting
196 Collections.sort(currApproverList, new Comparator<Approval>() {
198 public int compare(Approval a1, Approval a2) {
199 return a1.getUser().compareTo(a2.getUser());
203 String prevUser = null;
204 for (Approval appr : currApproverList) {
205 if(++line<MAX_LINE) { // limit number displayed at one time.
207 String user = appr.getUser();
208 if(user.equals(prevUser)) {
209 userCell = AbsCell.Null;
212 Organization org = OrganizationFactory.obtain(trans.env(), user);
216 Identity au = org.getIdentity(trans, user);
218 if(au.type().equals("MECHID")) {
219 title="title=Sponsor is " + au.responsibleTo();
221 title="title=" + au.fullName();
227 userCell = new RefCell(prevUser=user,
228 "" //TODO add Organization Link ability
231 AbsCell[] sa = new AbsCell[] {
233 new TextCell(appr.getMemo()),
234 new RadioCell("line"+ line,"approve", "approved|"+appr.getTicket()),
235 new RadioCell("line"+ line,"deny", "denied|"+appr.getTicket())
244 msg = "After these, there will be " + numLeft + " approvals left to process";
248 msg = "No Approvals to process at this time for user " + userParam +". You have "
249 + numLeft + " other approvals to process.";
251 msg = "No Approvals to process at this time";
254 } catch (Exception e) {
255 trans.error().log(e);
259 return new Cells(rv,msg);