Upgrade to latest oparent
[aaf/authz.git] / authz-batch / src / main / java / com / att / authz / CassBatch.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz;
5
6 import java.io.IOException;
7
8 import com.att.authz.env.AuthzTrans;
9 import org.onap.aaf.inno.env.APIException;
10 import org.onap.aaf.inno.env.Env;
11 import org.onap.aaf.inno.env.TimeTaken;
12 import org.onap.aaf.inno.env.impl.Log4JLogTarget;
13 import com.datastax.driver.core.ResultSet;
14 import com.datastax.driver.core.exceptions.InvalidQueryException;
15
16 public abstract class CassBatch extends Batch {
17
18         protected CassBatch(AuthzTrans trans, String log4JName) throws APIException, IOException {
19                 super(trans.env());
20                 // Flow all Env Logs to Log4j
21                 Log4JLogTarget.setLog4JEnv(log4JName, env);
22                 
23                 TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
24                 try {
25                         session = cluster.connect();
26                 } finally {
27                         tt.done();
28                 }
29         }
30
31         @Override
32         protected void _close(AuthzTrans trans) {
33             session.close();
34                 trans.info().log("Closed Session");
35         }
36
37         public ResultSet executeQuery(String cql) {
38                 return executeQuery(cql,"");
39         }
40
41         public ResultSet executeQuery(String cql, String extra) {
42                 if(isDryRun() && !cql.startsWith("SELECT")) {
43                         if(extra!=null)env.info().log("Would query" + extra + ": " + cql);
44                 } else {
45                         if(extra!=null)env.info().log("query" + extra + ": " + cql);
46                         try {
47                                 return session.execute(cql);
48                         } catch (InvalidQueryException e) {
49                                 if(extra==null) {
50                                         env.info().log("query: " + cql);
51                                 }
52                                 throw e;
53                         }
54                 } 
55                 return null;
56         }
57
58 }