agave-systems/systems-api/src/main/java/org/iplantc/service/systems/resources/SystemManagementResource.java
/**
	 * Handles mechanics of validating and persisting the posted system 
	 * description.
	 * 
	 * TODO: need to check the registered internal user auth configs and 
	 * remove any that are no longer valid after the update.
	 *  
	 * @param json
	 * @return
	 * @throws ResourceException 
	 * @throws SystemArgumentException 
	 * @throws PermissionException 
	 * @throws SystemException 
	 */
Handles mechanics of validating and persisting the posted system
description.

need to check the registered internal user auth configs and
remove any that are no longer valid after the update.


Handles mechanics of validating and persisting the posted system description . need to check the registered internal user auth configs and remove any that are no longer valid after the update .
private RemoteSystem doProcessSystemPost(JSONObject json) 
	throws ResourceException, SystemArgumentException, SystemException, PermissionException
	{
		RemoteSystem newSystem = null; 
		RemoteSystem existingSystem = null; 
		try 
		{
	    	if (!StringUtils.isEmpty(systemId))
	    	{
	        	existingSystem = dao.findActiveAndInactiveSystemBySystemId(systemId);
	        	if (existingSystem == null) {
	        		throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND,
	        				"No system found matching " + systemId, 
							new FileNotFoundException());
	        	}
	        	else if (!existingSystem.isAvailable()) 
				{
					throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
							"System has been disabled by the administrator. "
							+ "The system must be re-enabled before updating its description.",
							new SystemUnavailableException());
				}
	        	else if (systemManager.isManageableByUser(existingSystem, username))
	        	{
	        		if (existingSystem.isPubliclyAvailable()) {
	        			throw new ResourceException(Status.CLIENT_ERROR_FORBIDDEN,
	    						"Permission denied. Public systems cannot be updated. " +
	        					"The system must be re-enabled before updating its description.",
								new PermissionException());
	        		}
	        	}
	        	else 
	        	{
	        		throw new ResourceException(Status.CLIENT_ERROR_FORBIDDEN,
							"Permission denied. You do not have permission to update this system",
							new PermissionException());
		    	}
	        	if (json.has("id")) {
	    			if (!json.getString("id").equalsIgnoreCase(existingSystem.getSystemId())) {
						throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
							"Uploaded system description does not match target uri. You uploaded an " +
							"system description for " + json.getString("id") + " to the uri " +
							"for " + existingSystem.getSystemId() + ". Please either register a new " +
							"system or post your update to " + 
							Settings.IPLANT_JOB_SERVICE + "systems/" + json.getString("id") +
							" to update that system.", 
							new SystemArgumentException());
					}
	    		} else {
	    			throw new SystemArgumentException("Please specify a valid string value for the 'id' field.");
	    		}
	        	HibernateUtil.getSession().evict(existingSystem);
	        	newSystem = systemManager.parseSystem(json, existingSystem.getOwner(), existingSystem);
	        	newSystem = dao.merge(existingSystem);
	            newSystem.setRevision(existingSystem.getRevision() + 1);
	            eventProcessor.processSystemUpdateEvent(newSystem, SystemEventType.UPDATED, username);
	    	}
	    	else
	    	{
	    		if (json.has("id")) {
	    			existingSystem = dao.findBySystemId(json.getString("id"));
	    		} 
	    		if (existingSystem != null) 
	            {
	    			HibernateUtil.getSession().evict(existingSystem);
	    			newSystem = systemManager.parseSystem(json, username, existingSystem);
	        		newSystem.setRevision(existingSystem.getRevision() + 1);
	        		newSystem = dao.merge(newSystem);
	        		dao.persist(newSystem);
	        		eventProcessor.processSystemUpdateEvent(newSystem, SystemEventType.UPDATED, username);
	            } 
	            else 
	            {
	            	newSystem = systemManager.parseSystem(json, username);
	            	if (!dao.isSystemIdUnique(newSystem.getSystemId())) {
	            		throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "The id " + newSystem.getSystemId() + 
	            				" is already taken. Please select a different identifier for this system.",
	            				new SystemException("System id is already taken"));
	            	}
	            	eventProcessor.processSystemUpdateEvent(newSystem, SystemEventType.CREATED, username);
	            	dao.persist(newSystem);
	            }
	    	}
			return newSystem;
		} catch (JSONException e) {
			throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
					"Unable to parse JSON object", e);
		}
		catch (PermissionException e) {
			throw new ResourceException(Status.CLIENT_ERROR_FORBIDDEN,
					e.getMessage(), e);
		}
		catch (SystemException e) {
			throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
					e.getMessage(), e);
		}
		catch (ResourceException e) {
			throw e;
		}
		catch (Throwable e)
		{
			log.error(e);
			throw new ResourceException(Status.SERVER_ERROR_INTERNAL, 
					"An unexpected error occurred while saving system.", e);
		}
	}
