Open adminlazcool opened 5 years ago
If you want to browse a directory you'll have to create a specific Java class to do that. A Java class can be used in a template with "$loader". See "$loader" doc here : http://www.telosys.org/templates-doc/objects/loader.html
Hi, OK. I can browse the directory. I used apache-commons-io.jar. Now i have to generate the java classes per each json file. But I'm having problems. Please see my comment and help me here: https://stackoverflow.com/questions/55377622/browse-a-directory-in-velocity-template-language-with-telosys#comment98042139_55436632
Hello, 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). 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")
/*
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;
/**
*/ @JsonInclude(JsonInclude.Include.NON_NULL) @JsonPropertyOrder(alphabetic = true) @Getter @Setter @NoArgsConstructor @ToString
public class $entityDto implements Cloneable {
set($properties = $json.mappings.data.properties)
foreach($key in $properties.keySet())
set($field = $key.toLowerCase())
tab(1)private #javaType($properties.get($key).type,0)#tab(3)$field ;
end
foreach($key in $properties.keySet())
set($field = $key.toLowerCase())
tab(1)private SearchParam<#javaType($properties.get($key).type,0)>#tab(3)${field}$param;
end
} ` What i need now is to browse my json directory, get each json file and apply the template on that json to create a java classe. Thank you for helping me please !