Mass whitespace changes (Style Warnings)
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / RequestDetail.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.UUID;
30
31 import org.onap.aaf.auth.env.AuthzEnv;
32 import org.onap.aaf.auth.env.AuthzTrans;
33 import org.onap.aaf.auth.gui.AAF_GUI;
34 import org.onap.aaf.auth.gui.BreadCrumbs;
35 import org.onap.aaf.auth.gui.Page;
36 import org.onap.aaf.auth.gui.Table;
37 import org.onap.aaf.auth.gui.Table.Cells;
38 import org.onap.aaf.auth.gui.table.AbsCell;
39 import org.onap.aaf.auth.gui.table.TableData;
40 import org.onap.aaf.auth.gui.table.TextCell;
41 import org.onap.aaf.cadi.CadiException;
42 import org.onap.aaf.cadi.client.Future;
43 import org.onap.aaf.cadi.client.Rcli;
44 import org.onap.aaf.cadi.client.Retryable;
45 import org.onap.aaf.misc.env.APIException;
46 import org.onap.aaf.misc.env.Env;
47 import org.onap.aaf.misc.env.Slot;
48 import org.onap.aaf.misc.env.TimeTaken;
49
50 import aaf.v2_0.Approval;
51 import aaf.v2_0.Approvals;
52
53 public class RequestDetail extends Page {
54     public static final String HREF = "/gui/requestdetail";
55     public static final String NAME = "RequestDetail";
56     private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
57     public static final String[] FIELDS = {"ticket"};
58
59     public RequestDetail(final AAF_GUI gui, Page ... breadcrumbs) throws APIException, IOException {
60         super(gui.env, NAME, HREF, FIELDS,
61                 new BreadCrumbs(breadcrumbs),
62                 new Table<AAF_GUI,AuthzTrans>("Request Details",gui.env.newTransNoAvg(),new Model(gui.env),"class=detail")
63                 );
64     }
65
66     /**
67      * Implement the table content for Request Detail
68      * 
69      * @author Jeremiah
70      *
71      */
72     private static class Model extends TableData<AAF_GUI,AuthzTrans> {
73         final long NUM_100NS_INTERVALS_SINCE_UUID_EPOCH = 0x01b21dd213814000L;
74         private Slot sTicket;
75         public Model(AuthzEnv env) {
76             sTicket = env.slot(NAME+".ticket");
77         }
78
79         @Override
80         public Cells get(final AuthzTrans trans, final AAF_GUI gui) {
81             Cells rv=Cells.EMPTY;
82             final String ticket = trans.get(sTicket, null);
83             if (ticket!=null) {
84                 try {
85                     rv = gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Cells>() {
86                         @Override
87                         public Cells code(Rcli<?> client) throws CadiException, ConnectException, APIException {
88                             TimeTaken tt = trans.start("AAF Approval Details",Env.REMOTE);
89                             ArrayList<AbsCell[]> rv = new ArrayList<>();
90                             try {
91                                 Future<Approvals> fa = client.read(
92                                     "/authz/approval/ticket/"+ticket, 
93                                     gui.getDF(Approvals.class)
94                                     );
95                                 
96                                 if (fa.get(AAF_GUI.TIMEOUT)) {
97                                     if (!trans.user().equals(fa.value.getApprovals().get(0).getUser())) {
98                                         return Cells.EMPTY;
99                                     }
100                                     tt.done();
101                                     tt = trans.start("Load Data", Env.SUB);
102                                     boolean first = true;
103                                     for ( Approval approval : fa.value.getApprovals()) {
104                                         AbsCell[] approverLine = new AbsCell[4];
105                                         // only print common elements once
106                                         if (first) {
107                                             DateFormat createdDF = new SimpleDateFormat(DATE_TIME_FORMAT);
108                                             UUID id = UUID.fromString(approval.getId());
109                                             
110                                             rv.add(new AbsCell[]{new TextCell("Ticket ID:"),new TextCell(approval.getTicket(),"colspan=3")});
111                                             rv.add(new AbsCell[]{new TextCell("Memo:"),new TextCell(approval.getMemo(),"colspan=3")});
112                                             rv.add(new AbsCell[]{new TextCell("Requested On:"), 
113                                                     new TextCell(createdDF.format((id.timestamp() - NUM_100NS_INTERVALS_SINCE_UUID_EPOCH)/10000),"colspan=3")
114                                             });
115                                             rv.add(new AbsCell[]{new TextCell("Operation:"),new TextCell(decodeOp(approval.getOperation()),"colspan=3")});
116                                             String user = approval.getUser();
117                                             rv.add(new AbsCell[]{new TextCell("User:"),new TextCell(user,"colspan=3")});
118                                             
119                                             // headers for listing each approver
120                                             rv.add(new AbsCell[]{new TextCell(" ","colspan=4","class=blank_line")});
121                                             rv.add(new AbsCell[]{AbsCell.Null,
122                                                     new TextCell("Approver","class=bold"), 
123                                                     new TextCell("Type","class=bold"), 
124                                                     new TextCell("Status","class=bold")});
125                                             approverLine[0] = new TextCell("Approvals:");
126                                             
127                                             first = false;
128                                         } else {
129                                             approverLine[0] = AbsCell.Null;
130                                         }
131                                         
132                                         approverLine[1] = new TextCell(approval.getApprover());
133                                         String type = approval.getType();
134                                         if ("owner".equalsIgnoreCase(type)) {
135                                             type = "resource owner";
136                                         }
137                                         
138                                         approverLine[2] = new TextCell(type);
139                                         approverLine[3] = new TextCell(approval.getStatus());
140                                         rv.add(approverLine);
141                                     
142                                     }
143                                 } else {
144                                     rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***")});
145                                 }
146                             } finally {
147                                 tt.done();
148                             }
149                             return new Cells(rv,null);
150                         }
151                     });
152                 } catch (Exception e) {
153                     trans.error().log(e);
154                 }
155             }
156             return rv;
157         }
158
159         private String decodeOp(String operation) {
160             if ("C".equalsIgnoreCase(operation)) {
161                 return "Create";
162             } else if ("D".equalsIgnoreCase(operation)) {
163                 return "Delete";
164             } else if ("U".equalsIgnoreCase(operation)) {
165                 return "Update";
166             } else if ("G".equalsIgnoreCase(operation)) {
167                 return "Grant";
168             } else if ("UG".equalsIgnoreCase(operation)) {
169                 return "Un-Grant";
170             }
171             return operation;
172         }
173     }
174 }