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.List;
34
import java.util.Vector;
35

    
36
import org.apache.log4j.Logger;
37

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

    
43
public class XMLAccessAccess extends BaseAccess {
44
	
45
	private Logger logMetacat = Logger.getLogger(XMLAccessAccess.class);
46
			
47
	// Constructor
48
	public XMLAccessAccess() throws AccessException {}
49
	
50

    
51
	/**
52
	 * Get all xml access for a document
53
	 * 
54
	 * @param id
55
	 *            the id of the document
56
	 * @return an xml access DAO list
57
	 */ 
58
	public Vector<XMLAccessDAO> getXMLAccessForDoc(String guid) throws AccessException {
59

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

    
75
			String sql = "SELECT * FROM xml_access WHERE guid = ?";
76
			pstmt = conn.prepareStatement(sql);
77

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

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

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

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

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

    
410
			
411
			// Bind the values to the query
412
			pstmt.setString(1, guid);
413
			pstmt.setString(2, principalName);
414
			pstmt.setLong(3, permission);
415
			pstmt.setString(4, permType);
416
			pstmt.setString(5, permOrder);
417
			pstmt.setString(6, accessFileId);
418
			pstmt.setString(7, subTreeId);
419
			
420
			String sqlReport = "XMLAccessAccess.insertXMLAccess - SQL: " + sql;
421
			sqlReport += " [" + guid + "," + principalName + "," +  permission + "," +  permType + "," + permOrder + "]";
422
			
423
			logMetacat.info(sqlReport);
424

    
425
			pstmt.execute();
426
		} catch (SQLException sqle) {
427
			throw new AccessException("XMLAccessAccess.insertXMLAccess - SQL error when inserting"
428
					+ "xml access permissions for id: " + guid + ", principal: " + 
429
					principalName + ":" + sqle.getMessage());
430
		} finally {
431
			closeDBObjects(pstmt, conn, serialNumber, logMetacat); 
432
		}   
433
	}
434
	
435
	/**
436
	 * Update existing xml access permissions in the db.  The permission value should be the combined
437
	 * value of pre-existing permissions plus new permissions.
438
	 * 
439
	 * @param guid
440
	 *            document id
441
	 * @param principalName
442
	 *            principal credentials
443
	 * @param permission
444
	 *            permission bitmap
445
	 */
446
	private void updateXMLAccessPermission(String guid, String principalName, Long permission)
447
			throws AccessException {
448
		if (guid == null) {
449
			throw new AccessException("XMLAccessAccess.updateXMLAccessPermission - id is required when " + 
450
					"updating XML access record");
451
		}
452
		if (principalName == null) {
453
			throw new AccessException("XMLAccessAccess.updateXMLAccessPermission - principal is required when " + 
454
					"updating XML access record");
455
		}
456
		if (permission == null) {
457
			throw new AccessException("XMLAccessAccess.updateXMLAccessPermission - permission is required when " + 
458
					"updating XML access record");
459
		}
460
		
461
	    PreparedStatement pstmt = null;
462
		DBConnection conn = null;
463
		int serialNumber = -1;
464
		try {
465
			// check out DBConnection
466
			conn = DBConnectionPool.getDBConnection("XMLAccessAccess.updateXMLAccessPermission");
467
			serialNumber = conn.getCheckOutSerialNumber();
468
			
469
			String sql = "UPDATE xml_access SET permission = ?" +
470
				"WHERE guid = ? AND principal_name = ?";
471
			pstmt = conn.prepareStatement(sql);
472

    
473
			// Bind the values to the query
474
			pstmt.setLong(1, permission);
475
			pstmt.setString(2, guid);
476
			pstmt.setString(3, principalName);
477
			
478
			String sqlReport = "XMLAccessAccess.updateXMLAccessPermission - SQL: " + sql;
479
			sqlReport += " [" + permission + "," + guid + "," + principalName + "]";
480
			
481
			logMetacat.info(sqlReport);
482

    
483
			pstmt.execute();
484
		} catch (SQLException sqle) {
485
			throw new AccessException("XMLAccessAccess.updateXMLAccessPermission - SQL error when updating"
486
					+ "xml access permissions for id: " + guid + ", principal: " + 
487
					principalName + ":" + sqle.getMessage());
488
		} finally {
489
			closeDBObjects(pstmt, conn, serialNumber, logMetacat); 
490
		}
491
		
492
	}
