Clean up Sonar results 3
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / RolesShow.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
29 import org.onap.aaf.auth.env.AuthzTrans;
30 import org.onap.aaf.auth.gui.AAF_GUI;
31 import org.onap.aaf.auth.gui.BreadCrumbs;
32 import org.onap.aaf.auth.gui.Page;
33 import org.onap.aaf.auth.gui.Table;
34 import org.onap.aaf.auth.gui.Table.Cells;
35 import org.onap.aaf.auth.gui.table.AbsCell;
36 import org.onap.aaf.auth.gui.table.RefCell;
37 import org.onap.aaf.auth.gui.table.TableData;
38 import org.onap.aaf.auth.gui.table.TextCell;
39 import org.onap.aaf.cadi.CadiException;
40 import org.onap.aaf.cadi.client.Future;
41 import org.onap.aaf.cadi.client.Rcli;
42 import org.onap.aaf.cadi.client.Retryable;
43 import org.onap.aaf.misc.env.APIException;
44 import org.onap.aaf.misc.env.Env;
45 import org.onap.aaf.misc.env.TimeTaken;
46 import org.onap.aaf.misc.env.util.Chrono;
47
48 import aaf.v2_0.UserRole;
49 import aaf.v2_0.UserRoles;
50
51
52 /**
53  * Page content for My Roles
54  * 
55  * @author Jonathan
56  *
57  */
58 public class RolesShow extends Page {
59         public static final String HREF = "/gui/myroles";
60         private static final String DATE_TIME_FORMAT = "yyyy-MM-dd";
61         
62         public RolesShow(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
63                 super(gui.env, "MyRoles",HREF, NO_FIELDS,
64                         new BreadCrumbs(breadcrumbs), 
65                         new Table<AAF_GUI,AuthzTrans>("Roles",gui.env.newTransNoAvg(),new Model(), "class=std"));
66         }
67
68         /**
69          * Implement the Table Content for Permissions by User
70          * 
71          * @author Jonathan
72          *
73          */
74         private static class Model extends TableData<AAF_GUI,AuthzTrans> {
75                 private static final String[] headers = new String[] {"Role","Expires","Remediation","Actions"};
76
77                 @Override
78                 public String[] headers() {
79                         return headers;
80                 }
81                 
82                 @Override
83                 public Cells get(final AuthzTrans trans, final AAF_GUI gui) {
84                         Cells rv = Cells.EMPTY;
85
86                         try {
87                                 rv = gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Cells>() {
88                                         @Override
89                                         public Cells code(Rcli<?> client) throws CadiException, ConnectException, APIException {
90                                                 ArrayList<AbsCell[]> rv = new ArrayList<AbsCell[]>();
91                                                 TimeTaken tt = trans.start("AAF Roles by User",Env.REMOTE);
92                                                 try {
93                                                         Future<UserRoles> fur = client.read("/authz/userRoles/user/"+trans.user(),gui.getDF(UserRoles.class));
94                                                         if (fur.get(5000)) {
95                                                                 if(fur.value != null) for (UserRole u : fur.value.getUserRole()) {
96                                                                         if(u.getExpires().compare(Chrono.timeStamp()) < 0) {
97                                                                                 AbsCell[] sa = new AbsCell[] {
98                                                                                                 new TextCell(u.getRole() + "*", "class=expired"),
99                                                                                                 new TextCell(new SimpleDateFormat(DATE_TIME_FORMAT).format(u.getExpires().toGregorianCalendar().getTime()),"class=expired"),
100                                                                                                 new RefCell("Extend",
101                                                                                                                 UserRoleExtend.HREF + "?user="+trans.user()+"&role="+u.getRole(),
102                                                                                                                 false,
103                                                                                                                 new String[]{"class=expired"}),
104                                                                                                 new RefCell("Remove",
105                                                                                                         UserRoleRemove.HREF + "?user="+trans.user()+"&role="+u.getRole(),
106                                                                                                         false,
107                                                                                                         new String[]{"class=expired"})
108                                                                                                                 
109                                                                                         };
110                                                                                         rv.add(sa);
111                                                                         } else {
112                                                                                 AbsCell[] sa = new AbsCell[] {
113                                                                                                 new RefCell(u.getRole(),
114                                                                                                                 RoleDetail.HREF+"?role="+u.getRole(),
115                                                                                                                 false),
116                                                                                                 new TextCell(new SimpleDateFormat(DATE_TIME_FORMAT).format(u.getExpires().toGregorianCalendar().getTime())),
117                                                                                                 AbsCell.Null,
118                                                                                                 new RefCell("Remove",
119                                                                                                                 UserRoleRemove.HREF + "?user="+trans.user()+"&role="+u.getRole(),
120                                                                                                                 false)
121                                                                                         };
122                                                                                         rv.add(sa);
123                                                                         }
124                                                                 }
125                                                         }
126                                                         
127                                                 } finally {
128                                                         tt.done();
129                                                 }
130                                                 return new Cells(rv,null);
131                                         }
132                                 });
133                         } catch (Exception e) {
134                                 trans.error().log(e);
135                         }
136                         return rv;
137                 }
138         }
139 }