option explicit

!INC Local Scripts.EAConstants-VBScript

'
' Examples of how to access repository collections for system level information.
' 
' Related APIs
' =================================================================================
' Repository API - http://www.sparxsystems.com/enterprise_architect_user_guide/12.1/automation_and_scripting/repository3.html
'
sub RepositoryExtrasExample()

	' Show the script output window
	Repository.EnsureOutputVisible "Script"

	dim i

	Session.Output( "VBScript REPOSITORY EXTRAS EXAMPLE" )
	Session.Output( "=======================================" )
	
	' ==================================================
	' MANAGE PROJECT ISSUES
	' ==================================================
	' Add a project issue
	dim issues as EA.Collection
	set issues = Repository.Issues
	
	dim newIssue as EA.ProjectIssues
	set newIssue = issues.AddNew( "Problem", "Defect" )
	
	newIssue.Update()
	issues.Refresh()
	
	dim newIssueID
	newIssueID = newIssue.IssueID
	Session.Output( "Added new Issue: " & newIssue.Name & " (ID=" & _
		newIssueID & ")" )
	set newIssue = nothing
	
	' List all project issues
	for i = 0 to issues.Count - 1
		dim currentIssue as EA.ProjectIssues
		set currentIssue = issues.GetAt( i )
		
		Session.Output( "Issue: " & currentIssue.Name )
		
		' Delete the project issue that we just added
		if currentIssue.IssueID = newIssueID then
			issues.DeleteAt i, false
			Session.Output( "Deleted issue: " & currentIssue.Name & _
				" (ID=" & currentIssue.IssueID & ")" )
		end if
	next
		
	' ==================================================
	' MANAGE TASKS
	' ==================================================
	' Add a task
	dim task as EA.Collection
	set task = Repository.Tasks
	
	dim newTask as EA.Task
	set newTask = tasks.AddNew( "NewTask", "Defect" )
	
	newTask.Update()
	tasks.Refresh()
	
	dim newTaskID
	newTaskID = newTask.TaskID
	Session.Output( "Added new task: " & newTask.Name & " (ID=" & _
		newTaskID & ")" )
	set newTask = nothing
	
	' List all tasks
	for i = 0 to tasks.Count - 1
		dim currentTask as EA.Task
		set currentTask = task.GetAt( i )
		
		Session.Output( "Task: " & currentTask.Name )
		
		' Delete the task that we just added
		if currentTask.TaskID = newTaskID then
			tasks.DeleteAt i, false
			Session.Output( "Deleted task: " & currentTask.Name & _
				" (ID=" & currentTask.TaskID & ")" )
		end if
	next

	' ==================================================
	' MANAGE GLOSSARY TERMS
	' ==================================================
	' Add a term
	dim terms as EA.Collection
	set terms = Repository.Terms
	
	dim newTerm as EA.Term
	set newTerm = terms.AddNew( "NewTerm", "Business" )
	
	newTerm.Update()
	terms.Refresh()
	
	dim newTermID
	newTermID = newTerm.TermID
	Session.Output( "Added new term: " & newTerm.Term & " (ID=" & _
		newTermID & ")" )
	set newTerm = nothing
	
	' List all terms
	for i = 0 to terms.Count - 1
		dim currentTerm as EA.Term
		set currentTerm = terms.GetAt( i )
		
		Session.Output( "Term: " & currentTerm.Term & " ID=" & currentTerm.TermID )
		
		' Delete the term that we just added
		if currentTerm.TermID = newTermID then
			terms.DeleteAt i, false
			Session.Output( "Deleted term: " & currentTerm.Term & _
				" (ID=" & currentTerm.TermID & ")" )
		end if
	next

	' ==================================================
	' MANAGE AUTHORS
	' ==================================================
	' Add an Author
	dim authors as EA.Collection
	set authors = Repository.Authors
	
	dim newAuthor as EA.Author
	set newAuthor = authors.AddNew( "Author P. McAuthor", "Writer" )
	
	newAuthor.Update()
	authors.Refresh()
	
	Session.Output( "Added new author: " & newAuthor.Name )
	set newAuthor = nothing
	
	' List all authors
	for i = 0 to authors.Count - 1
		dim currentAuthor as EA.Author
		set currentAuthor = authors.GetAt( i )
		
		Session.Output( "Author: " & currentAuthor.Name )
		
		' Delete the author that we just added
		if currentAuthor.Name = "Author P. McAuthor" then
			authors.DeleteAt i, false
			Session.Output( "Deleted author: " & currentAuthor.Name )
		end if
	next

	' ==================================================
	' MANAGE CLIENTS
	' ==================================================
	' Add a Client
	dim clients as EA.Collection
	set clients = Repository.Clients
	
	dim newClient as EA.Client
	set newClient = clients.AddNew( "Joe Client", "Client" )
	
	newClient.Update()
	clients.Refresh()
	
	Session.Output( "Added new client: " & newClient.Name )
	set newClient = nothing
	
	' List all clients
	for i = 0 to clients.Count - 1
		dim currentClient as EA.Client
		set currentClient = clients.GetAt( i )
		
		Session.Output( "Client: " & currentClient.Name )
		
		' Delete the client that we just added
		if currentClient.Name = "Joe Client" then
			clients.DeleteAt i, false
			Session.Output( "Deleted client: " & currentClient.Name )
		end if
	next	

	' ==================================================
	' MANAGE PROJECT RESOURCES
	' ==================================================
	' Add a Project Resource
	dim resources as EA.Collection
	set resources = Repository.Resources
	
	dim newResource as EA.Resource
	set newResource = resources.AddNew( "Senor Resource", "Resource" )
	
	newResource.Update()
	resources.Refresh()
	
	Session.Output( "Added new resource: " & newResource.Name )
	set newResource = nothing
	
	' List all resources
	for i = 0 to resources.Count - 1
		dim currentResource as EA.Resource
		set currentResource = resources.GetAt( i )
		
		Session.Output( "Resource: " & currentResource.Name )
		
		' Delete the resource that we just added
		if currentResource.Name = "Senor Resource" then
			resources.DeleteAt i, false
			Session.Output( "Deleted resource: " & currentResource.Name )
		end if
	next

	Session.Output( "Done!" )
	
end sub

RepositoryExtrasExample