493
	
494
	/**
495
	 * Remove xml access.  This modifies the access in the database for a principal 
496
	 * for a given document.  If the provided permission is exactly the same as what 
497
	 * the principal has, the record is deleted from the database.
498
	 * 
499
	 * @param guid
500
	 *            document id
501
	 * @param principalName
502
	 *            principal credentials
503
	 */
504
	public void removeXMLAccessForPrincipal(String guid, String principalName, Long permission) throws AccessException {
505
		if (guid == null) {
506
			throw new AccessException("XMLAccessAccess.removeXMLAccessForPrincipal - id is required when " + 
507
					"removing XML access");
508
		}
509
		if (principalName == null) {
510
			throw new AccessException("XMLAccessAccess.removeXMLAccessForPrincipal - principal is required when " + 
511
					"deleting XML access");
512
		}
513
		if (permission == null) {
514
			throw new AccessException("XMLAccessAccess.removeXMLAccessForPrincipal - permission is required when " + 
515
					"updating XML access");
516
		}
517
		
518
		Vector<XMLAccessDAO> xmlAccessList = getXMLAccessForPrincipal(guid, principalName);
519
		if (xmlAccessList.size() == 0) {
520
			logMetacat.warn("XMLAccessAccess.removeXMLAccessForPrincipal - attempting to remove access when no " +
521
				"access record exists for id: " + guid + ", principal: " + principalName);
522
		} else {
523
			long permissionMask = 0;
524
			for (XMLAccessDAO xmlAccessDAO : xmlAccessList) {
525
				permissionMask |= xmlAccessDAO.getPermission();
526
			}
527
			permissionMask |= permission;
528
			
529
			// in this case, the only existing permissions are the ones we want to remove, so 
530
			// delete the record(s) for this principal on this document
531
			if ((permissionMask & permission) == permission) {
532
				deleteXMLAccessForPrincipal(guid, principalName);
533
			}
534
			
535
			if (xmlAccessList.size() > 1) {
536
				
537
			} else {
538
				updateXMLAccessPermission(guid, principalName, permission);
539
			}
540
		}
541
	   
542
	}
543
	
544
	/**
545
	 * Delete xml access.  This removes all access records from the database for a given document
546
	 * 
547
	 * @param id
548
	 *            document id
549
	 */
550
	public void deleteXMLAccessForDoc(String guid) throws AccessException {
551
		if (guid == null) {
552
			throw new AccessException("XMLAccessAccess.deleteXMLAccessForPrincipal - id is required when " + 
553
					"deleting XML access record");
554
		}
555
		
556
	    PreparedStatement pstmt = null;
557
		DBConnection conn = null;
558
		int serialNumber = -1;
559
		try {
560
			
561
			// check out DBConnection
562
			conn = DBConnectionPool.getDBConnection("XMLAccessAccess.deleteXMLAccessForDoc");
563
    		serialNumber = conn.getCheckOutSerialNumber();
564
    		
565
			String sql = "DELETE FROM xml_access WHERE guid = ?";
566
			pstmt = conn.prepareStatement(sql);
567

    
568
			// Bind the values to the query
569
			pstmt.setString(1, guid);
570

    
571
			String sqlReport = "XMLAccessAccess.deleteXMLAccessForDoc - SQL: " + sql;
572
			sqlReport += " [" + guid + "]";
573
			
574
			logMetacat.info(sqlReport);
575

    
576
			pstmt.execute();
577
		} catch (SQLException sqle) {
578
			throw new AccessException("XMLAccessAccess.deleteXMLAccessForDoc - SQL error when deleting"
579
					+ "xml access permissions for id: " + guid + ":" + sqle.getMessage());
580
		} finally {
581
			closeDBObjects(pstmt, conn, serialNumber, logMetacat); 
582
		}	   
583
	}
