Merge "Fixed major sonar issues in CMService"
[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) 2018 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      * @throws IllegalAccessException 
69      * @throws IllegalArgumentException 
70      */
71     public void set(String[] row ) throws BatchException {
72         if (row.length<4) {
73             throw new BatchException("Row of MiscID_XRef is too short");
74         }
75         id      = row[0];
76         sponsor = row[1];
77         created = row[2];
78         renewal = row[3];
79     }
80
81     public void set(Row row) {
82         id      = row.getString(0);
83         sponsor = row.getString(1);
84         created = row.getString(2);
85         renewal = row.getString(3);
86     }
87     
88
89     public static void load(Trans trans, Session session ) {
90         load(trans, session,"SELECT " + FIELD_STRING + " FROM authz.miscid;",data);
91     }
92
93     public static void load(Trans trans, Session session, Map<String,MiscID> map ) {
94         load(trans, session,"SELECT " + FIELD_STRING + " FROM authz.miscid;",map);
95     }
96
97     public static void loadOne(Trans trans, Session session, String id ) {
98         load(trans, session,"SELECT " + FIELD_STRING + " FROM authz.miscid WHERE id ='" + id + "';", data);
99     }
100
101     public static void load(Trans trans, Session session, String query, Map<String,MiscID> map) {
102         trans.info().log( "query: " + query );
103         TimeTaken tt = trans.start("Read MiscID", Env.REMOTE);
104        
105         ResultSet results;
106         try {
107             Statement stmt = new SimpleStatement( query );
108             results = session.execute(stmt);
109         } finally {
110             tt.done();
111         }
112         int count = 0;
113         try {
114             tt = trans.start("Load Map", Env.SUB);
115             try {
116                 for ( Row row : results.all()) {
117                     MiscID miscID = new MiscID();
118                     miscID.set(row);
119                     data.put(miscID.id,miscID);
120                     ++count;
121                 }
122             } finally {
123                 tt.done();
124             }
125         } finally {
126             trans.info().log("Found",count,"miscID records");
127         }
128     }
129
130     /* (non-Javadoc)
131      * @see java.lang.Object#hashCode()
132      */
133     @Override
134     public int hashCode() {
135         return id.hashCode();
136     }
137
138     /* (non-Javadoc)
139      * @see java.lang.Object#equals(java.lang.Object)
140      */
141     @Override
142     public boolean equals(Object obj) {
143         if (obj!=null && obj instanceof MiscID) {
144             return id.equals(((MiscID)obj).id);
145         }
146         return false;
147     }
148
149     public StringBuilder insertStmt() throws IllegalArgumentException, IllegalAccessException {
150         StringBuilder sb = new StringBuilder("INSERT INTO authz.miscid (");
151         sb.append(FIELD_STRING);
152         sb.append(") VALUES ('");
153         sb.append(id);
154         sb.append("','");
155         sb.append(sponsor);
156         sb.append("','");
157         sb.append(created);
158         sb.append("','");
159         sb.append(renewal);
160         sb.append("')");
161         return sb;
162     }
163     
164     public StringBuilder updateStmt(MiscID source) {
165         StringBuilder sb = null;
166         if (id.equals(source.id)) {
167             sb = addField(sb,"sponser",sponsor,source.sponsor);
168             sb = addField(sb,"created",created,source.created);
169             sb = addField(sb,"renewal",renewal,source.renewal);
170         }
171         if (sb!=null) {
172             sb.append(" WHERE id='");
173             sb.append(id);
174             sb.append('\'');
175         }
176         return sb;
177     }
178
179     private StringBuilder addField(StringBuilder sb, String name, String a, String b) {
180         if (!a.equals(b)) {
181             if (sb==null) {
182                 sb = new StringBuilder("UPDATE authz.miscid SET ");        
183             } else {
184                 sb.append(',');
185             }
186             sb.append(name);
187             sb.append("='");
188             sb.append(b);
189             sb.append('\'');
190         }
191         return sb;
192     }
193
194         
195 }