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