584
	
585
	/**
586
	 * Delete xml access.  This removes all access records from the database for a principal 
587
	 * for a given document
588
	 * 
589
	 * @param guid
590
	 *            document id
591
	 * @param principal
592
	 *            principal credentials
593
	 */
594
	private void deleteXMLAccessForPrincipal(String guid, String principalName) throws AccessException {
595
		if (guid == null) {
596
			throw new AccessException("XMLAccessAccess.deleteXMLAccessForPrincipal - id is required when " + 
597
					"deleting XML access record");
598
		}
599
		if (principalName == null) {
600
			throw new AccessException("XMLAccessAccess.deleteXMLAccessForPrincipal - principal is required when " + 
601
					"deleting XML access record");
602
		}
603
		
604
	    PreparedStatement pstmt = null;
605
		DBConnection conn = null;
606
		int serialNumber = -1;
607
		try {
608
			// check out DBConnection
609
			conn = DBConnectionPool.getDBConnection("XMLAccessAccess.deleteXMLAccessForPrincipal");
610
    		serialNumber = conn.getCheckOutSerialNumber();
611
    		
612
			String sql = "DELETE FROM xml_access WHERE guid = ? AND principal_name = ?";
613
			pstmt = conn.prepareStatement(sql);
614

    
615
			// Bind the values to the query
616
			pstmt.setString(1, guid);
617
			pstmt.setString(2, principalName);
618

    
619
			String sqlReport = "XMLAccessAccess.deleteXMLAccessForPrincipal - SQL: " + sql;
620
			sqlReport += " [" + guid + "," + principalName + "]";
621
			
622
			logMetacat.info(sqlReport);
623

    
624
			pstmt.execute();
625
		} catch (SQLException sqle) {
626
			throw new AccessException("XMLAccessAccess.deleteXMLAccessForPrincipal - SQL error when deleting"
627
					+ "xml access permissions for id: " + guid + ", principal: " + 
628
					principalName + ":" + sqle.getMessage());
629
		} finally {
630
			closeDBObjects(pstmt, conn, serialNumber, logMetacat);
631
		}	   
632
	}
633
	
634
	/**
635
	 * Delete xml access.  This removes all access records from the database for a principal 
636
	 * for a given document
637
	 * 
638
	 * @param guid
639
	 *            document id
640
	 * @param principal
641
	 *            principal credentials
642
	 */
643
	public void deleteXMLAccessForDoc(String guid, String permType) throws AccessException {
644
		if (guid == null) {
645
			throw new AccessException("XMLAccessAccess.deleteXMLAccessForDoc - id is required when " + 
646
					"deleting XML access record");
647
		}
648
		if (permType == null) {
649
			throw new AccessException("XMLAccessAccess.deleteXMLAccessForDoc - permType is required when " + 
650
					"deleting XML access record");
651
		}
652
		
653
	    PreparedStatement pstmt = null;
654
		DBConnection conn = null;
655
		int serialNumber = -1;
656
		try {
657
			// check out DBConnection
658
			conn = DBConnectionPool.getDBConnection("XMLAccessAccess.deleteXMLAccessForDoc");
659
    		serialNumber = conn.getCheckOutSerialNumber();
660
    		
661
			String sql = "DELETE FROM xml_access WHERE guid = ? AND perm_type = ?";
662
			pstmt = conn.prepareStatement(sql);
663

    
664
			// Bind the values to the query
665
			pstmt.setString(1, guid);
666
			pstmt.setString(2, permType);
667

    
668
			String sqlReport = "XMLAccessAccess.deleteXMLAccessForDoc - SQL: " + sql;
669
			sqlReport += " [" + guid + "," + permType + "]";
670
			
671
			logMetacat.info(sqlReport);
672

    
673
			pstmt.execute();
674
		} catch (SQLException sqle) {
675
			throw new AccessException("XMLAccessAccess.deleteXMLAccessForDoc - SQL error when deleting"
676
					+ "xml access permissions for id: " + guid + ", permType: " + 
677
					permType + ":" + sqle.getMessage());
678
		} finally {
679
			closeDBObjects(pstmt, conn, serialNumber, logMetacat);
680
		}	   
681
	}
