src/org/ohmage/query/impl/CampaignQueries.java
private void associateCampaignAndClass(PlatformTransactionManager transactionManager, TransactionStatus status, String campaignId, String classId) 
		throws DataAccessException {
		
		// Associate this class to the campaign.
		try {
			getJdbcTemplate().update(SQL_INSERT_CAMPAIGN_CLASS, new Object[] { campaignId, classId });
		}
		catch(org.springframework.dao.DuplicateKeyException e) {
			// If the campaign was already associated with the class, ignore
			// this call.
			return;
		}
		catch(org.springframework.dao.DataAccessException e) {
			transactionManager.rollback(status);
			throw new DataAccessException("Error executing SQL '" + SQL_INSERT_CAMPAIGN_CLASS + "' with parameters: " + 
					campaignId + ", " + classId, e);
		}
		
		// Insert the default campaign_class_default_role
		// relationships for privileged users.
		// TODO: This should be a parameter in the API.
		try {
			getJdbcTemplate().update(
					SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE, 
					new Object[] { 
							campaignId, 
							classId, 
							Clazz.Role.PRIVILEGED.toString(), 
							Campaign.Role.SUPERVISOR.toString() }
				);
		}
		catch(org.springframework.dao.DataAccessException e) {
			transactionManager.rollback(status);
			throw new DataAccessException("Error executing SQL '" + SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE + "' with parameters: " + 
					campaignId + ", " + classId + ", " + Clazz.Role.PRIVILEGED + ", " + Campaign.Role.SUPERVISOR, e);
		}
		try {
			getJdbcTemplate().update(
					SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE, 
					new Object[] { 
							campaignId, 
							classId, 
							Clazz.Role.PRIVILEGED.toString(), 
							Campaign.Role.PARTICIPANT.toString() }
					);
		}
		catch(org.springframework.dao.DataAccessException e) {
			transactionManager.rollback(status);
			throw new DataAccessException("Error executing SQL '" + SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE + "' with parameters: " + 
					campaignId + ", " + classId + ", " + Clazz.Role.PRIVILEGED + ", " + Campaign.Role.PARTICIPANT, e);
		}
		
		// Insert the default campaign_class_default_role
		// relationships for restricted users.
		// TODO: This should be a parameter in the API.
		try {
			getJdbcTemplate().update(
					SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE, 
					new Object[] { 
							campaignId, 
							classId, 
							Clazz.Role.RESTRICTED.toString(), 
							Campaign.Role.ANALYST.toString() }
					);
		}
		catch(org.springframework.dao.DataAccessException e) {
			transactionManager.rollback(status);
			throw new DataAccessException("Error executing SQL '" + SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE + "' with parameters: " + 
					campaignId + ", " + classId + ", " + Clazz.Role.RESTRICTED + ", " + Campaign.Role.ANALYST, e);
		}
		try {
			getJdbcTemplate().update(
					SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE, 
					new Object[] { 
							campaignId,
							classId,
							Clazz.Role.RESTRICTED.toString(), 
							Campaign.Role.PARTICIPANT.toString() }
					);
		}
		catch(org.springframework.dao.DataAccessException e) {
			transactionManager.rollback(status);
			throw new DataAccessException("Error executing SQL '" + SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE + "' with parameters: " + 
					campaignId + ", " + classId + ", " + Clazz.Role.RESTRICTED + ", " + Campaign.Role.PARTICIPANT, e);
		}
		
		// Get the list of users in the class.
		List<String> usernames;
		try {
			usernames = userClassQueries.getUsersInClass(classId);
		}
		catch(DataAccessException e) {
			transactionManager.rollback(status);
			throw e;
		}
		
		// For each of the users in the class, assign them their default roles
		// in the campaign.
		for(String username : usernames) {
			List<Campaign.Role> roles;
			try {
				roles = getJdbcTemplate().query(
						SQL_GET_USER_DEFAULT_ROLES, 
						new Object[] { username, campaignId, classId }, 
						new RowMapper<Campaign.Role>() {
							@Override
							public Campaign.Role mapRow(ResultSet rs, int rowNum) throws SQLException {
								return Campaign.Role.getValue(rs.getString("role"));
							}
						});
			}
			catch(org.springframework.dao.DataAccessException e) {
				transactionManager.rollback(status);
				throw new DataAccessException("Error executing SQL '" + SQL_GET_USER_DEFAULT_ROLES + "' with parameters: " + 
						username + ", " + campaignId + ", " + classId, e);
			}
			
			for(Campaign.Role role : roles) {
				try {
					getJdbcTemplate().update(
							SQL_INSERT_USER_ROLE_CAMPAIGN, 
							new Object[] { username, campaignId, role.toString() });
				}
				catch(org.springframework.dao.DuplicateKeyException e) {
					// If the user already has the role in the campaign then
					// ignore it.
				}
				catch(org.springframework.dao.DataAccessException e) {
					transactionManager.rollback(status);
					throw new DataAccessException("Error executing SQL '" + SQL_INSERT_USER_ROLE_CAMPAIGN + "' with parameters: " + 
							username + ", " + campaignId + ", " + role, e);
				}
			}
		}
	}
