src/org/ohmage/service/AccessRequestServices.java
public void updateAccessRequest(
			final String requester,
			final String requestId, 
			final String emailAddress, 
			final JSONObject requestContent, 
			final String requestType,
			final String requestStatus,
			final Boolean notifyUser) throws ServiceException {

		Boolean updateUserPrivileges = null;
		AccessRequest.Type finalType = null;
		Boolean isAdmin = false;

		try {
			
			// get the existing request.  
			AccessRequest request = userSetupRequestQueries.getAccessRequest(requester,requestId); 
			if (request == null) {
				throw new ServiceException(ErrorCode.USER_ACCESS_REQUEST_EXECUTION_ERROR,
						"Request doesn't exist.");
			} else {
				finalType = request.getType();
			}

			// verify that the requester is the owner or an admin 
			try {
				LOGGER.info("Checking that the user is an admin to update the request.");
				isAdmin = UserServices.instance().isUserAnAdmin(requester);
				if (isAdmin == false) {
					try {
						LOGGER.info("Checking that the user has privilege to update the request.");
						verifyUserCanAccessRequest(requestId, requester);
					} catch (ServiceException e) {
						throw new ServiceException(ErrorCode.USER_ACCESS_REQUEST_EXECUTION_ERROR,
								"User has no privilege to update the request : " + requestId, e);	
					}
				}
			} catch (ServiceException e) {
				throw new ServiceException(ErrorCode.USER_ACCESS_REQUEST_EXECUTION_ERROR,
						"Can't verify user privileges : " + requester, e);	
			}			
			
			// regular user cannot update the non-pending request
			if ((! isAdmin) && 
				(! request.getStatus().equals(AccessRequest.Status.PENDING)))
			{
				throw new ServiceException(ErrorCode.USER_ACCESS_REQUEST_EXECUTION_ERROR,
						"User cannot update a non-PENDING request : " + requestId);	
			}

			
			if (requestType == null) 
				finalType = request.getType();
			else finalType = AccessRequest.Type.getValue(requestType); 
			
			switch (finalType) {
			case USER_SETUP:
				// if (finalType.equals(Type.USER_SETUP)) {
				// if status has changed				
				if ((requestStatus != null) &&
					(! request.getStatus().toString().equals(requestStatus)))
				{
					// Only an admin can update the status.
					//UserServices.instance().verifyUserIsAdmin(requester);
					if (isAdmin == false) {
						throw new ServiceException(ErrorCode.USER_ACCESS_REQUEST_EXECUTION_ERROR,
								"User has no privilege to update the request status : " + requestId);	
					}
						
					if (requestStatus.equals(Status.APPROVED.toString())){
						updateUserPrivileges  = true;
					} else if (requestStatus.equals(Status.REJECTED.toString())){
						updateUserPrivileges = false;
					} else { // pending : 
						// check whether the request of the same user already exists
						if (userSetupRequestQueries.getRequestExists(request.getUsername(), requestType, requestStatus)) {
							throw new ServiceException(ErrorCode.USER_ACCESS_REQUEST_EXECUTION_ERROR,
									"There is already a pending request from the same user and type.");
						} 
						// check whether the user already have the privileges
						else {
							if (userSetupRequestQueries.getUserSetupPrivilegesExist(request.getUsername()))
								throw new ServiceException(ErrorCode.USER_ACCESS_REQUEST_EXECUTION_ERROR,
										"The user already has the privileges.");							
						}
					}				
				} 
											
				break;
			default:
				
			}			
			
			// update the request
			userSetupRequestQueries.updateAccessRequest(requestId, emailAddress, 
					(requestContent == null) ? null : requestContent, 
					requestType, requestStatus, updateUserPrivileges);

		}
		catch(DataAccessException e) {
			throw new ServiceException(e);
		}
	}
