Revision 9:16125cca68e4 src/my/com/upass/db/DBOperations.java

View differences:

src/my/com/upass/db/DBOperations.java
1 1
package my.com.upass.db;
2 2

  
3
import java.math.BigDecimal;
4
import java.sql.CallableStatement;
5 3
import java.sql.Connection;
6 4
import java.sql.PreparedStatement;
7 5
import java.sql.ResultSet;
8 6
import java.sql.SQLException;
9 7
import java.sql.Statement;
10
import java.util.Date;
11 8

  
12 9
import my.com.upass.Constants;
13 10
import my.com.upass.TokenBean;
14 11
import my.com.upass.pojo.TacBean;
15
import my.com.upass.pojo.UserBean;
16 12
import net.penril.generic.hibernate.HibernateUtils;
17 13

  
18
import org.apache.log4j.Logger;
19 14

  
20
public class DBOperations {
15
public class DBOperations extends MinimalDbOperations {
21 16

  
22
	public static final Logger logger = Logger.getLogger (DBOperations.class);
23
	public Connection conn = null;
24
	
25 17
	public DBOperations() {
26 18
		// constructor
27 19
		try {
......
84 76
		return conn;
85 77
	}
86 78
	
87
	public boolean isValidConnection() throws Exception {
88
		    if (this.conn == null || this.conn.isClosed()) { 
89
		    	System.out.println("ERR: Invalid connection!");
90
		    	return false; 
91
		    }
92
		    return true;
93
	}
94
	
95
	public void close() {
96
		// System.out.println("Close connection!");
97
		try {
98
			if (this.conn != null) {
99
				this.conn.close();
100
			}
101
		} catch (Exception e) {
102
			logger.error(getFullTrace(e));
103
		}
104
	}
105
	
106
	
107
	public ResultSet executeSQLSelect(PreparedStatement pstmt) throws Exception{
108
		
109
		ResultSet rs = null;
110
		try {
111
			if ( isValidConnection() ) {
112
				if (pstmt == null) 
113
					return null;
114
				rs = pstmt.executeQuery();
115
			} else {
116
				logger.info("ERR: executeSQL: Invalid DB Conn");
117
				return null;
118
			}// end conn not valid
119
			}catch(SQLException ex) {
120
				logger.error("EXC: executeSQL " + ex.getMessage());
121
				return null;
122
			}	
123
		return rs;
124
	}
125
	
126
	public int executeSQLUpdate(PreparedStatement pstmt) throws Exception {
127
		int rc = 0;
128
		try {
129
			if ( isValidConnection() ) {
130
				if (pstmt == null) 
131
					return 0;
132
				rc = pstmt.executeUpdate(); //rc is the row count for Insert/modi/dele
133
			} else {
134
				logger.info("ERR: executeSQLUpdate: Invalid DB Conn");
135
				return 0;
136
			}// end conn not valid
137
			}catch(SQLException ex) {
138
				logger.error("EXC: executeSQLUpdate " + ex.getMessage());
139
				//
140
				// rollback
141
				//
142
				try { 
143
					this.conn.rollback();
144
					logger.info("EXC:1 executeSQLUpdate :: rollback.");
145
				} catch(SQLException rbex) {
146
					logger.error("EXC:executeSQLUpdate :: rollback failed! " + rbex.getMessage());
147
				}
148
				// rollback completed			
149
				
150
				return 0;
151
			} finally {
152
				//
153
				//this.conn.close();
154
				//
155
			}
156
			
157
		return rc;
158
	}
159
	
160
	
161
	
162
	public static java.sql.Timestamp getCurrentSqlDate() {
163
		java.util.Date today = new java.util.Date();
164
		return new java.sql.Timestamp(today.getTime());
165
	}
166
	
167
	public static java.sql.Timestamp convertSqlDate(java.util.Date d) {
168
		if (d == null) {
169
			//d = new java.util.Date();
170
			return null;
171
		}
172
		return new java.sql.Timestamp(d.getTime());
173
	}
174
	
175 79
	public long getCurrSeqID(int mode) {
176 80
		
177 81

  
......
220 124
		return lVal;
221 125
	}
222 126
	
223
	public long getSqlServerSeqID() {
224

  
225
		// setup the unique ID
226
		BigDecimal nextValue = new BigDecimal(0);
227
		CallableStatement cstmt = null;
228
		try {
229
			cstmt = conn.prepareCall(//
230
					"{call nextval_SEQ_USER_ID(?)}");
231
			cstmt.registerOutParameter(1, java.sql.Types.BIGINT);
232
			cstmt.execute();
233
			nextValue = cstmt.getBigDecimal(1);
234
			logger.info("seq_user_id.value=" + nextValue);
235
		} catch (SQLException ex) {
236
			logger.error("SQLEXC: getSqlServerSeqID " + ex.getMessage());
237
		} finally {
238
			logger.info(": getSqlServerSeqID Final");
239
			try {
240
				cstmt.close();
241
			} catch (SQLException ex) {
242
				logger.error (getFullTrace (ex));
243
			}
244
		}
245
		return nextValue.longValue ();
246
	}
127
	
247 128

  
248 129
	///////////////////////////////////////////////////////////////////////
249 130
	// User Bean  
250 131
	///////////////////////////////////////////////////////////////////////
251 132
		
252
	/**
253
	 * 
254
	 */
255
	public UserBean getUserFromStore(String userAlias) {
256
		return getUserFromStore(userAlias, Constants.MODE_QUSERBEAN);
257
	}
258 133
	
259
	public UserBean getUserFromStore(String userAlias, int sqlMode) {
260
		
261
		String sqlStmt0 = "SELECT * FROM tb_am_user  WHERE u_user_alias = ?";
262
		String sqlStmt1 = "SELECT * FROM tb_am_user a, tb_am_tac b WHERE a.u_user_alias = ? AND a.u_user_id = b.t_user_id";
263
		String sqlStmt2 = "SELECT * FROM tb_am_user a, tb_am_vasco b WHERE a.u_user_alias = ? AND a.u_user_id = b.p_user_id";
264

  
265
		String sqlStmt = null;
266
		
267
		switch (sqlMode) {
268
			case Constants.MODE_QUSERBEAN:  sqlStmt = sqlStmt0; break;
269
			case Constants.MODE_QTACBEAN:  	sqlStmt = sqlStmt1; break;
270
			case Constants.MODE_QVASCOBEAN: sqlStmt = sqlStmt2; break;
271
		}
272
		
273
		
274
		ResultSet rs = null;
275
		PreparedStatement pstmt = null;
276
				
277
		UserBean ub = new UserBean();
278

  
279
		try {
280
			pstmt = this.conn.prepareStatement(sqlStmt);
281
			pstmt.setString(1, userAlias);
282
			rs = executeSQLSelect(pstmt);
283
      
284
			if (rs.next()) {
285
				ub.setUserID( rs.getLong("U_USER_ID") );
286
				ub.setUserAlias(rs.getString("U_USER_ALIAS"));
287
				ub.setUserType(rs.getInt("U_USER_TYPE"));
288
				ub.setDescription(rs.getString("U_USER_DESC"));
289
				ub.setUdateCreated( rs.getTimestamp("U_DATE_CREATED"));
290
				ub.setUdateLastUsed(rs.getTimestamp("U_DATE_LAST_USED"));
291
				ub.setUuseCount( rs.getInt("U_USE_COUNT"));
292
				ub.setUdateLastActivated(rs.getTimestamp("U_DATE_LAST_ACT"));
293
				ub.setUdateLastLocked(rs.getTimestamp("U_DATE_LAST_LOCK"));
294
				ub.setUstate(rs.getInt("U_USER_STATE"));
295
				ub.setUdateLockedFrom(rs.getTimestamp("U_DATE_LOCK_FROM"));
296
				ub.setUdateLockedTo(rs.getTimestamp("U_DATE_LOCK_TO"));
297
				ub.setPcipherText(rs.getString("P_PASSWD_CIPHERTEXT"));
298
				ub.setPstate(rs.getInt("P_PASSWD_STATE"));
299
				ub.setPdateCreated(rs.getTimestamp("P_DATE_GENERATED"));
300
				ub.setPdateFirstUsed(rs.getTimestamp("P_DATE_FIRST_USED"));
301
				ub.setPdateLastUsed(rs.getTimestamp("P_DATE_LAST_USED"));
302
				ub.setPuseCount(rs.getInt("P_USE_COUNT"));
303
				ub.setPerrorCount(rs.getInt("P_ERROR_COUNT"));
304
				ub.setPexpiredStatus(rs.getInt("P_EXPIRED_STATUS"));
305
				ub.setPdateExpired(rs.getTimestamp("P_DATE_EXPIRED"));
306
				ub.setPhistoryList(rs.getString("P_PASSWD_HIST"));
307
				
308
				
309
				switch (sqlMode) {
310
				case Constants.MODE_QUSERBEAN: 
311
					break;
312
				case Constants.MODE_QTACBEAN:  
313
					TacBean tb = new TacBean();
314
					tb.setCipherText(rs.getString("T_TAC_CIPHERTEXT"));
315
					tb.setState(rs.getInt("T_TAC_STATE"));
316
					tb.setDateCreated(rs.getTimestamp("T_DATE_GENERATED"));
317
					tb.setDateFirstUsed(rs.getTimestamp("T_DATE_FIRST_USED"));
318
					tb.setDateLastUsed(rs.getTimestamp("T_DATE_LAST_USED"));
319
					tb.setUseCount(rs.getInt("T_USE_COUNT"));
320
					tb.setErrorCount(rs.getInt("T_ERROR_COUNT"));
321
					break;
322
					
323
				case Constants.MODE_QVASCOBEAN: sqlStmt = sqlStmt2; 
324
					break;
325
			}
326
				
327
				
328
				
329
			} else { //no record found
330
				logger.info("ERR: no record found!");
331
				ub = null;
332
			} //
333
		} catch (SQLException ex) {
334
			logger.error("SQLEXC:1 getUserFromStore " + ex.getMessage());
335
			ub = null;
336
		} catch (Exception e) {
337
			logger.error("EXC:1 getUserFromStore " + e.getMessage());
338
			ub = null;
339
		} finally { // close database resources
340
			  try {
341
			      rs.close();
342
			      pstmt.close();
343
			      //this.conn.close();
344
			  } catch (SQLException ex) {
345
				  logger.error("SQLException:Final getUserFromStore " + ex.getMessage());
346
			  } catch (Exception e) {
347
				  logger.error("Exception:Final getUserFromStore " + e.getMessage());
348
			  }
349
		}//finally--
350

  
351
		return ub;
352
	}
353
	
354
	/**
355
	 * 
356
	 */
357
	public boolean updateUserToStore(UserBean ub) {
358
		
359
		boolean rc = false;
360
		String sqlStmt = "UPDATE tb_am_user SET " +
361
				"U_USER_TYPE=?, U_USER_DESC=?, U_DATE_LAST_USED=?, U_USE_COUNT=?, U_DATE_LAST_ACT=?, " +
362
				"U_DATE_LAST_LOCK=?, U_USER_STATE=?, U_DATE_LOCK_FROM=?, U_DATE_LOCK_TO=?, " +
363
				"P_PASSWD_CIPHERTEXT=?, P_PASSWD_STATE=?, P_DATE_GENERATED=?, P_DATE_FIRST_USED=?, " +
364
				"P_DATE_LAST_USED=?, P_USE_COUNT=?, P_ERROR_COUNT=?, P_EXPIRED_STATUS=?, P_DATE_EXPIRED=?, " +
365
				"P_PASSWD_HIST=? WHERE u_user_id = ?";
366
		int nRow = 0;
367
		
368
		// insert into DB
369
		PreparedStatement pstmt = null;
370
		try {
371
			pstmt = this.conn.prepareStatement(sqlStmt);
372
			pstmt.setInt(1,ub.getUserType());
373
			pstmt.setString(2, ub.getDescription());
374
			
375
			java.sql.Timestamp today = getCurrentSqlDate();
376
			pstmt.setTimestamp(3, today); //last used
377
			pstmt.setInt(4, ub.getUuseCount()+1);
378
			pstmt.setTimestamp(5, convertSqlDate(ub.getUdateLastActivated())); //last act
379
			pstmt.setTimestamp(6, convertSqlDate(ub.getUdateLastLocked())); //last lock
380
			
381
			pstmt.setInt(7,ub.getUstate());
382
			pstmt.setTimestamp(8, convertSqlDate(ub.getUdateLockedFrom())); // lock ft
383
			pstmt.setTimestamp(9, convertSqlDate(ub.getUdateLockedTo())); //lock to
384
			pstmt.setString(10, ub.getPcipherText());
385
			pstmt.setInt(11,ub.getPstate());
386
			pstmt.setTimestamp(12, convertSqlDate(ub.getUdateCreated() )); //generated
387
			pstmt.setTimestamp(13, convertSqlDate(ub.getPdateFirstUsed()) ); //1st used
388
			pstmt.setTimestamp(14, convertSqlDate(ub.getPdateLastUsed()) ); //1ast used
389
			pstmt.setInt(15,ub.getPuseCount());
390
			pstmt.setInt(16,ub.getPerrorCount());
391
			pstmt.setInt(17,ub.getPexpiredStatus());
392
			pstmt.setTimestamp(18, convertSqlDate(ub.getPdateExpired()) );
393
			pstmt.setString(19, ub.getPhistoryList());
394
			
395
			pstmt.setLong(20, ub.getUserID());
396
			
397
			nRow = executeSQLUpdate(pstmt);
398
			//// System.out.println(nRow + " Row modified to table Key=" + ub.getUserID() + "/" + ub.getUserAlias());
399
				
400
			if (nRow < 1) {
401
				System.out.println("ERR: no row modified into user table!");
402
				//
403
				// rollback
404
				//
405
				try { 
406
					this.conn.rollback();
407
					logger.info("INFO:1 updateUserToStore :: rollback.");
408
				} catch(Exception rbe) {
409
					logger.error("EXC:updateUserToStore :: rollback failed!");
410
				}
411
				// rollback completed
412
				rc = false;
413
			} else {
414
				////System.out.println("GOOD: perf db commit!");
415
				this.conn.commit();
416
				rc = true;
417
			}
418
			
419
			} catch (Exception e) {
420
				logger.error("EXC:1 updateUserToStore");
421
				logger.error(getFullTrace(e));
422
				rc = false;
423
			} finally { // close database resources
424
				  try {
425
				       pstmt.close();
426
				       //this.conn.close();
427
				  } catch (Exception e) {
428
					  logger.error("EXC:Final updateUserToStore");
429
					  logger.error(getFullTrace(e));
430
				  } 
431
			} //finally--
432
		
433
		return rc;
434
	}
435
	
436
	/**
437
	 * 
438
	 */
439
	public boolean insertUserToStore(UserBean ub) {
440
		boolean rc = false;
441
		int nRow = 0;
442
		
443
		long nextVal= getSqlServerSeqID();
444

  
445
		String sqlStmt1 = "INSERT INTO tb_am_usermas (m_user_alias, m_user_id) VALUES (?, ?)";
446
		
447
		//TODO: use buff string.. later
448
//		String sqlStmt2 = "INSERT ALL " +
449
//			"INTO tb_am_user (U_USER_ID, U_USER_ALIAS, U_USER_TYPE, U_USER_DESC, U_USER_STATE," +
450
//			"P_PASSWD_CIPHERTEXT, P_DATE_EXPIRED, P_PASSWD_HIST) VALUES (?, ?, ?, ?, ?, ?, ?, ?) " +
451
//			"INTO tb_am_tac (T_USER_ID) VALUES (?) SELECT * FROM dual";
452
		String sqlStmt2 = "INSERT " +
453
		"INTO tb_am_user (U_USER_ID, U_USER_ALIAS, U_USER_TYPE, U_USER_DESC, U_USER_STATE," +
454
		"P_PASSWD_CIPHERTEXT, P_DATE_EXPIRED, P_PASSWD_HIST, U_DATE_CREATED, U_USE_COUNT, P_PASSWD_STATE, P_DATE_GENERATED, P_USE_COUNT, P_ERROR_COUNT, P_EXPIRED_STATUS) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
455
		
456
		String sqlStmt3 = "INSERT INTO tb_am_tac (T_USER_ID, T_TAC_STATE, T_DATE_GENERATED, T_USE_COUNT, T_ERROR_COUNT) VALUES (?, ?, ?, ?, ?)";
457
		
458
		// insert into DB
459
		PreparedStatement pstmt = null;
460
		try {
461
			pstmt = this.conn.prepareStatement(sqlStmt1);
462
			pstmt.setString(1, ub.getUserAlias());
463
			pstmt.setLong(2, nextVal);
464
			
465
			nRow = executeSQLUpdate(pstmt);
466
			//// System.out.println(nRow + " Row inserted to table");
467
			
468
			if (nRow < 1) {
469
				logger.info("ERR: no row inserted into master table!");
470
				rc = false;
471
			} else {
472
				pstmt = this.conn.prepareStatement(sqlStmt2);
473
				pstmt.setLong(1, nextVal);
474
				pstmt.setString(2, ub.getUserAlias());
475
				pstmt.setInt(3,ub.getUserType());
476
				pstmt.setString(4, ub.getDescription());
477
				pstmt.setInt(5,ub.getUstate());
478
				pstmt.setString(6, ub.getPcipherText());
479
				pstmt.setTimestamp(7, convertSqlDate(ub.getPdateExpired()) );
480
				pstmt.setString(8, ub.getPhistoryList());
481
				pstmt.setTimestamp(9, convertSqlDate(new Date()));
482
				pstmt.setInt(10, 0);
483
				pstmt.setInt(11, 0);
484
				pstmt.setTimestamp(12, convertSqlDate(new Date()));
485
				pstmt.setInt(13, 0);
486
				pstmt.setInt(14, 0);
487
				pstmt.setInt(15, 1);
488
				
489
				nRow = executeSQLUpdate(pstmt);
490
				
491
				logger.info(nRow + " Row inserted to table");
492
				
493
				pstmt = this.conn.prepareStatement(sqlStmt3);
494
				pstmt.setLong(1, nextVal);
495
				pstmt.setLong(2, 0);
496
				pstmt.setTimestamp (3, convertSqlDate(new Date()));
497
				pstmt.setInt(4, 0);
498
				pstmt.setInt(5, 0);
499
				nRow = executeSQLUpdate(pstmt);
500
				
501
				logger.info(nRow + " Row inserted to table");
502
				
503
				if (nRow < 1) {
504
					logger.info("ERR: no row inserted into user table!");
505
					//
506
					// rollback
507
					//
508
					try { 
509
						this.conn.rollback();
510
						logger.info("INFO:1 insertUserToStore :: rollback.");
511
					} catch(Exception rbe) {
512
						logger.error("EXC:insertUserToStore :: rollback failed!");
513
					}
514
					// rollback completed
515
					rc = false;
516
				} else {
517
					////System.out.println("GOOD: perf db commit!");
518
					this.conn.commit();
519
					rc = true;
520
				}
521
			}
522
			
523
			} catch (Exception e) {
524
				logger.error(getFullTrace(e));
525
				logger.error("EXC:1 insertUserToStore");
526
				rc = false;
527
			} finally { // close database resources
528
				  try {
529
				       pstmt.close();
530
				       //this.conn.close();
531
				  } catch (Exception e) {
532
					  logger.error("EXC:Final insertUserToStore");
533
					  logger.error(getFullTrace(e));
534
				  }
535
			}//finally--
536
			
537
		return rc;
538
	}
539 134
	
540 135
	///////////////////////////////////////////////////////////////////////
541 136
	// TAC Bean
......
939 534
		return rc;
940 535
			
941 536
	}
942

  
943
	public static String getFullTrace (Exception e)
944
	{
945
		String newLine = "\r\n";
946
		StringBuffer m_sErrorMessage = new StringBuffer ();
947

  
948
		m_sErrorMessage.append (e.getMessage ());
949

  
950
		m_sErrorMessage.append (newLine);
951
		StackTraceElement[] m_oElements = e.getStackTrace ();
952
		m_sErrorMessage.append ("Exception class : "
953
				+ m_oElements[0].getClassName () + ", class method : "
954
				+ m_oElements[0].getMethodName () + ", line number : "
955
				+ m_oElements[0].getLineNumber ());
956
		m_sErrorMessage.append (newLine);
957

  
958
		for (int i = 0; i < m_oElements.length; i++)
959
		{
960
			m_sErrorMessage.append (m_oElements[i].toString ());
961
			m_sErrorMessage.append (newLine);
962
		}
963
		return m_sErrorMessage.toString ();
964
	}
965 537
}//end class

Also available in: Unified diff