MENU

  /   Velocity

MetaFactory functions for velocity

The function below can be called from a velocity snippet by using the $generator context variable

  • findChildByAttribute(Element parent, String elementType, String attributeName, String attributeValue): ElementFinds 1 child element of parent. The type element (name of element) must be elementType. The attribute is given by attributeName and attributeValue
The result is an Element
    see example10 operation vtest1

    example:

    ##Haal het model package op met de naam domain_model
    #set($modelPackage=$context.getModelPackage(“domain_model”))

    ##Haal nu een object op waarvan het attribuut naam de waarde Phone heeft
    #set($phoneObject=0)
    #set($phoneObject=$generator.findChildByAttribute($modelPackage, “object”, “name”, “Phone”))
    #if($phoneObject!=0)
    // gevonden object = $phoneObject.getAttributeValue(“name”)
    #else
    $generator.error(“Het Phone object is niet gevonden in het model”)
    #end

  • findChildrenByAttribute(Element parent, String elementType, String attributeName, String attributeValue): List<org.jdom.Element>Zoekt meerdere childeren van parent van type elementType die het juiste attribuut hebben.
    Het resultaat is een List (java.util.List) met Element objects.
    zie example10 operation vtest2
  • findChildByProperty(Element parent, String elementType, String propertyName): ElementZoekt 1 child element van parent. Het type element (naam van element) moet elementType zijn. Het element moet een property hebben met de naam propertyName (onafhankelijk van waarde!)
    Het resultaat is een Element
    zie example10 operation vtest3
  • findChildByProperty(Element parent, String elementType, String propertyName, String propertyValue): ElementZoekt 1 child element van parent. Het type element (naam van element) moet elementType zijn. Het element moet een property hebben met de naam propertyName en de waarde propertyValue
    Het resultaat is een Element
    zie example10 operation vtest4
  • findChildrenByProperty(Element parent, String elementType, String propertyName): List<org.jdom.Element>Zoekt meerdere childeren van parent van type elementType die de juiste property hebben.
    Het resultaat is een List (java.util.List) met Element objects.
    zie example10 operation vtest4
  • findChildrenByProperty(Element parent, String elementType, String propertyName, String propertyValue): List<org.jdom.Element>Zoekt meerdere childeren van parent van type elementType die de property propertyName met de waarde propertyVaue hebben.
    Het resultaat is een List (java.util.List) met Element objects.
    zie example10 operation vtest5
  • elementContainsProperty(Element element, String propertyName): booleanControleert of het element een property met de naam propertyName heeft. Deze methode geeft true terug als de property aanwezig is (onafhankelijk van de waarde van deze property) en false als de property er niet is.
    Zie example10 operation vtest6. Het resultaat staat in de file Test5.java
  • elementContainsProperty(Element element, String propertyName, String propertyValue): booleanControleert of het element een property met de naam propertyName en de waarde propertyValue heeft. Deze methode geeft true terug als de property aanwezig is en false als de property er niet is.
    Zie example10 operation vtest6. Het resultaat staat in de file Test5.java
  • getChildrenWith(Element parent, String childType, String selectorType, String selectorName, String value): List<org.jdom.Element>Zoekt children van parent van het type childType die voldoen aan een bepaalde voorwaarde. Deze voorwaarde wordt opgegeven d.m.v. de parameters selectorType, selectorName en selectorValue.
    selectoryType kan uitsluitend de waarden “attribute” of “property” hebben.
    Zie example10 operation vtest7. Het resultaat staat in de file Test5.java

    example: Zoek alle references onder someObjectElement met het attribuut multiplicity=1..n

    getChildrenWith(someObjectElement, “reference”, “attribute”, “multiplicity”, “1..n”)

  • getElementProperty(Element element, String propertyName): StringGeeft de waarde van de propertyName van element. Als de propertyName niet wordt gevonden, wordt een lege String (“”) teruggegeven.
    Zie example10 operation vtest8. Het resultaat staat in de file Test6.java
  • getElementProperty(Element element, String propertyName, String defaultValue): StringGeeft de waarde van de propertyName van element. Als de propertyName niet wordt gevonden, dan wordt de defaultValue teruggegeven. Deze default wordt toegevoegd aan het model!
    Zie example10 operation vtest8. Het resultaat staat in de file Test6.java
  • getElementPropertyAsInt(Element element, String propertyName, int defaultValue): intGeeft de waarde van de propertyName van element. Als de propertyName niet wordt gevonden, dan wordt de defaultValue teruggegeven. Deze default wordt toegevoegd aan het model!.
    De property moet een int zijn. Als de gevonden property niet naar een int kan worden geconverteerd, dan wordt de default waarde teruggegeven.
    Zie example10 operation vtest8. Het resultaat staat in de file Test6.java
  • getPropertyElements(Element element, String propertyName): java.util.List<org.jdom.Element>Geeft een lijst met properties met de naam propertyName.
  • getElementProperties(Element element, String propertyName, String propertyValue, String propertyCondition): java.util.List<org.jdom.Element>Finds propertyElements by looking whether propertyValue and propertyCondition match. A propertyCondition can be “=” or “!=”. If the operator is “=” it finds all elements where property value equals propertyValue. If condition is “!=” it finds all elements where property value is different from propertyValue
  • addProperty(Element element, String propertyName, String propertyValue): voidAdd a property with name “propertyName” and value “propertyValue” to the element.
  • addProperty(Element element, String propertyName, String propertyValue, Namespace namespace): voidAdd a property with name “propertyName” and value “propertyValue” in the specified namespace to the element.
  • asList(Object[] someArray): ListConverts a array to a list. This method can be called from velocity to convert a array to a list so it’s easier to get some element or to find it’s length
  • createEmptyList(): ListCreates a empty ArrayList (java.util.ArrayList)
    example:
  • createEmptyMap(): MapCreates a empty Map (java.util.Map)
    example:
  • addLibraryToClass(String library)Adds a library (import statement) to the current class
    example:
  • addLibraryToClass(String library, boolean isStatic)Adds a library (import statement) to current class. If isStatic is true, a static import is created, otherwise a “normal” import.
    example:
  • addLibrary(XMIClass sxmiClass, String library, boolean isStatic)Adds a library (import statement) to sxmiClass. If isStatic is true, a static import is created, otherwise a “normal” import.
    example:
  • addLibraryToInterface(String library)Adds a library (import statement) to the current interface
    example:
  • addLibraryToInterface(String library, boolean isStatic)Adds a library (import statement) to current interface. If isStatic is true, a static import is created, otherwise a “normal” import.
    example:
  • addLibrary(XMIInterface sxmiInterface, String library, boolean isStatic)Adds a library (import statement) to sxmiInterface. If isStatic is true, a static import is created, otherwise a “normal” import.
    example:
  • evaluateSnippet(String snippetName): StringEvaluates a velocity snippet. The snippetName has the the same syntax as the pattern, but without ${snippet. and without }.
    If the snippet can not be found, it’s automatically created with default content.

    example:
    If we have this snippet expression in the pattern: ${snippet.java.model.operation.tostring}.
    We can call this snippet from a velocity template this way:

    $generator.evaluateSnippet(“java.model.operation.tostring”)

  • evaluateSnippet(String snippetName, String defaultSnippetContent): StringEvaluates a velocity snippet. The snippetName has the the same syntax as the pattern, but without ${snippet. and without }.
    If the snippet can not be found, it’s automatically created with defaultSnippetContent.

    example:
    If we have this snippet expression in the pattern: ${snippet.java.model.operation.tostring}.
    We can call this snippet from a velocity template this way:

    $generator.evaluateSnippet(“java.model.operation.tostring”, “// TODO: implement toString”)

  • evaluateFreeMarkerSnippet(String snippetName): StringEvaluates a freemarker snippet. The snippetName has the the same syntax as the pattern, but without ${fmsnippet. and without }.
    If the snippet can not be found, it’s automatically created.

    example:
    If we have this snippet expression in the pattern: ${fmsnippet.java.cdi.beans.load}.
    We can call this snippet from a freemarker template this way:

    $generator.evaluateFreeMarkerSnippet(“java.cdi.beans.load”)

  • evaluateFreeMarkerSnippet(String snippetName, String defaultSnippetContent): StringEvaluates a freemarker snippet. The snippetName has the the same syntax as the pattern, but without ${fmsnippet. and without }.
    If the snippet can not be found, it’s automatically created with defaultSnippetContent.

    example:
    If we have this snippet expression in the pattern: ${fmsnippet.java.cdi.beans.load}.
    We can call this snippet from a freemarker template this way:

    $generator.evaluateFreeMarkerSnippet(“java.cdi.beans.load”, “default content of new snippet if it doesn’t exist yet”)

The function below can be called from a velocity snippet by using the $context context variable

  • getPatternPropertyValue(String propertyName): StringFinds the value of pattern property with name propertyName and throws PropertyNotFoundException if property not found
    example:Suppose you have the following pattern property:   <pattern xmlns="http://www.firstbase.nl/xsd/personaliom/pattern" >
    <properties>
    ...
    <images.webdir>/images</images.webdir>
    ...
    </properties>
    ...
    </pattern>

    The pattern property images.webdir can be used in a velocity snippet this way:

                  ${context.getPatternPropertyValue('images.webdir')}
  • getPatternPropertyValue(String key, String defaultValue): StringFinds the value of pattern property with name propertyName and returns defaultValue if property not found
    example:Suppose you have the following pattern property:   <pattern xmlns="http://www.firstbase.nl/xsd/personaliom/pattern" >
    <properties>
    ...
    <images.webdir>/images</images.webdir>
    ...
    </properties>
    ...
    </pattern>

    The pattern property images.webdir can be used in a velocity snippet this way:

     

                  ${context.getPatternPropertyValue('images.webdir', 'images')}            
  • existsPatternProperty(String key): booleanChecks if the pattern property with name is key exists.
    example:Suppose you have the following pattern property:<pattern xmlns="http://www.firstbase.nl/xsd/personaliom/pattern" >
    <properties>
    ...
    <images.webdir>/images</images.webdir>
    ...
    </properties>
    ...
    </pattern>

    The following code can be used to check if this property exists:

    #if($context.existsPatternProperty(‘images.webdir’))
    …do something
    #else
    …do something else
    #end

  • getProjectPropertyValue(String propertyName): StringFinds the value of project property with name propertyName. Throws a PropertyNotFoundException if property not found
  • getProjectPropertyValue(String key, String defaultValue): StringFinds the value of project property with name propertyName. Returns defaultValue if property not found
  • existsProjectProperty(String key): booleanChecks if the project property with specified key exists.

The function below can be called from a velocity snippet by using the $sxmiFactory context variable

  • TODOExplanation of sxmiFactory => mail me
    example:

First Base MetaFactory Help

Velocity best practices and lines you often need

  • Always check if your expectations are true. If you expect something to be loaded, check it and give a error with a good explanation => Fail Fast!.

    ##stop if $currentModelPackage is null
    #if(!$currentModelPackage)
    $generator.error("currentModelPackage not found in context")
    #end
    and / or

    ##stop if $currentModelObject is null
    #if(!$currentModelObject)
    $generator.error("currentModelObject not found in context")
    #end
    and / or

    ##stop if $currentModelAttribute is null
    #if(!$currentModelAttribute)
    $generator.error("currentModelAttribute not found in context")
    #end
    and / or

    ##stop if $currentModelReference is null
    #if(!$currentModelReference)
    $generator.error("currentModelReference not found in context")
    #end

  • Declare and define the variables you need in velocity.

    #set($modelpackage=$currentModelPackage) ## element of model
    #set($modelPackageName=$modelpackage.getAttributeValue("name"))
    #set($modelPackageNameFL=${generator.firstLower(${modelPackageName})})
    and / or

    #set($modelobject=$currentModelObject) ## element of model
    #set($modelObjectName=$modelobject.getAttributeValue("name"))
    #set($modelObjectNameFL=${generator.firstLower(${modelObjectName})})
    and / or

    #set($attributes=$modelobject.getChildren("attribute",${nsModel}))
    #foreach($attribute in $attributes)
    #set($attributeName=$attribute.getAttributeValue("name"))
    #set($attributeType=$attribute.getAttributeValue("type"))
    ...
    do something (look at tostring.vm of example 7)
    ...
    #end
    and / or

    #set($reference=$currentModelReference) ## element of model
    #set($referenceName=$reference.getAttributeValue("name"))
    #set($referenceType=$reference.getAttributeValue("type"))
    #set($referenceMultiplicity=$reference.getAttributeValue("multiplicity"))
    #set($referenceObjectElement=$generator.findChildByAttribute($currentModelPackage,"object","name",$referenceType))
    #set($referencePluralProperty=$generator.getElementProperty($referenceObjectElement,"name.plural"))
    #set($referencePluralPropertyFL=${generator.firstLower($referencePluralProperty)})

MetaFactory global variables for Velocity or Freemarker

  • ${currentModelPackage}the model package which is used (JDOM Element)
  • ${currentModelObject}the model object which is used (JDOM Element)
  • ${currentModelAttribute}the model attribute which is used (JDOM Element)
  • ${currentModelReference}the model reference which is used (JDOM Element)
  • ${packagePattern}the package of the current pattern (JDOM Element)
  • ${classPattern}the class of the current pattern (JDOM Element)
  • ${interfacePattern}the interface of the current pattern (JDOM Element)
  • ${operationPattern}the operation of the current pattern (JDOM Element)
  • ${attributePattern}the attribute of the current pattern (JDOM Element)
  • ${xmlDocumentPattern}the xml document of the current pattern (JDOM Element)
  • ${xmlElementPattern}the xml element of the current pattern (JDOM Element)
  • ${filePattern}the pattern file of the current pattern (JDOM Element)
  • ${generatedPackage}The package which is generated (SXMIPackage)
  • ${generatedClass}The class which is generated (SXMIClass)
  • ${generatedInterface}The interface which is generated (SXMIInterface)
  • ${generatedOperation}The method (operation) which is generated (SXMIOperation)
  • ${generatedAttribute}The attribute which is generated (SXMIattribute)