682
	
683
	/**
684
	 * Checks to see if there is a permission order conflict for a given document.  Each 
685
	 * document is only allowed to have a single permission order
686
	 * 
687
	 * @param guid
688
	 *            document id
689
	 * @param principal
690
	 *            principal credentials
691
	 */
692
	private void permOrderConflict(String guid, String permOrder) throws AccessException, PermOrderException {
693
		if (guid == null) {
694
			throw new AccessException("XMLAccessAccess.permOrderConflict - id is required when " + 
695
					"determining perm order conflict");
696
		}
697
		if (permOrder == null) {
698
			throw new AccessException("XMLAccessAccess.permOrderConflict - perm order is required when " + 
699
					"determining perm order conflict");
700
		}
701
		
702
	    PreparedStatement pstmt = null;
703
		DBConnection conn = null;
704
		int serialNumber = -1;
705
		try {
706
			// check out DBConnection
707
			conn = DBConnectionPool.getDBConnection("XMLAccessAccess.permOrderConflict");
708
    		serialNumber = conn.getCheckOutSerialNumber();
709
    		
710
			String sql = "SELECT * FROM xml_access WHERE guid = ? AND perm_order != ?";
711
			pstmt = conn.prepareStatement(sql);
712

    
713
			// Bind the values to the query
714
			pstmt.setString(1, guid);
715
			pstmt.setString(2, permOrder);
716
			
717
			String sqlReport = "XMLAccessAccess.permOrderConflict - SQL: " + sql;
718
			sqlReport += " [" + guid + "," + permOrder + "]";
719
			
720
			logMetacat.info(sqlReport);
721

    
722
			pstmt.execute();
723
			
724
			ResultSet resultSet = pstmt.getResultSet();
725
			if (resultSet.next()) {			
726
				throw new PermOrderException("XMLAccessAccess.addXMLAccess - cannot add permission " + 
727
					"record for id: " + guid + "with permOrder: " + permOrder + " due to permOrder conflict");
728
			}
729
		} catch (SQLException sqle) {
730
			throw new AccessException("XMLAccessAccess.permOrderConflict - SQL error when checking"
731
					+ "for perm order conflict on: " + guid + ":" + sqle.getMessage());
732
		} finally {
733
			closeDBObjects(pstmt, conn, serialNumber, logMetacat); 
734
		}
735
	   
736
	}
737
	
738
	/**
739
	 * Delete xml access.  This removes all access records from the database for a principal 
740
	 * for a given document, perm type and perm order
741
	 * 
742
	 * @param guid
743
	 *            document id
744
	 * @param principal
745
	 *            principal credentials
746
	 */
