Author Topic: DocumentGenerator and stylesheets  (Read 184 times)

deadbird

  • EA Novice
  • *
  • Posts: 18
  • Karma: +1/-0
    • View Profile
DocumentGenerator and stylesheets
« on: October 27, 2017, 09:17:00 pm »
Hi everyone.

Following my previous posts about discussions, here I am again with another problem, searching for a beacon of light in EA's darkness ^^

Here is the problem. I successfuly created a nice script that runs through all of the Discussions, eventually filtering the status. It generates a functional output. See attached script.

My problem is that I can't apply styles from a custom style sheet.
I load stylesheet using docGenerator.SetStyleSheetDocument("GAIO - Feuille de style") -> it works.
I use  docGenerator.InsertText("a string", "a style from the stylesheet"); -> plain text is inserted, although the style I chose I contained in the stylesheed I successfuly loaded...

Can someone help?

Thanks a lot in advance ;)

deadbird

  • EA Novice
  • *
  • Posts: 18
  • Karma: +1/-0
    • View Profile
Re: DocumentGenerator and stylesheets
« Reply #1 on: October 27, 2017, 09:18:16 pm »
The script itself:

Quote
!INC Local Scripts.EAConstants-JScript
!INC EAScriptLib.JScript-XML
 
var DISCUSSION_TEMPLATE = "discussion";
var OUTPUT_FILE = "c:\\temp\\TestDiscussions.rtf";
 
var docGenerator as EA.DocumentGenerator;
var generationSuccess = false;
var saveSuccess = false;
 
var statusOpen = "Ouverte";
var statusClosed = "Close";

var statusPending = "En attente de compléments";
 
function getNodeText(string, xpath) {
       var xmlDOM = XMLParseXML(string);
     
       if (!xmlDOM) {
             ReportFatal("XML fails");
       }
     
       value = XMLGetNodeText(xmlDOM, xpath);
 
       return value;
}
 
function hasDiscussions(element) {
       var currentElement as EA.Element;
       currentElement = element;
     
       result = Repository.SQLQuery("SELECT COUNT(*) AS nbdisc FROM t_document WHERE elementid = '" + currentElement.ElementGUID + "' AND doctype='EDisc'");
     
       return parseInt(getNodeText(result, "EADATA/Dataset_0/Data/Row/nbdisc"));
}
 
function generateDiscussions(element) {
       var currentElement as EA.Element;
       currentElement = element;
     
       var discussionStatus = statusOpen;
     
       //SQL query the discussions for the element
       result = Repository.SQLQuery("SELECT elementid, strcontent, doctype, style FROM t_document WHERE t_document.elementType = 'Post' AND elementid = '" + currentElement.ElementGUID + "' ORDER BY style");
       var xmlDOM = XMLParseXML(result);
       if (!xmlDOM) {
             ReportFatal("XML fails");
       }
     
       array = xmlDOM.selectNodes("EADATA/Dataset_0/Data/*");
       for (var i=0; i<array.length; i++) {
             node = array.nextNode;
             if (node != null) {
                    guid = getNodeText(node.xml, "Row/elementid");
                    str = getNodeText(node.xml, "Row/strcontent");
                    type = getNodeText(node.xml, "Row/doctype");
                    statusXML = getNodeText(node.xml, "Row/style");
                   
                    if (statusXML.indexOf("RS_PEND") != -1) {
                           discussionStatus = statusPending;
                    } else if (statusXML.indexOf("RS_CLSD") != -1)  {
                           discussionStatus = statusClosed;
                    } else {
                           discussionStatus = statusOpen;
                    }
                   
                    if (type == "EDisc") {
                           docGenerator.InsertText("Question: " + str + " - " + discussionStatus + "\n", "Discussion - titre");
                    } else {
                           docGenerator.InsertText("Réponse: " + str + "\n", "Discussion - notes");
                    }
             }
       }
       docGenerator.InsertText("\n\n", "");
}
 
