option explicit

!INC Local Scripts.EAConstants-VBScript

'
' An example of working with attributes.
' 
' NOTE: Requires an element to be selected in the Project Browser
' 
' Related APIs
' =================================================================================
' Attribute API - http://www.sparxsystems.com/enterprise_architect_user_guide/12.1/automation_and_scripting/attribute.html
'
sub AttributeLifeCycleExample()
	
	' 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, addedAttributeID
	
		Session.Output( "VBScript ATTRIBUTE LIFE CYCLE EXAMPLE" )
		Session.Output( "=======================================" )
		Session.Output( "Working on element '" & theElement.Name & "' (Type=" & theElement.Type & _
			", ID=" & theElement.ElementID & ")" )
			
		' ==================================================
		' ADD AN ATTRIBUTE
		' ==================================================
		' Create an attribute to work on
		dim attributes as EA.Collection
		set attributes = theElement.Attributes
		
		dim newAttribute as EA.Attribute
		set newAttribute = attributes.AddNew( "m_number", "int" )
		newAttribute.Update()
		attributes.Refresh()
		
		addedAttributeID = newAttribute.AttributeID
		
		Session.Output( "Added attribute: " & newAttribute.Name & _
			"(Type=" & newAttribute.Type & _
			", ID=" & addedAttributeID & ")" )
		
		' ==================================================
		' MANAGE ATTRIBUTE CONSTRAINTS
		' ==================================================
		' Add an attribute constraint
		dim constraints as EA.Collection
		set constraints = newAttribute.Constraints
		
		dim newConstraint as EA.AttributeConstraint
		set newConstraint = constraints.AddNew( "> 123", "Precision" )
		newConstraint.Update()
		constraints.Refresh()
		
		Session.Output( "Added constraint: " & newConstraint.Name )
		
		set newConstraint = nothing
		
		' Search the constraint collection for the added constraint and delete it		
		for i = 0 to constraints.Count - 1
			dim currentConstraint as EA.AttributeConstraint
			set currentConstraint = constraints.GetAt( i )
			
			Session.Output( "Attribute Constraint: " & currentConstraint.Name )
			
			' Delete the constraint we just added
			if currentConstraint.Name = "> 123" then
				constraints.DeleteAt i, false
				Session.Output( "Deleted Constraint: " & currentConstraint.Name )
			end if
		next
		
		set constraints = nothing
		
		' ==================================================
		' MANAGE ATTRIBUTE TAGGED VALUES
		' ==================================================
		' Add an attribute tag		
		dim tags as EA.Collection
		set tags = newAttribute.TaggedValues
		
		dim newTag as EA.AttributeTag
		set newTag = tags.AddNew( "MyAttributeTag", "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.AttributeTag
			set currentTag = tags.GetAt( i )
			
			Session.Output( "Attribute 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
		
		' ==================================================
		' LIST AND DELETE ATTRIBUTES
		' ==================================================
		set newAttribute = nothing
		
		' List attributes
		for i = 0 to attributes.Count - 1
			dim currentAttribute as EA.Attribute
			set currentAttribute = attributes.GetAt( i )
			
			Session.Output( "Attribute: " & currentAttribute.Name )
			
			' Delete the attribute we added
			if currentAttribute.AttributeID = addedAttributeID then
				attributes.DeleteAt i, false
				Session.Output( "Deleted Attribute: " & currentAttribute.Name & _
					"(Type=" & currentAttribute.Type & _
					", ID= " & currentAttribute.AttributeID & ")" )
			end if
		next
		
		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

AttributeLifeCycleExample
