agave-systems/systems-api/src/main/java/org/iplantc/service/systems/resources/BatchQueueManagementResource.java
/**
	 * Post action for adding (and overwriting) batch queues on a system object
	 */
Post action for adding (and overwriting) batch queues on a system object
Post action for adding ( and overwriting ) batch queues on a system object
@Override
	public void acceptRepresentation(Representation entity)
	{
	    this.username = getAuthenticatedUsername();
        AgaveLogServiceClient.log(AgaveLogServiceClient.ServiceKeys.SYSTEMS02.name(), 
                AgaveLogServiceClient.ActivityKeys.SystemBatchQueueUpdate.name(), 
                username, "", getRequest().getClientInfo().getUpstreamAddress());
	    try
        {
    	    if (StringUtils.isEmpty(systemId))
            {
                throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
                        "Please specify an system using its system id. ", 
						new SystemArgumentException());
            }
    	    RemoteSystem system = dao.findActiveAndInactiveSystemBySystemId(systemId);
            if (system == null)
            {
                throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND,
                        "No system found matching " + systemId, 
						new FileNotFoundException());
            }
            else if ( ! (system instanceof ExecutionSystem))
            {
                throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
                        "Invalid system id. " + systemId + " is a storage system. "
                      + "Only execution systems have batch queues.", 
						new SystemArgumentException());
            }
            else if (system.isPubliclyAvailable()) 
            {
                throw new ResourceException(Status.SERVER_ERROR_NOT_IMPLEMENTED,
                        "Public system queues cannot be updated. "
                        + "Please unpublish this system before updating its queues.", 
						new SystemArgumentException());
            }
            SystemManager systemManager = new SystemManager();
            if (systemManager.isManageableByUser(system, username))
            {
                JSONObject json = getPostedEntityAsJsonObject(true);
                if (json == null || json.length() == 0) {
                    throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
                            "No queue description found in the request.", 
							new SystemArgumentException());
                }
                try
    			{
    				String queueName = null;
    				if (StringUtils.isEmpty(queueId)) 
    				{
    				    throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
                                "Invalid system queue id. Please specify an system using its system id.", 
    							new SystemArgumentException());
    				} 
    				else if (json.has("name") && 
    						!StringUtils.equalsIgnoreCase(json.getString("name"), queueId)) 
    				{
    					throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
    							"The queue name value in the POST body, " + json.getString("name") + 
                    			", does not match the username in the URL, " + queueId, 
    							new SystemArgumentException());         
    				}
    				queueName = json.getString("name");
    				BatchQueue existingQueue = ((ExecutionSystem)system).getQueue(queueName);
    				BatchQueue newQueue = BatchQueue.fromJSON(json, existingQueue);
                    if (newQueue.isSystemDefault()) {
                        for (BatchQueue systemQueue: ((ExecutionSystem)system).getBatchQueues()) {
                            systemQueue.setSystemDefault(false);
                        }
                        newQueue.setSystemDefault(true);
                    }
                    newQueue.setLastUpdated(new Date());
                    dao.persist(system);
                    getResponse().setEntity(new IplantSuccessRepresentation(newQueue.toJSON()));
                }
                catch (SystemArgumentException e) {
                    throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
                            e.getMessage(), e);
                }
                catch (Throwable e)  {
                    throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
                            "An unexpected error occurred updating system queue. "
                            + "If this persists, please contact your system admin", 
                            e);
                }
            } else {
    			throw new ResourceException(Status.CLIENT_ERROR_FORBIDDEN,
    					"User does not have the necessary role to modify this system queue",
    					new PermissionException());
    		}
        }
		catch (ResourceException e) {
			getResponse().setEntity(
					new IplantErrorRepresentation(e.getMessage()));
			getResponse().setStatus(e.getStatus());
		}
	    catch (SystemException e) {
        	getResponse().setEntity(
					new IplantErrorRepresentation(e.getMessage()));
			getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        }
		catch (Throwable e)
        {
	        getResponse().setEntity(
                    new IplantErrorRepresentation("An unexpected error occurred updating the system queue. "
                            + "If this persists, please contact your system admin"));
            getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
        }
	}
