All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.jarch.apt.generate.BaseCodeGenerate Maven / Gradle / Ivy

The newest version!
package br.com.jarch.apt.generate;

import br.com.jarch.core.annotation.JArchGenerateCrud;
import br.com.jarch.util.FileUtils;
import br.com.jarch.util.LogUtils;
import br.com.jarch.apt.util.ProcessorUtils;
import br.com.jarch.apt.type.CodeType;
import br.com.jarch.apt.type.ModuleType;

import javax.lang.model.element.Element;
import java.io.File;
import java.io.IOException;

public abstract class BaseCodeGenerate implements ICodeGenerate {

    private final Element element;
    private final String useCase;
    private final String nameSubPackage;

    BaseCodeGenerate(Element element, JArchGenerateCrud generateCrud) {
        this.element = element;
        this.useCase = generateCrud.master().name();
        this.nameSubPackage = generateCrud.nameSubPackage();
    }

    BaseCodeGenerate(Element element, String useCase, String nameSubPackage) {
        this.element = element;
        this.useCase = useCase;
        this.nameSubPackage = nameSubPackage;
    }

    @Override
    public void generate() {
        if (ProcessorUtils.isFileExistsClientOrWebOrWs(getFile()))
            return;

        try {
            if (!getFile().getParentFile().exists())
                getFile().getParentFile().mkdir();

            FileUtils.save(getFile(), getContent());
            ProcessorUtils.messageNote("JARCH Created ==> " + getFile().getAbsoluteFile(), element);
        } catch (IOException e) {
            LogUtils.generate(e);
        }
    }

    @Override
    public Element getElement() {
        return element;
    }

    @Override
    public String getVarName() {
        return getType().getVarName(useCase);
    }

    @Override
    public String getNamePackage() {
        String namePackage = getModule().getNamePackage(element, nameSubPackage);

        if (isRenameWsToWeb())
            namePackage = namePackage.replace(".ws.", ".web.");

        return namePackage;
    }

    @Override
    public String getName() {
        return getType().getName(useCase);
    }

    @Override
    public String getFullName() {
        return getType().getFullName(getNamePackage(), useCase);
    }

    @Override
    public String getNameEntity() {
        return CodeType.ENTITY.getName(useCase);
    }

    @Override
    public String getFullNameEntity() {
        return CodeType.ENTITY.getFullName(ModuleType.CLIENT.getNamePackage(element, nameSubPackage), useCase);
    }

    @Override
    public String getNameService() {
        return CodeType.SERVICE.getName(useCase);
    }

    @Override
    public String getFullNameService() {
        return CodeType.SERVICE.getFullName(ModuleType.CLIENT.getNamePackage(element, nameSubPackage), useCase);
    }

    @Override
    public String getNameSearch() {
        return CodeType.SEARCH.getName(useCase);
    }

    @Override
    public String getFullNameSearch() {
        return CodeType.SEARCH.getFullName(ModuleType.CLIENT.getNamePackage(element, nameSubPackage), useCase);
    }

    @Override
    public String getNameRepository() {
        return CodeType.REPOSITORY.getName(useCase);
    }

    @Override
    public String getFullNameRepository() {
        return CodeType.REPOSITORY.getFullName(ModuleType.CLIENT.getNamePackage(element, nameSubPackage), useCase);
    }

    @Override
    public String getNameDao() {
        return CodeType.DAO.getName(useCase);
    }

    @Override
    public String getFullNameDao() {
        return CodeType.DAO.getFullName(ModuleType.CLIENT.getNamePackage(element, nameSubPackage), useCase);
    }

    private boolean isRenameWsToWeb() {
        return !getModule().getFolder(element, "").exists();
    }

    private File getFile() {
        File folder = getModule().getFolder(element, nameSubPackage);

        if (isRenameWsToWeb()) {
            folder = getModule().getFolder(element, nameSubPackage);
            folder = new File(folder.getAbsolutePath().replace("-ws", "-web").replace(File.separator + "ws" + File.separator, File.separator + "web" + File.separator));
        }

        return new File(folder.getAbsolutePath() + File.separator + getName() + getType().getPathExtension());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy