Book a Demo

Author Topic: Access document context stored in internal artifact  (Read 4084 times)

SergeyL

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Access document context stored in internal artifact
« on: September 22, 2022, 04:49:28 pm »
Hi,
I stored an xslt file in the model in order to use it for the model export transformation in my add-in.
I'd like to access the xslt contents using automation API ODER SQL request. Does somebody have any idea how to do that?
The queering over the corresponding model element
SELECT * FROM t_object o WHERE o.Object_ID ='169911'   
didn't give the expected result - in the resulting XML I cannot see the actual xslt stored in the file.
Any ideas?
Possibly another alternative method how to store add-in configuration(s) within the model, to access it efficiently?

Many thanks in advance.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Access document context stored in internal artifact
« Reply #1 on: September 22, 2022, 05:38:17 pm »
Did you look in t_document?
It might be stored in a weird binary format.

An alternative might be to create a regular element and use it's notes field to store the XSLT (since it's only plain text you need)

Geert

SergeyL

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Access document context stored in internal artifact
« Reply #2 on: September 22, 2022, 06:57:41 pm »
Many thanks Geert,

indeed, the document was stored in the t_document table and I could find it:
SELECT * FROM t_document d where d.docname = 'transform.xslt'

The xslt (I assume) is encoded in binary in the BINCONTENT field, and I'm now trying to decode it.

Storing an xml/xslt in the notes of an element is of course possible, but not very convenient, as for watching/editing the XSLT it needs to be copy-pasted to an external XML editor...

 

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Access document context stored in internal artifact
« Reply #3 on: September 22, 2022, 09:25:21 pm »
It's a zip with a single file str.dat inside.

q.

SergeyL

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Access document context stored in internal artifact
« Reply #4 on: September 22, 2022, 10:27:48 pm »
Thanks qwerty,

searching though the posts I came to the same idea, but still struggling to get the file (contents) out of the encoded string (still nub in C#).
I applied the following:

query = "SELECT * FROM t_document d where d.docname = '" + el.Name + "'";
xmlResult = repository.SQLQuery(query);
string base64str = doc.Descendants("BINCONTENT").First().Value;
byte[] xsltZIPfile = Convert.FromBase64String(base64str);

and now don't know how to convert the received byte array to the ZIP file and unzip it (str.dat) in within my code :-(
I'm still searching through teh WEb, so any help is highly appreciated!
Thanks in advance!

SergeyL

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Access document context stored in internal artifact
« Reply #5 on: September 22, 2022, 10:29:59 pm »
Found this post:
https://www.sparxsystems.com/forums/smf/index.php?topic=45064.0
but don't see the final solution for me yet, see my prev. post.

SergeyL

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Access document context stored in internal artifact
« Reply #6 on: September 22, 2022, 10:44:59 pm »
Finally, thanks the code example in the link of the previous post, it works!
Here is my final code:

EA.Package configPackage = repository.GetPackageByID(packageId);
foreach (EA.Element el in configPackage.Elements)
{
   if (el.Type == "Artifact" && el.Stereotype == "xslt")
   {
      query = "SELECT * FROM t_document d where d.docname = '" + el.Name + "'";
      xmlResult = repository.SQLQuery(query);

      doc = XDocument.Parse(xmlResult);
      string base64str = doc.Descendants("BINCONTENT").First().Value;
      
      // Convert the base64 string to a byte array.
      byte[] xsltZIPfile = Convert.FromBase64String(base64str);

      // Unzip
      String unzippedData = "unzip failed";
      using (var memoryStream = new System.IO.MemoryStream(xsltZIPfile))
      {
         using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Read))
         {
            foreach (var entry in archive.Entries)
            {
               using (var stream = entry.Open())
               using (var reader = new System.IO.StreamReader(stream))
               {
                  unzippedData = reader.ReadToEnd();
               }
            }
         }
      }
      // my logger
      exportLogger.WriteLine(unzippedData);
   }
}