AT&T 2.0.19 Code drop, stage 4
[aaf/authz.git] / authz-gui / src / main / java / com / att / authz / gui / pages / PendingRequestsShow.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.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;
15
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 org.onap.aaf.cadi.CadiException;
27 import org.onap.aaf.cadi.client.Future;
28 import org.onap.aaf.cadi.client.Rcli;
29 import org.onap.aaf.cadi.client.Retryable;
30 import org.onap.aaf.inno.env.APIException;
31 import org.onap.aaf.inno.env.Env;
32 import org.onap.aaf.inno.env.TimeTaken;
33 import com.att.xgen.Cache;
34 import com.att.xgen.DynamicCode;
35 import com.att.xgen.html.HTMLGen;
36
37 import aaf.v2_0.Approval;
38 import aaf.v2_0.Approvals;
39
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";
45         
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") {
50                         @Override
51                         public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
52                                 cache.dynamic(hgen, new DynamicCode<HTMLGen, AuthGUI, AuthzTrans>() {
53                                         @Override
54                                         public void code(AuthGUI gui, AuthzTrans trans, Cache<HTMLGen> cache, HTMLGen hgen)     throws APIException, IOException {
55                                                 hgen
56                                                         .leaf("p", "class=expedite_request").text("These are your submitted Requests that are awaiting Approval. ")
57                                                         .br()
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");
62                                                 hgen
63                                                         .incr(HTMLGen.OL)
64                                                         .incr(HTMLGen.LI)
65                                                         .leaf("a","href="+ApprovalForm.HREF+"?user="+trans.user(), "id=userApprove")
66                                                         .text("Copy This Link")
67                                                         .end()
68                                                         .end()
69                                                         .incr(HTMLGen.LI)
70                                                         .text("Send it to the Approver Listed")
71                                                         .end()
72                                                         .end()
73                                                         .text("NOTE: Using this link, the Approver will only see your requests. You only need to send this link once!")
74                                                         .end()
75                                                         .end();
76                                         }
77                                 });
78                         }
79                 },
80                         new Table<AuthGUI,AuthzTrans>("Pending Requests",gui.env.newTransNoAvg(),new Model(), "class=std")
81                 );
82                                         
83
84         }
85
86         /**
87          * Implement the Table Content for Requests by User
88          * 
89          *
90          */
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"};
95
96                 @Override
97                 public String[] headers() {
98                         return headers;
99                 }
100                 
101                 @Override
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[]>();
105                         try {
106                                 gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
107                                         @Override
108                                         public Void code(Rcli<?> client)throws CadiException, ConnectException, APIException {
109                                                 TimeTaken tt = trans.start("AAF Get Approvals by User",Env.REMOTE);
110                                                 try {
111                                                         Future<Approvals> fa = client.read("/authz/approval/user/"+trans.user(),gui.approvalsDF);
112                                                         if(fa.get(5000)) {
113                                                                 tt.done();
114                                                                 tt = trans.start("Load Data", Env.SUB);
115                                                                 if(fa.value!=null) {
116                                                                         List<Approval> approvals = fa.value.getApprovals();
117                                                                         Collections.sort(approvals, new Comparator<Approval>() {
118                                                                                 @Override
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;
123                                                                                 }
124                                                                         });
125                                                                         
126                                                                         String prevTicket = null;
127                                                                         for(Approval a : approvals) {
128                                                                                 String approver = a.getApprover();
129                                                                                 String approverShort = approver.substring(0,approver.indexOf('@'));
130                                                                                 
131                                                                                 AbsCell tsCell = null;
132                                                                                 String ticket = a.getTicket();
133                                                                                 if (ticket.equals(prevTicket)) {
134                                                                                         tsCell = AbsCell.Null;
135                                                                                 } else {
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());
139                                                                                         prevTicket = ticket;
140                                                                                 }
141                                                                                 
142                                                                                 AbsCell approverCell = null;
143                                                                                 if (approver.endsWith(CSP_ATT_COM)) {
144                                                                                         approverCell = new RefCell(approver, WEBPHONE + approverShort);
145                                                                                 } else {
146                                                                                         approverCell = new TextCell(approver);
147                                                                                 }
148                                                                                 AbsCell[] sa = new AbsCell[] {
149                                                                                         tsCell,
150                                                                                         new TextCell(a.getStatus()),
151                                                                                         new TextCell(a.getMemo()),
152                                                                                         approverCell
153                                                                                 };
154                                                                                 rv.add(sa);
155                                                                         }
156                                                                 }
157                                                         } else {
158                                                                 gui.writeError(trans, fa, null);
159                                                         }
160                                                 } finally {
161                                                         tt.done();
162                                                 }
163
164
165                                                 return null;
166                                         }
167                                 });
168                         } catch (Exception e) {
169                                 trans.error().log(e);
170                         }
171                         return new Cells(rv,null);
172                 }
173         }
174 }