From a434690ee0819203a6c18c783b8681a1eaeed2a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A1udio=20Gomes?= Date: Wed, 6 Mar 2024 13:34:02 -0300 Subject: [PATCH] refactor ParseMenu --- .../pirilampo/core/compilers/ParseMenu.java | 108 +++--------------- .../br/com/pirilampo/core/dto/MenuDto.java | 24 ++-- .../core/compilers/ParseMenuTest.java | 4 +- 3 files changed, 34 insertions(+), 102 deletions(-) diff --git a/core/src/main/java/br/com/pirilampo/core/compilers/ParseMenu.java b/core/src/main/java/br/com/pirilampo/core/compilers/ParseMenu.java index 6d71b1b..15b0d21 100644 --- a/core/src/main/java/br/com/pirilampo/core/compilers/ParseMenu.java +++ b/core/src/main/java/br/com/pirilampo/core/compilers/ParseMenu.java @@ -6,30 +6,13 @@ import lombok.Getter; import java.io.File; -import java.util.List; -import java.util.Objects; -import java.util.OptionalInt; -import java.util.stream.IntStream; +import java.util.Optional; +import java.util.Set; public class ParseMenu extends Compiler { @Getter private final MenuDto menu; - private ParametroDto parametro; - private int level; - private String[] nodes; - private int htmlNodeNum; - - private String featureId = "ID"; - private String featureName = "TITULO"; - private DiffEnum diff = DiffEnum.NAO_COMPARADO; - - public static final String HTML_MENU_FILHO = "\t\t
  • %s%s
  • \n"; - public static final String HTML_MENU_PAI = "
  • \n" + - "\t%s\n" + - "\t\n" + - "
  • \n"; - public static final String HTML_MENU_ICON_DIFF_NOVO = " "; - public static final String HTML_MENU_ICON_DIFF_DIFERENTE = " "; + private final ParametroDto parametro; public ParseMenu(ParametroDto parametro){ this.menu = new MenuDto("ROOT"); @@ -37,85 +20,32 @@ public ParseMenu(ParametroDto parametro){ } public void addMenuItem(File feature, DiffEnum diff, String featureTitulo){ - final String curDir = parametro.getTxtSrcFonte().getAbsolutePath(); - this.featureId = getFeatureMetadata(parametro, feature).getId(); - this.featureName = featureTitulo; - this.diff = diff; - - String item = feature.getAbsolutePath().replace(curDir, ""); - item = item.replaceFirst("^[\\/|\\\\]", ""); - nodes = item.split("(\\\\|/)"); - level = 0; - - walker(menu.getChildren()); - } + MenuDto item = new MenuDto( + featureTitulo, + getFeatureMetadata(parametro, feature).getId(), + diff + ); - public String getHtml(){ - htmlNodeNum = 0; + String[] nodes = getFeaturePathWithoutAbsolute(parametro.getTxtSrcFonte(), feature).split("(\\\\|/)"); - return getHtml(menu).toString(); + walker(menu.getChildren(), nodes, 0, item); } - private StringBuilder getHtml(MenuDto node){ - StringBuilder buffer = new StringBuilder(); - - if(node.getChildren().isEmpty()){ - buffer.append(String.format( - HTML_MENU_FILHO, - node.getUrl(), - diffIcon(node.getDiff()), - node.getTitle() - )); - }else { - for (MenuDto item : node.getChildren()) { - htmlNodeNum++; - - if(!item.getChildren().isEmpty()) { - buffer.append(String.format( - HTML_MENU_PAI, - htmlNodeNum, - item.getTitle(), - htmlNodeNum, - getHtml(item) - )); - }else{ - buffer.append(getHtml(item)); - } - } - } - - return buffer; - } - - private void walker(List node){ - OptionalInt oi = IntStream - .range(0, node.size()) - .filter(i -> Objects.equals(node.get(i).getTitle(), nodes[level])) + private void walker(Set children, String[] nodes, final int level, MenuDto itemToAdd){ + Optional child = children.stream() + .filter(item -> item.getTitle().equals(nodes[level])) .findFirst(); - if(oi.isPresent()){ + if(child.isPresent()){ if(level == nodes.length - 1){ - node.get(oi.getAsInt()).setUrl(this.featureId); - node.get(oi.getAsInt()).setTitle(this.featureName); - node.get(oi.getAsInt()).setDiff(this.diff); + children.remove(child.get()); + children.add(itemToAdd); }else{ - level++; - walker(node.get(oi.getAsInt()).getChildren()); + walker(child.get().getChildren(), nodes, level +1, itemToAdd); } }else{ - node.add(new MenuDto(nodes[level])); - walker(node); - } - } - - private String diffIcon(DiffEnum diff){ - switch (diff){ - case NOVO: - return HTML_MENU_ICON_DIFF_NOVO; - case DIFERENTE: - return HTML_MENU_ICON_DIFF_DIFERENTE; - default: - return ""; + children.add(new MenuDto(nodes[level])); + walker(children, nodes, level, itemToAdd); } } } diff --git a/core/src/main/java/br/com/pirilampo/core/dto/MenuDto.java b/core/src/main/java/br/com/pirilampo/core/dto/MenuDto.java index 818cfbf..f57f5b7 100644 --- a/core/src/main/java/br/com/pirilampo/core/dto/MenuDto.java +++ b/core/src/main/java/br/com/pirilampo/core/dto/MenuDto.java @@ -3,26 +3,26 @@ import br.com.pirilampo.core.enums.DiffEnum; import lombok.Data; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.util.Set; +import java.util.TreeSet; @Data public class MenuDto implements Comparable { - private String title; - private String url = null; - private DiffEnum diff = DiffEnum.NAO_COMPARADO; - private List children; + private final String title; + private final String url; + private final DiffEnum diff; + private final Set children = new TreeSet<>(); public MenuDto(String title){ this.title = title; - this.children = new ArrayList<>(); + this.url = null; + this.diff = DiffEnum.NAO_COMPARADO; } - public List getChildren(){ - Collections.sort(this.children); - - return this.children; + public MenuDto(String title, String url, DiffEnum diff) { + this.title = title; + this.url = url; + this.diff = diff; } @Override diff --git a/core/src/test/java/br/com/pirilampo/core/compilers/ParseMenuTest.java b/core/src/test/java/br/com/pirilampo/core/compilers/ParseMenuTest.java index 8fbd6df..2354091 100644 --- a/core/src/test/java/br/com/pirilampo/core/compilers/ParseMenuTest.java +++ b/core/src/test/java/br/com/pirilampo/core/compilers/ParseMenuTest.java @@ -37,7 +37,8 @@ public class ParseMenuTest extends Common { new File("feature/Mensagem/PAB-Emails.feature"), new File("feature/Mensagem/PAB-Mensagens.feature"), new File("feature/Mensagem/PAB-Termos.feature"), - new File("feature/Regras_de_negocio/US01-PAB-Regras_de_negocio.feature") + new File("feature/Regras_de_negocio/US01-PAB-Regras_de_negocio.feature"), + new File("feature/A/A.feature") ); private final ParametroDto parametro = new ParametroDto(){{ @@ -65,6 +66,7 @@ public void walker_level_0(){ .collect(Collectors.toList()) ).containsExactly( "01 - Módulo I - Portal Público", + "A", "Mensagem", "Regras_de_negocio" );