Duplicate test with a script

Some time ago, I developed an extension that allows you to easily perform duplicate checks. Especially when working in teams within a large repository, the risk of elements with the same name and similarities is high. To prevent this, I developed an extension that immediately alerts you to a potential duplicate.

Unfortunately, introducing a DLL from a security perspective isn't feasible in every organization. In that case, using a script is a good alternative.

The image shows the screen layout.

 width=

The script validates based on the name and the stereotype. This can be adjusted in the SQL statement if you want to validate on other attributes.

You can also choose to write the messages to the output screen or to a prompt; a logical constant is included in the script for this purpose.

Interestingly, it is a recursive script, meaning that it validates all subpackages within a package, and within those subpackages, all subpackages, etc.

The script below is an example of such a script.

 

option explicit

 

!INC Local Scripts.EAConstants-VBScript

' Use a prompt dialog or the session output window for the results

const useprompt = false

 

Sub CheckPackage(strPackage)

' Check the uniques of the elements placed in a package, this is a recursive function because it processes also the subpackages and daigrams in the packages

Dim oPackage as EA.Package

Dim theElement as EA.Element

Dim theSubPackage as EA.Package

Dim theDiagram as EA.Diagram

Dim strSubPackage

 

Set oPackage = Repository.GetPackageByID(strPackage)

For Each theSubPackage in oPackage.Packages

       strSubPackage = theSubPackage.PackageID

       CheckPackage strSubPackage

Next

 

For each theDiagram in oPackage.Diagrams

       CheckDiagram theDiagram.DiagramID

Next

 

For Each theElement in oPackage.Elements 

       CheckUniqueElement theElement.ElementId

Next

 

End Sub

 

Sub CheckDiagram(strDiagram)

' Check the uniques of the elements placed on a diagram

Dim oDiagram as EA.Diagram

Dim theDO as EA.DiagramObject

 

Set oDiagram = Repository.GetDiagramByID(strDiagram)

For Each theDO in oDiagram.DiagramObjects 

       CheckUniqueElement theDO.ElementId

Next

 

End Sub

 

Sub CheckUniqueElement(strElement)

' Check the uniques of the elements based on the elementid

Dim sSql 

Dim strVal 

Dim oElement as EA.Element

 

Set oElement = Repository.GetElementByID(strElement)

If InStr(oElement.Stereotype, 'ArchiMate>') >= 0 Then

       sSql = 'SELECT Count(*) as aantal FROM t_object WHERE t_object.name = 'webcontent' AND t_object.Stereotype='#stereotype#' AND t_object.ea_guid<>'#guid#' '

       sSql = Replace(sSql, 'webcontent', oElement.Name)

       sSql = Replace(sSql, '#stereotype#', oElement.Stereotype)

       sSql = Replace(sSql, '#guid#', oElement.ElementGUID)

 

       strVal = Repository.SQLQuery(sSql)

       

       If InStr(strVal, '0') > 0 Then

If UsePrompt = true Then

             Session.Output oElement.name &  ' is unique!!'

end if

Else

If UsePrompt = true Then

                 Session.Prompt oElement.name &  ' is NOT Unique!!', promptOK 

Else

Session.Output oElement.name &  ' is NOT Unique!!'

End If

       End If

End if

End Sub

 

 

sub OnProjectBrowserScript()

       

       ' Get the type of element selected in the Project Browser

       dim treeSelectedType

       dim theElement as EA.Element

 

    treeSelectedType = Repository.GetTreeSelectedItemType()

       

       ' Handling Code: based on the itemtype a specific sub is selected

       ' For package this is a recursive implementation

       select case treeSelectedType

       

             case otElement

                    ' Code for when an element is selected

                    set theElement = Repository.GetTreeSelectedObject()

               CheckUniqueElement theElement.ElementID

             case otPackage

                    ' Code for when a package is selected

                    dim thePackage as EA.Package

                    set thePackage = Repository.GetTreeSelectedObject()

                    CheckPackage(thePackage.PackageID)

             case otDiagram

                    ' Code for when a diagram is selected

                    dim theDiagram as EA.Diagram

                    set theDiagram = Repository.GetTreeSelectedObject()

                    CheckDiagram(theDiagram.DiagramID)

             case else

                    ' Error message

                    Session.Prompt 'This script does not support items of this type.', promptOK

                    

       end select

       Session.OutPut 'VALIDATION COMPLETE!!'

end sub

 

OnProjectBrowserScript

 

Please contact me for more information