/**
	 * Creates the association between a class and a campaign in the database.
	 * It then creates a set of default roles for all users of the classes and
	 * adds all of the users in the class to the campaign with the default 
	 * roles.
	 * 
	 * @param transactionManager The PlatformTransactionManager that is 
	 * 							 managing the transaction from which this was
	 * 							 called.
	 * 
	 * @param status The current status of the transaction.
	 * 
	 * @param campaignId The unique identifier for the campaign.
	 * 
	 * @param classId The unique identifier for the class.
	 */
Creates the association between a class and a campaign in the database.
It then creates a set of default roles for all users of the classes and
adds all of the users in the class to the campaign with the default
roles.

@param transactionManager The PlatformTransactionManager that is
managing the transaction from which this was
called.

@param status The current status of the transaction.

@param campaignId The unique identifier for the campaign.

@param classId The unique identifier for the class.
Creates the association between a class and a campaign in the database . It then creates a set of default roles for all users of the classes and adds all of the users in the class to the campaign with the default roles . @param transactionManager The PlatformTransactionManager that is managing the transaction from which this was called . @param status The current status of the transaction . @param campaignId The unique identifier for the campaign . @param classId The unique identifier for the class .
private void associateCampaignAndClass(PlatformTransactionManager transactionManager, TransactionStatus status, String campaignId, String classId) 
		throws DataAccessException {
		try {
			getJdbcTemplate().update(SQL_INSERT_CAMPAIGN_CLASS, new Object[] { campaignId, classId });
		}
		catch(org.springframework.dao.DuplicateKeyException e) {
			return;
		}
		catch(org.springframework.dao.DataAccessException e) {
			transactionManager.rollback(status);
			throw new DataAccessException("Error executing SQL '" + SQL_INSERT_CAMPAIGN_CLASS + "' with parameters: " + 
					campaignId + ", " + classId, e);
		}
		try {
			getJdbcTemplate().update(
					SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE, 
					new Object[] { 
							campaignId, 
							classId, 
							Clazz.Role.PRIVILEGED.toString(), 
							Campaign.Role.SUPERVISOR.toString() }
				);
		}
		catch(org.springframework.dao.DataAccessException e) {
			transactionManager.rollback(status);
			throw new DataAccessException("Error executing SQL '" + SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE + "' with parameters: " + 
					campaignId + ", " + classId + ", " + Clazz.Role.PRIVILEGED + ", " + Campaign.Role.SUPERVISOR, e);
		}
		try {
			getJdbcTemplate().update(
					SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE, 
					new Object[] { 
							campaignId, 
							classId, 
							Clazz.Role.PRIVILEGED.toString(), 
							Campaign.Role.PARTICIPANT.toString() }
					);
		}
		catch(org.springframework.dao.DataAccessException e) {
			transactionManager.rollback(status);
			throw new DataAccessException("Error executing SQL '" + SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE + "' with parameters: " + 
					campaignId + ", " + classId + ", " + Clazz.Role.PRIVILEGED + ", " + Campaign.Role.PARTICIPANT, e);
		}
		try {
			getJdbcTemplate().update(
					SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE, 
					new Object[] { 
							campaignId, 
							classId, 
							Clazz.Role.RESTRICTED.toString(), 
							Campaign.Role.ANALYST.toString() }
					);
		}
		catch(org.springframework.dao.DataAccessException e) {
			transactionManager.rollback(status);
			throw new DataAccessException("Error executing SQL '" + SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE + "' with parameters: " + 
					campaignId + ", " + classId + ", " + Clazz.Role.RESTRICTED + ", " + Campaign.Role.ANALYST, e);
		}
		try {
			getJdbcTemplate().update(
					SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE, 
					new Object[] { 
							campaignId,
							classId,
							Clazz.Role.RESTRICTED.toString(), 
							Campaign.Role.PARTICIPANT.toString() }
					);
		}
		catch(org.springframework.dao.DataAccessException e) {
			transactionManager.rollback(status);
			throw new DataAccessException("Error executing SQL '" + SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE + "' with parameters: " + 
					campaignId + ", " + classId + ", " + Clazz.Role.RESTRICTED + ", " + Campaign.Role.PARTICIPANT, e);
		}
		List<String> usernames;
		try {
			usernames = userClassQueries.getUsersInClass(classId);
		}
		catch(DataAccessException e) {
			transactionManager.rollback(status);
			throw e;
		}
		for(String username : usernames) {
			List<Campaign.Role> roles;
			try {
				roles = getJdbcTemplate().query(
						SQL_GET_USER_DEFAULT_ROLES, 
						new Object[] { username, campaignId, classId }, 
						new RowMapper<Campaign.Role>() {
							@Override
							public Campaign.Role mapRow(ResultSet rs, int rowNum) throws SQLException {
								return Campaign.Role.getValue(rs.getString("role"));
							}
						});
			}
			catch(org.springframework.dao.DataAccessException e) {
				transactionManager.rollback(status);
				throw new DataAccessException("Error executing SQL '" + SQL_GET_USER_DEFAULT_ROLES + "' with parameters: " + 
						username + ", " + campaignId + ", " + classId, e);
			}
			for(Campaign.Role role : roles) {
				try {
					getJdbcTemplate().update(
							SQL_INSERT_USER_ROLE_CAMPAIGN, 
							new Object[] { username, campaignId, role.toString() });
				}
				catch(org.springframework.dao.DuplicateKeyException e) {
				}
				catch(org.springframework.dao.DataAccessException e) {
					transactionManager.rollback(status);
					throw new DataAccessException("Error executing SQL '" + SQL_INSERT_USER_ROLE_CAMPAIGN + "' with parameters: " + 
							username + ", " + campaignId + ", " + role, e);
				}
			}
		}
	}
