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

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

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

import br.com.jarch.apt.util.ProcessorUtils;
import br.com.jarch.core.annotation.JArchGenerateCrud;
import br.com.jarch.core.cdi.GlobalInformation;
import br.com.jarch.core.form.FieldForm;
import br.com.jarch.util.FileUtils;
import br.com.jarch.util.LogUtils;
import br.com.jarch.util.UnicodeUtils;

import javax.lang.model.element.Element;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public final class UpdateBundle {

    private final Element element;
    private final JArchGenerateCrud generateCrud;
    private File fileBundle;

    public UpdateBundle(Element element, JArchGenerateCrud generateCrud) {
        this.element = element;
        this.generateCrud = generateCrud;
        configure(element, generateCrud.nameSubPackage());
    }

    public static void update(Element element, JArchGenerateCrud generateCrud) {
        new UpdateBundle(element, generateCrud).update();
    }

    private void update() {
        try {
            if (!ProcessorUtils.isFileExistsClientOrWebOrWs(fileBundle))
                return;

            List listaAdicaoBundle = new ArrayList<>();
            List listaAtual = new ArrayList<>();

            try (BufferedReader leitor = new BufferedReader(new InputStreamReader(new FileInputStream(fileBundle), StandardCharsets.UTF_8))) {
                String line;
                while ((line = leitor.readLine()) != null)
                    listaAtual.add(line);
            }

            List listaBundleArch = new ArrayList<>();

            InputStream resourceBundle = getClass().getResourceAsStream(File.separator + "bundle" + File.separator + "bundleJArch_" + GlobalInformation.getInstance().getLocale().getLanguage() + ".properties");

            if (resourceBundle != null) {
                try (BufferedReader leitor = new BufferedReader(new InputStreamReader(resourceBundle, StandardCharsets.UTF_8))) {
                    String line;
                    while ((line = leitor.readLine()) != null)
                        listaBundleArch.add(line);
                }
            }

            List listaUnificada = new ArrayList<>();
            listaUnificada.addAll(listaAtual);
            listaUnificada.addAll(listaBundleArch);

            addKeyValue(generateCrud.master().name(), ProcessorUtils.getListFieldForm(generateCrud.master().fields()), listaAdicaoBundle, listaUnificada);

            Arrays
                    .stream(generateCrud.master().details())
                    .forEach(detail -> {
                        addKeyValue(detail.name(), ProcessorUtils.getListFieldForm(detail.fields()), listaAdicaoBundle, listaUnificada);

                        Arrays
                                .stream(detail.subDetails())
                                .forEach(subDetail -> addKeyValue(subDetail.name(), ProcessorUtils.getListFieldForm(subDetail.fields()), listaAdicaoBundle, listaUnificada));
                    });


            if (listaAdicaoBundle.isEmpty())
                return;

            listaAtual.addAll(listaAdicaoBundle);

            FileUtils.save(fileBundle,
                    listaAtual
                            .stream()
                            .filter(l -> !l.trim().isEmpty())
                            .map(UnicodeUtils::convertSpecialCharacter)
                            .distinct()
                            .collect(Collectors.toList()));

            ProcessorUtils.messageNote("JARCH Created ==> " + fileBundle.getAbsoluteFile(), element);
        } catch (Exception ex) {
            LogUtils.generate(ex);
        }
    }

    private void addKeyValue(String name, List fieldForms, List listaAdicaoBundle, List listaUnificada) {
        String chaveMestre = "label." + ProcessorUtils.getNameWithoutCharEspecialStartLowerCase(name);

        if (listaUnificada
                .stream()
                .noneMatch(l -> l.contains(chaveMestre + "=") || l.contains(chaveMestre + " "))) {
            listaAdicaoBundle.add(chaveMestre + "=" + name);
        }

        for (FieldForm c : fieldForms) {
            if (listaUnificada
                    .stream()
                    .noneMatch(l -> l.contains(c.getBundleCodigoJava() + "=") || l.contains(c.getBundleCodigoJava() + " "))) {
                listaAdicaoBundle.add(c.getBundleCodigoJava() + "=" + c.getDescricao());
            }
        }
    }

    private void configure(Element element, String nameSubPackage) {
        File folder = new File(ProcessorUtils.getPathBundle(element, nameSubPackage));
        fileBundle = new File(folder.getAbsolutePath() + File.separator + "bundle_pt_BR.properties");

        if (!fileBundle.exists()) {
            if (folder.getAbsolutePath().contains("-web")) {
                folder = new File(ProcessorUtils.getPathBundle(element, nameSubPackage).replace("-web", "-client"));
            } else if (folder.getAbsolutePath().contains("-client")) {
                folder = new File(ProcessorUtils.getPathBundle(element, nameSubPackage).replace("-client", "-web"));
            }

            fileBundle = new File(folder.getAbsolutePath() + File.separator + "bundle_pt_BR.properties");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy