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