Remove Tabs, per Jococo
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / approvalsets / ApprovalSet.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 package org.onap.aaf.auth.batch.approvalsets;
22
23 import java.nio.ByteBuffer;
24 import java.util.ArrayList;
25 import java.util.GregorianCalendar;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Set;
29
30 import org.onap.aaf.auth.dao.cass.ApprovalDAO;
31 import org.onap.aaf.auth.dao.cass.FutureDAO;
32 import org.onap.aaf.auth.env.AuthzTrans;
33 import org.onap.aaf.auth.layer.Result;
34 import org.onap.aaf.cadi.CadiException;
35 import org.onap.aaf.misc.env.util.Chrono;
36
37 public class ApprovalSet {
38     private DataView dataview;
39     protected FutureDAO.Data fdd;
40     protected List<ApprovalDAO.Data> ladd;
41     
42     public ApprovalSet(final GregorianCalendar start, final String target, final DataView dv) {
43         dataview = dv;
44         fdd = new FutureDAO.Data();
45         fdd.id = Chrono.dateToUUID(System.currentTimeMillis());
46         fdd.target = target;
47         fdd.start = start.getTime();
48         ladd = new ArrayList<>();
49     }
50     
51     protected void setConstruct(final ByteBuffer bytes) {
52         fdd.construct = bytes;
53     }
54
55     protected void setMemo(final String memo) {
56         fdd.memo = memo;
57     }
58     
59     protected void setExpires(final GregorianCalendar expires) {
60         fdd.expires = expires.getTime();
61     }
62     
63     public Result<Void> write(AuthzTrans trans) {
64         StringBuilder errs = null;
65         if(ladd == null || ladd.isEmpty()) {
66             errs = new StringBuilder("No Approvers for ");
67             errs .append(fdd.memo);
68         } else {
69             Result<FutureDAO.Data> rf = dataview.insert(trans, fdd);
70             if(rf.notOK()) {
71                 errs = new StringBuilder();
72                 errs.append(rf.errorString());
73             } else {
74                 for(ApprovalDAO.Data add : ladd) {
75                     Result<ApprovalDAO.Data> af = dataview.insert(trans, add);
76                     if(af.notOK()) {
77                         if(errs==null) {
78                             errs = new StringBuilder();
79                         } else {
80                             errs.append('\n');
81                         }
82                         errs.append(af.errorString());
83                     }
84                 }
85             }
86         }
87         return errs==null?Result.ok():Result.err(Result.ERR_Backend,errs.toString());
88     }
89
90     public boolean hasApprovals() {
91         return !ladd.isEmpty();
92     }
93     
94     public Set<String> approvers() {
95         Set<String> rv = new HashSet<>();
96         for(ApprovalDAO.Data app : ladd) {
97             rv.add(app.approver);
98         }
99         return rv;
100     }
101 }