Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that manages database access of xml access  
4
 *             information.
5
 *  Copyright: 2009 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Michael Daigle
8
 * 
9
 *   '$Author: daigle $'
10
 *     '$Date: 2009-03-23 13:56:56 -0800 (Mon, 23 Mar 2009) $'
11
 * '$Revision: 4854 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27

    
28
package edu.ucsb.nceas.metacat.accesscontrol;
29

    
30
import java.sql.PreparedStatement;
31
import java.sql.ResultSet;
32
import java.sql.SQLException;
33
import java.util.Vector;
34

    
35
import org.apache.log4j.Logger;
36

    
37
import edu.ucsb.nceas.metacat.database.DBConnection;
38
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
39
import edu.ucsb.nceas.metacat.shared.AccessException;
40
import edu.ucsb.nceas.metacat.shared.BaseAccess;
41

    
42
public class XMLAccessAccess extends BaseAccess {
43
	
44
	private Logger logMetacat = Logger.getLogger(XMLAccessAccess.class);
45
	
46
	// Constructor
47
	public XMLAccessAccess() throws AccessException {}
48
	
49
	/**
50
	 * Get all xml access for a document
51
	 * 
52
	 * @param docId
53
	 *            the id of the document
54
	 * @return an xml access DAO list
55
	 */ 
56
	public Vector<XMLAccessDAO> getXMLAccessForDoc(String docId) throws AccessException {
57

    
58
		Vector<XMLAccessDAO> xmlAccessList = new Vector<XMLAccessDAO>();
59
		
60
		if (docId == null) {
61
			throw new AccessException("XMLAccessAccess.getXMLAccessForDoc - doc id " + 
62
					"must be specified when selecting xml_access record");
63
		}
64
			
65
		// first get the xml access from the db and put it into a DAO list
66
		PreparedStatement pstmt = null;
67
		DBConnection conn = null;
68
		int serialNumber = -1;
69
		try {			
70
			conn = DBConnectionPool.getDBConnection("XMLAccessAccess.getXMLAccessForDoc");
71
    		serialNumber = conn.getCheckOutSerialNumber();
72

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

    
76
			pstmt.setString(1, docId);
77
			
78
			String sqlReport = "XMLAccessAccess.getXMLAccessForDoc - SQL: " + sql;
79
			sqlReport += " [" + docId + "]";
80
			
81
			logMetacat.info(sqlReport);
82
			
83
			pstmt.execute();
84
			
85
			ResultSet resultSet = pstmt.getResultSet();
86
			while (resultSet.next()) {
87
				XMLAccessDAO xmlAccessDAO = populateDAO(resultSet);
88
				xmlAccessList.add(xmlAccessDAO);
89
			}
90
					
91
			// make sure permission orders do not conflict in the database
92
			validateDocXMLAccessList(xmlAccessList);
93
			
94
			return xmlAccessList;
95
			
96
		} catch (SQLException sqle) {
97
			throw new AccessException("XMLAccessAccess.getXMLAccessForDoc - SQL error when getting access " + 
98
					" for doc id: " + docId  + " : "  + sqle.getMessage());
99
		} catch (PermOrderException poe) {
100
			String errorStr = "XMLAccessAccess.getXMLAccessForDoc - Permission order error when getting " + 
101
				"access record for doc id: " + docId + " : "  + poe.getMessage();
102
			logMetacat.error(errorStr);
103
			throw new AccessException(errorStr);
104
		} finally {
105
			closeDBObjects(pstmt, conn, serialNumber, logMetacat);
106
		}		
107
	}
108
	
109
	/**
110
	 * Get all xml access for a principal for a certain document
111
	 * 
112
	 * @param docId
113
	 *            the id of the document
114
	 * @param principalName
115
	 *            the credentials of the principal in the database
116
	 * @return an xml access DAO list
117
	 */ 
118
	public Vector<XMLAccessDAO> getXMLAccessForPrincipal(String docId, String principalName) 
119
			throws AccessException {
120

    
121
		Vector<XMLAccessDAO> xmlAccessList = new Vector<XMLAccessDAO>();
122
		
123
		if (docId == null) { 
124
			throw new AccessException("XMLAccessAccess.getXMLAccessForPrincipal - doc id " + 
125
					"must be specified when selecting xml_access record");
126
		}
127
		if (principalName == null) { 
128
			throw new AccessException("XMLAccessAccess.getXMLAccessForPrincipal - doc id " + 
129
					"must be specified when selecting xml_access record");
130
		}
131
			
132
		// first get the xml access for this principal from the db and put it into a DAO list
133
		PreparedStatement pstmt = null;
134
		DBConnection conn = null;
135
		int serialNumber = -1;
136
		try {
137
			conn = DBConnectionPool.getDBConnection("XMLAccessAccess.getXMLAccessForPrincipal");
138
    		serialNumber = conn.getCheckOutSerialNumber();
139
			
140
			String sql = "SELECT * FROM xml_access WHERE docid = ? AND principal_name = ?";
141
			pstmt = conn.prepareStatement(sql);
142

    
143
			pstmt.setString(1, docId);
144
			pstmt.setString(2, principalName);
145
			
146
			String sqlReport = "XMLAccessAccess.getXMLAccessForPrincipal - SQL: " + sql;
147
			sqlReport += " [" + docId + "," + principalName + "]";
148
			
149
			logMetacat.info(sqlReport);
150
			
151
			pstmt.execute();
152
			
153
			ResultSet resultSet = pstmt.getResultSet();
154
			while (resultSet.next()) {
155
				XMLAccessDAO xmlAccessDAO = populateDAO(resultSet);
156
				xmlAccessList.add(xmlAccessDAO);
157
			}
158
			
159
			// make sure permission orders do not conflict in the database
160
			validatePrincipalXMLAccessList(xmlAccessList);
161
			
162
			return xmlAccessList;
163
			
164
		} catch (SQLException sqle) {
165
			throw new AccessException("XMLAccessAccess.getXMLAccessForPrincipal - SQL error when getting access " + 
166
					" for doc id: " + docId + ", principal: " + principalName  + " : "  + sqle.getMessage());
167
		} catch (PermOrderException poe) {
168
			String errorStr = "XMLAccessAccess.getXMLAccessForPrincipal - Permission order error when getting " + 
169
				"access record for doc id: " + docId + ", principal: " + principalName + " : "  + poe.getMessage();
170
			logMetacat.error(errorStr);
171
			throw new AccessException(errorStr);
172
		} finally {
173
			closeDBObjects(pstmt, conn, serialNumber, logMetacat); 
174
		}		
175
	}
176
	
177
	/**
178
	 * Get all xml access for a principal/permType/permOrder for a certain document
179
	 * 
180
	 * @param docId
181
	 *            the id of the document
182
	 * @param principalName
183
	 *            the credentials of the principal in the database
184
	 * @return an xml access DAO list
185
	 */ 
186
	public Vector<XMLAccessDAO> getXMLAccessForPrincipal(String docId, String principalName, String permType, String permOrder) 
187
			throws AccessException {
188

    
189
		Vector<XMLAccessDAO> xmlAccessList = new Vector<XMLAccessDAO>();
190
		
191
		if (docId == null) { 
192
			throw new AccessException("XMLAccessAccess.getXMLAccessForPrincipal - doc id " + 
193
					"must be specified when selecting xml_access record");
194
		}
195
		if (principalName == null) { 
196
			throw new AccessException("XMLAccessAccess.getXMLAccessForPrincipal - doc id " + 
197
					"must be specified when selecting xml_access record");
198
		}
199
		if (permType == null) { 
200
			throw new AccessException("XMLAccessAccess.getXMLAccessForPrincipal - permission type " + 
201
					"must be specified when selecting xml_access record");
202
		}
203
		if (permOrder == null) { 
204
			throw new AccessException("XMLAccessAccess.getXMLAccessForPrincipal - permission order " + 
205
					"must be specified when selecting xml_access record");
206
		}
207
					
208
		// first get the xml access for this principal from the db and put it into a DAO list
209
		PreparedStatement pstmt = null;
210
		DBConnection conn = null;
211
		int serialNumber = -1;
212
		try {
213
			conn = DBConnectionPool.getDBConnection("XMLAccessAccess.getXMLAccessForPrincipal");
214
    		serialNumber = conn.getCheckOutSerialNumber();
215
    		
216
			String sql = "SELECT * FROM xml_access WHERE docid = ? AND principal_name = ? " + 
217
				"AND perm_type = ? AND perm_order = ?";
218
			pstmt = conn.prepareStatement(sql);
219

    
220
			pstmt.setString(1, docId);
221
			pstmt.setString(2, principalName);
222
			pstmt.setString(3, permType);			
223
			pstmt.setString(4, permOrder);
224
			
225
			String sqlReport = "XMLAccessAccess.getXMLAccessForPrincipal - SQL: " + sql;
226
			sqlReport += " [" + docId + "," + principalName + "," +  permType + "," + permOrder + "]";
227
			
228
			logMetacat.info(sqlReport);
229
			
230
			pstmt.execute();
231
			
232
			ResultSet resultSet = pstmt.getResultSet();
233
			while (resultSet.next()) {
234
				XMLAccessDAO xmlAccessDAO = populateDAO(resultSet);
235
				xmlAccessList.add(xmlAccessDAO);
236
			}
237
			
238
			validatePrincipalXMLAccessList(xmlAccessList);
239
			
240
			return xmlAccessList;
241
			
242
		} catch (SQLException sqle) {
243
			throw new AccessException("XMLAccessAccess.getXMLAccessForPrincipal - SQL error when getting access " + 
244
					" for doc id: " + docId + ", principal: " + principalName  + " : "  + sqle.getMessage());
245
		} catch (PermOrderException poe) {
246
			String errorStr = "XMLAccessAccess.getXMLAccessForPrincipal - Permission order error when getting " + 
247
				"access record for doc id: " + docId + ", principal: " + principalName + " : "  + poe.getMessage();
248
			logMetacat.error(errorStr);
249
			throw new AccessException(errorStr);
250
		} finally {
251
			closeDBObjects(pstmt, conn, serialNumber, logMetacat); 
252
		}		
253
	}
254
	
255
	/**
256
	 * Add permissions for a given principal on a given document. If the
257
	 * principal already exists, bitwise OR the permission to the existing
258
	 * permission and update.
259
	 * 
260
	 * @param docId
261
	 *            document id
262
	 * @param principalName
263
	 *            principal credentials
264
	 * @param permission
265
	 *            permission bitmap
266
	 * @param permType
267
	 *            permission type
268
	 * @param permOrder
269
	 *            permission order
270
	 */
271
	public void addXMLAccess(String docId, String principalName, Long permission, String permType, 
272
			String permOrder) throws AccessException, PermOrderException {
273
		
274
		permOrderConflict(docId, permOrder);
275
		
276
		Vector<XMLAccessDAO> xmlAccessList = 
277
			getXMLAccessForPrincipal(docId, principalName, permType, permOrder);
278
		
279
		// if more than one record exists for this principal on this document with the same
280
		// access type / access order combination, call cleanup to combine common access and then
281
		// re-retrieve the access list.
282
		if (xmlAccessList.size() == 0) {
283
			insertXMLAccess(docId, principalName, permission, permType, permOrder);
284
			return;
285
		}
286
		
287
		if (xmlAccessList.size() > 1) {
288
			cleanupXMLAccessForPrincipal(xmlAccessList);
289
			xmlAccessList = getXMLAccessForPrincipal(docId, principalName, permType, permOrder);
290
		}
291
		
292
		if (xmlAccessList.size() == 0) {
293
			throw new AccessException("XMLAccessAccess.addXMLAccess - xml access list is empty when " + 
294
				"it shouldn't be for docid: " + docId + ", prinicpal name: " + principalName + ", perm type " + 
295
				permType + ", perm order: " + permOrder);	
296
		}
297
		
298
		XMLAccessDAO xmlAccessDAO = xmlAccessList.get(0);
299
				
300
		// if the permission on the xml access dao does not already contain the permission we are 
301
		//trying to add, update the access record with the existing permission bitwis OR-ed with our
302
		// new permission
303
		if ((xmlAccessDAO.getPermission() & permission) != permission) {		
304
			updateXMLAccessPermission(docId, principalName, xmlAccessDAO.getPermission() | permission);
305
		}
306
	}
307
	
308
	/**
309
	 * Set permissions for a given document. This means first removing all access control for the
310
	 * document and then adding the given rules.
311
	 * 
312
	 * @param docId
313
	 *            document id
314
	 * @param xmlAccessList
315
	 *            list of xml access dao objects that hold new access for the document
316
	 */
317
	public void replaceAccess(String docId, Vector<XMLAccessDAO> xmlAccessList) throws AccessException {
318
		deleteXMLAccessForDoc(docId);
319
		
320
		// if more than one record exists for this principal on this document with the same
321
		// access type / access order combination, call cleanup to combine common access and then
322
		// re-retrieve the access list.
323
		for(XMLAccessDAO xmlAccessDAO : xmlAccessList) {
324
			insertXMLAccess(docId, xmlAccessDAO.getPrincipalName(), xmlAccessDAO.getPermission(), 
325
					xmlAccessDAO.getPermType(), xmlAccessDAO.getPermOrder());
326
		}
327
	}
328
	
329
	/**
330
	 * Insert an xml access record.  It is assumed that the checks have already been made to 
331
	 * make sure the principal does not already have an access record for this document.  If 
332
	 * one does already exist, that record should be updated and this insert not called.
333
	 * 
334
	 * @param docId
335
	 *            document id
336
	 * @param principal
337
	 *            principal credentials
338
	 * @param permission
339
	 *            permission bitmap
340
	 * @param permType
341
	 *            permission type
342
	 * @param permOrder
343
	 *            permission order
344
	 */
345
	private void insertXMLAccess(String docId, String principalName, Long permission, String permType,
346
			String permOrder) throws AccessException {
347
		if (docId == null) {
348
			throw new AccessException("XMLAccessAccess.insertXMLAccess - docid is required when " + 
349
					"inserting XML access record");
350
		}
351
		if (principalName == null) {
352
			throw new AccessException("XMLAccessAccess.insertXMLAccess - principal is required when " + 
353
					"inserting XML access record");
354
		}
355
		if (permission == null) {
356
			throw new AccessException("XMLAccessAccess.insertXMLAccess - permission is required when " + 
357
					"inserting XML access record");
358
		}
359
		if (permType == null) {
360
			throw new AccessException("XMLAccessAccess.insertXMLAccess - permType is required when " + 
361
					"inserting XML access record");
362
		}
363
		if (permOrder == null) {
364
			permOrder = AccessControlInterface.ALLOWFIRST;
365
		}
366
		
367
	    PreparedStatement pstmt = null;
368
		DBConnection conn = null;
369
		int serialNumber = -1;
370
		try {
371
			// check out DBConnection
372
			conn = DBConnectionPool.getDBConnection("XMLAccessAccess.insertXMLAccess");
373
			serialNumber = conn.getCheckOutSerialNumber();
374
			String sql = "INSERT INTO xml_access " +
375
				"(docid, principal_name, permission, perm_type, perm_order ) " + 
376
				"VALUES (?,?,?,?,?)";
377
			pstmt = conn.prepareStatement(sql);
378

    
379
			// Bind the values to the query
380
			pstmt.setString(1, docId);
381
			pstmt.setString(2, principalName);
382
			pstmt.setLong(3, permission);
383
			pstmt.setString(4, permType);
384
			pstmt.setString(5, permOrder);
385
			
386
			String sqlReport = "XMLAccessAccess.insertXMLAccess - SQL: " + sql;
387
			sqlReport += " [" + docId + "," + principalName + "," +  permission + "," +  permType + "," + permOrder + "]";
388
			
389
			logMetacat.info(sqlReport);
390

    
391
			pstmt.execute();
392
		} catch (SQLException sqle) {
393
			throw new AccessException("XMLAccessAccess.insertXMLAccess - SQL error when inserting"
394
					+ "xml access permissions for doc id: " + docId + ", principal: " + 
395
					principalName + ":" + sqle.getMessage());
396
		} finally {
397
			closeDBObjects(pstmt, conn, serialNumber, logMetacat); 
398
		}   
399
	}
400
	
401
	/**
402
	 * Update existing xml access permissions in the db.  The permission value should be the combined
403
	 * value of pre-existing permissions plus new permissions.
404
	 * 
405
	 * @param docId
406
	 *            document id
407
	 * @param principalName
408
	 *            principal credentials
409
	 * @param permission
410
	 *            permission bitmap
411
	 */
412
	private void updateXMLAccessPermission(String docId, String principalName, Long permission)
413
			throws AccessException {
414
		if (docId == null) {
415
			throw new AccessException("XMLAccessAccess.updateXMLAccessPermission - docid is required when " + 
416
					"updating XML access record");
417
		}
418
		if (principalName == null) {
419
			throw new AccessException("XMLAccessAccess.updateXMLAccessPermission - principal is required when " + 
420
					"updating XML access record");
421
		}
422
		if (permission == null) {
423
			throw new AccessException("XMLAccessAccess.updateXMLAccessPermission - permission is required when " + 
424
					"updating XML access record");
425
		}
426
		
427
	    PreparedStatement pstmt = null;
428
		DBConnection conn = null;
429
		int serialNumber = -1;
430
		try {
431
			// check out DBConnection
432
			conn = DBConnectionPool.getDBConnection("XMLAccessAccess.updateXMLAccessPermission");
433
			serialNumber = conn.getCheckOutSerialNumber();
434
			String sql = "UPDATE xml_access SET permission = ?" +
435
				"WHERE docid = ? AND principal_name = ?";
436
			pstmt = conn.prepareStatement(sql);
437

    
438
			// Bind the values to the query
439
			pstmt.setLong(1, permission);
440
			pstmt.setString(2, docId);
441
			pstmt.setString(3, principalName);
442
			
443
			String sqlReport = "XMLAccessAccess.updateXMLAccessPermission - SQL: " + sql;
444
			sqlReport += " [" + permission + "," + docId + "," + principalName + "]";
445
			
446
			logMetacat.info(sqlReport);
447

    
448
			pstmt.execute();
449
		} catch (SQLException sqle) {
450
			throw new AccessException("XMLAccessAccess.updateXMLAccessPermission - SQL error when updating"
451
					+ "xml access permissions for doc id: " + docId + ", principal: " + 
452
					principalName + ":" + sqle.getMessage());
453
		} finally {
454
			closeDBObjects(pstmt, conn, serialNumber, logMetacat); 
455
		}
456
		
457
	}
458
	
459
	/**
460
	 * Remove xml access.  This modifies the access in the database for a principal 
461
	 * for a given document.  If the provided permission is exactly the same as what 
462
	 * the principal has, the record is deleted from the database.
463
	 * 
464
	 * @param docId
465
	 *            document id
466
	 * @param principalName
467
	 *            principal credentials
468
	 */
469
	public void removeXMLAccessForPrincipal(String docId, String principalName, Long permission) throws AccessException {
470
		if (docId == null) {
471
			throw new AccessException("XMLAccessAccess.removeXMLAccessForPrincipal - docid is required when " + 
472
					"removing XML access");
473
		}
474
		if (principalName == null) {
475
			throw new AccessException("XMLAccessAccess.removeXMLAccessForPrincipal - principal is required when " + 
476
					"deleting XML access");
477
		}
478
		if (permission == null) {
479
			throw new AccessException("XMLAccessAccess.removeXMLAccessForPrincipal - permission is required when " + 
480
					"updating XML access");
481
		}
482
		
483
		Vector<XMLAccessDAO> xmlAccessList = getXMLAccessForPrincipal(docId, principalName);
484
		if (xmlAccessList.size() == 0) {
485
			logMetacat.warn("XMLAccessAccess.removeXMLAccessForPrincipal - attempting to remove access when no " +
486
				"access record exists for docid: " + docId + ", principal: " + principalName);
487
		} else {
488
			long permissionMask = 0;
489
			for (XMLAccessDAO xmlAccessDAO : xmlAccessList) {
490
				permissionMask |= xmlAccessDAO.getPermission();
491
			}
492
			permissionMask |= permission;
493
			
494
			// in this case, the only existing permissions are the ones we want to remove, so 
495
			// delete the record(s) for this principal on this document
496
			if ((permissionMask & permission) == permission) {
497
				deleteXMLAccessForPrincipal(docId, principalName);
498
			}
499
			
500
			if (xmlAccessList.size() > 1) {
501
				
502
			} else {
503
				updateXMLAccessPermission(docId, principalName, permission);
504
			}
505
		}
506
	   
507
	}
508
	
509
	/**
510
	 * Delete xml access.  This removes all access records from the database for a given document
511
	 * 
512
	 * @param docId
513
	 *            document id
514
	 */
515
	public void deleteXMLAccessForDoc(String docId) throws AccessException {
516
		if (docId == null) {
517
			throw new AccessException("XMLAccessAccess.deleteXMLAccessForPrincipal - docid is required when " + 
518
					"deleting XML access record");
519
		}
520
		
521
	    PreparedStatement pstmt = null;
522
		DBConnection conn = null;
523
		int serialNumber = -1;
524
		try {
525
			// check out DBConnection
526
			conn = DBConnectionPool.getDBConnection("XMLAccessAccess.deleteXMLAccessForDoc");
527
			String sql = "DELETE FROM xml_access WHERE docid = ?";
528
			pstmt = conn.prepareStatement(sql);
529

    
530
			// Bind the values to the query
531
			pstmt.setString(1, docId);
532

    
533
			String sqlReport = "XMLAccessAccess.deleteXMLAccessForDoc - SQL: " + sql;
534
			sqlReport += " [" + docId + "]";
535
			
536
			logMetacat.info(sqlReport);
537

    
538
			pstmt.execute();
539
		} catch (SQLException sqle) {
540
			throw new AccessException("XMLAccessAccess.deleteXMLAccessForDoc - SQL error when deleting"
541
					+ "xml access permissions for doc id: " + docId + ":" + sqle.getMessage());
542
		} finally {
543
			closeDBObjects(pstmt, conn, serialNumber, logMetacat); 
544
		}	   
545
	}
546
	
547
	/**
548
	 * Delete xml access.  This removes all access records from the database for a principal 
549
	 * for a given document
550
	 * 
551
	 * @param docId
552
	 *            document id
553
	 * @param principal
554
	 *            principal credentials
555
	 */
556
	private void deleteXMLAccessForPrincipal(String docId, String principalName) throws AccessException {
557
		if (docId == null) {
558
			throw new AccessException("XMLAccessAccess.deleteXMLAccessForPrincipal - docid is required when " + 
559
					"deleting XML access record");
560
		}
561
		if (principalName == null) {
562
			throw new AccessException("XMLAccessAccess.deleteXMLAccessForPrincipal - principal is required when " + 
563
					"deleting XML access record");
564
		}
565
		
566
	    PreparedStatement pstmt = null;
567
		DBConnection conn = null;
568
		int serialNumber = -1;
569
		try {
570
			// check out DBConnection
571
			conn = DBConnectionPool.getDBConnection("XMLAccessAccess.deleteXMLAccessForPrincipal");
572
			String sql = "DELETE FROM xml_access WHERE docid = ? AND principal_name = ?";
573
			pstmt = conn.prepareStatement(sql);
574

    
575
			// Bind the values to the query
576
			pstmt.setString(1, docId);
577
			pstmt.setString(2, principalName);
578

    
579
			String sqlReport = "XMLAccessAccess.deleteXMLAccessForPrincipal - SQL: " + sql;
580
			sqlReport += " [" + docId + "," + principalName + "]";
581
			
582
			logMetacat.info(sqlReport);
583

    
584
			pstmt.execute();
585
		} catch (SQLException sqle) {
586
			throw new AccessException("XMLAccessAccess.deleteXMLAccessForPrincipal - SQL error when deleting"
587
					+ "xml access permissions for doc id: " + docId + ", principal: " + 
588
					principalName + ":" + sqle.getMessage());
589
		} finally {
590
			closeDBObjects(pstmt, conn, serialNumber, logMetacat);
591
		}	   
592
	}
593
	
594
	/**
595
	 * Checks to see if there is a permission order conflict for a given document.  Each 
596
	 * document is only allowed to have a single permission order
597
	 * 
598
	 * @param docId
599
	 *            document id
600
	 * @param principal
601
	 *            principal credentials
602
	 */
603
	private void permOrderConflict(String docId, String permOrder) throws AccessException, PermOrderException {
604
		if (docId == null) {
605
			throw new AccessException("XMLAccessAccess.permOrderConflict - docid is required when " + 
606
					"determining perm order conflict");
607
		}
608
		if (permOrder == null) {
609
			throw new AccessException("XMLAccessAccess.permOrderConflict - perm order is required when " + 
610
					"determining perm order conflict");
611
		}
612
		
613
	    PreparedStatement pstmt = null;
614
		DBConnection conn = null;
615
		int serialNumber = -1;
616
		try {
617
			// check out DBConnection
618
			conn = DBConnectionPool.getDBConnection("XMLAccessAccess.permOrderConflict");
619
			String sql = "SELECT * FROM xml_access WHERE docid = ? AND perm_order != ?";
620
			pstmt = conn.prepareStatement(sql);
621

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

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

    
686
			// Bind the values to the query
687
			pstmt.setString(1, docId);
688
			pstmt.setString(2, principalName);
689
			pstmt.setString(3, permType);
690
			pstmt.setString(4, permOrder);
691
			
692
			String sqlReport = "XMLAccessAccess.deleteXMLAccessForPrincipal - SQL: " + sql;
693
			sqlReport += " [" + docId + "," + principalName + "," + permType + "," + permOrder + "]";
694
			
695
			logMetacat.info(sqlReport);
696

    
697
			pstmt.execute();
698
		} catch (SQLException sqle) {
699
			throw new AccessException("XMLAccessAccess.deleteXMLAccessForPrincipal - SQL error when deleting"
700
					+ "xml access permissions for doc id: " + docId + ", principal: " + 
701
					principalName + ":" + sqle.getMessage());
702
		} finally {
703
			closeDBObjects(pstmt, conn, serialNumber, logMetacat); 
704
		}
705
	   
706
	}
707

    
708
	/**
709
	 * Make sure that only one record exists per principal/permType/document. If
710
	 * more than one record exists, delete the existing records, consolidate the
711
	 * permissions insert the new record.
712
	 * 
713
	 * @param xmlAccessList the access dao list
714
	 */
715
	private void cleanupXMLAccessForPrincipal(Vector<XMLAccessDAO> xmlAccessList) throws AccessException{
716
		
717
		int numAllowRecords = 0;
718
		int numDenyRecords = 0;
719
		long allowPermissionMask = 0;
720
		long denyPermissionMask = 0;
721
		String docId = null;
722
		String principalName = null;
723
		String permType = null;
724
		String permOrder = null;
725
	
726
		
727
		// iterate through the list of access dao objects and bttwise or the permissions.  Most
728
		// of this is just doing some error checking to make sure each record is valid.
729
		for (XMLAccessDAO xmlAccessDAO : xmlAccessList) {
730
			if (docId == null) {
731
				docId = xmlAccessDAO.getDocId();
732
			} else {
733
				if (!docId.equals(xmlAccessDAO.getDocId())) {
734
					throw new AccessException("XMLAccessAccess.cleanupXMLAccessForPrincipal - " + 
735
							" Conflicting doc ids " + xmlAccessDAO.getDocId() +
736
							" and " + docId);
737
				}
738
			}
739
			if (principalName == null) {
740
				principalName = xmlAccessDAO.getPrincipalName();
741
			} else {
742
				if (!principalName.equals(xmlAccessDAO.getPrincipalName())) {
743
					throw new AccessException("XMLAccessAccess.cleanupXMLAccessForPrincipal - " + 
744
							" Conflicting prinicpal names " + xmlAccessDAO.getPrincipalName() +
745
							" and principalName " + principalName);
746
				}
747
			}
748
			if (permType == null) {
749
				permType = xmlAccessDAO.getPermType();
750
			} else {
751
				if (!permType.equals(xmlAccessDAO.getPermType())) {
752
					throw new AccessException("XMLAccessAccess.cleanupXMLAccessForPrincipal - " + 
753
							" Conflicting permission orders for document " + xmlAccessDAO.getDocId() +
754
							"principalName " + principalName + ". Database intervention required ");
755
				}
756
			}
757
			if (permOrder == null) {
758
				permOrder = xmlAccessDAO.getPermOrder();
759
			} else {
760
				if (!permOrder.equals(xmlAccessDAO.getPermOrder())) {
761
					throw new AccessException("XMLAccessAccess.cleanupXMLAccessForPrincipal - " + 
762
							" Conflicting permission types for document " + xmlAccessDAO.getDocId() +
763
							"principalName " + principalName + ". Database intervention required ");
764
				}
765
			}
766
			if (permType == null) {
767
				permType = xmlAccessDAO.getPermType();
768
			} else {
769
				if (!permType.equals(xmlAccessDAO.getPermType())) {
770
					throw new AccessException("XMLAccessAccess.cleanupXMLAccessForPrincipal - " + 
771
							" Conflicting permission orders for document " + xmlAccessDAO.getDocId() +
772
							"principalName " + principalName + ". Database intervention required ");
773
				}
774
			}
775
			if (permType.equals(AccessControlInterface.ALLOW)) {
776
				numAllowRecords++;
777
				allowPermissionMask |= xmlAccessDAO.getPermission();
778
			} else if (permType.equals(AccessControlInterface.DENY)) {
779
				numDenyRecords++;
780
				denyPermissionMask |= xmlAccessDAO.getPermission();
781
			}
782
		}
783
		
784
		// if there was more than one allow record, remove all allow records for this user on this doc 
785
		// with this perm type and perm order then insert a single record 
786
		if (numAllowRecords > 1) {
787
			deleteXMLAccessForPrincipal(docId, principalName, AccessControlInterface.ALLOW, permOrder);
788
			insertXMLAccess(docId, principalName, allowPermissionMask, AccessControlInterface.ALLOW, permOrder);
789
		}
790
		// if there was more than one deny record, remove all deny records for this user on this doc 
791
		// with this perm type and perm order then insert a single record 
792
		if (numDenyRecords > 1) {
793
			deleteXMLAccessForPrincipal(docId, principalName, AccessControlInterface.DENY, permOrder);
794
			insertXMLAccess(docId, principalName, denyPermissionMask, AccessControlInterface.DENY, permOrder);
795
		}
796
	}
797
	
798
	/**
799
	 * Make sure for a given list of access DAOs that only one perm order
800
	 * exists. It is assumed that all the DAOs are for the same doc
801
	 * 
802
	 * @param xmlAccessList
803
	 *            the access dao list
804
	 */
805
	private void validateDocXMLAccessList(Vector<XMLAccessDAO> xmlAccessList) throws PermOrderException {
806
		String permOrder = null;
807
		for(XMLAccessDAO xmlAccessDAO : xmlAccessList) {
808
			if (permOrder == null) {
809
				permOrder = xmlAccessDAO.getPermOrder();
810
			} else {
811
				if(!permOrder.equals(xmlAccessDAO.getPermOrder())) {
812
					throw new PermOrderException("XMLAccessAccess.validateXMLAccessList - " + 
813
						" Conflicting permission orders for document " + xmlAccessDAO.getDocId() +
814
						". Database intervention required ");
815
				}
816
			}
817
		}		
818
	}
819
	
820
	/**
821
	 * Check that only one permOrder exists for each principal. 
822
	 * TODO add check that one of each permType exists as well
823
	 * 
824
	 * @param xmlAccessList
825
	 *            the access dao list
826
	 */
827
	private void validatePrincipalXMLAccessList(Vector<XMLAccessDAO> xmlAccessList) 
828
			throws PermOrderException {
829
		
830
		boolean allowFirst = false;
831
		boolean denyFirst = false;
832
		String docId = null;
833
		
834
		// These vectors will hold all combinations of access DAOs with different permission
835
		// orders and permission types.  
836
		Vector<XMLAccessDAO> allowFirstAllows = new Vector<XMLAccessDAO>();
837
		Vector<XMLAccessDAO> allowFirstDenys = new Vector<XMLAccessDAO>();
838
		Vector<XMLAccessDAO> denyFirstAllows = new Vector<XMLAccessDAO>();
839
		Vector<XMLAccessDAO> denyFirstDenys = new Vector<XMLAccessDAO>();
840
		
841
		// sort the access dao records into the appropriate vector
842
		for(XMLAccessDAO xmlAccessDAO : xmlAccessList) {
843
			if (docId == null) {
844
				docId = xmlAccessDAO.getDocId();
845
			}
846
			if (xmlAccessDAO.getPermOrder().equals(AccessControlInterface.ALLOWFIRST)) {
847
				allowFirst = true;
848
				if (xmlAccessDAO.getPermType().equals(AccessControlInterface.ALLOW)) {
849
					allowFirstAllows.add(xmlAccessDAO);
850
				} else if (xmlAccessDAO.getPermType().equals(AccessControlInterface.DENY)) {
851
					allowFirstDenys.add(xmlAccessDAO);
852
				} else {
853
					throw new PermOrderException("XMLAccessAccess.validatePrincipalXMLAccessList - " + 
854
							" Invalid permission type: " + xmlAccessDAO.getPermType() + " for document " + 
855
							xmlAccessDAO.getDocId() + ". Database intervention required "); 
856
				}
857
			} else if (xmlAccessDAO.getPermOrder().equals(AccessControlInterface.DENYFIRST)) {
858
				denyFirst = true;
859
				if (xmlAccessDAO.getPermType().equals(AccessControlInterface.ALLOW)) {
860
					denyFirstAllows.add(xmlAccessDAO);
861
				} else if (xmlAccessDAO.getPermType().equals(AccessControlInterface.DENY)) {
862
					denyFirstDenys.add(xmlAccessDAO);
863
				} else {
864
					throw new PermOrderException("XMLAccessAccess.validatePrincipalXMLAccessList - " + 
865
							" Invalid permission type: " + xmlAccessDAO.getPermType() + " for document " + 
866
							xmlAccessDAO.getDocId() + ". Database intervention required "); 
867
				}
868
			} else {
869
				throw new PermOrderException("XMLAccessAccess.validatePrincipalXMLAccessList - " + 
870
						" Invalid permission order: " + xmlAccessDAO.getPermOrder() + " for document " + 
871
						xmlAccessDAO.getDocId() + ". Database intervention required "); 
872
			}
873
		}
874
		
875
		// for a given user, there cannot be allowfirst and denyfirst records on the same 
876
		// document
877
		if(allowFirst && denyFirst) {
878
			throw new PermOrderException("XMLAccessAccess.validatePrincipalXMLAccessList - " + 
879
					" Conflicting permission orders for document " + docId +
880
					". Database intervention required ");
881
		}	
882
	}
883
	
884
	/**
885
	 * Populate a job data object with the current row in a resultset
886
	 * 
887
	 * @param resultSet
888
	 *            the result set which is already pointing to the desired row.
889
	 * @return a scheduled job data object
890
	 */
891
	protected XMLAccessDAO populateDAO(ResultSet resultSet) throws SQLException {
892

    
893
		XMLAccessDAO xmlAccessDAO = new XMLAccessDAO();
894
		xmlAccessDAO.setDocId(resultSet.getString("docid"));
895
		xmlAccessDAO.setAccessFileId(resultSet.getString("accessfileid"));
896
		xmlAccessDAO.setPrincipalName(resultSet.getString("principal_name"));
897
		xmlAccessDAO.setPermission(resultSet.getLong("permission"));
898
		xmlAccessDAO.setPermType(resultSet.getString("perm_type"));
899
		xmlAccessDAO.setPermOrder(resultSet.getString("perm_order"));
900
		xmlAccessDAO.setBeginTime(resultSet.getDate("begin_time"));
901
		xmlAccessDAO.setEndTime(resultSet.getDate("end_time"));
902
		xmlAccessDAO.setTicketCount(resultSet.getLong("ticket_count"));
903
		xmlAccessDAO.setSubTreeId(resultSet.getString("subtreeid"));
904
		xmlAccessDAO.setStartNodeId(resultSet.getString("startnodeid"));
905
		xmlAccessDAO.setEndNodeId(resultSet.getString("endnodeid"));
906

    
907
		return xmlAccessDAO;
908
	}
909
	
910
}
(8-8/9)