Author Topic: Mass Conversion of Classes to Packages  (Read 1331 times)

Sunshine

  • EA User
  • **
  • Posts: 500
  • Karma: +33/-1
  • Amicorum omnia communia
    • View Profile
Mass Conversion of Classes to Packages
« on: April 27, 2016, 11:00:33 am »
Problem:
I've received a model which has lots of classes nested within each other. Upon review I realise these should have been modelled as packages. There are a few hundred of these classes that should have been packages. For each layer of nesting there is a diagram showing an overview of the classes for the next level down. There are about 40 diagrams.

Outcome:
I'd like to convert the classes to packages preserving the hierarchy and the diagrams.

Tried:
Exporting CSV and search and replace from class to package on Type column followed by import. Result was creating of packages but left original classes which were in diagrams. Nearly there but not quite.

Has anyone done this before and if so how?
If not any ideas - looking for inspiration

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 7730
  • Karma: +165/-21
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Mass Conversion of Classes to Packages
« Reply #1 on: April 27, 2016, 04:10:50 pm »
I would write a little script to do that for me.
The following script does something similar and could be used as an inspiration

Code: [Select]
'[path=\Projects\Project A\Project Browser Package Group]
'[group=Project Browser Package Group]
option explicit

!INC Local Scripts.EAConstants-VBScript

'
' Script Name: DivideInPackages
' Author: Geert Bellekens
' Purpose: Puts each BusinessProcess or SubProcess/Activity in each own package. To be run from a package in the project browser
' Date: 08/09/2015
'
sub main
dim package as EA.Package
set package = Repository.GetTreeSelectedPackage()
dim process as EA.Element
for each process in package.Elements
if process.Stereotype = "BusinessProcess" _
or process.Stereotype = "Activity" then
dim subPackage as EA.Package
set subPackage = package.Packages.AddNew(process.Name,"Package")
'msgbox subPackage.Name
subPackage.Update
process.PackageID = subPackage.PackageID
process.Update
end if
next
Repository.RefreshModelView package.PackageID
msgbox "finished!"
end sub

main

Geert

Sunshine

  • EA User
  • **
  • Posts: 500
  • Karma: +33/-1
  • Amicorum omnia communia
    • View Profile
Re: Mass Conversion of Classes to Packages
« Reply #2 on: April 29, 2016, 06:44:53 am »
Thanks for the suggestion Geert whilst it will create the packages it won't address the issue of the diagram objects referencing the packages instead of the classes. I'd actually thought of that approach but realised its limitation for my particular problem. After sleeping on it  I think I'll try to attack the problem  at the database level with some SQL like the following [yet to be fully tested]. I've just got to find something that will run the SQL on the eap file aka Access 97. Unfortunately I don't have Access 97 but 2010 which  won't run queries unless I upgrade the file to Access 2010 format but if I do that  then Sparx EA won't read it. Maybe I'll try transferring the eap model to another repository like feap or SQL Server and find a SQL developer type tool to run it on. Just sharing some thoughts on my way forward.

For now here is that Access SQL I mentioned earlier:

Code: [Select]
/*Convert objects to packages*/
UPDATE t_object
SET t_object.Object_Type = "Package"
WHERE (((t_object.[Object_Type])="Class"));
 
/*Insert packages into t_packages*/
INSERT INTO t_package
(Name,
Parent_ID,
CreatedDate,
ModifiedDate,
Notes,
ea_guid)
 
SELECT
t_object.Name,
t_object.ParentID,
t_object.CreatedDate,
t_object.ModifiedDate,
t_object.Object_ID,
t_object.ea_guid
FROM t_object
WHERE t_object.Object_ID>7; /*First 7 elements were already packages so exclude them*/
 
/*Map old t_object parent IDs to new ones in t_package using notes field in t_package*/
UPDATE t_package AS T1, t_package AS T2 SET T1.Parent_ID = [T2].[Package_ID]
WHERE (T1.Parent_ID)= IIF (IsNumeric ([T2].[Notes]), CInt([T2].[Notes]),0);


