AT&T 2.0.19 Code drop, stage 4
[aaf/authz.git] / authz-gui / src / main / java / com / att / authz / gui / pages / RoleDetail.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz.gui.pages;
5
6 import java.io.IOException;
7 import java.net.ConnectException;
8 import java.util.ArrayList;
9
10 import com.att.authz.env.AuthzEnv;
11 import com.att.authz.env.AuthzTrans;
12 import com.att.authz.gui.AuthGUI;
13 import com.att.authz.gui.BreadCrumbs;
14 import com.att.authz.gui.Page;
15 import com.att.authz.gui.Table;
16 import com.att.authz.gui.Table.Cells;
17 import com.att.authz.gui.table.AbsCell;
18 import com.att.authz.gui.table.RefCell;
19 import com.att.authz.gui.table.TextCell;
20 import org.onap.aaf.cadi.CadiException;
21 import org.onap.aaf.cadi.client.Future;
22 import org.onap.aaf.cadi.client.Rcli;
23 import org.onap.aaf.cadi.client.Retryable;
24 import org.onap.aaf.inno.env.APIException;
25 import org.onap.aaf.inno.env.Env;
26 import org.onap.aaf.inno.env.Slot;
27 import org.onap.aaf.inno.env.TimeTaken;
28
29 import aaf.v2_0.Pkey;
30 import aaf.v2_0.Role;
31 import aaf.v2_0.Roles;
32
33 /**
34  * Detail Page for Permissions
35  * 
36  *
37  */
38 public class RoleDetail extends Page {
39         public static final String HREF = "/gui/roledetail";
40         public static final String NAME = "RoleDetail";
41         private static final String BLANK = "";
42
43         public RoleDetail(final AuthGUI gui, Page ... breadcrumbs) throws APIException, IOException {
44                 super(gui.env, NAME, HREF, new String[] {"role"},
45                                 new BreadCrumbs(breadcrumbs),
46                                 new Table<AuthGUI,AuthzTrans>("Role Details",gui.env.newTransNoAvg(),new Model(gui.env()),"class=detail")
47                                 );
48         }
49
50         /**
51          * Implement the table content for Permissions Detail
52          * 
53          *
54          */
55         private static class Model implements Table.Data<AuthGUI,AuthzTrans> {
56                 private static final String[] headers = new String[0];
57                 private Slot role;
58                 public Model(AuthzEnv env) {
59                         role = env.slot(NAME+".role");
60                 }
61
62                 @Override
63                 public String[] headers() {
64                         return headers;
65                 }
66                 
67                 @Override
68                 public Cells get(final AuthGUI gui, final AuthzTrans trans) {
69                         final String pRole = trans.get(role, null);
70                         Cells rv = Cells.EMPTY;
71                         if(pRole!=null) {
72                                 try { 
73                                         rv = gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Cells>() {
74                                                 @Override
75                                                 public Cells code(Rcli<?> client) throws CadiException, ConnectException, APIException {
76                                                         ArrayList<AbsCell[]> rv = new ArrayList<AbsCell[]>();
77                                                         rv.add(new AbsCell[]{new TextCell("Role:"),new TextCell(pRole)});
78                                                         
79                                                         TimeTaken tt = trans.start("AAF Role Details",Env.REMOTE);
80                                                         try {
81                                                                 
82                                                                 Future<Roles> fr = client.read("/authz/roles/"+pRole,gui.rolesDF);
83                                                                 if(fr.get(AuthGUI.TIMEOUT)) {
84                                                                         tt.done();
85                                                                         tt = trans.start("Load Data", Env.SUB);
86                                                                         Role role = fr.value.getRole().get(0);
87                                                                         String desc = (role.getDescription()!=null?role.getDescription():BLANK);
88                                                                         rv.add(new AbsCell[]{new TextCell("Description:"),new TextCell(desc)});
89                                                                         boolean first=true;
90                                                                         for(Pkey r : role.getPerms()) {
91                                                                                 if(first){
92                                                                                         first=false;
93                                                                                         rv.add(new AbsCell[] {
94                                                                                                         new TextCell("Associated Permissions:"),
95                                                                                                         new TextCell(r.getType() +
96                                                                                                                         " | " + r.getInstance() +
97                                                                                                                         " | " + r.getAction()
98                                                                                                                         )
99                                                                                                 });
100                                                                                 } else {
101                                                                                         rv.add(new AbsCell[] {
102                                                                                                 AbsCell.Null,
103                                                                                                 new TextCell(r.getType() +
104                                                                                                                 " | " + r.getInstance() +
105                                                                                                                 " | " + r.getAction()
106                                                                                                                 )
107                                                                                         });
108                                                                                 }
109                                                                         }
110                                                                         String historyLink = RoleHistory.HREF 
111                                                                                         + "?role=" + pRole;
112                                                                         rv.add(new AbsCell[] {new RefCell("See History",historyLink)});
113                                                                 } else {
114                                                                         rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***")});
115                                                                 }
116                                                         } finally {
117                                                                 tt.done();
118                                                         }
119                                                         return new Cells(rv,null);
120                                                 }
121                                         });
122                                 } catch (Exception e) {
123                                         trans.error().log(e);
124                                 }
125                         }
126                         return rv;
127                 }
128         }
129 }               
130