Reputation: 63
I am trying to write templates in VTL to generate java classes with telosys. My starting point is a directory (src/main/resources/templates/es) that contains json files (mapping1.json, mapping2.json, ...). The number and names of json files are unknown and may vary. I have to generate several java classes per json files.
I have already write a template for each java classe that i have to generate. But these templates work with a static embedded json object.
This an example:
#set($json = {
"template":"acces_formation",
"mappings":{
"data":{
"properties":{
"MATRICULE":{
"type":"string",
"index":"not_analyzed"
},
"NOM":{
"type":"string",
"index":"not_analyzed"
},
"DATE_NAIS":{
"type":"date",
"format":"dd/MM/yyyy"
},
"SEXE":{
"type":"string",
"index":"not_analyzed"
},
"GRADE":{
"type":"string",
"index":"not_analyzed"
}
}
}
}
})
#macro(javaName $s)$s.substring(0,1).toUpperCase()$s.substring(1)#end
#macro(setter $s)set#javaName($s)#end
#macro(getter $s)get#javaName($s)#end
###############################start macro toCamelCase
#macro(toCamelCase $s)
#set($datas = $s.split("_"))
#set($name = "")
#foreach($data in $datas)
#set($data = $data.substring(0,1).toUpperCase()+$data.substring(1))
#set($name = $name+$data)
#end
$name##
#end
###############################End macro toCamelCase
###############################Start macro tab
#macro(tab $nbreTotal)
#if($nbreTotal >= 1)
#foreach($nbre in [1..$nbreTotal])
##
#end
#end
#end
###############################End macro tab
###############################Start macro javaType
#macro(javaType $f, $nbreTab)
#if($f.equals("string"))
#tab($nbreTab)String##
#elseif($f.equals("boolean"))
#tab($nbreTab)Boolean##
#elseif($f.equals("long"))
#tab($nbreTab)Long##
#elseif($f.equals("float"))
#tab($nbreTab)Float##
#elseif($f.equals("double"))
#tab($nbreTab)Double##
#elseif($f.equals("int"))
#tab($nbreTab)Integer##
#elseif($f.equals("integer"))
#tab($nbreTab)Integer##
#elseif($f.equals("date"))
#tab($nbreTab)String##
#elseif($f.equals("array"))
#tab($nbreTab)java.util.List<#javaType($f)>##
#else
#tab($nbreTab)$f##
#end
#end
###############################End macro javaType
#############################################
#set($templateName = $json.template)
#set($entity = "#toCamelCase($json.template)")
#set($entityDto = $entity+"Dto")
#set($param = "_param")
/*
Java dto for elasticSearch template $templateName
Created on $today.date ( Time $today.time )
Generator tool : $generator.name ( version $generator.version )
*/
package ${target.javaPackageFromFolder(${SRC})};
import java.util.Date;
import ${ROOT_PKG}.helper.contract.SearchParam;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
/**
DTO for elasticSearch template "$templateName"
@author Lazare yao
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder(alphabetic = true)
@getter
@Setter
@NoArgsConstructor
@tostring
public class $entityDto implements Cloneable {
#set($properties = $json.mappings.data.properties)
private String#tab(3)_id;
#foreach($key in $properties.keySet())
#set($field = $key.toLowerCase())
#tab(1)private #javaType($properties.get($key).type,0)#tab(3)$field ;
#end
private SearchParam<String>#tab(3)_id$param;
#foreach($key in $properties.keySet())
#set($field = $key.toLowerCase())
#tab(1)private SearchParam<#javaType($properties.get($key).type,0)>#tab(3)${field}$param;
#end
private String orderDirection;
private String orderField;
}
What i need now is to:
1- browse my json directory, get each json file and apply the templates on that json to create the java classes for that json file.
2- properly configure the file templates.cfg to create java class file with the java class name. Concerning this point, i heve tried to modify the variable ${BEANNAME}, ${BEANNAME_UC}, ${BEANNAME_LC} (given by default by telosys) since the template. But it did not work: They still empty and No file is generated. This is the content of the file templates.cfg :
#---------------------------------------------------------
# Templates configuration file
# Values separated by ";"
# . value 1 : the label
# . value 2 : the file to be generated ( var allowed : ${BEANNAME}, ${BEANNAME_UC}, ${BEANNAME_LC} )
# . value 3 : the project folder where to generate ( var allowed : ${BEANNAME}, ${BEANNAME_UC}, ${BEANNAME_LC} )
# . value 4 : the template to use
# . value 5 : number of execution : "1" for "ONCE" for all entities, default is multiple executions ( executed for each entity )
#---------------------------------------------------------
# Since v 2.0 the project's variables can be used in file and folder name
#---------------------------------------------------------
Dto ; ${BEANNAME}Dto.java ; ${SRC}/${ROOT_PKG}/helper/dto ; dto.vm
CustomDto ; _${BEANNAME}Dto.java ; ${SRC}/${ROOT_PKG}/helper/dto/customize ; _dto.vm
Enum ; ${BEANNAME}Enum.java ; ${SRC}/${ROOT_PKG}/helper/enums ; enum.vm
Repository ; ${BEANNAME}Repository.java ; ${SRC}/${ROOT_PKG}/dao/repository/es ; repository.vm ;
Custom Repository ; _${BEANNAME}Repository.java ; ${SRC}/${ROOT_PKG}/dao/repository/es/customize ; _repository.vm ;
Business ; ${BEANNAME}Business.java ; ${SRC}/${ROOT_PKG}/business/ ; business.vm
Controller ; ${BEANNAME}Controller.java ; ${SRC}/${ROOT_PKG}/rest/api ; controller.vm
Thank you for helping me please !
Upvotes: 2
Views: 536
Reputation: 63
Thank you for your help.
I did what @lgu explained to me above and it works very well. But with this version i can't choose which json file i want to generate the java classes. I can not also choose which template file i want to apply. These 2 problems make me think to an other version with an other way to do what i want: I thought about taking advantage of the dbrep file. So in 2nd version, i generate the dbrep file (which is an xml file) with the DOM of java. I followed this tutorial to fill the dbrep xml tag with the json files elements. Each json file becomes a <table>
element in the the <tableList>
element. And the json file properties become the <column>
elements in the <table>
element. The final dbrep file look like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<tableList databaseId="1" databaseName="test" databaseProductName="MySQL" generation="2019-04-18 16:52:19">
<table catalog="bd_om_analytics" databaseType="TABLE" javaBean="AirtimeTrendHorarire" name="airtime_trend_horarire" schema="trend_horaire_airtime">
<column dbComment="ElasticSearch zone_commerciale field" dbDefaultValue="" dbName="zone_commerciale" dbNotNull="false" dbPosition="1" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="zoneCommerciale" javaType="java.lang.String" jdbcTypeCode="12" label="zone commerciale" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch tete_de_pont field" dbDefaultValue="" dbName="tete_de_pont" dbNotNull="false" dbPosition="2" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="teteDePont" javaType="java.lang.String" jdbcTypeCode="12" label="tete de pont" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch trend_horaire field" dbDefaultValue="" dbName="trend_horaire" dbNotNull="false" dbPosition="3" dbPrimaryKey="false" dbSize="22" dbTypeName="DOUBLE" inputType="number" javaName="trendHoraire" javaType="java.lang.Double" jdbcTypeCode="8" label="trend horaire" notNull="false" selected="true"/>
<column dateType="DT" dbComment="ElasticSearch date_jour field" dbDefaultValue="" dbName="date_jour" dbNotNull="false" dbPosition="4" dbPrimaryKey="false" dbSize="19" dbTypeName="DATETIME" inputType="" javaName="dateJour" javaType="java.util.Date" jdbcTypeCode="93" label="date jour" notNull="false" selected="true"/>
<column dateType="DT" dbComment="ElasticSearch date_alerte field" dbDefaultValue="" dbName="date_alerte" dbNotNull="false" dbPosition="5" dbPrimaryKey="false" dbSize="19" dbTypeName="DATETIME" inputType="" javaName="dateAlerte" javaType="java.util.Date" jdbcTypeCode="93" label="date alerte" notNull="false" selected="true"/>
<column dbComment="ElasticSearch heure field" dbDefaultValue="" dbName="heure" dbNotNull="false" dbPosition="6" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="heure" javaType="java.lang.String" jdbcTypeCode="12" label="heure" maxLength="65000" notNull="false" selected="true"/>
<column dbAutoIncremented="true" dbComment="" dbDefaultValue="" dbName="id" dbNotNull="true" dbPosition="7" dbPrimaryKey="true" dbSize="10" dbTypeName="INT" inputType="number" javaName="id" javaType="java.lang.Integer" jdbcTypeCode="4" label="Id" notNull="true" selected="true"/>
<column dbComment="ElasticSearch source _id" dbDefaultValue="" dbName="_id" dbNotNull="false" dbPosition="8" dbPrimaryKey="false" dbSize="255" dbTypeName="VARCHAR" inputType="text" javaName="_id" javaType="java.lang.String" jdbcTypeCode="12" label="Id" maxLength="255" notNull="true" selected="true"/>
</table>
<table catalog="bd_om_analytics" databaseType="TABLE" javaBean="OmerDetailPdvSolde" name="omer_detail_pdv_solde" schema="airtime_detail_pdv_*">
<column dbComment="ElasticSearch zone_commerciale field" dbDefaultValue="" dbName="zone_commerciale" dbNotNull="false" dbPosition="1" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="zoneCommerciale" javaType="java.lang.String" jdbcTypeCode="12" label="zone commerciale" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch COMMERCIAL field" dbDefaultValue="" dbName="COMMERCIAL" dbNotNull="false" dbPosition="2" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="cOMMERCIAL" javaType="java.lang.String" jdbcTypeCode="12" label="COMMERCIAL" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch PDV field" dbDefaultValue="" dbName="PDV" dbNotNull="false" dbPosition="3" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="pDV" javaType="java.lang.String" jdbcTypeCode="12" label="PDV" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch RUPTURE field" dbDefaultValue="" dbName="RUPTURE" dbNotNull="false" dbPosition="4" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="rUPTURE" javaType="java.lang.String" jdbcTypeCode="12" label="RUPTURE" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch Canal field" dbDefaultValue="" dbName="Canal" dbNotNull="false" dbPosition="5" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="canal" javaType="java.lang.String" jdbcTypeCode="12" label="Canal" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch BALANCE field" dbDefaultValue="" dbName="BALANCE" dbNotNull="false" dbPosition="6" dbPrimaryKey="false" dbSize="20" dbTypeName="BIGINT" inputType="number" javaName="bALANCE" javaType="java.lang.Long" jdbcTypeCode="java.lang.Long" label="BALANCE" notNull="false" selected="true"/>
<column dbComment="ElasticSearch SEUIL field" dbDefaultValue="" dbName="SEUIL" dbNotNull="false" dbPosition="7" dbPrimaryKey="false" dbSize="20" dbTypeName="BIGINT" inputType="number" javaName="sEUIL" javaType="java.lang.Long" jdbcTypeCode="java.lang.Long" label="SEUIL" notNull="false" selected="true"/>
<column dateType="DT" dbComment="ElasticSearch date_id field" dbDefaultValue="" dbName="date_id" dbNotNull="false" dbPosition="8" dbPrimaryKey="false" dbSize="19" dbTypeName="DATETIME" inputType="" javaName="dateId" javaType="java.util.Date" jdbcTypeCode="93" label="date id" notNull="false" selected="true"/>
<column dateType="DT" dbComment="ElasticSearch date_jour field" dbDefaultValue="" dbName="date_jour" dbNotNull="false" dbPosition="9" dbPrimaryKey="false" dbSize="19" dbTypeName="DATETIME" inputType="" javaName="dateJour" javaType="java.util.Date" jdbcTypeCode="93" label="date jour" notNull="false" selected="true"/>
<column dbComment="ElasticSearch heure field" dbDefaultValue="" dbName="heure" dbNotNull="false" dbPosition="10" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="heure" javaType="java.lang.String" jdbcTypeCode="12" label="heure" maxLength="65000" notNull="false" selected="true"/>
<column dbAutoIncremented="true" dbComment="" dbDefaultValue="" dbName="id" dbNotNull="true" dbPosition="11" dbPrimaryKey="true" dbSize="10" dbTypeName="INT" inputType="number" javaName="id" javaType="java.lang.Integer" jdbcTypeCode="4" label="Id" notNull="true" selected="true"/>
<column dbComment="ElasticSearch source _id" dbDefaultValue="" dbName="_id" dbNotNull="false" dbPosition="12" dbPrimaryKey="false" dbSize="255" dbTypeName="VARCHAR" inputType="text" javaName="_id" javaType="java.lang.String" jdbcTypeCode="12" label="Id" maxLength="255" notNull="true" selected="true"/>
</table>
<table catalog="bd_om_analytics" databaseType="TABLE" javaBean="AirtimeReseauDistribution" name="airtime_reseau_distribution" schema="reseau_distribution">
<column dbComment="ElasticSearch UID field" dbDefaultValue="" dbName="UID" dbNotNull="false" dbPosition="1" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="uID" javaType="java.lang.String" jdbcTypeCode="12" label="UID" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch COMPTE_STK field" dbDefaultValue="" dbName="COMPTE_STK" dbNotNull="false" dbPosition="2" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="cOMPTESTK" javaType="java.lang.String" jdbcTypeCode="12" label="COMPTE STK" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch NOM_STK field" dbDefaultValue="" dbName="NOM_STK" dbNotNull="false" dbPosition="3" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="nOMSTK" javaType="java.lang.String" jdbcTypeCode="12" label="NOM STK" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch TYPE_STK field" dbDefaultValue="" dbName="TYPE_STK" dbNotNull="false" dbPosition="4" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="tYPESTK" javaType="java.lang.String" jdbcTypeCode="12" label="TYPE STK" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch COMPTE_COMMERCIAL field" dbDefaultValue="" dbName="COMPTE_COMMERCIAL" dbNotNull="false" dbPosition="5" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="cOMPTECOMMERCIAL" javaType="java.lang.String" jdbcTypeCode="12" label="COMPTE COMMERCIAL" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch NOM_COMMERCIAL field" dbDefaultValue="" dbName="NOM_COMMERCIAL" dbNotNull="false" dbPosition="6" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="nOMCOMMERCIAL" javaType="java.lang.String" jdbcTypeCode="12" label="NOM COMMERCIAL" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch COMPTE_DEMI_GROSSISTE field" dbDefaultValue="" dbName="COMPTE_DEMI_GROSSISTE" dbNotNull="false" dbPosition="7" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="cOMPTEDEMIGROSSISTE" javaType="java.lang.String" jdbcTypeCode="12" label="COMPTE DEMI GROSSISTE" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch NOM_DEMI_GROSSISTE field" dbDefaultValue="" dbName="NOM_DEMI_GROSSISTE" dbNotNull="false" dbPosition="8" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="nOMDEMIGROSSISTE" javaType="java.lang.String" jdbcTypeCode="12" label="NOM DEMI GROSSISTE" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch COMPTE_PUCE_MERE field" dbDefaultValue="" dbName="COMPTE_PUCE_MERE" dbNotNull="false" dbPosition="9" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="cOMPTEPUCEMERE" javaType="java.lang.String" jdbcTypeCode="12" label="COMPTE PUCE MERE" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch NOM_PARTENAIRE field" dbDefaultValue="" dbName="NOM_PARTENAIRE" dbNotNull="false" dbPosition="10" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="nOMPARTENAIRE" javaType="java.lang.String" jdbcTypeCode="12" label="NOM PARTENAIRE" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch COMPTE_CPP field" dbDefaultValue="" dbName="COMPTE_CPP" dbNotNull="false" dbPosition="11" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="cOMPTECPP" javaType="java.lang.String" jdbcTypeCode="12" label="COMPTE CPP" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch COMPTE_MANAGER_OCI field" dbDefaultValue="" dbName="COMPTE_MANAGER_OCI" dbNotNull="false" dbPosition="12" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="cOMPTEMANAGEROCI" javaType="java.lang.String" jdbcTypeCode="12" label="COMPTE MANAGER OCI" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch COMPTE_SPD field" dbDefaultValue="" dbName="COMPTE_SPD" dbNotNull="false" dbPosition="13" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="cOMPTESPD" javaType="java.lang.String" jdbcTypeCode="12" label="COMPTE SPD" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch COMPTE_SUPERVISEUR field" dbDefaultValue="" dbName="COMPTE_SUPERVISEUR" dbNotNull="false" dbPosition="14" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="cOMPTESUPERVISEUR" javaType="java.lang.String" jdbcTypeCode="12" label="COMPTE SUPERVISEUR" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch CUID field" dbDefaultValue="" dbName="CUID" dbNotNull="false" dbPosition="15" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="cUID" javaType="java.lang.String" jdbcTypeCode="12" label="CUID" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch NOM_CPP field" dbDefaultValue="" dbName="NOM_CPP" dbNotNull="false" dbPosition="16" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="nOMCPP" javaType="java.lang.String" jdbcTypeCode="12" label="NOM CPP" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch NOM_MANAGER_OCI field" dbDefaultValue="" dbName="NOM_MANAGER_OCI" dbNotNull="false" dbPosition="17" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="nOMMANAGEROCI" javaType="java.lang.String" jdbcTypeCode="12" label="NOM MANAGER OCI" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch NOM_SPD field" dbDefaultValue="" dbName="NOM_SPD" dbNotNull="false" dbPosition="18" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="nOMSPD" javaType="java.lang.String" jdbcTypeCode="12" label="NOM SPD" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch NOM_SUPERVISEUR field" dbDefaultValue="" dbName="NOM_SUPERVISEUR" dbNotNull="false" dbPosition="19" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="nOMSUPERVISEUR" javaType="java.lang.String" jdbcTypeCode="12" label="NOM SUPERVISEUR" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch REGION_OCI field" dbDefaultValue="" dbName="REGION_OCI" dbNotNull="false" dbPosition="20" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="rEGIONOCI" javaType="java.lang.String" jdbcTypeCode="12" label="REGION OCI" maxLength="65000" notNull="false" selected="true"/>
<column dbComment="ElasticSearch TYPE_CODE field" dbDefaultValue="" dbName="TYPE_CODE" dbNotNull="false" dbPosition="21" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="tYPECODE" javaType="java.lang.String" jdbcTypeCode="12" label="TYPE CODE" maxLength="65000" notNull="false" selected="true"/>
<column dbAutoIncremented="true" dbComment="" dbDefaultValue="" dbName="id" dbNotNull="true" dbPosition="22" dbPrimaryKey="true" dbSize="10" dbTypeName="INT" inputType="number" javaName="id" javaType="java.lang.Integer" jdbcTypeCode="4" label="Id" notNull="true" selected="true"/>
<column dbComment="ElasticSearch source _id" dbDefaultValue="" dbName="_id" dbNotNull="false" dbPosition="23" dbPrimaryKey="false" dbSize="255" dbTypeName="VARCHAR" inputType="text" javaName="_id" javaType="java.lang.String" jdbcTypeCode="12" label="Id" maxLength="255" notNull="true" selected="true"/>
</table>
<table catalog="bd_om_analytics" databaseType="TABLE" javaBean="SubsAggZoneCom" name="subs_agg_zone_com" schema="subs_agg_zone_com">
<column dbComment="ElasticSearch ACTIVATION field" dbDefaultValue="" dbName="ACTIVATION" dbNotNull="false" dbPosition="1" dbPrimaryKey="false" dbSize="20" dbTypeName="BIGINT" inputType="number" javaName="aCTIVATION" javaType="java.lang.Long" jdbcTypeCode="java.lang.Long" label="ACTIVATION" notNull="false" selected="true"/>
<column dateType="DT" dbComment="ElasticSearch CREATION_DATE field" dbDefaultValue="" dbName="CREATION_DATE" dbNotNull="false" dbPosition="2" dbPrimaryKey="false" dbSize="19" dbTypeName="DATETIME" inputType="" javaName="cREATIONDATE" javaType="java.util.Date" jdbcTypeCode="93" label="CREATION DATE" notNull="false" selected="true"/>
<column dbComment="ElasticSearch CHANNEL_ZONE_COMMERCIALE field" dbDefaultValue="" dbName="CHANNEL_ZONE_COMMERCIALE" dbNotNull="false" dbPosition="3" dbPrimaryKey="false" dbSize="65000" dbTypeName="VARCHAR" inputType="text" javaName="cHANNELZONECOMMERCIALE" javaType="java.lang.String" jdbcTypeCode="12" label="CHANNEL ZONE COMMERCIALE" maxLength="65000" notNull="false" selected="true"/>
<column dbAutoIncremented="true" dbComment="" dbDefaultValue="" dbName="id" dbNotNull="true" dbPosition="4" dbPrimaryKey="true" dbSize="10" dbTypeName="INT" inputType="number" javaName="id" javaType="java.lang.Integer" jdbcTypeCode="4" label="Id" notNull="true" selected="true"/>
<column dbComment="ElasticSearch source _id" dbDefaultValue="" dbName="_id" dbNotNull="false" dbPosition="5" dbPrimaryKey="false" dbSize="255" dbTypeName="VARCHAR" inputType="text" javaName="_id" javaType="java.lang.String" jdbcTypeCode="12" label="Id" maxLength="255" notNull="true" selected="true"/>
</table>
</tableList>
</root>
I did not need to write more templates. I used the same tempates I use for generation from Mysql and everything is done
Upvotes: 0
Reputation: 2460
If I understand correctly you want to replace the Telosys entity model with a set of JSON files, is that it?
I think it is possible but we will have to divert the behavior of Telosys…
First it’s impossible to launch Telosys code generation without a model so you must create a void DSL model, for example the DSL model “void-model.model” with a single “fake entity” (eg “Fake” entity with only an ‘id’ attribute)
Second, the "templates.cfg" file applies to all the entities defined in the model and in your case there’s no entity, only JSON files. For each line of the "templates.cfg" file, you must specify a cardinality of "1" so that each template is executed once regardless of the number of entities.
You just have to add "1" after the last ";" each line, example :
Dto ; Dto.txt ; log ; dto.vm ; 1
At this step it’s possible to launch once each template defined in "templates.cfg"
The idea is now to iterate all the JSON files in each template and to execute a "sub-template" for each JSON file. This “sub-template” is the template that will do the real job.
To browse the directory it will be necessary to create a specific Java class that will provide the list of JSON files. Then the $generator object can be used to execute a “sub-template” for each JSON file.
Main template example :
## This template generates only a log file (see templates.cfg)
## Create instance of the specific class
#set($dir = $loader.newInstance('mypackage.MyClass'))
## Use specific class instance to get all the JSON files
#set($jsonFiles = $dir.getJSONFiles() )
#foreach($json in $jsonFiles)
## Printed in log file
Processing $json
#set( $fileName = "${json}-file.txt")
#set( $folder = 'dest' )
## Execute the sub-template 'dto_sub.vm'
$generator.generate('Fake', $fileName , $folder, "dto_sub.vm" )
#end
In the sub-template ( eg "dto_sub.vm" ) use $target.file to identify the target and hence the JSON file needed
And use another specific class to load the JSON file content and just use it to generate the code.
See documentation :
Specific Java class : http://www.telosys.org/templates-doc/objects/loader.html
Sub-template generation : http://www.telosys.org/templates-doc/objects/generator.html
Upvotes: 1