2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalsdk.analytics.scheduler;
22 import java.io.ByteArrayInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.math.BigDecimal;
27 import java.sql.Connection;
28 import java.sql.PreparedStatement;
29 import java.sql.ResultSet;
30 import java.sql.SQLException;
31 import java.sql.Statement;
32 import java.sql.Types;
33 import java.text.SimpleDateFormat;
34 import java.util.Calendar;
35 import java.util.Date;
36 import java.util.List;
37 import java.util.Locale;
39 import org.openecomp.portalsdk.analytics.error.ReportSQLException;
40 import org.openecomp.portalsdk.analytics.system.DbUtils;
42 //import oracle.jdbc.*;
43 //import oracle.sql.BLOB;
46 public class SchedulerUtil {
48 private Connection conn = null;
50 protected Connection getConnection() {
54 protected void setConnection(Connection _conn) {
58 protected Connection init() throws SQLException, ReportSQLException{
61 conn = DbUtils.getConnection();
65 protected void closeConnection() throws SQLException {
66 if(conn != null) conn.close();
69 public void insertOrUpdate(String sql) throws SQLException, ReportSQLException {
71 Statement stat = null;
73 //conn = getConnection();
74 stat = conn.createStatement();
75 stat.executeUpdate(sql);
83 public void updateBinaryStream(String sql, BigDecimal id, InputStream is, int size) throws SQLException, ReportSQLException, IOException {
85 // cludge hack for oracle databases
86 if(conn.getMetaData().getDatabaseProductName().toLowerCase().contains("oracle")) {
87 /* updateBlob(sql,id,is,size);
88 */ throw new ReportSQLException("only maria db support for this ");
92 PreparedStatement stat = null;
94 stat = conn.prepareStatement(sql);
95 stat.setBigDecimal(2, id);
96 stat.setBinaryStream(1, is, size);
105 /*public void updateBlob(String sql, BigDecimal id, InputStream is, int size) throws SQLException, ReportSQLException, IOException {
106 PreparedStatement stat = null;
107 OutputStream out = null;
110 stat = conn.prepareStatement(sql);
111 blob = BLOB.createTemporary(conn,false, BLOB.DURATION_SESSION);
112 out = blob.getBinaryOutputStream();
115 while((read = is.read()) != -1) {
120 stat.setBigDecimal(2, id);
121 stat.setBlob(1, blob);
122 stat.executeUpdate();
125 catch (SQLException sqL) {
126 sqL.printStackTrace();
136 public void insertOrUpdateWithPrepared(String sql, List<Object> params, List<Integer> types) throws SQLException, ReportSQLException {
138 PreparedStatement stat = null;
140 //conn = getConnection();
141 stat = conn.prepareStatement(sql);
144 int paramLength = params.size();
145 for(int i = 0 ; i< paramLength ; i++) {
147 Object param = params.get(i);
148 int type = types.get(i);
150 if(param.equals("NULL")) {
151 stat.setNull(i2, type);
153 else if(type == Types.VARCHAR) {
154 stat.setString(i2, (String)param);
156 else if(type == Types.INTEGER) {
157 stat.setInt(i2, (Integer)param);
159 else if(type == Types.NUMERIC) {
160 stat.setLong(i2, (Long)param);
162 else if(type == Types.DOUBLE) {
163 stat.setDouble(i2, (Double)param);
165 else if(type == Types.DATE) {
166 stat.setDate(i2, (java.sql.Date)param);
168 else if(type == Types.TIMESTAMP) {
169 stat.setTimestamp(i2, (java.sql.Timestamp)param);
171 else if(type == Types.BIGINT) {
172 stat.setBigDecimal(i2, (BigDecimal)param);
175 throw new SQLException("Unidentified Object; Please contact admin and have this method updated with the current object type");
179 stat.executeUpdate();
188 public Object getSingleResult(String sql, String fieldname) throws SQLException, ReportSQLException{
190 Statement stat = null;
194 //conn = getConnection();
195 stat = conn.createStatement();
196 rs = stat.executeQuery(sql);
199 o = rs.getObject(fieldname);
202 catch(SQLException sqlE){
203 sqlE.printStackTrace();
216 public InputStream getDBStream(String sql, String fieldname) throws SQLException, ReportSQLException, IOException{
218 // cludge hack for oracle databases
219 if(conn.getMetaData().getDatabaseProductName().toLowerCase().contains("oracle")) {
220 /*return getDBBlob(sql,fieldname);*/
221 throw new ReportSQLException("only maria db support for this ");
226 Statement stat = null;
230 //conn = getConnection();
231 stat = conn.createStatement();
232 rs = stat.executeQuery(sql);
235 o = rs.getBinaryStream(fieldname);
238 catch(SQLException sqlE){
239 sqlE.printStackTrace();
252 /*public InputStream getDBBlob(String sql, String fieldname) throws SQLException, ReportSQLException, IOException{
255 Statement stat = null;
258 ByteArrayInputStream in = null;
260 stat = conn.createStatement();
261 rs = stat.executeQuery(sql);
264 blob = ((OracleResultSet) rs).getBLOB(fieldname);
265 in = new ByteArrayInputStream(blob.getBytes(1,(int)blob.length()));
269 catch(SQLException sqlE){
270 sqlE.printStackTrace();
284 public void getAndExecute(String sql, Executor executor) throws SQLException, ReportSQLException{
285 //Connection conn = getConnection();
286 Statement stat = conn.createStatement();
287 ResultSet rs = stat.executeQuery(sql);
291 executor.execute(rs);
302 public void execute(ResultSet rs) throws SQLException;
310 public static Date trunc_hour(Date v_date) {
312 Calendar calendar = Calendar.getInstance();
313 calendar.setTime(v_date);
314 calendar.set(Calendar.MILLISECOND, 0);
315 calendar.set(Calendar.SECOND, 0);
316 calendar.set(Calendar.MINUTE, 0);
317 return calendar.getTime();
320 public static Date add_hours(Date v_date, int i) {
322 Calendar cal = Calendar.getInstance();
324 cal.add(Calendar.HOUR, i);
325 return cal.getTime();
328 public static Date add_months(Date v_date, int i) {
330 Calendar cal = Calendar.getInstance();
332 cal.add(Calendar.MONTH, i);
333 return cal.getTime();
336 public static Date add_days(Date v_date, int i) {
338 Calendar cal = Calendar.getInstance();
340 cal.add(Calendar.DATE, i);
341 return cal.getTime();
344 public static Date to_date(String input, String format) {
348 date = new SimpleDateFormat(format, Locale.ENGLISH).parse(input);
349 } catch (Exception e) {
354 public static String to_date_str(Date input, String format) {
358 date = new SimpleDateFormat(format, Locale.ENGLISH).format(input);
359 } catch (Exception e) {
364 public static String[] cr_dissecturl(String formfields, String delimiter){
365 if(formfields == null || formfields.isEmpty())
366 return new String[]{};
367 return formfields.split("&");