Hello.
I have been working on automating HTML report creation. During testing I noticed that after cloning a package with package.Clone(), RunHTMLReport() creates no output and RunReport() raises Error creating output file.
Even if I try to run the report on the original package or some other package, the result is the same: no output for RunHTMLReport and error from RunReport.
Running the reports on their own, without cloning the package work as expected and both html report and normal report are created.
the Clone function itself works just fine and creates a clone of the package as expected.
Here is the code I used for testing:
import org.sparx.Collection;
import org.sparx.Package;
import org.sparx.Project;
import org.sparx.Repository;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.FileNotFoundException;
class CloneTest {
public static void main(String[] args){
//repository object
Repository r = new Repository();
Project pi = r.GetProjectInterface();
//parse given input: connection string for repository, package guid and output path for html report
String connectionString = args[0];
String pGUID = args[1];
String output = args[2];
//connect to EA repository
System.out.println("connecting to repository");
boolean b = r.OpenFile(connectionString);
if (b == false){
System.out.println("Could not connect to repository, check that connection is working and connection string is correct.");
Finish(r);
}
//get given package from repostitory and store into package object
Package p = null;
System.out.println("finding package: " + pGUID);
try{
p = r.GetPackageByGuid(pGUID);
} catch(NullPointerException e) {
System.out.println("Package not found. check package GUID is correct");
Finish(r);
}
//make sure package was found
if (p == null) {
System.out.println("package NOT found");
Finish(r);
}
System.out.println("package found: " + p.GetName());
// get parent package
Package parent = null;
parent = r.GetPackageByID(p.GetParentID());
//package clone
Package pc = null;
pc = p.Clone();
//add package for testing
// pc = parent.GetPackages().AddNew(p.GetName(),"");
//update package clone and package parent
pc.Update();
parent.Update();
//rename package clone
System.out.println("renaming package: " + pc.GetName());
pc.SetName(pc.GetName() + "_html");
//update package clone and parent
pc.Update();
parent.Update();
System.out.println("renamed package: " + pc.GetName());
System.out.println("Original package GUID: " + p.GetPackageGUID());
System.out.println("Cloned package GUID: " + pc.GetPackageGUID());
// Package names in ea can contain chars that are forbidden in filenames (\,/ etc).
//regex replaces forbidden chars with "_".
String projectNameFolder = pc.GetName();
projectNameFolder = projectNameFolder.replaceAll("[^a-zA-Z0-9\\.\\-]", "_");
// creates a new folder for the project if it doesn't already exist. removes old files if folder already exists
File outputFolder = new File(output + projectNameFolder);
if (!outputFolder.exists()){
outputFolder.mkdir();
} else {
System.out.println("Folder already exists. Removing old HTML report files");
File[] files = outputFolder.listFiles();
for (File f: files){
deleteFolder(f);
}
}
// generate the HTML report to the specified outputpath
String outputPath = outputFolder.getPath();
System.out.println("OUTPUTPATH: " + outputPath);
//refresh and reload project
r.GetModels().Refresh();
pi.ReloadProject();
// run html report and regular report on cloned package
pi.RunReport(pc.GetPackageGUID(), "Diagram Report", outputPath + "/" + p.GetName() + "_.rtf");
pi.RunHTMLReport(pc.GetPackageGUID(), outputPath + "/", "PNG", "<default>", ".html");
// run report on original package for testing
// pi.RunReport(p.GetPackageGUID(), "Diagram Report", outputPath + "/" + p.GetName() + "_.rtf");
// pi.RunHTMLReport(p.GetPackageGUID(), outputPath + "/", "PNG", "<default>", ".html");
//call finish function to close ea repository
Finish(r);
}
//recursively deletes given folder and its contents
public static void deleteFolder(File folder) {
File[] files = folder.listFiles();
if(files!=null) { //some JVMs return null for empty dirs
for(File f: files) {
if(f.isDirectory()) {
deleteFolder(f);
} else {
f.delete();
}
}
}
folder.delete();
}
//finish method to close EA repository after program is done
public static void Finish( Repository r){
System.out.println("SHUTTING DOWN EA");
r.CloseFile();
r.Exit();
r=null;
System.exit(0);
}
}
commenting out the line:
pc = p.Clone();
and commenting in the line:
// pc = parent.GetPackages().AddNew(p.GetName(),"");
makes the RunHTMLReport() and RunReport() work normally.
I am not sure why the RunHTMLReport() and RunReport() don't work after cloning a package, is there something I am missing in the code or is it a bug?