747
	private void deleteXMLAccessForPrincipal(String guid, String principalName, String permType, String permOrder) throws AccessException {
748
		if (guid == null) {
749
			throw new AccessException("XMLAccessAccess.deleteXMLAccessForPrincipal - id is required when " + 
750
					"deleting XML access record");
751
		}
752
		if (principalName == null) {
753
			throw new AccessException("XMLAccessAccess.deleteXMLAccessForPrincipal - principal is required when " + 
754
					"deleting XML access record");
755
		}
756
		if (permType == null) {
757
			throw new AccessException(
758
					"XMLAccessAccess.deleteXMLAccessForPrincipal - perm type is required when "
759
							+ "deleting XML access record");
760
		}
761
		if (permOrder == null) {
762
			throw new AccessException(
763
					"XMLAccessAccess.deleteXMLAccessForPrincipal - perm order is required when "
764
							+ "deleting XML access record");
765
		}
766
		
767
	    PreparedStatement pstmt = null;
768
		DBConnection conn = null;
769
		int serialNumber = -1;
770
		try {
771
			// check out DBConnection
772
			conn = DBConnectionPool.getDBConnection("XMLAccessAccess.deleteXMLAccessForPrincipal");
773
    		serialNumber = conn.getCheckOutSerialNumber();
774
    		
775
			String sql = "DELETE FROM xml_access WHERE guid = ? AND principal_name = ? " +
776
				"AND perm_type = ? AND perm_order = ?";
777
			pstmt = conn.prepareStatement(sql);
778

    
779
			// Bind the values to the query
780
			pstmt.setString(1, guid);
781
			pstmt.setString(2, principalName);
782
			pstmt.setString(3, permType);
783
			pstmt.setString(4, permOrder);
784
			
785
			String sqlReport = "XMLAccessAccess.deleteXMLAccessForPrincipal - SQL: " + sql;
786
			sqlReport += " [" + guid + "," + principalName + "," + permType + "," + permOrder + "]";
787
			
788
			logMetacat.info(sqlReport);
789

    
790
			pstmt.execute();
791
		} catch (SQLException sqle) {
792
			throw new AccessException("XMLAccessAccess.deleteXMLAccessForPrincipal - SQL error when deleting"
793
					+ "xml access permissions for id: " + guid + ", principal: " + 
794
					principalName + ":" + sqle.getMessage());
795
		} finally {
796
			closeDBObjects(pstmt, conn, serialNumber, logMetacat); 
797
		}
798
	   
799
	}
800

    
801
	/**
802
	 * Make sure that only one record exists per principal/permType/document. If
803
	 * more than one record exists, delete the existing records, consolidate the
804
	 * permissions insert the new record.
805
	 * 
806
	 * @param xmlAccessList the access dao list
807
	 */
808
	private void cleanupXMLAccessForPrincipal(Vector<XMLAccessDAO> xmlAccessList) throws AccessException{
809
		
810
		int numAllowRecords = 0;
811
		int numDenyRecords = 0;
812
		long allowPermissionMask = 0;
813
		long denyPermissionMask = 0;
814
		String guid = null;
815
		String principalName = null;
816
		String permType = null;
817
		String permOrder = null;
818
		// TODO: handle these fields
819
		String accessFileId = null;
820
		String subTreeId = null;
821
	
822
		
823
		// iterate through the list of access dao objects and bttwise or the permissions.  Most
824
		// of this is just doing some error checking to make sure each record is valid.
825
		for (XMLAccessDAO xmlAccessDAO : xmlAccessList) {
826
			String daoId = xmlAccessDAO.getGuid();
827
			if (guid == null) {
828
				guid = daoId;
829
			} else {
830
				if (!guid.equals(daoId)) {
831
					throw new AccessException("XMLAccessAccess.cleanupXMLAccessForPrincipal - " + 
832
							" Conflicting ids " + daoId + " and " + guid);
833
				}
834
			}
835
			if (principalName == null) {
836
				principalName = xmlAccessDAO.getPrincipalName();
837
			} else {
838
				if (!principalName.equals(xmlAccessDAO.getPrincipalName())) {
839
					throw new AccessException("XMLAccessAccess.cleanupXMLAccessForPrincipal - " + 
840
							" Conflicting prinicpal names " + xmlAccessDAO.getPrincipalName() +
841
							" and principalName " + principalName);
842
				}
843
			}
844
			if (permType == null) {
845
				permType = xmlAccessDAO.getPermType();
846
			} else {
847
				if (!permType.equals(xmlAccessDAO.getPermType())) {
848
					throw new AccessException("XMLAccessAccess.cleanupXMLAccessForPrincipal - " + 
849
							" Conflicting permission orders for document " + daoId +
850
							"principalName " + principalName + ". Database intervention required ");
851
				}
852
			}
853
			if (permOrder == null) {
854
				permOrder = xmlAccessDAO.getPermOrder();
855
			} else {
856
				if (!permOrder.equals(xmlAccessDAO.getPermOrder())) {
857
					throw new AccessException("XMLAccessAccess.cleanupXMLAccessForPrincipal - " + 
858
							" Conflicting permission types for document " + daoId +
859
							"principalName " + principalName + ". Database intervention required ");
860
				}
861
			}
862
			if (permType == null) {
863
				permType = xmlAccessDAO.getPermType();
864
			} else {
865
				if (!permType.equals(xmlAccessDAO.getPermType())) {
866
					throw new AccessException("XMLAccessAccess.cleanupXMLAccessForPrincipal - " + 
867
							" Conflicting permission orders for document " + daoId +
868
							"principalName " + principalName + ". Database intervention required ");
869
				}
870
			}
871
			if (permType.equals(AccessControlInterface.ALLOW)) {
872
				numAllowRecords++;
873
				allowPermissionMask |= xmlAccessDAO.getPermission();
874
			} else if (permType.equals(AccessControlInterface.DENY)) {
875
				numDenyRecords++;
876
				denyPermissionMask |= xmlAccessDAO.getPermission();
877
			}
878
			if (accessFileId == null) {
879
				accessFileId = xmlAccessDAO.getAccessFileId();
880
			}
881
			if (subTreeId == null) {
882
				subTreeId = xmlAccessDAO.getSubTreeId();
883
			}
884
		}
885
		
886
		// if there was more than one allow record, remove all allow records for this user on this doc 
887
		// with this perm type and perm order then insert a single record 
888
		if (numAllowRecords > 1) {
889
			deleteXMLAccessForPrincipal(guid, principalName, AccessControlInterface.ALLOW, permOrder);
890
			insertXMLAccess(guid, principalName, allowPermissionMask, AccessControlInterface.ALLOW, permOrder, accessFileId, subTreeId);
891
		}
892
		// if there was more than one deny record, remove all deny records for this user on this doc 
893
		// with this perm type and perm order then insert a single record 
894
		if (numDenyRecords > 1) {
895
			deleteXMLAccessForPrincipal(guid, principalName, AccessControlInterface.DENY, permOrder);
896
			insertXMLAccess(guid, principalName, denyPermissionMask, AccessControlInterface.DENY, permOrder, accessFileId, subTreeId);
897
		}
898
	}