private void associateCampaignAndClass ( PlatformTransactionManager transactionManager , TransactionStatus status , String campaignId , String classId ) throws DataAccessException { try { getJdbcTemplate ( ) . update ( SQL_INSERT_CAMPAIGN_CLASS , new Object [ ] { campaignId , classId } ) ; } catch ( org . springframework . dao . DuplicateKeyException e ) { return ; } catch ( org . springframework . dao . DataAccessException e ) { transactionManager . rollback ( status ) ; throw new DataAccessException ( "Error executing SQL '" + SQL_INSERT_CAMPAIGN_CLASS + "' with parameters: " + campaignId + ", " + classId , e ) ; } try { getJdbcTemplate ( ) . update ( SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE , new Object [ ] { campaignId , classId , Clazz . Role . PRIVILEGED . toString ( ) , Campaign . Role . SUPERVISOR . toString ( ) } ) ; } catch ( org . springframework . dao . DataAccessException e ) { transactionManager . rollback ( status ) ; throw new DataAccessException ( "Error executing SQL '" + SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE + "' with parameters: " + campaignId + ", " + classId + ", " + Clazz . Role . PRIVILEGED + ", " + Campaign . Role . SUPERVISOR , e ) ; } try { getJdbcTemplate ( ) . update ( SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE , new Object [ ] { campaignId , classId , Clazz . Role . PRIVILEGED . toString ( ) , Campaign . Role . PARTICIPANT . toString ( ) } ) ; } catch ( org . springframework . dao . DataAccessException e ) { transactionManager . rollback ( status ) ; throw new DataAccessException ( "Error executing SQL '" + SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE + "' with parameters: " + campaignId + ", " + classId + ", " + Clazz . Role . PRIVILEGED + ", " + Campaign . Role . PARTICIPANT , e ) ; } try { getJdbcTemplate ( ) . update ( SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE , new Object [ ] { campaignId , classId , Clazz . Role . RESTRICTED . toString ( ) , Campaign . Role . ANALYST . toString ( ) } ) ; } catch ( org . springframework . dao . DataAccessException e ) { transactionManager . rollback ( status ) ; throw new DataAccessException ( "Error executing SQL '" + SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE + "' with parameters: " + campaignId + ", " + classId + ", " + Clazz . Role . RESTRICTED + ", " + Campaign . Role . ANALYST , e ) ; } try { getJdbcTemplate ( ) . update ( SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE , new Object [ ] { campaignId , classId , Clazz . Role . RESTRICTED . toString ( ) , Campaign . Role . PARTICIPANT . toString ( ) } ) ; } catch ( org . springframework . dao . DataAccessException e ) { transactionManager . rollback ( status ) ; throw new DataAccessException ( "Error executing SQL '" + SQL_INSERT_CAMPAIGN_CLASS_DEFAULT_ROLE + "' with parameters: " + campaignId + ", " + classId + ", " + Clazz . Role . RESTRICTED + ", " + Campaign . Role . PARTICIPANT , e ) ; } List < String > usernames ; try { usernames = userClassQueries . getUsersInClass ( classId ) ; } catch ( DataAccessException e ) { transactionManager . rollback ( status ) ; throw e ; } for ( String username : usernames ) { List < Campaign . Role > roles ; try { roles = getJdbcTemplate ( ) . query ( SQL_GET_USER_DEFAULT_ROLES , new Object [ ] { username , campaignId , classId } , new RowMapper < Campaign . Role > ( ) { @ Override public Campaign . Role mapRow ( ResultSet rs , int rowNum ) throws SQLException { return Campaign . Role . getValue ( rs . getString ( "role" ) ) ; } } ) ; } catch ( org . springframework . dao . DataAccessException e ) { transactionManager . rollback ( status ) ; throw new DataAccessException ( "Error executing SQL '" + SQL_GET_USER_DEFAULT_ROLES + "' with parameters: " + username + ", " + campaignId + ", " + classId , e ) ; } for ( Campaign . Role role : roles ) { try { getJdbcTemplate ( ) . update ( SQL_INSERT_USER_ROLE_CAMPAIGN , new Object [ ] { username , campaignId , role . toString ( ) } ) ; } catch ( org . springframework . dao . DuplicateKeyException e ) { } catch ( org . springframework . dao . DataAccessException e ) { transactionManager . rollback ( status ) ; throw new DataAccessException ( "Error executing SQL '" + SQL_INSERT_USER_ROLE_CAMPAIGN + "' with parameters: " + username + ", " + campaignId + ", " + role , e ) ; } } } }
Creates the association between a class and a campaign in the database.
Creates the association between a class and a campaign in the database .
// Associate this class to the campaign. // If the campaign was already associated with the class, ignore // this call. // Insert the default campaign_class_default_role // relationships for privileged users. // TODO: This should be a parameter in the API. // Insert the default campaign_class_default_role // relationships for restricted users. // TODO: This should be a parameter in the API. // Get the list of users in the class. // For each of the users in the class, assign them their default roles // in the campaign. // If the user already has the role in the campaign then // ignore it.