5d720c2f63bae09b38ffe0d4a89f9e3f27b4d067
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / approvalsets / Pending.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.text.ParseException;
24 import java.util.Date;
25 import java.util.List;
26
27 import org.onap.aaf.cadi.util.CSV.Writer;
28 import org.onap.aaf.misc.env.util.Chrono;
29
30 public class Pending {
31         public static final String REMIND = "remind";
32         
33         int qty;
34         boolean hasNew;
35         Date earliest;
36         
37         /**
38          * Use this constructor to indicate when last Notified
39          * @param last_notified
40          */
41         public Pending(Date last_notified) {
42                 qty = 1;
43                 hasNew = last_notified==null;
44                 earliest = last_notified;
45         }
46
47         /**
48          * Create from CSV Row
49          * @param row
50          * @throws ParseException
51          */
52         public Pending(List<String> row) throws ParseException {
53                 hasNew = Boolean.parseBoolean(row.get(2));
54                 String d = row.get(3);
55                 if(d==null || d.isEmpty()) {
56                         earliest = null;
57                 } else {
58                         earliest = Chrono.dateOnlyFmt.parse(d);
59                 }
60                 qty = Integer.parseInt(row.get(4));
61         }
62
63         /**
64          *  Write CSV Row
65          * @param approveCW
66          * @param key
67          */
68         public void row(Writer approveCW, String key) {
69                 approveCW.row(REMIND,key,hasNew,Chrono.dateOnlyStamp(earliest),qty);
70         }
71
72         public void inc() {
73                 ++qty;
74         }
75         
76         public void inc(Pending value) {
77                 qty+=value.qty;
78                 if(earliest==null) {
79                         earliest = value.earliest;
80                 } else if(value.earliest!=null && value.earliest.before(earliest)) {
81                         earliest = value.earliest;
82                 }
83         }
84
85         public void earliest(Date lastnotified) {
86                 if(lastnotified==null) {
87                         hasNew=true;
88                 } else if (earliest==null || lastnotified.before(earliest)) {
89                         earliest = lastnotified;
90                 }
91         }
92         
93         public int qty() {
94                 return qty;
95         }
96         
97         public Date earliest() {
98                 return earliest;
99         }
100         
101         public boolean newApprovals() {
102                 return hasNew;
103         }
104
105         public static Pending create() {
106                 return new Pending((Date)null);
107         }
108
109 }