Project

General

Profile

« Previous | Next » 

Revision 5108

Added by daigle over 14 years ago

move dbconnection object out of base class and create it for each db access so it can be released

View differences:

XMLAccessAccess.java
44 44
	private Logger logMetacat = Logger.getLogger(XMLAccessAccess.class);
45 45
	
46 46
	// Constructor
47
	public XMLAccessAccess() throws AccessException {
48
		super("XMLAccessAccess");
49
	}
47
	public XMLAccessAccess() throws AccessException {}
50 48
	
51 49
	/**
52 50
	 * Get all xml access for a document
......
66 64
			
67 65
		// first get the job from the db and put it into a DAO
68 66
		PreparedStatement pstmt = null;
69
		try {
67
		DBConnection conn = null;
68
		int serialNumber = -1;
69
		try {			
70
			conn = DBConnectionPool.getDBConnection("XMLAccessAccess.getXMLAccessForDoc");
71
    		serialNumber = conn.getCheckOutSerialNumber();
72

  
70 73
			String sql = "SELECT * FROM xml_access WHERE docid = ?";
71 74
			pstmt = conn.prepareStatement(sql);
72 75

  
......
98 101
			logMetacat.error(errorStr);
99 102
			throw new AccessException(errorStr);
100 103
		} finally {
101
			try {
102
				if (pstmt != null) {
103
					pstmt.close();
104
				}
105
			} catch (SQLException sqle) {
106
				logMetacat.error("XMLAccessAccess.getXMLAccessForDoc - An error occurred " 
107
						+ "closing prepared statement: " + sqle.getMessage());
108
			} 
104
			closeDBObjects(pstmt, conn, serialNumber, logMetacat);
109 105
		}		
110 106
	}
111 107
	
......
134 130
			
135 131
		// first get the job from the db and put it into a DAO
136 132
		PreparedStatement pstmt = null;
133
		DBConnection conn = null;
134
		int serialNumber = -1;
137 135
		try {
136
			conn = DBConnectionPool.getDBConnection("XMLAccessAccess.getXMLAccessForPrincipal");
137
    		serialNumber = conn.getCheckOutSerialNumber();
138
			
138 139
			String sql = "SELECT * FROM xml_access WHERE docid = ? AND principal_name = ?";
139 140
			pstmt = conn.prepareStatement(sql);
140 141

  
......
167 168
			logMetacat.error(errorStr);
168 169
			throw new AccessException(errorStr);
169 170
		} finally {
170
			try {
171
				if (pstmt != null) {
172
					pstmt.close();
173
				}
174
			} catch (SQLException sqle) {
175
				logMetacat.error("XMLAccessAccess.getXMLAccessForPrincipal - An error occurred " 
176
						+ "closing prepared statement: " + sqle.getMessage());
177
			} 
171
			closeDBObjects(pstmt, conn, serialNumber, logMetacat); 
178 172
		}		
179 173
	}
180 174
	
......
211 205
					
212 206
		// first get the job from the db and put it into a DAO
213 207
		PreparedStatement pstmt = null;
208
		DBConnection conn = null;
209
		int serialNumber = -1;
214 210
		try {
211
			conn = DBConnectionPool.getDBConnection("XMLAccessAccess.getXMLAccessForPrincipal");
212
    		serialNumber = conn.getCheckOutSerialNumber();
213
    		
215 214
			String sql = "SELECT * FROM xml_access WHERE docid = ? AND principal_name = ? " + 
216 215
				"AND perm_type = ? AND perm_order = ?";
217 216
			pstmt = conn.prepareStatement(sql);
......
247 246
			logMetacat.error(errorStr);
248 247
			throw new AccessException(errorStr);
249 248
		} finally {
250
			try {
251
				if (pstmt != null) {
252
					pstmt.close();
253
				}
254
			} catch (SQLException sqle) {
255
				logMetacat.error("XMLAccessAccess.getXMLAccessForPrincipal - An error occurred " 
256
						+ "closing prepared statement: " + sqle.getMessage());
257
			} 
249
			closeDBObjects(pstmt, conn, serialNumber, logMetacat); 
258 250
		}		
259 251
	}
260 252
	
......
275 267
	 *            permission order
276 268
	 */