PS: Ignore that SQL as I found it didn't work. See later post for TSQL version that does work.
« Last Edit: May 10, 2016, 07:45:49 pm by Sunshine »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 7730
  • Karma: +165/-21
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Mass Conversion of Classes to Packages
« Reply #3 on: April 29, 2016, 12:43:09 pm »
That could work too, but I try to avoid editing the database whenever possible.

You can try this on a .eap file using a generic SQL Client. I've been using the free [ur=https://www.sqlmaestro.com/download/l]AnySQL Maestro[/url] for this purpose.

Or you can run it through a script using Repository.Execute(SQLString)

Geert

Sunshine

  • EA User
  • **
  • Posts: 500
  • Karma: +33/-1
  • Amicorum omnia communia
    • View Profile
Re: Mass Conversion of Classes to Packages
« Reply #4 on: May 10, 2016, 04:20:23 pm »
That could work too, but I try to avoid editing the database whenever possible.

You can try this on a .eap file using a generic SQL Client. I've been using the free [ur=https://www.sqlmaestro.com/download/l]AnySQL Maestro[/url] for this purpose.

Or you can run it through a script using Repository.Execute(SQLString)

Geert
Thanks Geert I concur about avoiding the editing the Database whenever possible but in this case I can't see how to do it via the API as it just creates new packages. I tried using MS Access initially but had to upgrade the eap file to MS Access 2010 got some SQL working but Sparx EA wouldn't read it so changed to using MS SQL Express.
Will post my SQL solution shortly
« Last Edit: May 10, 2016, 07:44:41 pm by Sunshine »

Sunshine

  • EA User
  • **
  • Posts: 500
  • Karma: +33/-1
  • Amicorum omnia communia
    • View Profile
Re: Mass Conversion of Classes to Packages
« Reply #5 on: May 10, 2016, 04:27:34 pm »
Okay after some distractions at work I managed to get back to looking at this particular problem again. Here is what I did to solve it.
  • Installed SQL Server Express and Management studio
  • Created a SQL Repository in SQL Server
  • Did a project Transfer using Sparx EA from eap file to SQL repository
  • Run the script below
  • Did project integrity check just to make sure everything worked ok
  • Did another project transfer using Sparx EA from SQL repository to a new eap file
Code: [Select]
/*Converts objects originally created as classes into packages*/
/*Copy Classes into into t_Package*/
INSERT INTO t_package
(Name,
Parent_ID,
CreatedDate,
ModifiedDate,
ea_guid)
SELECT
t_object.Name,
t_object.ParentID,
t_object.CreatedDate,
t_object.ModifiedDate,
t_object.ea_guid
FROM t_object
WHERE  ((t_object.[Object_Type])='Class');
 
/*Update objects with objects as parents in t_packages*/
UPDATE p
SET
p.Parent_ID = p2.package_ID
FROM dbo.t_package as p, dbo.t_object as o, dbo.t_package as p2
WHERE (p.parent_id=o.Object_ID) and (o.ea_guid = p2.ea_guid) and o.Object_Type='Class'

/*Update objects with packages as parents in t_packages*/
UPDATE p
SET
p.Parent_ID = o.package_ID
FROM dbo.t_package as p, dbo.t_object as o
WHERE (o.parentid=0) and (p.ea_guid=o.ea_guid) and (o.Object_Type='Class')

/*Set t_object table values*/
UPDATE t_object
SET
object_type = 'Package',
package_id = p.parent_id,
parentid =0,
pdata1 = p.Package_ID
from t_object, t_package p
Where  t_object.ea_guid = p.ea_guid AND t_object.object_type = 'Class'

/*Update diagrams parent ids*/
UPDATE d
SET
d.Package_ID = o.PDATA1,
d.ParentID=0
FROM dbo.t_diagram as d, dbo.t_object as o
WHERE (d.ParentID!=0) and (d.ParentID=o.Object_ID)

GeordieKiwi

  • EA Novice
  • *
  • Posts: 4
  • Karma: +1/-0
    • View Profile
Re: Mass Conversion of Classes to Packages
« Reply #6 on: May 18, 2016, 08:26:48 pm »
Thanks Sunshine. That code worked  a treat.