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.text.DateFormat;
9 import java.text.SimpleDateFormat;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.Comparator;
13 import java.util.List;
14 import java.util.UUID;
16 import com.att.authz.env.AuthzTrans;
17 import com.att.authz.gui.AuthGUI;
18 import com.att.authz.gui.BreadCrumbs;
19 import com.att.authz.gui.NamedCode;
20 import com.att.authz.gui.Page;
21 import com.att.authz.gui.Table;
22 import com.att.authz.gui.Table.Cells;
23 import com.att.authz.gui.table.AbsCell;
24 import com.att.authz.gui.table.RefCell;
25 import com.att.authz.gui.table.TextCell;
26 import com.att.cadi.CadiException;
27 import com.att.cadi.client.Future;
28 import com.att.cadi.client.Rcli;
29 import com.att.cadi.client.Retryable;
30 import com.att.inno.env.APIException;
31 import com.att.inno.env.Env;
32 import com.att.inno.env.TimeTaken;
33 import com.att.xgen.Cache;
34 import com.att.xgen.DynamicCode;
35 import com.att.xgen.html.HTMLGen;
37 import aaf.v2_0.Approval;
38 import aaf.v2_0.Approvals;
40 public class PendingRequestsShow extends Page {
41 public static final String HREF = "/gui/myrequests";
42 public static final String NAME = "MyRequests";
43 static final String WEBPHONE = "http://webphone.att.com/cgi-bin/webphones.pl?id=";
44 private static final String DATE_TIME_FORMAT = "yyyy-MM-dd";
46 public PendingRequestsShow(final AuthGUI gui, final Page ... breadcrumbs) throws APIException, IOException {
47 super(gui.env, NAME,HREF, NO_FIELDS,
48 new BreadCrumbs(breadcrumbs),
49 new NamedCode(true,"expedite") {
51 public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
52 cache.dynamic(hgen, new DynamicCode<HTMLGen, AuthGUI, AuthzTrans>() {
54 public void code(AuthGUI gui, AuthzTrans trans, Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
56 .leaf("p", "class=expedite_request").text("These are your submitted Requests that are awaiting Approval. ")
58 .text("To Expedite a Request: ")
59 .leaf("a","href=#expedite_directions","onclick=divVisibility('expedite_directions');")
60 .text("Click Here").end()
61 .divID("expedite_directions", "style=display:none");
65 .leaf("a","href="+ApprovalForm.HREF+"?user="+trans.user(), "id=userApprove")
66 .text("Copy This Link")
70 .text("Send it to the Approver Listed")
73 .text("NOTE: Using this link, the Approver will only see your requests. You only need to send this link once!")
80 new Table<AuthGUI,AuthzTrans>("Pending Requests",gui.env.newTransNoAvg(),new Model(), "class=std")
87 * Implement the Table Content for Requests by User
91 private static class Model implements Table.Data<AuthGUI,AuthzTrans> {
92 private static final String CSP_ATT_COM = "@csp.att.com";
93 final long NUM_100NS_INTERVALS_SINCE_UUID_EPOCH = 0x01b21dd213814000L;
94 private static final String[] headers = new String[] {"Request Date","Status","Memo","Approver"};
97 public String[] headers() {
102 public Cells get(final AuthGUI gui, final AuthzTrans trans) {
103 DateFormat createdDF = new SimpleDateFormat(DATE_TIME_FORMAT);
104 ArrayList<AbsCell[]> rv = new ArrayList<AbsCell[]>();
106 gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
108 public Void code(Rcli<?> client)throws CadiException, ConnectException, APIException {
109 TimeTaken tt = trans.start("AAF Get Approvals by User",Env.REMOTE);
111 Future<Approvals> fa = client.read("/authz/approval/user/"+trans.user(),gui.approvalsDF);
114 tt = trans.start("Load Data", Env.SUB);
116 List<Approval> approvals = fa.value.getApprovals();
117 Collections.sort(approvals, new Comparator<Approval>() {
119 public int compare(Approval a1, Approval a2) {
120 UUID id1 = UUID.fromString(a1.getId());
121 UUID id2 = UUID.fromString(a2.getId());
122 return id1.timestamp()<=id2.timestamp()?1:-1;
126 String prevTicket = null;
127 for(Approval a : approvals) {
128 String approver = a.getApprover();
129 String approverShort = approver.substring(0,approver.indexOf('@'));
131 AbsCell tsCell = null;
132 String ticket = a.getTicket();
133 if (ticket.equals(prevTicket)) {
134 tsCell = AbsCell.Null;
136 UUID id = UUID.fromString(a.getId());
137 tsCell = new RefCell(createdDF.format((id.timestamp() - NUM_100NS_INTERVALS_SINCE_UUID_EPOCH)/10000),
138 RequestDetail.HREF + "?ticket=" + a.getTicket());
142 AbsCell approverCell = null;
143 if (approver.endsWith(CSP_ATT_COM)) {
144 approverCell = new RefCell(approver, WEBPHONE + approverShort);
146 approverCell = new TextCell(approver);
148 AbsCell[] sa = new AbsCell[] {
150 new TextCell(a.getStatus()),
151 new TextCell(a.getMemo()),
158 gui.writeError(trans, fa, null);
168 } catch (Exception e) {
169 trans.error().log(e);
171 return new Cells(rv,null);