A simple wastebin in Enterprise Architect

One of the organisations I support in using Enterprise Architect was encountering problems with deleting elements from a repository. Sometimes modellers deleted elements by accident and in other cases where teams are modelling it was not clear that an element was still used.

However there is a model manager who is maintaining the repository content so from an organisational point of view this problem shoud be easy to solve. He suggested that a Wastebin should solve the problem. This wastebin should have the same functionality als the wastebin in Windows. When somebody deletes an element or package it is not deleted but moved to a certain location in the repository.

 

Since the organisation is using the IDEA AddOn we decided to develop a function in the AddOn, which is developed in VB.Net. It turned out to be relative easy. There is an adaption necessary in the AddIN Predelete event and an extra class (not necessary but for us a best practice) is added. Below you see the two code snippets.

Function EA_OnPreDeleteElement(Repository As EA.Repository, _

Info As EA.EventProperties) As Boolean

Return WasteBin.WasteBinElement(Repository, Info.Get(0).Value)

End Function

Function EA_OnPreDeletePackage(Repository As EA.Repository, _

Info As EA.EventProperties) As Boolean

Return WasteBin.WasteBinPackage(Repository, Info.Get(0).Value)

End Function

This piece of code is returning a true or false to deleted the selected package or not. When it is a regular user, the element is moved before the delete and therefore a false is returned. When somebody is authorized and the element is in the WasteBin package the element can be deleted so a true is returned. 

In the code snippet below you see the code of the WasteBin class

Public Class WasteBin
    Public Shared Function WasteBinElement(Repository As EA.Repository, strEntityId As String) As Boolean
        Dim oElement As EA.Element
        Dim intPackage_id As Int32 = GetWasteBinPackage_id()
 
        Try
            If intPackage_id <> -999 Then
                oElement = Repository.GetElementByID(Convert.ToInt32(strEntityId))
                If oElement.PackageID <> intPackage_id Then
                    Repository.EnableUIUpdates = False
                    oElement.PackageID = intPackage_id
                    oElement.Update()
                    Repository.EnableUIUpdates = True
                    Return False
                Else
                    Return DeleteFromWasteBin(Repository)
                End If
            End If
            Return True
        Catch ex As Exception
            DLA2EAHelper.Error2Log(ex)
        End Try
        Return False
    End Function
 
Private Shared Function DeleteFromWasteBin(Repository As EA.Repository) As Boolean
        If Repository.IsSecurityEnabled = False Or DLA2EAHelper.IsUserGroupMember(Repository, "Administrators") Then
            Return True
        Else
            Return False
        End If
    End Function
    Public Shared Function WasteBinPackage(Repository As EA.Repository, strEntityId As String) As Boolean
        Dim oPackage As EA.Package
        Dim intPackage_id As Int32 = GetWasteBinPackage_id()
 
        Try
            If intPackage_id <> -999 Then
                oPackage = Repository.GetPackageByID(Convert.ToInt32(strEntityId))
                If oPackage.ParentID <> intPackage_id Then
                    Repository.EnableUIUpdates = False
                    oPackage.ParentID = intPackage_id
                    oPackage.Update()
                    Repository.EnableUIUpdates = True
                    Return False
                Else
                    Return DeleteFromWasteBin(Repository)
                End If
            End If
            Return True
        Catch ex As Exception
            DLA2EAHelper.Error2Log(ex)
        End Try
        Return False
    End Function
    Shared Function GetWasteBinPackage_id() As Int32
        Dim oDef As New IDEADefinitions()
        Dim strPackage_id As String
        Dim intPackage_id As Int32 = -999
        strPackage_id = oDef.GetSettingValue("WasteBinPackage_id")
        If strPackage_id.Length > 0 Then
            intPackage_id = Convert.ToInt32(strPackage_id)
        End If
        Return intPackage_id
    End Function
End Class

In the code we check first if the element is in the wastebin package or not, if not the element is moved to the wastebin. If the element or the package is in the wastebin then an authorization check is done. If the user is authorized then the element is permanent removed from the repository

 

This routine is part of the IDEA AddOn. This Open Source tool can be downloaded from http://eaxpertise.nl/ideanl.aspx. When you want to include it in a ModelAddIn this is probably a relatively small modification.