/**
	 * update the user setup request in the system.
	 * 
	 * @param requestId The unique identifier for the existing request
	 * 					to be updated.
	 * 
	 * @param emailAddress The user's email address to be updated.
	 * 
	 * @param requestContent The request content to be updated.
	 * 
	 * @param requestStatus The new request status to be updated.
	 * @throws ServiceException Thrown if there is an error.
	 */
update the user setup request in the system.

@param requestId The unique identifier for the existing request
to be updated.

@param emailAddress The user's email address to be updated.

@param requestContent The request content to be updated.

@param requestStatus The new request status to be updated.
@throws ServiceException Thrown if there is an error.
update the user setup request in the system . @param requestId The unique identifier for the existing request to be updated . @param emailAddress The user ' s email address to be updated . @param requestContent The request content to be updated . @param requestStatus The new request status to be updated . @throws ServiceException Thrown if there is an error .
public void updateAccessRequest(
			final String requester,
			final String requestId, 
			final String emailAddress, 
			final JSONObject requestContent, 
			final String requestType,
			final String requestStatus,
			final Boolean notifyUser) throws ServiceException {
		Boolean updateUserPrivileges = null;
		AccessRequest.Type finalType = null;
		Boolean isAdmin = false;
		try {
			AccessRequest request = userSetupRequestQueries.getAccessRequest(requester,requestId); 
			if (request == null) {
				throw new ServiceException(ErrorCode.USER_ACCESS_REQUEST_EXECUTION_ERROR,
						"Request doesn't exist.");
			} else {
				finalType = request.getType();
			}
			try {
				LOGGER.info("Checking that the user is an admin to update the request.");
				isAdmin = UserServices.instance().isUserAnAdmin(requester);
				if (isAdmin == false) {
					try {
						LOGGER.info("Checking that the user has privilege to update the request.");
						verifyUserCanAccessRequest(requestId, requester);
					} catch (ServiceException e) {
						throw new ServiceException(ErrorCode.USER_ACCESS_REQUEST_EXECUTION_ERROR,
								"User has no privilege to update the request : " + requestId, e);	
					}
				}
			} catch (ServiceException e) {
				throw new ServiceException(ErrorCode.USER_ACCESS_REQUEST_EXECUTION_ERROR,
						"Can't verify user privileges : " + requester, e);	
			}			
			if ((! isAdmin) && 
				(! request.getStatus().equals(AccessRequest.Status.PENDING)))
			{
				throw new ServiceException(ErrorCode.USER_ACCESS_REQUEST_EXECUTION_ERROR,
						"User cannot update a non-PENDING request : " + requestId);	
			}
			if (requestType == null) 
				finalType = request.getType();
			else finalType = AccessRequest.Type.getValue(requestType); 
			switch (finalType) {
			case USER_SETUP:
				if ((requestStatus != null) &&
					(! request.getStatus().toString().equals(requestStatus)))
				{
					if (isAdmin == false) {
						throw new ServiceException(ErrorCode.USER_ACCESS_REQUEST_EXECUTION_ERROR,
								"User has no privilege to update the request status : " + requestId);	
					}
					if (requestStatus.equals(Status.APPROVED.toString())){
						updateUserPrivileges  = true;
					} else if (requestStatus.equals(Status.REJECTED.toString())){
						updateUserPrivileges = false;
					} else { 
						if (userSetupRequestQueries.getRequestExists(request.getUsername(), requestType, requestStatus)) {
							throw new ServiceException(ErrorCode.USER_ACCESS_REQUEST_EXECUTION_ERROR,
									"There is already a pending request from the same user and type.");
						} 
						else {
							if (userSetupRequestQueries.getUserSetupPrivilegesExist(request.getUsername()))
								throw new ServiceException(ErrorCode.USER_ACCESS_REQUEST_EXECUTION_ERROR,
										"The user already has the privileges.");							
						}
					}				
				} 
				break;
			default:
			}			
			userSetupRequestQueries.updateAccessRequest(requestId, emailAddress, 
					(requestContent == null) ? null : requestContent, 
					requestType, requestStatus, updateUserPrivileges);
		}
		catch(DataAccessException e) {
			throw new ServiceException(e);
		}
	}
