Collection syntax change because of Sonar
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / NssShow.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.util.ArrayList;
27 import java.util.Collections;
28 import java.util.Comparator;
29 import java.util.List;
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.RefCell;
40 import org.onap.aaf.auth.gui.table.TableData;
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.Nss;
51 import aaf.v2_0.Nss.Ns;
52
53 public class NssShow extends Page {
54         public static final String HREF = "/gui/ns";
55
56         public NssShow(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
57                 super(gui.env, "MyNamespaces",HREF, NO_FIELDS,
58                                 new BreadCrumbs(breadcrumbs), 
59                                 new Table<AAF_GUI,AuthzTrans>("Namespaces I administer",gui.env.newTransNoAvg(),new Model(true,"Administrator",gui.env), 
60                                                 "class=std", "style=display: inline-block; width: 45%; margin: 10px;"),
61                                 new Table<AAF_GUI,AuthzTrans>("Namespaces I own",gui.env.newTransNoAvg(),new Model(false,"Owner",gui.env),
62                                                 "class=std", "style=display: inline-block; width: 45%; margin: 10px;"));
63         }
64         
65         private static class Model extends TableData<AAF_GUI,AuthzTrans> {
66                 private String[] headers;
67                 private String privilege = null;
68                 public final Slot sNssByUser;
69                 private boolean isAdmin;
70
71                 public Model(boolean admin, String privilege,AuthzEnv env) {
72                         super();
73                         headers = new String[] {privilege};
74                         this.privilege = privilege;
75                         isAdmin = admin;
76                         sNssByUser = env.slot("NSS_SHOW_MODEL_DATA");
77                 }
78
79                 @Override
80                 public String[] headers() {
81                         return headers;
82                 }
83                 
84                 @Override
85                 public Cells get(final AuthzTrans trans, final AAF_GUI gui) {
86                         ArrayList<AbsCell[]> rv = new ArrayList<>();
87                         List<Ns> nss = trans.get(sNssByUser, null);
88                         if(nss==null) {
89                                 TimeTaken tt = trans.start("AAF Nss by User for " + privilege,Env.REMOTE);
90                                 try {
91                                         nss = gui.clientAsUser(trans.getUserPrincipal(), new Retryable<List<Ns>>() {
92                                                 @Override
93                                                 public List<Ns> code(Rcli<?> client) throws CadiException, ConnectException, APIException {
94                                                         List<Ns> nss = null;
95                                                         Future<Nss> fp = client.read("/authz/nss/either/" + trans.user(),gui.getDF(Nss.class));
96                                                         if(fp.get(AAF_GUI.TIMEOUT)) {
97                                                                 TimeTaken tt = trans.start("Load Data for " + privilege, Env.SUB);
98                                                                 try {
99                                                                         if(fp.value!=null) {
100                                                                                 nss = fp.value.getNs();
101                                                                                 Collections.sort(nss, new Comparator<Ns>() {
102                                                                                         public int compare(Ns ns1, Ns ns2) {
103                                                                                                 return ns1.getName().compareToIgnoreCase(ns2.getName());
104                                                                                         }
105                                                                                 });
106                                                                                 trans.put(sNssByUser,nss);
107                                                                         } 
108                                                                 } finally {
109                                                                         tt.done();
110                                                                 }
111                                                         }else {
112                                                                 gui.writeError(trans, fp, null,0);
113                                                         }
114                                                         return nss;
115                                                 }
116                                         });
117                                 } catch (Exception e) {
118                                         trans.error().log(e);
119                                 } finally {
120                                         tt.done();
121                                 }
122                         }
123                         
124                         if(nss!=null) {
125                                 for(Ns n : nss) {
126                                         if((isAdmin && !n.getAdmin().isEmpty())
127                                           || (!isAdmin && !n.getResponsible().isEmpty())) {
128                                                 AbsCell[] sa = new AbsCell[] {
129                                                         new RefCell(n.getName(),NsDetail.HREF
130                                                                         +"?ns="+n.getName(),false),
131                                                 };
132                                                 rv.add(sa);
133                                         }
134                                 }
135                         }
136
137                         return new Cells(rv,null);
138                 }
139         }
140         
141
142 }