package com.ekino.lesaint.dozerannihilation.processor.impl;

import java.io.BufferedWriter;
import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;

import org.springframework.stereotype.Component;

/**
* MapperFactoryFileGenerator -
*
* @author Sébastien Lesaint
*/
class MapperFactoryFileGenerator extends AbstractFileGenerator {
    private static final List<DAName> SPRING_COMPONENT_EXTRA_IMPORTS = ImmutableList.of(
            DANameFactory.from(Component.class.getCanonicalName()),
            DANameFactory.from(Resource.class.getCanonicalName())
    );

    @Override
    public String fileName(FileGeneratorContext context) {
        return context.getMapperClass().type.qualifiedName.getName() + "MapperFactory";
    }

    @Override
    public void writeFile(BufferedWriter bw, FileGeneratorContext context) throws IOException {
        DAMapperClass daMapperClass = context.getMapperClass();
        List<DAName> imports = context.getMapperFactoryImports();
        if (daMapperClass.instantiationType == InstantiationType.SPRING_COMPONENT) {
            appendHeader(bw, daMapperClass, ImmutableList.copyOf(Iterables.concat(imports, SPRING_COMPONENT_EXTRA_IMPORTS)));
        }
        else {
            appendHeader(bw, daMapperClass, imports);
        }

        // générer la factory
        //     -> nom du package
        //     -> nom de la classe (infère nom de la factory et nom du Mapper)
        //     -> type d'instantiation (si enum, le nom de la valeur d'enum à utiliser)
        if (daMapperClass.instantiationType == InstantiationType.SPRING_COMPONENT) {
            bw.append("@Component");
            bw.newLine();
        }
        bw.append("class ").append(daMapperClass.type.simpleName).append("MapperFactory").append(" {");
        bw.newLine();
        bw.newLine();
        if (daMapperClass.instantiationType == InstantiationType.SPRING_COMPONENT) {
            bw.append(INDENT).append("@Resource");
            bw.newLine();
            bw.append(INDENT).append("private ").append(daMapperClass.type.simpleName).append(" instance").append(";");
            bw.newLine();
            bw.newLine();
        }
        bw.append(INDENT).append("public static ").append(daMapperClass.type.simpleName).append(" instance() {");
        bw.newLine();
        switch (daMapperClass.instantiationType) {
            case SINGLETON_ENUM:
                // TOIMPROVE générer le code de la factory dans le cas enum avec un nom d'enum dynamique
                bw.append(INDENT).append(INDENT).append("return ").append(daMapperClass.type.simpleName).append(".INSTANCE;");
                break;
            case CONSTRUCTOR:
                bw.append(INDENT).append(INDENT).append("return new ").append(daMapperClass.type.simpleName).append("();");
                break;
            case SPRING_COMPONENT:
                bw.append(INDENT).append(INDENT).append("return instance;");
                break;
        }
        bw.newLine();
        bw.append(INDENT).append("}");
        bw.newLine();

        appendFooter(bw);

        bw.flush();
        bw.close();
    }
}

@@ -117,1279 +117,559 @@
 on;%0A
-import java.util.List;%0Aimport javax.annotation.Resource;%0Aimport com.google.common.collect.ImmutableList;%0Aimport com.google.common.collect.Iterables;%0A%0Aimport org.springframework.stereotype.Component;%0A%0A/**%0A* MapperFactoryFileGenerator -%0A*%0A* @author S%C3%A9bastien Lesaint%0A*/%0Aclass MapperFactoryFileGenerator extends AbstractFileGenerator %7B%0A    private static final List%3CDAName%3E SPRING_COMPONENT_EXTRA_IMPORTS = ImmutableList.of(%0A            DANameFactory.from(Component.class.getCanonicalName()),%0A            DANameFactory.from(Resource.class.getCanonicalName())%0A    );%0A%0A    @Override%0A    public String fileName(FileGeneratorContext context) %7B%0A        return context.getMapperClass().type.qualifiedName.getName() + %22MapperFactory%22;%0A    %7D%0A%0A    @Override%0A    public void writeFile(BufferedWriter bw, FileGeneratorContext context) throws IOException %7B%0A        DAMapperClass daMapperClass = context.getMapperClass();%0A        List%3CDAName%3E imports = context.getMapperFactoryImports();%0A        if (daMapperClass.instantiationType == InstantiationType.SPRING_COMPONENT) %7B%0A            appendHeader(bw, daMapperClass, ImmutableList.copyOf(Iterables.concat(imports, SPRING_COMPONENT_EXTRA_IMPORTS)));%0A        %7D%0A        else %7B%0A            appendHeader(bw, daMapperClass, imports);%0A        %7D
+%0A/**%0A* MapperFactoryFileGenerator -%0A*%0A* @author S%C3%A9bastien Lesaint%0A*/%0Aclass MapperFactoryFileGenerator extends AbstractFileGenerator %7B%0A    @Override%0A    public String fileName(FileGeneratorContext context) %7B%0A        return context.getMapperClass().type.qualifiedName.getName() + %22MapperFactory%22;%0A    %7D%0A%0A    @Override%0A    public void writeFile(BufferedWriter bw, FileGeneratorContext context) throws IOException %7B%0A        DAMapperClass daMapperClass = context.getMapperClass();%0A        appendHeader(bw, daMapperClass, context.getMapperFactoryImports());
 %0A%0A  
@@ -900,166 +900,8 @@
 er)%0A
-        if (daMapperClass.instantiationType == InstantiationType.SPRING_COMPONENT) %7B%0A            bw.append(%22@Component%22);%0A            bw.newLine();%0A        %7D%0A
     

Revert "working a fixing @Component mapper test u"