899
	
900
	/**
901
	 * Make sure for a given list of access DAOs that only one perm order
902
	 * exists. It is assumed that all the DAOs are for the same doc
903
	 * 
904
	 * @param xmlAccessList
905
	 *            the access dao list
906
	 */
907
	private void validateDocXMLAccessList(Vector<XMLAccessDAO> xmlAccessList) throws PermOrderException {
908
		String permOrder = null;
909
		for(XMLAccessDAO xmlAccessDAO : xmlAccessList) {
910
			String daoId = xmlAccessDAO.getGuid();
911
			if (permOrder == null) {
912
				permOrder = xmlAccessDAO.getPermOrder();
913
			} else {
914
				if(!permOrder.equals(xmlAccessDAO.getPermOrder())) {
915
					throw new PermOrderException("XMLAccessAccess.validateXMLAccessList - " + 
916
						" Conflicting permission orders for document " + daoId +
917
						". Database intervention required ");
918
				}
919
			}
920
		}		
921
	}
922
	
923
	/**
924
	 * Check that only one permOrder exists for each principal. 
925
	 * TODO add check that one of each permType exists as well
926
	 * 
927
	 * @param xmlAccessList
928
	 *            the access dao list
929
	 */
930
	private void validatePrincipalXMLAccessList(Vector<XMLAccessDAO> xmlAccessList) 
