AT&T 2.0.19 Code drop, stage 4
[aaf/authz.git] / authz-gui / src / main / java / com / att / authz / gui / pages / ApprovalAction.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
8 import com.att.authz.env.AuthzTrans;
9 import com.att.authz.gui.AuthGUI;
10 import com.att.authz.gui.BreadCrumbs;
11 import com.att.authz.gui.NamedCode;
12 import com.att.authz.gui.Page;
13 import org.onap.aaf.cadi.CadiException;
14 import org.onap.aaf.cadi.client.Future;
15 import org.onap.aaf.cadi.client.Rcli;
16 import org.onap.aaf.cadi.client.Retryable;
17 import org.onap.aaf.inno.env.APIException;
18 import org.onap.aaf.inno.env.Env;
19 import org.onap.aaf.inno.env.Slot;
20 import org.onap.aaf.inno.env.TimeTaken;
21 import com.att.xgen.Cache;
22 import com.att.xgen.DynamicCode;
23 import com.att.xgen.html.HTMLGen;
24
25 import aaf.v2_0.Approval;
26 import aaf.v2_0.Approvals;
27
28 public class ApprovalAction extends Page {
29         public ApprovalAction(final AuthGUI gui, final Page ... breadcrumbs) throws APIException, IOException {
30                 super(gui.env,"Approvals",ApprovalForm.HREF, ApprovalForm.FIELDS,
31                         new BreadCrumbs(breadcrumbs),
32                         new NamedCode(true,"content") {
33                                 final Slot sAppr = gui.env.slot(ApprovalForm.NAME+'.'+ApprovalForm.FIELDS[0]);
34                                 final Slot sUser = gui.env.slot(ApprovalForm.NAME+'.'+ApprovalForm.FIELDS[1]);
35                                 
36                                 @Override
37                                 public void code(Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {                         
38                                         cache.dynamic(hgen, new DynamicCode<HTMLGen,AuthGUI, AuthzTrans>() {
39                                                 @Override
40                                                 public void code(final AuthGUI gui, final AuthzTrans trans,Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
41                                                         boolean fail = true;
42                                                         String[] appr = trans.get(sAppr,null);
43                                                         String user = trans.get(sUser,null);
44                                                         String lastPage = ApprovalForm.HREF;
45                                                         if (user != null) {
46                                                                 lastPage += "?user="+user;
47                                                         }
48                                                         
49                                                         if(appr==null) {
50                                                                 hgen.p("No Approvals have been selected.");
51                                                         } else {
52                                                                 Approval app;
53                                                                 final Approvals apps = new Approvals();
54                                                                 int count = 0;
55                                                                 for(String a : appr) {
56                                                                         if(a!=null) {
57                                                                                 int idx = a.indexOf('|');
58                                                                                 if(idx>=0) {
59                                                                                         app = new Approval();
60                                                                                         app.setStatus(a.substring(0,idx));
61                                                                                         app.setTicket(a.substring(++idx));
62                                                                                         app.setApprover(trans.getUserPrincipal().getName());
63                                                                                         apps.getApprovals().add(app);
64                                                                                         ++count;
65                                                                                 }
66                                                                         }
67                                                                 }
68                                                                 if(apps.getApprovals().isEmpty()) {
69                                                                         hgen.p("No Approvals have been sent.");
70                                                                 } else {
71                                                                         TimeTaken tt = trans.start("AAF Update Approvals",Env.REMOTE);
72                                                                         try {
73                                                                                 final int total = count;
74                                                                                 fail = gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Boolean>() {
75                                                                                         @Override
76                                                                                         public Boolean code(Rcli<?> client) throws APIException, CadiException  {
77                                                                                                 boolean fail2 = true;
78                                                                                                 Future<Approvals> fa = client.update("/authz/approval",gui.approvalsDF,apps);
79                                                                                                 if(fa.get(AuthGUI.TIMEOUT)) {
80                                                                                                         // Do Remote Call
81                                                                                                         fail2 = false;
82                                                                                                         hgen.p(total + (total==1?" Approval has":" Approvals have") + " been Saved");
83                                                                                                 } else {
84                                                                                                         gui.writeError(trans, fa, hgen);
85                                                                                                 }
86                                                                                                 return fail2;
87                                                                                         }
88                                                                                 });
89                                                                         } catch (Exception e) {
90                                                                                 e.printStackTrace();
91                                                                         } finally {
92                                                                                 tt.done();
93                                                                         }
94                                                                 }
95
96                                                         hgen.br();
97                                                         if(fail) {
98                                                                 hgen.incr("a",true,"href="+lastPage).text("Try again").end();
99                                                         } else {
100                                                                 hgen.incr("a",true,"href="+Home.HREF).text("Home").end(); 
101                                                         }
102                                                         }
103                                                 }
104                                         });
105                                 }
106                         });
107         }
108 }