src/org/ohmage/query/impl/DocumentQueries.java
private void updateEntityRoleList(String documentId, 
			Map<String, Document.Role> entityAndRolesToAdd, 
			Collection<String> entitiesToRemove, 
			String sqlInsertEntity, String sqlUpdateEntity, String sqlDeleteEntity) 
	throws CacheMissException, DataAccessException {

		// Delete roles
		if(entitiesToRemove != null) {
			Iterator<String> removeListIter = entitiesToRemove.iterator();
			while(removeListIter.hasNext()) {
				// Get the campaign's String ID.
				String entityId = removeListIter.next();
				
				// Delete the document-campaign role.
				try {
					getJdbcTemplate().update(sqlDeleteEntity, new Object[] { documentId, entityId });
				}
				catch(org.springframework.dao.DataAccessException e) {
					throw new DataAccessException("Error executing SQL '" + sqlDeleteEntity + "' with parameters: " + documentId + ", " + entityId, e);
				}
			}
		}
		
		// Add roles
		if(entityAndRolesToAdd != null) {
			Iterator<String> addMapIter = entityAndRolesToAdd.keySet().iterator();
			while(addMapIter.hasNext()) {
				// Get the entity's String ID.
				String entityId = addMapIter.next();
				
				// Get the entity's role.
				Document.Role role = entityAndRolesToAdd.get(entityId);
				
				// Add the document-entity role.
				try {
					getJdbcTemplate().update(sqlInsertEntity, 
							new Object[] { documentId, entityId, role.toString() });
				}
				catch(org.springframework.dao.DataIntegrityViolationException duplicateEntryException) {
					// If the entity is already associated with the document, then
					// they must be attempting an update.
					try {
						getJdbcTemplate().update(sqlUpdateEntity, new Object[] { role.toString(), documentId, entityId });
					}
					catch(org.springframework.dao.DataAccessException e) {
						throw new DataAccessException("Error executing SQL '" + sqlUpdateEntity + "' with parameters: " + 
								role + ", " + documentId + ", " + entityId, e);
					}
				}
				catch(org.springframework.dao.DataAccessException e) {
					throw new DataAccessException("Error executing SQL '" + sqlInsertEntity + "' with parameters: " + 
							documentId + ", " + entityId + ", " + role, e);
				}
			}
		}
	}
/**
	 * Updates the association between an group of entities and the document
	 * that is being updated.
	 * 
	 * @param documentId The unique identifier for the document that is being
	 * 					 updated.
	 * 
	 * @param entityAndRolesToAdd A Map of entity IDs to document roles that
	 * 							  should be associated with the document or, if
	 * 							  already associated should have their role
	 * 							  updated. These entities should all be of the
	 * 							  same type (user, class, campaign, etc.) and
	 * 							  should be the same as the types in 
	 * 							  'entitiesToRemove'.
	 * 
	 * @param entitiesToRemove A List of entity IDs that should no longer be
	 * 						   associated with the campaign. These entities
	 * 						   should all be of the same type (user, class,
	 * 						   campaign, etc.) and should be the same as the
	 * 						   types in 'entityAndRolesToAdd'.
	 * 
	 * @param sqlInsertEntity The SQL to use to add the entity's association
	 * 						  into the database.
	 * 
	 * @param sqlUpdateEntity The SQL to use to update the entity's role with
	 * 						  this role in the database.
	 * 
	 * @param sqlDeleteEntity The SQL to use to delete the entity's association
	 * 						  with this document.
	 * 
	 * @throws CacheMissException Thrown if there is an issue getting the 
	 * 							  database ID for the document.
	 */
Updates the association between an group of entities and the document
that is being updated.

@param documentId The unique identifier for the document that is being
updated.

@param entityAndRolesToAdd A Map of entity IDs to document roles that
should be associated with the document or, if
already associated should have their role
updated. These entities should all be of the
same type (user, class, campaign, etc.) and
should be the same as the types in
'entitiesToRemove'.

@param entitiesToRemove A List of entity IDs that should no longer be
associated with the campaign. These entities
should all be of the same type (user, class,
campaign, etc.) and should be the same as the
types in 'entityAndRolesToAdd'.

@param sqlInsertEntity The SQL to use to add the entity's association
into the database.

@param sqlUpdateEntity The SQL to use to update the entity's role with
this role in the database.

@param sqlDeleteEntity The SQL to use to delete the entity's association
with this document.

