AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / 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  * 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
22 package org.onap.aaf.auth.helpers;
23
24 import java.util.Map;
25 import java.util.TreeMap;
26
27 import org.onap.aaf.auth.BatchException;
28 import org.onap.aaf.misc.env.Env;
29 import org.onap.aaf.misc.env.TimeTaken;
30 import org.onap.aaf.misc.env.Trans;
31
32 import com.datastax.driver.core.ResultSet;
33 import com.datastax.driver.core.Row;
34 import com.datastax.driver.core.Session;
35 import com.datastax.driver.core.SimpleStatement;
36 import com.datastax.driver.core.Statement;
37
38 public class MiscID  {
39         public static final TreeMap<String,MiscID> data = new TreeMap<String,MiscID>();
40         /*
41         Sample Record
42         aad890|mj9030|20040902|20120207
43
44         **** Field Definitions ****
45         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)
46         SPONSOR_ATTUID - ATTUID of MiscID Sponsor (Owner)
47         CREATE_DATE - Date when MiscID was created 
48         LAST_RENEWAL_DATE - Date when MiscID Sponsorship was last renewed
49         */
50         public String id,sponsor,created,renewal;
51
52         private static final String fieldString = "id,created,sponsor,renewal";
53         
54         /**
55          * Load a Row of Strings (from CSV file).
56          * 
57          * Be CAREFUL that the Row lists match the Fields above!!!  If this changes, change
58          * 1) This Object
59          * 2) DB "suits.cql"
60          * 3) Alter existing Tables
61          * @param row
62          * @throws BatchException 
63          * @throws IllegalAccessException 
64          * @throws IllegalArgumentException 
65          */
66         public void set(String row []) throws BatchException {
67                 if(row.length<4) {throw new BatchException("Row of MiscID_XRef is too short");}
68                 id      = row[0];
69                 sponsor = row[1];
70                 created = row[2];
71                 renewal = row[3];
72         }
73
74         public void set(Row row) {
75                 id      = row.getString(0);
76                 sponsor = row.getString(1);
77                 created = row.getString(2);
78                 renewal = row.getString(3);
79         }
80         
81
82         public static void load(Trans trans, Session session ) {
83                 load(trans, session,"SELECT " + fieldString + " FROM authz.miscid;",data);
84         }
85
86         public static void load(Trans trans, Session session, Map<String,MiscID> map ) {
87                 load(trans, session,"SELECT " + fieldString + " FROM authz.miscid;",map);
88         }
89
90         public static void loadOne(Trans trans, Session session, String id ) {
91                 load(trans, session,"SELECT " + fieldString + " FROM authz.miscid WHERE id ='" + id + "';", data);
92         }
93
94         public static void load(Trans trans, Session session, String query, Map<String,MiscID> map) {
95         trans.info().log( "query: " + query );
96         TimeTaken tt = trans.start("Read MiscID", Env.REMOTE);
97        
98         ResultSet results;
99                 try {
100                 Statement stmt = new SimpleStatement( query );
101                 results = session.execute(stmt);
102         } finally {
103                 tt.done();
104         }
105                 int count = 0;
106         try {
107                 tt = trans.start("Load Map", Env.SUB);
108                 try {
109                         for( Row row : results.all()) {
110                                 MiscID miscID = new MiscID();
111                                 miscID.set(row);
112                                 data.put(miscID.id,miscID);
113                                 ++count;
114                         }
115                         } finally {
116                         tt.done();
117                 }
118         } finally {
119                 trans.info().log("Found",count,"miscID records");
120         }
121         }
122
123         /* (non-Javadoc)
124          * @see java.lang.Object#hashCode()
125          */
126         @Override
127         public int hashCode() {
128                 return id.hashCode();
129         }
130
131         /* (non-Javadoc)
132          * @see java.lang.Object#equals(java.lang.Object)
133          */
134         @Override
135         public boolean equals(Object obj) {
136                 if(obj!=null && obj instanceof MiscID) {
137                         return id.equals(((MiscID)obj).id);
138                 }
139                 return false;
140         }
141
142         public StringBuilder insertStmt() throws IllegalArgumentException, IllegalAccessException {
143                 StringBuilder sb = new StringBuilder("INSERT INTO authz.miscid (");
144                 sb.append(fieldString);
145                 sb.append(") VALUES ('");
146                 sb.append(id);
147                 sb.append("','");
148                 sb.append(sponsor);
149                 sb.append("','");
150                 sb.append(created);
151                 sb.append("','");
152                 sb.append(renewal);
153                 sb.append("')");
154                 return sb;
155         }
156         
157         public StringBuilder updateStmt(MiscID source) {
158                 StringBuilder sb = null;
159                 if(id.equals(source.id)) {
160                         sb = addField(sb,"sponser",sponsor,source.sponsor);
161                         sb = addField(sb,"created",created,source.created);
162                         sb = addField(sb,"renewal",renewal,source.renewal);
163                 }
164                 if(sb!=null) {
165                         sb.append(" WHERE id='");
166                         sb.append(id);
167                         sb.append('\'');
168                 }
169                 return sb;
170         }
171
172         private StringBuilder addField(StringBuilder sb, String name, String a, String b) {
173                 if(!a.equals(b)) {
174                         if(sb==null) {
175                                 sb = new StringBuilder("UPDATE authz.miscid SET ");             
176                         } else {
177                                 sb.append(',');
178                         }
179                         sb.append(name);
180                         sb.append("='");
181                         sb.append(b);
182                         sb.append('\'');
183                 }
184                 return sb;
185         }
186
187                 
188 }