br.com.jarch.faces.parameter.BaseParameterController Maven / Gradle / Ivy
package br.com.jarch.faces.parameter;
import br.com.jarch.core.crud.parameter.BaseParameterService;
import br.com.jarch.core.crud.parameter.IParameter;
import br.com.jarch.core.crud.parameter.JArchParameterFilterSystem;
import br.com.jarch.core.crud.parameter.Parameter;
import br.com.jarch.core.model.ProgressInformation;
import br.com.jarch.core.util.BundleUtils;
import br.com.jarch.faces.parameter.JArchParametersViewShow.FieldSet;
import br.com.jarch.faces.parameter.JArchParametersViewShow.Menu;
import br.com.jarch.faces.parameter.JArchParametersViewShow.Tab;
import br.com.jarch.faces.util.JavaScriptUtils;
import br.com.jarch.util.CollectionUtils;
import br.com.jarch.util.LogUtils;
import br.com.jarch.util.StringUtils;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import javax.annotation.PostConstruct;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import java.io.ByteArrayInputStream;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public abstract class BaseParameterController> implements Serializable {
@Inject
private transient S service;
@Inject
@JArchParameterFilterSystem
private Instance> filterSystems;
private ProgressInformation progress;
private String menu = "";
private String tab = "";
private List menus = new ArrayList<>();
private List tabs = new ArrayList<>();
private List fieldSets = new ArrayList<>();
private Map>>> parameterViews;
private String menuLast;
private String tabLast;
private String fieldSetLast;
private List paramsLast;
@PostConstruct
private void init() {
resetValues();
configMenus();
}
private void resetValues() {
parameterViews = new HashMap<>();
menuLast = "";
tabLast = "";
fieldSetLast = "";
paramsLast = new ArrayList<>();
}
public String getMenu() {
return menu;
}
private void configMenus() {
menus = allMenus();
}
public void setMenu(String menu) {
if (!this.menu.equals(menu)) {
tabs.clear();
fieldSets.clear();
}
this.menu = menu;
}
public List getMenus() {
return menus;
}
public List configTabList(String menu) {
if (tabs.isEmpty() || !this.menu.equals(menu)) {
configTabs(menu);
}
return tabs;
}
private void configTabs(String menu) {
this.menu = menu;
tabs = allMenusStream()
.filter(t -> getLabel(t.labelMenu()).equals(menu))
.flatMap(t -> Arrays.stream(t.tabs()))
.map(t -> getLabel(t.labelTab()))
.collect(Collectors.toList());
}
public List configFieldSetList(String menu, String tab) {
if (StringUtils.isNullOrEmpty(menu))
menu = getLabel("label.geral");
if (StringUtils.isNullOrEmpty(tab))
tab = getLabel("label.geral");
if (StringUtils.isNullOrEmpty(this.menu))
this.menu = getLabel("label.geral");
if (StringUtils.isNullOrEmpty(this.tab))
this.tab = getLabel("label.geral");
if (CollectionUtils.isNullOrEmpty(fieldSets)
|| !this.menu.equals(menu)
|| !this.tab.equals(tab))
configFieldSets(menu, tab);
return fieldSets;
}
private static String getLabel(String key) {
if (StringUtils.isNullOrEmpty(key))
return BundleUtils.messageBundle("label.geral");
else
return BundleUtils.messageBundle(key);
}
private void configFieldSets(String menu, String tab) {
this.menu = menu;
this.tab = tab;
fieldSets = allFieldSetsStream(menu, tab)
.map(fs -> getLabel(fs.labelFieldSet()))
.collect(Collectors.toList());
}
public List parameterList(String menu, String tab, String fieldSet) {
return searchAllBy(menu, tab, fieldSet);
}
private int counterParameters(Object obj) {
int count = 0;
if (obj instanceof Map, ?>) {
Map, ?> map = (Map, ?>) obj;
for (Object value : map.values()) {
count += counterParameters(value);
}
} else {
return ((List)obj).size();
}
return count;
}
public void save() {
String lastMenu = null;
String lastTab = null;
String lastFieldSet = null;
String lastDescription = null;
if (parameterViews.isEmpty()) {
getProgress().status("Carregando Parâmetros...");
loadAllParameters();
}
getProgress().size(counterParameters(parameterViews));
try {
for (var menu : parameterViews.keySet()) {
lastMenu = menu;
for (var tab : parameterViews.get(menu).keySet()) {
lastTab = tab;
for (var fieldSet : parameterViews.get(menu).get(tab).keySet()) {
lastFieldSet = fieldSet;
for (ParameterView parameterView : parameterViews
.get(menu)
.get(tab)
.get(fieldSet)) {
lastDescription = parameterView.getParameter().description();
getProgress().increment();
getProgress().status(menu + " -> " + tab + " -> " + fieldSet + " -> " + lastDescription);
service.save(parameterView.getParameter());
}
}
}
}
JavaScriptUtils.showMessageHeaderSuccess(BundleUtils.messageBundle("label.parametros"),
BundleUtils.messageBundle("message.procedimentoRealizadoSucesso"));
} catch (Exception ex) {
String message = menuTabFieldSetDescription(lastMenu, lastTab, lastFieldSet, lastDescription);
message += ex.getMessage();
JavaScriptUtils.showMessageHeaderError(message);
} finally {
getProgress().reset();
}
}
public String menuTabFieldSetDescription(String menu, String tab, String fieldSet, String description) {
String message = isNullOrBlankOrGeral(menu) ? "" : "-> " + menu;
message += isNullOrBlankOrGeral(tab) ? "" : " -> " + tab;
message += isNullOrBlankOrGeral(fieldSet) ? "" : " -> " + fieldSet;
message += description == null ? "" : " -> " + description;
if (!StringUtils.isNullOrEmpty(message))
message += ": ";
if (message.startsWith("-> "))
message = message.substring(3);
return message;
}
private boolean isNullOrBlankOrGeral(String menuAtual) {
return StringUtils.isNullOrEmpty(menuAtual) || menuAtual.equals(getLabel("label.geral"));
}
public void handleFileUpload(FileUploadEvent event) {
String idJsf = event.getComponent().getClientId();
var parameterUpload = getByIdJsf(idJsf);
if (parameterUpload.isPresent()) {
parameterUpload.get().getParameter().setValueBinary(event.getFile().getContent());
parameterUpload.get().getParameter().setValueOtherInformation(event.getFile().getFileName());
}
}
private List searchAllBy(String menu, String tab, String fieldSet) {
if (StringUtils.isNullOrEmpty(menu))
menu = getLabel("label.geral");
if (StringUtils.isNullOrEmpty(tab))
tab = getLabel("label.geral");
if (StringUtils.isNullOrEmpty(fieldSet))
fieldSet = getLabel("label.geral");
if (!paramsLast.isEmpty()
&& menuLast != null
&& menuLast.equals(menu)
&& tabLast != null
&& tabLast.equals(tab)
&& fieldSetLast != null
&& fieldSetLast.equals(fieldSet)) {
return paramsLast;
}
loadAllParameters();
if (parameterViews.containsKey(menu)
&& parameterViews.get(menu).containsKey(tab)
&& parameterViews.get(menu).get(tab).containsKey(fieldSet))
paramsLast = parameterViews.get(menu).get(tab).get(fieldSet);
else
paramsLast = Collections.emptyList();
menuLast = menu;
tabLast = tab;
fieldSetLast = fieldSet;
return paramsLast;
}
private void loadAllParameters() {
if (!parameterViews.isEmpty())
return;
parameterViews = new HashMap<>();
for (String menu : allMenus()) {
LogUtils.generate(menu);
for (String tab : allTabs(menu)) {
LogUtils.generate(menu + " -> " + tab);
for (String fieldSet : allFieldSets(menu, tab)) {
LogUtils.generate(menu + " -> " + tab + " -> " + fieldSet);
parameterViews.computeIfAbsent(menu, l -> new HashMap<>());
parameterViews.get(menu).computeIfAbsent(tab, l -> new HashMap<>());
parameterViews.get(menu).get(tab).computeIfAbsent(fieldSet, l -> new ArrayList<>());
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy