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