5cb8ff2c682f0970c50e6e6164e34052c966b0dc
[aaf/authz.git] / authz-gui / src / main / java / com / att / authz / gui / pages / NsInfoForm.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz.gui.pages;
5
6 import static com.att.xgen.html.HTMLGen.A;
7 import static com.att.xgen.html.HTMLGen.TABLE;
8
9 import java.io.IOException;
10 import java.net.ConnectException;
11 import java.util.List;
12
13 import com.att.authz.env.AuthzTrans;
14 import com.att.authz.gui.AuthGUI;
15 import com.att.authz.gui.BreadCrumbs;
16 import com.att.authz.gui.NamedCode;
17 import com.att.authz.gui.Page;
18 import org.onap.aaf.cadi.CadiException;
19 import org.onap.aaf.cadi.client.Future;
20 import org.onap.aaf.cadi.client.Rcli;
21 import org.onap.aaf.cadi.client.Retryable;
22 import org.onap.aaf.inno.env.APIException;
23 import org.onap.aaf.inno.env.Env;
24 import org.onap.aaf.inno.env.Slot;
25 import org.onap.aaf.inno.env.TimeTaken;
26 import com.att.xgen.Cache;
27 import com.att.xgen.DynamicCode;
28 import com.att.xgen.Mark;
29 import com.att.xgen.html.HTMLGen;
30
31 import aaf.v2_0.Nss;
32 import aaf.v2_0.Nss.Ns;
33 import aaf.v2_0.Nss.Ns.Attrib;
34
35 public class NsInfoForm extends Page {
36         // Package on purpose
37         static final String HREF = "/gui/onboard";
38         static final String NAME = "Onboarding";
39         static final String fields[] = {"ns","description","mots","owners","admins"};
40         
41         public NsInfoForm(final AuthGUI gui, final Page ... breadcrumbs) throws APIException, IOException {
42                 super(gui.env,NAME,HREF, fields,
43                         new BreadCrumbs(breadcrumbs),
44                         new NamedCode(true,"content") {
45
46                         private final Slot sID = gui.env.slot(NsInfoForm.NAME+'.'+NsInfoForm.fields[0]);
47                         @Override
48                         public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
49                                 // p tags not closing right using .p() - causes issues in IE8 password form - so using leaf for the moment
50                                 hgen.leaf(HTMLGen.H2).text("Namespace Info").end()
51                                      .leaf("p").text("Hover over Fields for Tool Tips, or click ")
52                                         .leaf(A,"href="+gui.env.getProperty("aaf_url.gui_onboard","")).text("Here").end()
53                                         .text(" for more information")
54                                      .end()
55                                         .incr("form","method=post");
56                                 Mark table = new Mark(TABLE);
57                                 hgen.incr(table);
58                                 cache.dynamic(hgen, new DynamicCode<HTMLGen, AuthGUI, AuthzTrans>() {
59                                         @SuppressWarnings("unchecked")
60                                         @Override
61                                         public void code(final AuthGUI gui, AuthzTrans trans, Cache<HTMLGen> cache, HTMLGen hgen)       throws APIException, IOException {
62                                                 final String incomingID= trans.get(sID, "");
63                                                 final String[] info = new String[fields.length];
64                                                 final Object own_adm[] = new Object[2]; 
65                                                 for(int i=0;i<info.length;++i) {
66                                                         info[i]="";
67                                                 }
68                                                 if(incomingID.length()>0) {
69                                                         TimeTaken tt = trans.start("AAF Namespace Info",Env.REMOTE);
70                                                         try {
71                                                                 gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
72                                                                         @Override
73                                                                         public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
74                                                                                 Future<Nss> fn = client.read("/authz/nss/"+incomingID,gui.nssDF);
75                                                                                 if(fn.get(AuthGUI.TIMEOUT)) {
76                                                                                         for(Ns ns : fn.value.getNs()) {
77                                                                                                 info[0]=ns.getName();
78                                                                                                 info[1]=ns.getDescription();
79                                                                                                 for(Attrib attr: ns.getAttrib()) {
80                                                                                                         switch(attr.getKey()) {
81                                                                                                                 case "mots":
82                                                                                                                         info[2]=attr.getValue();
83                                                                                                                 default:
84                                                                                                         }
85                                                                                                 }
86                                                                                                 own_adm[0]=ns.getResponsible();
87                                                                                                 own_adm[1]=ns.getAdmin();
88                                                                                         }
89                                                                                 } else {
90                                                                                         trans.error().log(fn.body());
91                                                                                 }
92                                                                                 return null;
93                                                                         }
94                                                                 });
95                                                         } catch (Exception e) {
96                                                                 trans.error().log("Unable to access AAF for NS Info",incomingID);
97                                                                 e.printStackTrace();
98                                                         } finally {
99                                                                 tt.done();
100                                                         }
101                                                 }
102                                                 hgen.input(fields[0],"Namespace",false,"value="+info[0],"title=AAF Namespace")
103                                                         .input(fields[1],"Description*",true,"value="+info[1],"title=Full Application Name, Tool Name or Group")
104                                                         .input(fields[2],"MOTS ID",false,"value="+info[2],"title=MOTS ID if this is an Application, and has MOTS");
105                                                 Mark endTD = new Mark(),endTR=new Mark();
106                                                 // Owners
107                                                 hgen.incr(endTR,HTMLGen.TR)
108                                                                 .incr(endTD,HTMLGen.TD)
109                                                                         .leaf("label","for="+fields[3]).text("Responsible Party")
110                                                                 .end(endTD)
111                                                                 .incr(endTD,HTMLGen.TD)
112                                                                         .tagOnly("input","id="+fields[3],"title=Owner of App, must be an Non-Bargained Employee");
113                                                                         if(own_adm[0]!=null) {
114                                                                                 for(String s : (List<String>)own_adm[0]) {
115                                                                                         hgen.incr("label",true).text(s).end();
116                                                                                 }
117                                                                         }
118                                                         hgen.end(endTR);
119
120                                                         // Admins
121                                                         hgen.incr(endTR,HTMLGen.TR)
122                                                                 .incr(endTD,HTMLGen.TD)
123                                                                         .leaf("label","for="+fields[4]).text("Administrators")
124                                                                 .end(endTD)
125                                                                 .incr(endTD,HTMLGen.TD)
126                                                                         .tagOnly("input","id="+fields[4],"title=Admins may be employees, contractors or mechIDs");
127                                                                         if(own_adm[1]!=null) {
128                                                                                 for(String s : (List<String>)own_adm[1]) {
129                                                                                         hgen.incr(HTMLGen.P,true).text(s).end();
130                                                                                 }
131                                                                         }
132                                                                 hgen.end(endTR)
133                                                 .end();
134                                         }
135                                 });
136                                 hgen.end();
137                                 hgen.tagOnly("input", "type=submit", "value=Submit")
138                                         .end();
139
140                         }
141                 });
142         }
143
144 }