private RemoteSystem doProcessSystemPost ( JSONObject json ) throws ResourceException , SystemArgumentException , SystemException , PermissionException { RemoteSystem newSystem = null ; RemoteSystem existingSystem = null ; try { if ( ! StringUtils . isEmpty ( systemId ) ) { existingSystem = dao . findActiveAndInactiveSystemBySystemId ( systemId ) ; if ( existingSystem == null ) { throw new ResourceException ( Status . CLIENT_ERROR_NOT_FOUND , "No system found matching " + systemId , new FileNotFoundException ( ) ) ; } else if ( ! existingSystem . isAvailable ( ) ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "System has been disabled by the administrator. " + "The system must be re-enabled before updating its description." , new SystemUnavailableException ( ) ) ; } else if ( systemManager . isManageableByUser ( existingSystem , username ) ) { if ( existingSystem . isPubliclyAvailable ( ) ) { throw new ResourceException ( Status . CLIENT_ERROR_FORBIDDEN , "Permission denied. Public systems cannot be updated. " + "The system must be re-enabled before updating its description." , new PermissionException ( ) ) ; } } else { throw new ResourceException ( Status . CLIENT_ERROR_FORBIDDEN , "Permission denied. You do not have permission to update this system" , new PermissionException ( ) ) ; } if ( json . has ( "id" ) ) { if ( ! json . getString ( "id" ) . equalsIgnoreCase ( existingSystem . getSystemId ( ) ) ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "Uploaded system description does not match target uri. You uploaded an " + "system description for " + json . getString ( "id" ) + " to the uri " + "for " + existingSystem . getSystemId ( ) + ". Please either register a new " + "system or post your update to " + Settings . IPLANT_JOB_SERVICE + "systems/" + json . getString ( "id" ) + " to update that system." , new SystemArgumentException ( ) ) ; } } else { throw new SystemArgumentException ( "Please specify a valid string value for the 'id' field." ) ; } HibernateUtil . getSession ( ) . evict ( existingSystem ) ; newSystem = systemManager . parseSystem ( json , existingSystem . getOwner ( ) , existingSystem ) ; newSystem = dao . merge ( existingSystem ) ; newSystem . setRevision ( existingSystem . getRevision ( ) + 1 ) ; eventProcessor . processSystemUpdateEvent ( newSystem , SystemEventType . UPDATED , username ) ; } else { if ( json . has ( "id" ) ) { existingSystem = dao . findBySystemId ( json . getString ( "id" ) ) ; } if ( existingSystem != null ) { HibernateUtil . getSession ( ) . evict ( existingSystem ) ; newSystem = systemManager . parseSystem ( json , username , existingSystem ) ; newSystem . setRevision ( existingSystem . getRevision ( ) + 1 ) ; newSystem = dao . merge ( newSystem ) ; dao . persist ( newSystem ) ; eventProcessor . processSystemUpdateEvent ( newSystem , SystemEventType . UPDATED , username ) ; } else { newSystem = systemManager . parseSystem ( json , username ) ; if ( ! dao . isSystemIdUnique ( newSystem . getSystemId ( ) ) ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "The id " + newSystem . getSystemId ( ) + " is already taken. Please select a different identifier for this system." , new SystemException ( "System id is already taken" ) ) ; } eventProcessor . processSystemUpdateEvent ( newSystem , SystemEventType . CREATED , username ) ; dao . persist ( newSystem ) ; } } return newSystem ; } catch ( JSONException e ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "Unable to parse JSON object" , e ) ; } catch ( PermissionException e ) { throw new ResourceException ( Status . CLIENT_ERROR_FORBIDDEN , e . getMessage ( ) , e ) ; } catch ( SystemException e ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , e . getMessage ( ) , e ) ; } catch ( ResourceException e ) { throw e ; } catch ( Throwable e ) { log . error ( e ) ; throw new ResourceException ( Status . SERVER_ERROR_INTERNAL , "An unexpected error occurred while saving system." , e ) ; } }
Handles mechanics of validating and persisting the posted system
 description.
Handles mechanics of validating and persisting the posted system description .
// update if the existing system belongs to the user, otherwise throw an exception // force the system to be reenabled before it's updated // check user access rights // 403 if you don't have the rights // now we can merge/update // parse and validate the json // TODO: should we be invalidating any auth configs that aren't valid after this update? // TODO: what is the proper way of notifying the user that things have gone awry here? //            dao.persist(newSystem); //	            NotificationManager.process(newSystem.getUuid(), "UPDATED", username); // parse and validate the json //	        		NotificationManager.process(newSystem.getUuid(), "UPDATED", username); //	            	NotificationManager.process(newSystem.getUuid(), "CREATED", username);