public void updateAccessRequest ( final String requester , final String requestId , final String emailAddress , final JSONObject requestContent , final String requestType , final String requestStatus , final Boolean notifyUser ) throws ServiceException { Boolean updateUserPrivileges = null ; AccessRequest . Type finalType = null ; Boolean isAdmin = false ; try { AccessRequest request = userSetupRequestQueries . getAccessRequest ( requester , requestId ) ; if ( request == null ) { throw new ServiceException ( ErrorCode . USER_ACCESS_REQUEST_EXECUTION_ERROR , "Request doesn't exist." ) ; } else { finalType = request . getType ( ) ; } try { LOGGER . info ( "Checking that the user is an admin to update the request." ) ; isAdmin = UserServices . instance ( ) . isUserAnAdmin ( requester ) ; if ( isAdmin == false ) { try { LOGGER . info ( "Checking that the user has privilege to update the request." ) ; verifyUserCanAccessRequest ( requestId , requester ) ; } catch ( ServiceException e ) { throw new ServiceException ( ErrorCode . USER_ACCESS_REQUEST_EXECUTION_ERROR , "User has no privilege to update the request : " + requestId , e ) ; } } } catch ( ServiceException e ) { throw new ServiceException ( ErrorCode . USER_ACCESS_REQUEST_EXECUTION_ERROR , "Can't verify user privileges : " + requester , e ) ; } if ( ( ! isAdmin ) && ( ! request . getStatus ( ) . equals ( AccessRequest . Status . PENDING ) ) ) { throw new ServiceException ( ErrorCode . USER_ACCESS_REQUEST_EXECUTION_ERROR , "User cannot update a non-PENDING request : " + requestId ) ; } if ( requestType == null ) finalType = request . getType ( ) ; else finalType = AccessRequest . Type . getValue ( requestType ) ; switch ( finalType ) { case USER_SETUP : if ( ( requestStatus != null ) && ( ! request . getStatus ( ) . toString ( ) . equals ( requestStatus ) ) ) { if ( isAdmin == false ) { throw new ServiceException ( ErrorCode . USER_ACCESS_REQUEST_EXECUTION_ERROR , "User has no privilege to update the request status : " + requestId ) ; } if ( requestStatus . equals ( Status . APPROVED . toString ( ) ) ) { updateUserPrivileges = true ; } else if ( requestStatus . equals ( Status . REJECTED . toString ( ) ) ) { updateUserPrivileges = false ; } else { if ( userSetupRequestQueries . getRequestExists ( request . getUsername ( ) , requestType , requestStatus ) ) { throw new ServiceException ( ErrorCode . USER_ACCESS_REQUEST_EXECUTION_ERROR , "There is already a pending request from the same user and type." ) ; } else { if ( userSetupRequestQueries . getUserSetupPrivilegesExist ( request . getUsername ( ) ) ) throw new ServiceException ( ErrorCode . USER_ACCESS_REQUEST_EXECUTION_ERROR , "The user already has the privileges." ) ; } } } break ; default : } userSetupRequestQueries . updateAccessRequest ( requestId , emailAddress , ( requestContent == null ) ? null : requestContent , requestType , requestStatus , updateUserPrivileges ) ; } catch ( DataAccessException e ) { throw new ServiceException ( e ) ; } }
update the user setup request in the system.
update the user setup request in the system .
// get the existing request.   // verify that the requester is the owner or an admin  // regular user cannot update the non-pending request // if (finalType.equals(Type.USER_SETUP)) { // if status has changed				 // Only an admin can update the status. //UserServices.instance().verifyUserIsAdmin(requester); // pending :  // check whether the request of the same user already exists // check whether the user already have the privileges // update the request