277 269
	public void addXMLAccess(String docId, String principalName, Long permission, String permType, 
278
			String permOrder) throws AccessException {
270
			String permOrder) throws AccessException, PermOrderException {
271
		
272
		permOrderConflict(docId, permOrder);
273
		
279 274
		Vector<XMLAccessDAO> xmlAccessList = 
280 275
			getXMLAccessForPrincipal(docId, principalName, permType, permOrder);
281 276
		
......
299 294
		}
300 295
		
301 296
		XMLAccessDAO xmlAccessDAO = xmlAccessList.get(0);
302
		
303
		
297
				
304 298
		// if the permission on the xml access dao does not already contain the permission we are 
305 299
		//trying to add, update the access record with the existing permission bitwis OR-ed with our
306 300
		// new permission
......
318 312
	 * @param xmlAccessList
319 313
	 *            list of xml access dao objects that hold new access for the document
320 314
	 */
321
	public void setXMLAccess(String docId, Vector<XMLAccessDAO> xmlAccessList) throws AccessException {
315
	public void replaceAccess(String docId, Vector<XMLAccessDAO> xmlAccessList) throws AccessException {
322 316
		deleteXMLAccessForDoc(docId);
323 317
		
324 318
		// if more than one record exists for this principal on this document with the same
......
398 392
					+ "xml access permissions for doc id: " + docId + ", principal: " + 
399 393
					principalName + ":" + sqle.getMessage());
400 394
		} finally {
401
			try {
402
				if (pstmt != null) {
403
					pstmt.close();
404
				}
405
			} catch (SQLException sqle) {
406
				logMetacat.error("XMLAccessAccess.insertXMLAccess - SQL error when closing prepared " + 
407
						"statement after inserting xml access permissions for doc id: " + docId + 
408
						", principal: " +  principalName + ":" + sqle.getMessage());
409
			} finally {
410
				DBConnectionPool.returnDBConnection(conn, serialNumber);
411
			}
395
			closeDBObjects(pstmt, conn, serialNumber, logMetacat); 
412 396
		}   
413 397
	}
414 398
	
......
465 449
					+ "xml access permissions for doc id: " + docId + ", principal: " + 
466 450
					principalName + ":" + sqle.getMessage());
467 451
		} finally {
468
			try {
469
				if (pstmt != null) {
470
					pstmt.close();
471
				}
472
			} catch (SQLException sqle) {
473
				logMetacat.error("XMLAccessAccess.updateXMLAccessPermission - SQL error when closing prepared " + 
474
						"statement after updating xml access permissions for doc id: " + docId + 
475
						", principal: " +  principalName + ":" + sqle.getMessage());
476
			} finally {
477
				DBConnectionPool.returnDBConnection(conn, serialNumber);
478
			}
452
			closeDBObjects(pstmt, conn, serialNumber, logMetacat); 
479 453
		}
480 454
		
481 455
	}
......
564 538
			throw new AccessException("XMLAccessAccess.deleteXMLAccessForDoc - SQL error when deleting"
565 539
					+ "xml access permissions for doc id: " + docId + ":" + sqle.getMessage());
566 540
		} finally {
567
			try {
568
				if (pstmt != null) {
569
					pstmt.close();
570
				}
571
			} catch (SQLException sqle) {
572
				logMetacat.error("XMLAccessAccess.deleteXMLAccessForDoc - SQL error when closing prepared " + 
573
						"statement after deleting xml access permissions for doc id: " + ":" + sqle.getMessage());
574
			} finally {
575
				DBConnectionPool.returnDBConnection(conn, serialNumber);
576
			}
541
			closeDBObjects(pstmt, conn, serialNumber, logMetacat); 
577 542
		}	   
578 543
	}
579 544
	
......
620 585
					+ "xml access permissions for doc id: " + docId + ", principal: " + 
621 586
					principalName + ":" + sqle.getMessage());
622 587
		} finally {
623
			try {
624
				if (pstmt != null) {
625
					pstmt.close();
626
				}
627
			} catch (SQLException sqle) {
628
				logMetacat.error("XMLAccessAccess.deleteXMLAccessForPrincipal - SQL error when closing prepared " + 
629
						"statement after deleting xml access permissions for doc id: " + docId + 
630
						", principal: " +  principalName + ":" + sqle.getMessage());
631
			} finally {
632
				DBConnectionPool.returnDBConnection(conn, serialNumber);
633
			}
588
			closeDBObjects(pstmt, conn, serialNumber, logMetacat);
634 589
		}	   
