option explicit

!INC Local Scripts.EAConstants-VBScript

'
' An example of working with methods.
' 
' NOTE: Requires an element to be selected in the Project Browser
' 
' Related APIs
' =================================================================================
' Method API - http://www.sparxsystems.com/enterprise_architect_user_guide/12.1/automation_and_scripting/method.html
'
sub ManageMethodsExample()
	
	' Show the script output window
	Repository.EnsureOutputVisible "Script"
	
	' Get the currently selected element in the tree to work on
	dim theElement as EA.Element
	set theElement = Repository.GetTreeSelectedObject()
	
	if not theElement is nothing and theElement.ObjectType = otElement then
	
		dim i, addedMethodID
	
		Session.Output( "VBScript METHOD LIFE CYCLE EXAMPLE" )
		Session.Output( "=======================================" )
		Session.Output( "Working on element '" & theElement.Name & "' (Type=" & theElement.Type & _
			", ID=" & theElement.ElementID & ")" )
			
		' ==================================================
		' ADD A METHOD
		' ==================================================
		' Create a method to work on
		dim methods as EA.Collection
		set methods = theElement.Methods
		
		dim newMethod as EA.Method
		set newMethod = methods.AddNew( "TestMethod", "bool" )
		newMethod.Update()
		methods.Refresh()
		
		addedMethodID = newMethod.MethodID
		
		Session.Output( "Added method: " & newMethod.Name & _
			"(ID=" & addedMethodID & ")" )
		
		' ==================================================
		' MANAGE METHOD PRECONDITIONS
		' ==================================================
		' Add a PreCondition
		dim preConditions as EA.Collection
		set preConditions = newMethod.PreConditions
		
		dim newPreCondition as EA.MethodConstraint
		set newPreCondition = preConditions.AddNew( "TestPreCondition", "Precision" )
		newPreCondition.Update()
		preConditions.Refresh()
		
		Session.Output( "Added PreCondition: " & newPreCondition.Name )
		
		set newPreCondition = nothing
		
		' Search the collection for the added PreCondition and delete it
		for i = 0 to preConditions.Count - 1
			dim currentPreCondition as EA.MethodConstraint
			set currentPreCondition = preConditions.GetAt( i )
			
			Session.Output( "PreCondition: " & currentPreCondition.Name )
			
			' Delete the PreCondition we just added
			if currentPreCondition.Name = "TestPreCondition" then
				preConditions.DeleteAt i, false
				Session.Output( "Deleted PreCondition: " & currentPreCondition.Name )
			end if
		next
		
		set preConditions = nothing
		
		' ==================================================
		' MANAGE METHOD POSTCONDITIONS
		' ==================================================
		' Add a PostCondition
		dim postConditions as EA.Collection
		set postConditions = newMethod.PostConditions
		
		dim newPostCondition as EA.MethodConstraint
		set newPostCondition = postConditions.AddNew( "TestPostCondition", "Precision" )
		newPostCondition.Update()
		postConditions.Refresh()
		
		Session.Output( "Added PostCondition: " & newPostCondition.Name )
		
		set newPostCondition = nothing
		
		' Search the collection for the added PostCondition and delete it
		for i = 0 to postConditions.Count - 1
			dim currentPostCondition as EA.MethodConstraint
			set currentPostCondition = postConditions.GetAt( i )
			
			Session.Output( "PostCondition: " & currentPostCondition.Name )
			
			' Delete the PostCondition we just added
			if currentPostCondition.Name = "TestPostCondition" then
				postConditions.DeleteAt i, false
				Session.Output( "Deleted PostCondition: " & currentPostCondition.Name )
			end if
		next
		
		set postConditions = nothing		
		
		' ==================================================
		' MANAGE METHOD TAGGED VALUES
		' ==================================================
		' Add a method tag
		dim tags as EA.Collection
		set tags = newMethod.TaggedValues
		
		dim newTag as EA.MethodTag
		set newTag = tags.AddNew( "MyMethodTag", "Number" )
		newTag.Update()
		tags.Refresh()
		
		dim newTagID
		newTagID = newTag.TagID
		Session.Output( "Added tag: " & newTag.Name & " (ID=" & newTagID & ")" )
			
		set newTag = nothing
		
		' Search the tag collection for the added tag and delete it
		for i = 0 to tags.Count - 1
			dim currentTag as EA.MethodTag
			set currentTag = tags.GetAt( i )
			
			Session.Output( "Method Tag: " & currentTag.Name )
			
			' Delete the tag we just added
			if currentTag.TagID = newTagID then
				tags.DeleteAt i, false
				Session.Output( "Deleted Tag: " & currentTag.Name & _
					" (ID=" & currentTag.TagID & ")" )
			end if
		next
		
		set tags = nothing
		
		' ==================================================
		' MANAGE METHOD PARAMETERS
		' ==================================================
		' Add a method parameter
		dim parameters as EA.Collection
		set parameters = newMethod.Parameters
		
		dim newParameter as EA.Parameter
		set newParameter = parameters.AddNew( "sName", "String" )
		newParameter.Update()
		parameters.Refresh()
		
		Session.Output( "Added parameter: " & newParameter.Name )
		
		set newParameter = nothing
		
		' Search the parameter collection for the added parameter and delete it
		for i = 0 to parameters.Count - 1
			dim currentParameter as EA.Parameter
			set currentParameter = parameters.GetAt( i )
			
			Session.Output( "Method Parameter: " & currentParameter.Name )
			
			' Delete the parameter we just added
			if currentParameter.Name = "sName" then
				parameters.DeleteAt i, false
				Session.Output( "Deleted Parameter: " & currentParameter.Name )
			end if
		next
		
		set parameters = nothing
		
		' ==================================================
		' LIST AND DELETE METHODS
		' ==================================================
		set newMethod = nothing
		
		' List methods
		for i = 0 to methods.Count - 1
			dim currentMethod as EA.Method
			set currentMethod = methods.GetAt( i )
			
			Session.Output( "Method: " & currentMethod.Name )
			
			' Delete the method we added
			if currentMethod.MethodID = addedMethodID then
				methods.DeleteAt i, false
				Session.Output( "Deleted Method: " & currentMethod.Name & _
					" (ID=" & currentMethod.MethodID & ")" )
			end if
		next
		
		set methods = nothing
		
		Session.Output( "Done!" )
		
	else
		' No item selected in the tree, or the item selected was not an element
		MsgBox( "This script requires an element be selected in the Project Browser." & vbCrLf & _
			"Please select an element in the Project Browser and try again." )
	end if

	
end sub

ManageMethodsExample
