e3f2694abf124092cec5915f066f2a910daed647
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * eCOMP Portal SDK
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
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  * ================================================================================
19  */
20 package org.openecomp.portalsdk.analytics.scheduler;
21
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;
38
39 import org.openecomp.portalsdk.analytics.error.ReportSQLException;
40 import org.openecomp.portalsdk.analytics.system.DbUtils;
41
42 //import oracle.jdbc.*;
43 //import oracle.sql.BLOB;
44
45
46 public class SchedulerUtil {
47         
48         private  Connection conn = null;
49         
50         protected Connection getConnection() {
51                 return conn;
52         }
53         
54         protected void setConnection(Connection _conn) {
55                 conn = _conn;
56         }
57         
58         protected  Connection init() throws SQLException, ReportSQLException{
59                 if(conn != null)
60                         return conn;
61             conn = DbUtils.getConnection();
62                 return conn;
63         }
64         
65         protected  void closeConnection() throws SQLException {
66                 if(conn != null) conn.close();
67         }
68         
69     public  void insertOrUpdate(String sql) throws SQLException, ReportSQLException {
70         
71         Statement stat = null;
72                 try{
73                 //conn = getConnection();
74                         stat = conn.createStatement();
75                         stat.executeUpdate(sql);
76                         
77                 } finally{
78                         stat.close();
79                         //conn.close();
80                 }
81         }
82     
83     public  void updateBinaryStream(String sql, BigDecimal id, InputStream is, int size) throws SQLException, ReportSQLException, IOException {
84         
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 ");
89
90         }
91         
92         PreparedStatement stat = null;
93         try {
94                 stat = conn.prepareStatement(sql);
95                 stat.setBigDecimal(2, id);
96                 stat.setBinaryStream(1, is, size);
97                 stat.executeUpdate();
98         
99         } finally{
100                         stat.close();
101                 }
102                 
103     }
104     
105     /*public  void updateBlob(String sql, BigDecimal id, InputStream is, int size) throws SQLException, ReportSQLException, IOException {
106         PreparedStatement stat = null;
107         OutputStream out = null;
108         BLOB blob = null;
109         try {
110                 stat = conn.prepareStatement(sql);
111                 blob = BLOB.createTemporary(conn,false, BLOB.DURATION_SESSION);
112                 out = blob.getBinaryOutputStream();
113
114                 int read;
115                 while((read = is.read()) != -1) {
116                         out.write(read);
117                 }
118                 out.flush();
119                 
120                 stat.setBigDecimal(2, id);
121                 stat.setBlob(1, blob);
122                 stat.executeUpdate();
123         
124         }
125         catch (SQLException sqL) {
126                 sqL.printStackTrace();
127         }
128         finally{
129                 out.close();
130                 stat.close();
131                 }
132                 
133     }*/
134     
135         
136     public  void insertOrUpdateWithPrepared(String sql, List<Object> params, List<Integer> types) throws SQLException, ReportSQLException {
137         
138         PreparedStatement stat = null;
139                 try{
140                 //conn = getConnection();
141                         stat = conn.prepareStatement(sql);
142                         conn.getMetaData();
143                         int i2;
144                         int paramLength = params.size();
145                         for(int i = 0 ; i< paramLength ; i++) {
146                                 i2 = i+1;
147                                 Object param = params.get(i);
148                                 int type = types.get(i);
149                                 
150                                 if(param.equals("NULL")) {
151                                         stat.setNull(i2, type);
152                                 }
153                                 else if(type == Types.VARCHAR) {
154                                         stat.setString(i2, (String)param);
155                                 }
156                                 else if(type == Types.INTEGER) {
157                                         stat.setInt(i2, (Integer)param);        
158                                 }
159                                 else if(type == Types.NUMERIC) {
160                                         stat.setLong(i2, (Long)param);  
161                                 }
162                                 else if(type == Types.DOUBLE) {
163                                         stat.setDouble(i2, (Double)param);      
164                                 }
165                                 else if(type == Types.DATE) {
166                                         stat.setDate(i2, (java.sql.Date)param); 
167                                 }
168                                 else if(type == Types.TIMESTAMP) {
169                                         stat.setTimestamp(i2, (java.sql.Timestamp)param);       
170                                 }
171                                 else if(type == Types.BIGINT) {
172                                         stat.setBigDecimal(i2, (BigDecimal)param);
173                                 }
174                                 else 
175                                         throw new SQLException("Unidentified Object; Please contact admin and have this method updated with the current object type");
176                                 
177                         }
178                         
179                         stat.executeUpdate();
180                         
181                 } finally{
182                         stat.close();
183                         //conn.close();
184                 }
185         }
186     
187     
188         public  Object getSingleResult(String sql, String fieldname) throws SQLException, ReportSQLException{
189         
190                 Statement stat = null;
191                 ResultSet rs = null;
192                 Object o=null;
193                 try{
194                         //conn = getConnection();
195                         stat = conn.createStatement();
196                         rs = stat.executeQuery(sql);
197                         
198                         while (rs.next()) {
199                                 o = rs.getObject(fieldname);
200                         }
201                 }
202                 catch(SQLException sqlE){
203                         sqlE.printStackTrace();
204                 }
205                 
206                 finally{
207                            if(rs!=null) 
208                                         rs.close();
209                            if(stat!=null)
210                                 stat.close();
211                         //conn.close();
212                 }
213         return o;
214     }
215         
216         public  InputStream getDBStream(String sql, String fieldname) throws SQLException, ReportSQLException, IOException{
217                 
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 ");
222
223                 }
224                 
225                 
226                         Statement stat = null;
227                         ResultSet rs = null;
228                         InputStream o=null;
229                         try{
230                                 //conn = getConnection();
231                                 stat = conn.createStatement();
232                                 rs = stat.executeQuery(sql);
233                                 
234                                 while (rs.next()) {
235                                         o = rs.getBinaryStream(fieldname);
236                                 }
237                         }
238                         catch(SQLException sqlE){
239                                 sqlE.printStackTrace();
240                         }
241                         
242                         finally{
243                                    if(rs!=null) 
244                                                 rs.close();
245                                    if(stat!=null)
246                                         stat.close();
247                                 //conn.close();
248                         }
249                 return o;
250             }
251
252                 /*public  InputStream getDBBlob(String sql, String fieldname) throws SQLException, ReportSQLException, IOException{
253                         
254                         
255                         Statement stat = null;
256                         ResultSet rs = null;
257                         BLOB blob=null;
258                         ByteArrayInputStream in = null;
259                         try{
260                                 stat = conn.createStatement();
261                                 rs = stat.executeQuery(sql);
262                                 
263                                 if (rs.next()) {
264                                         blob = ((OracleResultSet) rs).getBLOB(fieldname);
265                                     in = new ByteArrayInputStream(blob.getBytes(1,(int)blob.length()));
266                                     
267                                 }
268                         }
269                         catch(SQLException sqlE){
270                                 sqlE.printStackTrace();
271                         }
272                         
273                         finally{
274                                    if(rs!=null) 
275                                                 rs.close();
276                                    if(stat!=null)
277                                         stat.close();
278                                 //conn.close();
279                         }
280                         return in;
281                 }
282         */
283         
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);
288                 
289                 
290                 while (rs.next()) {
291                         executor.execute(rs);
292                 }
293                 
294                    if(rs!=null) 
295                                 rs.close();
296                    if(stat!=null)
297                         stat.close();
298                 //conn.close();
299         }
300
301         interface Executor{
302                 public void execute(ResultSet rs) throws SQLException;
303         }
304         
305         
306         
307         
308         
309         
310         public static Date trunc_hour(Date v_date) {
311
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();
318         }
319
320         public static Date add_hours(Date v_date, int i) {
321
322                 Calendar cal = Calendar.getInstance();
323                 cal.setTime(v_date);
324                 cal.add(Calendar.HOUR, i);
325                 return cal.getTime();
326         }
327
328         public static Date add_months(Date v_date, int i) {
329
330                 Calendar cal = Calendar.getInstance();
331                 cal.setTime(v_date);
332                 cal.add(Calendar.MONTH, i);
333                 return cal.getTime();
334         }
335
336         public static Date add_days(Date v_date, int i) {
337
338                 Calendar cal = Calendar.getInstance();
339                 cal.setTime(v_date);
340                 cal.add(Calendar.DATE, i);
341                 return cal.getTime();
342         }
343
344         public static Date to_date(String input, String format) {
345
346                 Date date = null;
347                 try {
348                         date = new SimpleDateFormat(format, Locale.ENGLISH).parse(input);
349                 } catch (Exception e) {
350                 }
351                 return date;
352         }
353         
354         public static String to_date_str(Date input, String format) {
355
356                 String date = null;
357                 try {
358                         date = new SimpleDateFormat(format, Locale.ENGLISH).format(input);
359                 } catch (Exception e) {
360                 }
361                 return date;
362         }
363         
364         public static String[] cr_dissecturl(String formfields, String delimiter){
365                 if(formfields == null || formfields.isEmpty())
366                         return new String[]{};
367                 return formfields.split("&");
368         }
369 }