Skip to content

Commit

Permalink
refactor ParseMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
clagomess committed Mar 6, 2024
1 parent 25fa4ac commit a434690
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 102 deletions.
108 changes: 19 additions & 89 deletions core/src/main/java/br/com/pirilampo/core/compilers/ParseMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,116 +6,46 @@
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<li><a href=\"#/feature/%s\">%s%s</a></li>\n";
public static final String HTML_MENU_PAI = "<li>\n" +
"\t<a href=\"javascript:;\" data-toggle=\"collapse\" data-target=\"#menu-%s\">%s</a>\n" +
"\t<ul id=\"menu-%s\" class=\"collapse\">\n%s\t</ul>\n" +
"</li>\n";
public static final String HTML_MENU_ICON_DIFF_NOVO = "<span class=\"icon-diff-novo\"></span> ";
public static final String HTML_MENU_ICON_DIFF_DIFERENTE = "<span class=\"icon-diff-diferente\"></span> ";
private final ParametroDto parametro;

public ParseMenu(ParametroDto parametro){
this.menu = new MenuDto("ROOT");
this.parametro = 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<MenuDto> node){
OptionalInt oi = IntStream
.range(0, node.size())
.filter(i -> Objects.equals(node.get(i).getTitle(), nodes[level]))
private void walker(Set<MenuDto> children, String[] nodes, final int level, MenuDto itemToAdd){
Optional<MenuDto> 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);
}
}
}
24 changes: 12 additions & 12 deletions core/src/main/java/br/com/pirilampo/core/dto/MenuDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<MenuDto> {
private String title;
private String url = null;
private DiffEnum diff = DiffEnum.NAO_COMPARADO;
private List<MenuDto> children;
private final String title;
private final String url;
private final DiffEnum diff;
private final Set<MenuDto> children = new TreeSet<>();

public MenuDto(String title){
this.title = title;
this.children = new ArrayList<>();
this.url = null;
this.diff = DiffEnum.NAO_COMPARADO;
}

public List<MenuDto> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(){{
Expand Down Expand Up @@ -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"
);
Expand Down

0 comments on commit a434690

Please sign in to comment.