@throws CacheMissException Thrown if there is an issue getting the
database ID for the document.
Updates the association between an group of entities and the document that is being updated . @param documentId The unique identifier for the document that is being updated . @param entityAndRolesToAdd A Map of entity IDs to document roles that should be associated with the document or if already associated should have their role updated . These entities should all be of the same type ( user class campaign etc . ) and should be the same as the types in ' entitiesToRemove ' . @param entitiesToRemove A List of entity IDs that should no longer be associated with the campaign . These entities should all be of the same type ( user class campaign etc . ) and should be the same as the types in ' entityAndRolesToAdd ' . @param sqlInsertEntity The SQL to use to add the entity ' s association into the database . @param sqlUpdateEntity The SQL to use to update the entity ' s role with this role in the database . @param sqlDeleteEntity The SQL to use to delete the entity ' s association with this document . @throws CacheMissException Thrown if there is an issue getting the database ID for the document .
private void updateEntityRoleList(String documentId, 
			Map<String, Document.Role> entityAndRolesToAdd, 
			Collection<String> entitiesToRemove, 
			String sqlInsertEntity, String sqlUpdateEntity, String sqlDeleteEntity) 
	throws CacheMissException, DataAccessException {
		if(entitiesToRemove != null) {
			Iterator<String> removeListIter = entitiesToRemove.iterator();
			while(removeListIter.hasNext()) {
				String entityId = removeListIter.next();
				try {
					getJdbcTemplate().update(sqlDeleteEntity, new Object[] { documentId, entityId });
				}
				catch(org.springframework.dao.DataAccessException e) {
					throw new DataAccessException("Error executing SQL '" + sqlDeleteEntity + "' with parameters: " + documentId + ", " + entityId, e);
				}
			}
		}
		if(entityAndRolesToAdd != null) {
			Iterator<String> addMapIter = entityAndRolesToAdd.keySet().iterator();
			while(addMapIter.hasNext()) {
				String entityId = addMapIter.next();
				Document.Role role = entityAndRolesToAdd.get(entityId);
				try {
					getJdbcTemplate().update(sqlInsertEntity, 
							new Object[] { documentId, entityId, role.toString() });
				}
				catch(org.springframework.dao.DataIntegrityViolationException duplicateEntryException) {
					try {
						getJdbcTemplate().update(sqlUpdateEntity, new Object[] { role.toString(), documentId, entityId });
					}
					catch(org.springframework.dao.DataAccessException e) {
						throw new DataAccessException("Error executing SQL '" + sqlUpdateEntity + "' with parameters: " + 
								role + ", " + documentId + ", " + entityId, e);
					}
				}
				catch(org.springframework.dao.DataAccessException e) {
					throw new DataAccessException("Error executing SQL '" + sqlInsertEntity + "' with parameters: " + 
							documentId + ", " + entityId + ", " + role, e);
				}
			}
		}
	}
private void updateEntityRoleList ( String documentId , Map < String , Document . Role > entityAndRolesToAdd , Collection < String > entitiesToRemove , String sqlInsertEntity , String sqlUpdateEntity , String sqlDeleteEntity ) throws CacheMissException , DataAccessException { if ( entitiesToRemove != null ) { Iterator < String > removeListIter = entitiesToRemove . iterator ( ) ; while ( removeListIter . hasNext ( ) ) { String entityId = removeListIter . next ( ) ; try { getJdbcTemplate ( ) . update ( sqlDeleteEntity , new Object [ ] { documentId , entityId } ) ; } catch ( org . springframework . dao . DataAccessException e ) { throw new DataAccessException ( "Error executing SQL '" + sqlDeleteEntity + "' with parameters: " + documentId + ", " + entityId , e ) ; } } } if ( entityAndRolesToAdd != null ) { Iterator < String > addMapIter = entityAndRolesToAdd . keySet ( ) . iterator ( ) ; while ( addMapIter . hasNext ( ) ) { String entityId = addMapIter . next ( ) ; Document . Role role = entityAndRolesToAdd . get ( entityId ) ; try { getJdbcTemplate ( ) . update ( sqlInsertEntity , new Object [ ] { documentId , entityId , role . toString ( ) } ) ; } catch ( org . springframework . dao . DataIntegrityViolationException duplicateEntryException ) { try { getJdbcTemplate ( ) . update ( sqlUpdateEntity , new Object [ ] { role . toString ( ) , documentId , entityId } ) ; } catch ( org . springframework . dao . DataAccessException e ) { throw new DataAccessException ( "Error executing SQL '" + sqlUpdateEntity + "' with parameters: " + role + ", " + documentId + ", " + entityId , e ) ; } } catch ( org . springframework . dao . DataAccessException e ) { throw new DataAccessException ( "Error executing SQL '" + sqlInsertEntity + "' with parameters: " + documentId + ", " + entityId + ", " + role , e ) ; } } } }
Updates the association between an group of entities and the document
 that is being updated.
Updates the association between an group of entities and the document that is being updated .
// Delete roles // Get the campaign's String ID. // Delete the document-campaign role. // Add roles // Get the entity's String ID. // Get the entity's role. // Add the document-entity role. // If the entity is already associated with the document, then // they must be attempting an update.