635 590
	}
636 591
	
......
642 597
	 * @param principal
643 598
	 *            principal credentials
644 599
	 */
600
	private void permOrderConflict(String docId, String permOrder) throws AccessException, PermOrderException {
601
		if (docId == null) {
602
			throw new AccessException("XMLAccessAccess.permOrderConflict - docid is required when " + 
603
					"determining perm order conflict");
604
		}
605
		if (permOrder == null) {
606
			throw new AccessException("XMLAccessAccess.permOrderConflict - perm order is required when " + 
607
					"determining perm order conflict");
608
		}
609
		
610
	    PreparedStatement pstmt = null;
611
		DBConnection conn = null;
612
		int serialNumber = -1;
613
		try {
614
			// check out DBConnection
615
			conn = DBConnectionPool.getDBConnection("XMLAccessAccess.permOrderConflict");
616
			String sql = "SELECT * FROM xml_access WHERE docid = ? AND perm_order != ?";
617
			pstmt = conn.prepareStatement(sql);
618

  
619
			// Bind the values to the query
620
			pstmt.setString(1, docId);
621
			pstmt.setString(2, permOrder);
622
			
623
			String sqlReport = "XMLAccessAccess.permOrderConflict - SQL: " + sql;
624
			sqlReport += " [" + docId + "," + permOrder + "]";
625
			
626
			logMetacat.info(sqlReport);
627

  
628
			pstmt.execute();
629
			
630
			ResultSet resultSet = pstmt.getResultSet();
631
			if (resultSet.next()) {			
632
				throw new PermOrderException("XMLAccessAccess.addXMLAccess - cannot add permission " + 
633
					"record for doc id: " + docId + "with permOrder: " + permOrder + " due to permOrder conflict");
634
			}
635
		} catch (SQLException sqle) {
636
			throw new AccessException("XMLAccessAccess.permOrderConflict - SQL error when checking"
637
					+ "for perm order conflict on: " + docId + ":" + sqle.getMessage());
638
		} finally {
639
			closeDBObjects(pstmt, conn, serialNumber, logMetacat); 
640
		}
641
	   
642
	}
643
	
644
	/**
645
	 * Delete xml access.  This removes all access records from the database for a principal 
646
	 * for a given document, perm type and perm order
647
	 * @param docId
648
	 *            document id
649
	 * @param principal
650
	 *            principal credentials
651
	 */
645 652
	private void deleteXMLAccessForPrincipal(String docId, String principalName, String permType, String permOrder) throws AccessException {
646 653
		if (docId == null) {
647 654
			throw new AccessException("XMLAccessAccess.deleteXMLAccessForPrincipal - docid is required when " + 
......
668 675
		try {
669 676
			// check out DBConnection
670 677
			conn = DBConnectionPool.getDBConnection("XMLAccessAccess.deleteXMLAccessForPrincipal");
671
			String sql = "DELETE FROM xml_access WHERE docid = ? AND principal_name = ?" +
672
				"AND perm_type = ? AND perm_ord";
678
			String sql = "DELETE FROM xml_access WHERE docid = ? AND principal_name = ? " +
679
				"AND perm_type = ? AND perm_order = ?";
673 680
			pstmt = conn.prepareStatement(sql);
674 681

  
675 682
			// Bind the values to the query
......
689 696
					+ "xml access permissions for doc id: " + docId + ", principal: " + 
690 697
					principalName + ":" + sqle.getMessage());
691 698
		} finally {
692
			try {
693
				if (pstmt != null) {
694
					pstmt.close();
695
				}
696
			} catch (SQLException sqle) {
697
				logMetacat.error("XMLAccessAccess.deleteXMLAccessForPrincipal - SQL error when closing prepared " + 
698
						"statement after deleting xml access permissions for doc id: " + docId + 
699
						", principal: " +  principalName + ":" + sqle.getMessage());
700
			} finally {
701
				DBConnectionPool.returnDBConnection(conn, serialNumber);
702
			}
699
			closeDBObjects(pstmt, conn, serialNumber, logMetacat); 
703 700
		}
704 701
	   
705 702
	}

Also available in: Unified diff