@ Override public void acceptRepresentation ( Representation entity ) { this . username = getAuthenticatedUsername ( ) ; AgaveLogServiceClient . log ( AgaveLogServiceClient . ServiceKeys . SYSTEMS02 . name ( ) , AgaveLogServiceClient . ActivityKeys . SystemBatchQueueUpdate . name ( ) , username , "" , getRequest ( ) . getClientInfo ( ) . getUpstreamAddress ( ) ) ; try { if ( StringUtils . isEmpty ( systemId ) ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "Please specify an system using its system id. " , new SystemArgumentException ( ) ) ; } RemoteSystem system = dao . findActiveAndInactiveSystemBySystemId ( systemId ) ; if ( system == null ) { throw new ResourceException ( Status . CLIENT_ERROR_NOT_FOUND , "No system found matching " + systemId , new FileNotFoundException ( ) ) ; } else if ( ! ( system instanceof ExecutionSystem ) ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "Invalid system id. " + systemId + " is a storage system. " + "Only execution systems have batch queues." , new SystemArgumentException ( ) ) ; } else if ( system . isPubliclyAvailable ( ) ) { throw new ResourceException ( Status . SERVER_ERROR_NOT_IMPLEMENTED , "Public system queues cannot be updated. " + "Please unpublish this system before updating its queues." , new SystemArgumentException ( ) ) ; } SystemManager systemManager = new SystemManager ( ) ; if ( systemManager . isManageableByUser ( system , username ) ) { JSONObject json = getPostedEntityAsJsonObject ( true ) ; if ( json == null || json . length ( ) == 0 ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "No queue description found in the request." , new SystemArgumentException ( ) ) ; } try { String queueName = null ; if ( StringUtils . isEmpty ( queueId ) ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "Invalid system queue id. Please specify an system using its system id." , new SystemArgumentException ( ) ) ; } else if ( json . has ( "name" ) && ! StringUtils . equalsIgnoreCase ( json . getString ( "name" ) , queueId ) ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "The queue name value in the POST body, " + json . getString ( "name" ) + ", does not match the username in the URL, " + queueId , new SystemArgumentException ( ) ) ; } queueName = json . getString ( "name" ) ; BatchQueue existingQueue = ( ( ExecutionSystem ) system ) . getQueue ( queueName ) ; BatchQueue newQueue = BatchQueue . fromJSON ( json , existingQueue ) ; if ( newQueue . isSystemDefault ( ) ) { for ( BatchQueue systemQueue : ( ( ExecutionSystem ) system ) . getBatchQueues ( ) ) { systemQueue . setSystemDefault ( false ) ; } newQueue . setSystemDefault ( true ) ; } newQueue . setLastUpdated ( new Date ( ) ) ; dao . persist ( system ) ; getResponse ( ) . setEntity ( new IplantSuccessRepresentation ( newQueue . toJSON ( ) ) ) ; } catch ( SystemArgumentException e ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , e . getMessage ( ) , e ) ; } catch ( Throwable e ) { throw new ResourceException ( Status . SERVER_ERROR_INTERNAL , "An unexpected error occurred updating system queue. " + "If this persists, please contact your system admin" , e ) ; } } else { throw new ResourceException ( Status . CLIENT_ERROR_FORBIDDEN , "User does not have the necessary role to modify this system queue" , new PermissionException ( ) ) ; } } catch ( ResourceException e ) { getResponse ( ) . setEntity ( new IplantErrorRepresentation ( e . getMessage ( ) ) ) ; getResponse ( ) . setStatus ( e . getStatus ( ) ) ; } catch ( SystemException e ) { getResponse ( ) . setEntity ( new IplantErrorRepresentation ( e . getMessage ( ) ) ) ; getResponse ( ) . setStatus ( Status . CLIENT_ERROR_BAD_REQUEST ) ; } catch ( Throwable e ) { getResponse ( ) . setEntity ( new IplantErrorRepresentation ( "An unexpected error occurred updating the system queue. " + "If this persists, please contact your system admin" ) ) ; getResponse ( ) . setStatus ( Status . SERVER_ERROR_INTERNAL ) ; } }
Post action for adding (and overwriting) batch queues on a system object
Post action for adding ( and overwriting ) batch queues on a system object
// parse the form to get the job specs