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