Multiple Sonar Fixes - MiscID.java
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / helpers / MiscID.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Modifications Copyright (C) 2019 IBM.
8  * ===========================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END====================================================
21  *
22  */
23
24 package org.onap.aaf.auth.batch.helpers;
25
26 import java.util.Map;
27 import java.util.TreeMap;
28
29 import org.onap.aaf.auth.batch.BatchException;
30 import org.onap.aaf.misc.env.Env;
31 import org.onap.aaf.misc.env.TimeTaken;
32 import org.onap.aaf.misc.env.Trans;
33
34 import com.datastax.driver.core.ResultSet;
35 import com.datastax.driver.core.Row;
36 import com.datastax.driver.core.Session;
37 import com.datastax.driver.core.SimpleStatement;
38 import com.datastax.driver.core.Statement;
39
40 public class MiscID  {
41     public static final TreeMap<String,MiscID> data = new TreeMap<>();
42     /*
43     Sample Record
44     aad890|mj9030|20040902|20120207
45
46     **** Field Definitions ****
47     MISCID - AT&T Miscellaneous ID - Non-User ID (Types: Internal Mechanized ID, External Mechanized ID, Datagate ID, Customer ID, Vendor ID, Exchange Mail ID, CLEC ID, Specialized ID, Training ID)
48     SPONSOR_ATTUID - ATTUID of MiscID Sponsor (Owner)
49     CREATE_DATE - Date when MiscID was created 
50     LAST_RENEWAL_DATE - Date when MiscID Sponsorship was last renewed
51     */
52     public String id;
53     public String sponsor;
54     public String created;
55     public String renewal;
56
57     private static final String FIELD_STRING = "id,created,sponsor,renewal";
58     
59     /**
60      * Load a Row of Strings (from CSV file).
61      * 
62      * Be CAREFUL that the Row lists match the Fields above!!!  If this changes, change
63      * 1) This Object
64      * 2) DB "suits.cql"
65      * 3) Alter existing Tables
66      * @param row
67      * @throws BatchException
68      */
69     public void set(String[] row ) throws BatchException {
70         if (row.length<4) {
71             throw new BatchException("Row of MiscID_XRef is too short");
72         }
73         id      = row[0];
74         sponsor = row[1];
75         created = row[2];
76         renewal = row[3];
77     }
78
79     public void set(Row row) {
80         id      = row.getString(0);
81         sponsor = row.getString(1);
82         created = row.getString(2);
83         renewal = row.getString(3);
84     }
85     
86
87     public static void load(Trans trans, Session session ) {
88         load(trans, session,"SELECT " + FIELD_STRING + " FROM authz.miscid;",data);
89     }
90
91     public static void load(Trans trans, Session session, Map<String,MiscID> map ) {
92         load(trans, session,"SELECT " + FIELD_STRING + " FROM authz.miscid;",map);
93     }
94
95     public static void loadOne(Trans trans, Session session, String id ) {
96         load(trans, session,"SELECT " + FIELD_STRING + " FROM authz.miscid WHERE id ='" + id + "';", data);
97     }
98
99     public static void load(Trans trans, Session session, String query, Map<String,MiscID> map) {
100         trans.info().log( "query: " + query );
101         TimeTaken tt = trans.start("Read MiscID", Env.REMOTE);
102        
103         ResultSet results;
104         try {
105             Statement stmt = new SimpleStatement( query );
106             results = session.execute(stmt);
107         } finally {
108             tt.done();
109         }
110         int count = 0;
111         try {
112             tt = trans.start("Load Map", Env.SUB);
113             try {
114                 for ( Row row : results.all()) {
115                     MiscID miscID = new MiscID();
116                     miscID.set(row);
117                     data.put(miscID.id,miscID);
118                     ++count;
119                 }
120             } finally {
121                 tt.done();
122             }
123         } finally {
124             trans.info().log("Found",count,"miscID records");
125         }
126     }
127
128     /* (non-Javadoc)
129      * @see java.lang.Object#hashCode()
130      */
131     @Override
132     public int hashCode() {
133         return id.hashCode();
134     }
135
136     /* (non-Javadoc)
137      * @see java.lang.Object#equals(java.lang.Object)
138      */
139     @Override
140     public boolean equals(Object obj) {
141         if (null!=obj && obj instanceof MiscID) {
142             return id.equals(((MiscID)obj).id);
143         }
144         return false;
145     }
146
147     public StringBuilder insertStmt() {
148         StringBuilder sb = new StringBuilder("INSERT INTO authz.miscid (");
149         sb.append(FIELD_STRING);
150         sb.append(") VALUES ('");
151         sb.append(id);
152         sb.append("','");
153         sb.append(sponsor);
154         sb.append("','");
155         sb.append(created);
156         sb.append("','");
157         sb.append(renewal);
158         sb.append("')");
159         return sb;
160     }
161     
162     public StringBuilder updateStmt(MiscID source) {
163         StringBuilder sb = null;
164         if (id.equals(source.id)) {
165             sb = addField(sb,"sponser",sponsor,source.sponsor);
166             sb = addField(sb,"created",created,source.created);
167             sb = addField(sb,"renewal",renewal,source.renewal);
168         }
169         if (sb!=null) {
170             sb.append(" WHERE id='");
171             sb.append(id);
172             sb.append('\'');
173         }
174         return sb;
175     }
176
177     private StringBuilder addField(StringBuilder sb, String name, String a, String b) {
178         if (!a.equals(b)) {
179             if (sb==null) {
180                 sb = new StringBuilder("UPDATE authz.miscid SET ");        
181             } else {
182                 sb.append(',');
183             }
184             sb.append(name);
185             sb.append("='");
186             sb.append(b);
187             sb.append('\'');
188         }
189         return sb;
190     }
191
192         
193 }