function DumpElements(thePackage) {
       var currentPackage as EA.Package;
       currentPackage = thePackage;
           
       var elementEnumerator = new Enumerator(currentPackage.Elements);
       while (!elementEnumerator.atEnd()) {
             var currentElement as EA.Element;
             currentElement = elementEnumerator.item();
           
             nb = hasDiscussions(currentElement);
             if (nb > 0) {
                    ReportInfo("J'ai trouvé " + nb + " discussions, je génère une doc pour cet élément");
                    docGenerator.InsertText("Element: " + currentElement.Name + "\n", "");
                    docGenerator.InsertText("Type: " + currentElement.Type + "\n", "");
                    generateDiscussions(currentElement);
             }
             elementEnumerator.moveNext();
       }
}
 
function DumpPackage(thePackage) {
       //cast du paramètre en EA.Package pour avoir accès à l'autocomplétion
       var currentPackage as EA.Package;
       currentPackage = thePackage;
     
       // Dump the elements this package contains
       DumpElements(currentPackage);
     
       // Recursively process any child packages
       var childPackageEnumerator = new Enumerator(currentPackage.Packages);
       while (!childPackageEnumerator.atEnd()) {
             var childPackage as EA.Package;
             childPackage = childPackageEnumerator.item();
           
             DumpPackage(childPackage);
           
             childPackageEnumerator.moveNext();
       }
}
 
function parcoursArborescence() {
       //Parcours de tous les objets du modèle, root node par root node
       var modelEnumerator = new Enumerator(Repository.Models);
       while (!modelEnumerator.atEnd())
       {
             var currentModel as EA.Package;
             currentModel = modelEnumerator.item();
           
             // Recursively process this package
             DumpPackage(currentModel);
           
             modelEnumerator.moveNext();
       }
       ReportInfo("Parcours de l'arborescence terminé.");
}
function main() {
       //active la fenetre console
       Repository.EnsureOutputVisible("Script");
 
       Session.Output("Parcours de l'arborescence d'un projet et génération de doc en Javascript");
       Session.Output("----------------------------------------------------------------------------------------");
     
       //création du générateur de doc
       docGenerator = Repository.CreateDocumentGenerator();
       if (docGenerator.SetStyleSheetDocument("GAIO - Feuille de style") == false) {
             ReportWarning("Stylesheet non appliquée :(");
       }
       docGenerator.NewDocument("");
     
       //parcours de l'arborescence
       parcoursArborescence();
     
       //enregistrement du fichier
       saveSuccess = docGenerator.SaveDocument(OUTPUT_FILE, dtRTF);
       if ( saveSuccess )
             ReportInfo( "Documentation complete!" );
       else
             ReportWarning( "Error saving file: " + docGenerator.GetLastError() );
}
 
main();
 
function ReportInfo(message) {
       Session.Output("[INFO] " + message);
}
 
function ReportWarning(message) {
       Session.Output("[WARNING] " + message);
}
 
function ReportFatal(message) {
       Session.Output("[FATAL] " + message);
       Session.Prompt(message, promptOK);
}

Uffe

  • EA Practitioner
  • ***
  • Posts: 1072
  • Karma: +81/-5
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: DocumentGenerator and stylesheets
« Reply #2 on: October 27, 2017, 09:37:35 pm »
Hello,

This isn't quite my area, more Geert's, but the API documentation says "style: String - the name of the style in the template; defaults to Normal style".

So if you're specifying a style that exists in the stylesheet but not in the template, that might be why. Try specifying a style that's in the template but not the stylesheet and see if that works.


/Uffe
My theories are always correct, just apply them to the right reality.

deadbird

  • EA Novice
  • *
  • Posts: 18
  • Karma: +1/-0
    • View Profile
Re: DocumentGenerator and stylesheets
« Reply #3 on: October 27, 2017, 09:41:15 pm »
Here I don't even use templates, and here's why: discussions have no "ea_guid". Only their attached element can be use as a entry point for a template. And it's not what I want, for some reasons that are too long to explain here ^^

deadbird

  • EA Novice
  • *
  • Posts: 18
  • Karma: +1/-0
    • View Profile
Re: DocumentGenerator and stylesheets
« Reply #4 on: October 27, 2017, 10:36:46 pm »
Found the solution, thanks to you Uffe. Your answer definitely put me on the way. I created a blank template, assigned my stylesheet to it, and used its name in "docGenerator.NewDocument". And it works!