Announcement Announcement Module
Collapse
No announcement yet.
Template-Based Code Generation using Spring batch Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Template-Based Code Generation using Spring batch

    I can manage to couple ItemReader using Spring batch and Apache Velocity .
    It is possible to wrap VelocityEngine "replacement" within Spring batch ItemProcessor
    My aim is to :
    1/ start from a Reader (database,flat file...) [OK]

    2/ treat it with ItemProcessor (the template base file) ; That's my question how to Wrap my VelocityEngineBean into a Composite Bean Spring containing ItemProcessor<T,V>
    bellow a Snipset code coupling apache velocity and Spring batch, it would like to make a "farm ItemProcessor" of common treatement.

    3/ generate N different Files according from the input .
    (but can load dynamically the output writer ..with Spring Batch ? :confused



    Here is my "Spring batch " config
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:batch="http://www.springframework.org/schema/batch"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jms="http://www.springframework.org/schema/jms" xmlns:amq="http://activemq.apache.org/schema/core"
    xmlns:util="http://www.springframework.org/schema/util"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...ring-beans.xsd

    http://www.springframework.org/schema/batch http://www.springframework.org/schem...-batch-2.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schem...ng-context.xsd
    http://www.springframework.org/schema/jms
    http://www.springframework.org/schem...spring-jms.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schem...pring-util.xsd
    ">


    <!-- 1) USE ANNOTATIONS TO CONFIGURE SPRING BEANS -->
    <context:component-scan base-package="com.batch.velocity.genClass" />


    <bean id="velocityEngine"
    class="org.springframework.ui.velocity.VelocityEng ineFactoryBean">
    <property name="velocityProperties">
    <value>
    resource.loader=class
    class.resource.loader.class=org.apache.velocity.ru ntime.resource.loader.ClasspathResourceLoader
    </value>
    </property>
    </bean>




    <bean id="itemReader" class="org.springframework.batch.item.file.FlatFil eItemReader">
    <property name="resource" value="classpath:com/batch/velocity/genClass/dataTest.csv" />
    <!-- property name="linesToSkip" value="1" /-->
    <property name="lineMapper">
    <bean class="org.springframework.batch.item.file.mapping .DefaultLineMapper">
    <property name="lineTokenizer">
    <bean class="org.springframework.batch.item.file.transfo rm.DelimitedLineTokenizer">
    <!--<property name="delimiter" value="MonDel"/> -->
    <property name="names" value="champ1,data1Type,documentation1,champ2,data 2Type,documentation2" />
    </bean>
    </property>
    <property name="fieldSetMapper" ref="genMapperFactoryMapper" />
    </bean>
    </property>
    </bean>
    </beans>



    /**
    * Apache VELOCITY *.vm using VTL
    */
    public class $name {


    #foreach( $element in $mylist )

    private $element.Data1Type $element.champ1;


    /**
    *
    * Map : $element.champ1,$element.data1Type,$element.docume ntation1
    * To : $element.champ2,$element.data2Type,$element.docume ntation2
    * $element.documentation1
    * @return the $element.champ1
    *
    */
    public $element.data1Type get${utility.getInputU($element.champ1)}() {
    return $element.champ1;
    }

    /**
    * Map : $element.champ1,$element.data1Type,$element.docume ntation1
    * To : $element.champ2,$element.data2Type,$element.docume ntation2
    * $element.documentation1
    * @param $element.champ1 the $element.champ1 to set
    * #set( $setter = "set" )
    */

    public void set${utility.getInputU($element.champ1)}($element.data1Type $element.champ1) {
    this.$element.champ1 = $element.champ1;
    }

    #end

    }

    package com.batch.velocity.genClass;

    import org.apache.velocity.util.StringUtils;

    public class GeneratorUtility {




    /**
    * set$utility.firstToUpperCase($att.Name)
    * @param input the input to set
    */
    public String getInputU(String pInput) {
    pInput.split("_");
    return StringUtils.capitalizeFirstLetter(pInput);
    }
    }




    /***Test Junit ***/
    package com.batch.velocity.genClass;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    import org.apache.velocity.app.VelocityEngine;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.batch.item.ExecutionContext;
    import org.springframework.batch.item.ParseException;
    import org.springframework.batch.item.UnexpectedInputExce ption;
    import org.springframework.batch.item.file.FlatFileItemRe ader;
    import org.springframework.beans.factory.annotation.Autow ired;
    import org.springframework.test.context.ContextConfigurat ion;
    import org.springframework.test.context.junit4.SpringJUni t4ClassRunner;
    import org.springframework.ui.velocity.VelocityEngineUtil s;

    import com.batch.velocity.Project;



    @ContextConfiguration(locations = "classpath:com/batch/velocity/genClass/contextVelocity.xml")
    @RunWith(SpringJUnit4ClassRunner.class)
    public class VelocityCodeTemplateGenClassGenerator {



    @Autowired
    private VelocityEngine velocityEngine;

    @Autowired
    private FlatFileItemReader vFlatFileItemReader;

    @Test
    public void testFlatReaderItemToVelocityProcessor(){



    System.out.println("[INFO] ----------------------------------------------------------------------------------");
    System.out.println("[INFO] Running test : testFlatReaderItemToVelocityProcessor / "+ getClass().getName());
    System.out.println("[INFO] ----------------------------------------------------------------------------------");



    vFlatFileItemReader.open(new ExecutionContext());
    boolean hasNext = true ;

    GenMapperFactory vGenMapperFactory = null;

    GeneratorUtility vUtility = new GeneratorUtility();
    List<GenMapperFactory> vGenMapperFactoryList = new ArrayList<GenMapperFactory>();


    //vMyClasseGen.setGenMapperFactory(new ArrayList<GenMapperFactory>());

    while (hasNext) {
    try {
    vGenMapperFactory = (GenMapperFactory) vFlatFileItemReader.read();
    vGenMapperFactoryList.add(vGenMapperFactory);

    } catch (UnexpectedInputException e) {
    e.printStackTrace();
    } catch (ParseException e) {
    e.printStackTrace();
    } catch (Exception e) {
    e.printStackTrace();
    }


    if (vGenMapperFactory == null) {
    hasNext = false;

    }
    else {


    Map model = new HashMap();

    model.put("utility", vUtility);
    model.put("mylist", vGenMapperFactoryList);
    model.put("name", "ClassDeTest");

    String text = VelocityEngineUtils.mergeTemplateIntoString(veloci tyEngine, "com/batch/velocity/genClass/example2.vm", model);
    System.out.println(" "+text);

    System.out.println(vGenMapperFactory.toString());
    }
    }
    }

    }


    /**Fieldset Mapper Spring */

    package com.batch.velocity.genClass;

    import java.util.List;

    import org.springframework.batch.item.file.mapping.FieldS etMapper;
    import org.springframework.batch.item.file.transform.Fiel dSet;
    import org.springframework.stereotype.Service;
    import org.springframework.validation.BindException;
    import com.batch.velocity.genClass.GenMapperFactory;


    @Service("genMapperFactoryMapper")
    public class GenMapperFactoryMapper implements FieldSetMapper<GenMapperFactory> {


    public GenMapperFactory mapFieldSet(FieldSet fs) throws BindException {
    GenMapperFactory item = new GenMapperFactory();

    int idx = 0 ;

    item.setChamp1(fs.readString("champ1"));
    item.setData1Type(fs.readString("data1Type"));
    item.setDocumentation1(fs.readString("documentatio n1"));
    System.out.println("1:idx: > "+idx+" " +item.getChamp1()+ " | "+item.getData1Type()+" | "+item.getDocumentation1());


    item.setChamp2(fs.readString("champ2"));
    item.setData2Type(fs.readString("data2Type"));
    item.setDocumentation2(fs.readString("documentatio n2"));
    System.out.println("2:idx: > "+idx+" " +item.getChamp2()+ " | "+item.getData2Type()+" | "+item.getDocumentation2());

    System.out.println(item.toString());

    return item;
    }

    }
    Last edited by 4promachos; Mar 25th, 2010, 04:19 AM.
Working...
X