AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / PendingRequestsShow.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.text.DateFormat;
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.List;
32 import java.util.UUID;
33
34 import org.onap.aaf.auth.env.AuthzTrans;
35 import org.onap.aaf.auth.gui.AAF_GUI;
36 import org.onap.aaf.auth.gui.BreadCrumbs;
37 import org.onap.aaf.auth.gui.NamedCode;
38 import org.onap.aaf.auth.gui.Page;
39 import org.onap.aaf.auth.gui.Table;
40 import org.onap.aaf.auth.gui.Table.Cells;
41 import org.onap.aaf.auth.gui.table.AbsCell;
42 import org.onap.aaf.auth.gui.table.RefCell;
43 import org.onap.aaf.auth.gui.table.TableData;
44 import org.onap.aaf.auth.gui.table.TextCell;
45 import org.onap.aaf.cadi.CadiException;
46 import org.onap.aaf.cadi.client.Future;
47 import org.onap.aaf.cadi.client.Rcli;
48 import org.onap.aaf.cadi.client.Retryable;
49 import org.onap.aaf.misc.env.APIException;
50 import org.onap.aaf.misc.env.Env;
51 import org.onap.aaf.misc.env.TimeTaken;
52 import org.onap.aaf.misc.xgen.Cache;
53 import org.onap.aaf.misc.xgen.DynamicCode;
54 import org.onap.aaf.misc.xgen.html.HTMLGen;
55
56 import aaf.v2_0.Approval;
57 import aaf.v2_0.Approvals;
58
59 public class PendingRequestsShow extends Page {
60         public static final String HREF = "/gui/myrequests";
61         public static final String NAME = "MyRequests";
62         static final String WEBPHONE = "http://webphone.att.com/cgi-bin/webphones.pl?id=";
63         private static DateFormat createdDF = new SimpleDateFormat("yyyy-MM-dd");
64         
65         public PendingRequestsShow(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
66                 super(gui.env, NAME,HREF, NO_FIELDS,
67                         new BreadCrumbs(breadcrumbs), 
68                         new NamedCode(true,"expedite") {
69                         @Override
70                         public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
71                                 cache.dynamic(hgen, new DynamicCode<HTMLGen, AAF_GUI, AuthzTrans>() {
72                                         @Override
73                                         public void code(final AAF_GUI gui, final AuthzTrans trans,     final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
74                                                 hgen
75                                                         .leaf("p", "class=expedite_request").text("These are your submitted Requests that are awaiting Approval. ")
76                                                         .br()
77                                                         .text("To Expedite a Request: ")
78                                                         .leaf("a","href=#expedite_directions","onclick=divVisibility('expedite_directions');")
79                                                                 .text("Click Here").end()
80                                                         .divID("expedite_directions", "style=display:none");
81                                                 hgen
82                                                         .incr(HTMLGen.OL)
83                                                         .incr(HTMLGen.LI)
84                                                         .leaf("a","href="+ApprovalForm.HREF+"?user="+trans.user(), "id=userApprove")
85                                                         .text("Copy This Link")
86                                                         .end()
87                                                         .end()
88                                                         .incr(HTMLGen.LI)
89                                                         .text("Send it to the Approver Listed")
90                                                         .end()
91                                                         .end()
92                                                         .text("NOTE: Using this link, the Approver will only see your requests. You only need to send this link once!")
93                                                         .end()
94                                                         .end();
95                                         }
96                                 });
97                         }
98                 },
99                         new Table<AAF_GUI,AuthzTrans>("Pending Requests",gui.env.newTransNoAvg(),new Model(), "class=std")
100                 );
101                                         
102
103         }
104
105         /**
106          * Implement the Table Content for Requests by User
107          * 
108          * @author Jeremiah
109          *
110          */
111         private static class Model extends TableData<AAF_GUI,AuthzTrans> {
112                 private static final String CSP_ATT_COM = "@csp.att.com";
113                 final long NUM_100NS_INTERVALS_SINCE_UUID_EPOCH = 0x01b21dd213814000L;
114                 private static final String[] headers = new String[] {"Request Date","Status","Memo","Approver"};
115
116                 @Override
117                 public String[] headers() {
118                         return headers;
119                 }
120                 
121                 @Override
122                 public Cells get(final AuthzTrans trans, final AAF_GUI gui) {
123                         final ArrayList<AbsCell[]> rv = new ArrayList<AbsCell[]>();
124                         try {
125                                 gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
126                                         @Override
127                                         public Void code(Rcli<?> client)throws CadiException, ConnectException, APIException {
128                                                 TimeTaken tt = trans.start("AAF Get Approvals by User",Env.REMOTE);
129                                                 try {
130                                                         Future<Approvals> fa = client.read("/authz/approval/user/"+trans.user(),gui.getDF(Approvals.class));
131                                                         if(fa.get(5000)) {
132                                                                 tt.done();
133                                                                 tt = trans.start("Load Data", Env.SUB);
134                                                                 if(fa.value!=null) {
135                                                                         List<Approval> approvals = fa.value.getApprovals();
136                                                                         Collections.sort(approvals, new Comparator<Approval>() {
137                                                                                 @Override
138                                                                                 public int compare(Approval a1, Approval a2) {
139                                                                                         UUID id1 = UUID.fromString(a1.getId());
140                                                                                         UUID id2 = UUID.fromString(a2.getId());
141                                                                                         return id1.timestamp()<=id2.timestamp()?1:-1;
142                                                                                 }
143                                                                         });
144                                                                         
145                                                                         String prevTicket = null;
146                                                                         for(Approval a : approvals) {
147                                                                                 String approver = a.getApprover();
148                                                                                 String approverShort = approver.substring(0,approver.indexOf('@'));
149                                                                                 
150                                                                                 AbsCell tsCell = null;
151                                                                                 String ticket = a.getTicket();
152                                                                                 if (ticket==null || ticket.equals(prevTicket)) {
153                                                                                         tsCell = AbsCell.Null;
154                                                                                 } else {
155                                                                                         UUID id = UUID.fromString(a.getId());
156                                                                                         tsCell = new RefCell(createdDF.format((id.timestamp() - NUM_100NS_INTERVALS_SINCE_UUID_EPOCH)/10000),
157                                                                                                         RequestDetail.HREF + "?ticket=" + ticket,false);
158                                                                                         prevTicket = ticket;
159                                                                                 }
160                                                                                 
161                                                                                 AbsCell approverCell = null;
162                                                                                 if (approver.endsWith(CSP_ATT_COM)) {
163                                                                                         approverCell = new RefCell(approver, WEBPHONE + approverShort,true);
164                                                                                 } else {
165                                                                                         approverCell = new TextCell(approver);
166                                                                                 }
167                                                                                 AbsCell[] sa = new AbsCell[] {
168                                                                                         tsCell,
169                                                                                         new TextCell(a.getStatus()),
170                                                                                         new TextCell(a.getMemo()),
171                                                                                         approverCell
172                                                                                 };
173                                                                                 rv.add(sa);
174                                                                         }
175                                                                 }
176                                                         } else {
177                                                                 gui.writeError(trans, fa, null, 0);
178                                                         }
179                                                 } finally {
180                                                         tt.done();
181                                                 }
182
183
184                                                 return null;
185                                         }
186                                 });
187                         } catch (Exception e) {
188                                 trans.error().log(e);
189                         }
190                         return new Cells(rv,null);
191                 }
192         }
193 }