931
			throws PermOrderException {
932
		
933
		boolean allowFirst = false;
934
		boolean denyFirst = false;
935
		String guid = null;
936
		
937
		// These vectors will hold all combinations of access DAOs with different permission
938
		// orders and permission types.  
939
		Vector<XMLAccessDAO> allowFirstAllows = new Vector<XMLAccessDAO>();
940
		Vector<XMLAccessDAO> allowFirstDenys = new Vector<XMLAccessDAO>();
941
		Vector<XMLAccessDAO> denyFirstAllows = new Vector<XMLAccessDAO>();
942
		Vector<XMLAccessDAO> denyFirstDenys = new Vector<XMLAccessDAO>();
943
		
944
		// sort the access dao records into the appropriate vector
945
		for(XMLAccessDAO xmlAccessDAO : xmlAccessList) {
946
			String daoId = xmlAccessDAO.getGuid();
947
			if (guid == null) {
948
				guid = daoId;
949
			}
950
			if (xmlAccessDAO.getPermOrder().equals(AccessControlInterface.ALLOWFIRST)) {
951
				allowFirst = true;
952
				if (xmlAccessDAO.getPermType().equals(AccessControlInterface.ALLOW)) {
953
					allowFirstAllows.add(xmlAccessDAO);
954
				} else if (xmlAccessDAO.getPermType().equals(AccessControlInterface.DENY)) {
955
					allowFirstDenys.add(xmlAccessDAO);
956
				} else {
957
					throw new PermOrderException("XMLAccessAccess.validatePrincipalXMLAccessList - " + 
958
							" Invalid permission type: " + xmlAccessDAO.getPermType() + " for document " + 
959
							daoId + ". Database intervention required "); 
960
				}
961
			} else if (xmlAccessDAO.getPermOrder().equals(AccessControlInterface.DENYFIRST)) {
962
				denyFirst = true;
963
				if (xmlAccessDAO.getPermType().equals(AccessControlInterface.ALLOW)) {
964
					denyFirstAllows.add(xmlAccessDAO);
965
				} else if (xmlAccessDAO.getPermType().equals(AccessControlInterface.DENY)) {
966
					denyFirstDenys.add(xmlAccessDAO);
967
				} else {
968
					throw new PermOrderException("XMLAccessAccess.validatePrincipalXMLAccessList - " + 
969
							" Invalid permission type: " + xmlAccessDAO.getPermType() + " for document " + 
970
							daoId + ". Database intervention required "); 
971
				}
972
			} else {
973
				throw new PermOrderException("XMLAccessAccess.validatePrincipalXMLAccessList - " + 
974
						" Invalid permission order: " + xmlAccessDAO.getPermOrder() + " for document " + 
975
						daoId + ". Database intervention required "); 
976
			}
977
		}
978
		
979
		// for a given user, there cannot be allowfirst and denyfirst records on the same 
980
		// document
981
		if(allowFirst && denyFirst) {
982
			throw new PermOrderException("XMLAccessAccess.validatePrincipalXMLAccessList - " + 
983
					" Conflicting permission orders for document " + guid +
984
					". Database intervention required ");
985
		}	
986
	}
987
	
988
	/**
989
	 * Populate a job data object with the current row in a resultset
990
	 * 
991
	 * @param resultSet
992
	 *            the result set which is already pointing to the desired row.
993
	 * @return a scheduled job data object
994
	 */
995
	protected XMLAccessDAO populateDAO(ResultSet resultSet) throws SQLException {
996

    
997
		XMLAccessDAO xmlAccessDAO = new XMLAccessDAO();
998
		xmlAccessDAO.setGuid(resultSet.getString("guid"));
999
		xmlAccessDAO.setAccessFileId(resultSet.getString("accessfileid"));
1000
		xmlAccessDAO.setPrincipalName(resultSet.getString("principal_name"));
1001
		xmlAccessDAO.setPermission(resultSet.getLong("permission"));
1002
		xmlAccessDAO.setPermType(resultSet.getString("perm_type"));
1003
		xmlAccessDAO.setPermOrder(resultSet.getString("perm_order"));
1004
		xmlAccessDAO.setBeginTime(resultSet.getDate("begin_time"));
1005
		xmlAccessDAO.setEndTime(resultSet.getDate("end_time"));
1006
		xmlAccessDAO.setTicketCount(resultSet.getLong("ticket_count"));
1007
		xmlAccessDAO.setSubTreeId(resultSet.getString("subtreeid"));
1008
		xmlAccessDAO.setStartNodeId(resultSet.getString("startnodeid"));
1009
		xmlAccessDAO.setEndNodeId(resultSet.getString("endnodeid"));
1010

    
1011
		return xmlAccessDAO;
1012
	}
1013
	
1014
}
(8-8/9)