import * as vscode from 'vscode' ; import { FileBuilder } from '../fileBuilder' ; import { AZSymbolInformation } from '../../symbollibraries/azSymbolInformation' ; import { AZSymbolKind } from '../../symbollibraries/azSymbolKind' ; import { ALSymbolsBasedWizard } from './alSymbolsBasedWizard' ; import { ALSyntaxWriter } from '../../allanguage/alSyntaxWriter' ; import { DevToolsExtensionContext } from '../../devToolsExtensionContext' ; export class ALSymbolsBasedXmlPortWizard extends ALSymbolsBasedWizard { constructor ( context ) { super ( context ) ; } async showWizard ( tableSymbols ) { if ( tableSymbols . length == 1 ) await this . showXmlPortWizard ( tableSymbols [ 0 ] ) ; else await this . showMultiXmlPortWizard ( tableSymbols ) ; } async showMultiXmlPortWizard ( tableSymbols ) { if ( ! FileBuilder . checkCrsExtensionFileNamePatternRequired ( ) ) return ; const objType = AZSymbolKind . XmlPortObject ; let relativeFileDir = await this . getRelativeFileDir ( objType ) ; let startObjectId = await this . getObjectId ( relativeFileDir , "xmlport" , `Please enter a starting ID for the xmlport objects.` , 0 ) ; if ( startObjectId < 0 ) { return ; } let fieldsAsElements = await this . promptForFieldsAsElements ( ) ; if ( fieldsAsElements === undefined ) { return ; } for ( let i = 0 ; i < tableSymbols . length ; i ++ ) { let tableSymbol = tableSymbols [ i ] ; let objectId = startObjectId + i ; let objectName = this . getDefaultXmlPortName ( tableSymbol ) ; await this . createAndShowNewXmlPort ( tableSymbol , objType , objectId , objectName , fieldsAsElements , relativeFileDir ) ; } } async showXmlPortWizard ( tableSymbol ) { if ( ! FileBuilder . checkCrsFileNamePatternRequired ( ) ) return ; const objType = AZSymbolKind . XmlPortObject ; let relativeFileDir = await this . getRelativeFileDir ( objType ) ; let objectId = await this . getObjectId ( relativeFileDir , "xmlport" , "Please enter an ID for the xmlport object." , 0 ) ; if ( objectId < 0 ) { return ; } let objectName = await this . getObjectName ( "Please enter a name for the xmlport object." , this . getDefaultXmlPortName ( tableSymbol ) ) ; if ( ! objectName ) return ; let fieldsAsElements = await this . promptForFieldsAsElements ( ) ; if ( fieldsAsElements === undefined ) { return ; } await this . createAndShowNewXmlPort ( tableSymbol , objType , objectId , objectName , fieldsAsElements , relativeFileDir ) ; } private async createAndShowNewXmlPort ( tableSymbol , objType , objectId , objectName , fieldsAsElements , relativeFileDir ) { let fileName = await FileBuilder . getPatternGeneratedFullObjectFileName ( objType , objectId , objectName ) ; this . showNewDocument ( this . buildXmlPortForTable ( tableSymbol , objectId , objectName , fieldsAsElements ) , fileName , relativeFileDir ) ; } buildXmlPortForTable ( tableSymbol , objectId , objectName , fieldsAsElements ) { let writer = new ALSyntaxWriter ( undefined ) ; writer . writeStartObject ( "xmlport" , objectId . toString ( ) , objectName ) ; this . appendSchema ( writer , tableSymbol , fieldsAsElements ) ; this . appendReportRequestPage ( writer ) ; writer . writeEndObject ( ) ; return writer . toWizardGeneratedString ( ) ; } private appendSchema ( writer , tableSymbol , fieldsAsElements ) { var tableElementName = writer . createName ( tableSymbol . name ) ; var fieldNodeName ; if ( fieldsAsElements ) fieldNodeName = "fieldelement" ; else fieldNodeName = "fieldattribute" ; writer . writeStartNamedBlock ( "schema" ) ; writer . writeStartGroup ( "textelement" , "RootNodeName" ) ; writer . writeStartNameSourceBlock ( "tableelement" , tableElementName , writer . encodeName ( tableSymbol . name ) ) ; let fieldList = [ ] ; tableSymbol . collectChildSymbols ( AZSymbolKind . Field , true , fieldList ) ; fieldList . forEach ( item => { writer . writeNameSourceBlock ( fieldNodeName , writer . createName ( item . name ) , tableElementName + "." + writer . encodeName ( item . name ) ) ; } ) ; writer . writeEndBlock ( ) ; writer . writeEndBlock ( ) ; writer . writeEndBlock ( ) ; } private appendReportRequestPage ( writer ) { writer . writeStartNamedBlock ( "requestpage" ) ; writer . writeStartLayout ( ) ; writer . writeStartGroup ( "area" , "content" ) ; writer . writeStartGroup ( "group" , "GroupName" ) ; writer . writeEndBlock ( ) ; writer . writeEndBlock ( ) ; writer . writeEndLayout ( ) ; writer . writeStartNamedBlock ( "actions" ) ; writer . writeStartGroup ( "area" , "processing" ) ; writer . writeEndBlock ( ) ; writer . writeEndBlock ( ) ; writer . writeEndBlock ( ) ; } private async promptForFieldsAsElements ( ) { let fieldAsAttributeText = "Table fields as xml attributes" ; let fieldAsElementText = "Table fields as xml elements" ; let fieldNodeTypes = [ { label : fieldAsAttributeText , description : "" } , { label : fieldAsElementText , description : "" } ] ; let selectedNodeType = await vscode . window . showQuickPick ( fieldNodeTypes ) ; if ( ( ! selectedNodeType ) || ( ! selectedNodeType . label ) ) { return undefined ; } return ( selectedNodeType . label == fieldAsElementText ) ; } private getDefaultXmlPortName ( tableSymbol ) { return `${ tableSymbol . name . trim ( ) } XmlPort` ; } }