@@ -1,25 +0,0 @@
|
||||
package de.thpeetz.kontor.admin.data;
|
||||
|
||||
import de.thpeetz.kontor.common.data.AbstractEntity;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Index;
|
||||
import jakarta.persistence.Table;
|
||||
import jakarta.persistence.UniqueConstraint;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@Table(
|
||||
indexes = @Index(columnList = "moduleName"),
|
||||
uniqueConstraints = @UniqueConstraint(columnNames = {"moduleName"})
|
||||
)
|
||||
public class ModuleData extends AbstractEntity {
|
||||
|
||||
@NotEmpty
|
||||
private String moduleName;
|
||||
|
||||
private Boolean importData;
|
||||
}
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
package de.thpeetz.kontor.admin.repository;
|
||||
|
||||
import de.thpeetz.kontor.admin.data.ModuleData;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ModuleDataRepository extends JpaRepository<ModuleData, String> {
|
||||
|
||||
@Query("select m from ModuleData m where lower(m.moduleName) like lower(concat('%', :searchTerm, '%')) ")
|
||||
List<ModuleData> search(@Param("searchTerm") String searchTerm);
|
||||
|
||||
ModuleData findByModuleName(String moduleName);
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
package de.thpeetz.kontor.admin.services;
|
||||
|
||||
import de.thpeetz.kontor.admin.data.ModuleData;
|
||||
import de.thpeetz.kontor.admin.repository.ModuleDataRepository;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ModuleService {
|
||||
|
||||
private final ModuleDataRepository moduleDataRepository;
|
||||
|
||||
public ModuleService(ModuleDataRepository moduleDataRepository) {
|
||||
this.moduleDataRepository = moduleDataRepository;
|
||||
}
|
||||
|
||||
public List<ModuleData> findAll(String stringFilter) {
|
||||
if (stringFilter == null || stringFilter.isEmpty()) {
|
||||
return moduleDataRepository.findAll();
|
||||
} else {
|
||||
return moduleDataRepository.search(stringFilter);
|
||||
}
|
||||
}
|
||||
|
||||
public ModuleData findByName(String moduleName) {
|
||||
if (moduleName == null || moduleName.isEmpty()) {
|
||||
return null;
|
||||
} else {
|
||||
return moduleDataRepository.findByModuleName(moduleName);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean importData(String moduleName) {
|
||||
ModuleData module = moduleDataRepository.findByModuleName(moduleName);
|
||||
if (module != null) {
|
||||
return module.getImportData();
|
||||
} else {
|
||||
log.info("Module {} not found, should import data", moduleName);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public void setDataImported(String moduleName) {
|
||||
ModuleData module = moduleDataRepository.findByModuleName(moduleName);
|
||||
if (module == null) {
|
||||
log.info("Module {} not found, will create it", moduleName);
|
||||
module = new ModuleData();
|
||||
module.setModuleName(moduleName);
|
||||
module.setImportData(false);
|
||||
moduleDataRepository.save(module);
|
||||
} else {
|
||||
log.info("Module {} found, change import data", module);
|
||||
module.setImportData(false);
|
||||
moduleDataRepository.save(module);
|
||||
}
|
||||
}
|
||||
|
||||
public void saveModuleData(ModuleData moduleData) {
|
||||
if (moduleData == null) {
|
||||
log.warn("ModuleData is null, can't save it.");
|
||||
} else {
|
||||
moduleDataRepository.save(moduleData);
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteModuleData(ModuleData moduleData) {
|
||||
if (moduleData == null) {
|
||||
log.warn("ModuleData is null, can't delete it.");
|
||||
} else {
|
||||
moduleDataRepository.delete(moduleData);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
package de.thpeetz.kontor.admin.views;
|
||||
|
||||
import com.vaadin.flow.component.ComponentEvent;
|
||||
import com.vaadin.flow.component.ComponentEventListener;
|
||||
import com.vaadin.flow.component.Key;
|
||||
import com.vaadin.flow.component.button.Button;
|
||||
import com.vaadin.flow.component.button.ButtonVariant;
|
||||
import com.vaadin.flow.component.checkbox.Checkbox;
|
||||
import com.vaadin.flow.component.formlayout.FormLayout;
|
||||
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
|
||||
import com.vaadin.flow.component.textfield.TextField;
|
||||
import com.vaadin.flow.data.binder.BeanValidationBinder;
|
||||
import com.vaadin.flow.data.binder.Binder;
|
||||
import de.thpeetz.kontor.admin.data.ModuleData;
|
||||
|
||||
public class ModuleDataForm extends FormLayout {
|
||||
TextField moduleName = new TextField("Module Name");
|
||||
Checkbox importData = new Checkbox("Import Data");
|
||||
|
||||
Button save = new com.vaadin.flow.component.button.Button("Save");
|
||||
Button delete = new com.vaadin.flow.component.button.Button("Delete");
|
||||
Button close = new Button("Cancel");
|
||||
|
||||
Binder<ModuleData> binder = new BeanValidationBinder<>(ModuleData.class);
|
||||
|
||||
public ModuleDataForm() {
|
||||
addClassName("moduleData-form");
|
||||
binder.bindInstanceFields(this);
|
||||
add(moduleName, importData, createButtonsLayout());
|
||||
}
|
||||
|
||||
private HorizontalLayout createButtonsLayout() {
|
||||
save.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
|
||||
delete.addThemeVariants(ButtonVariant.LUMO_ERROR);
|
||||
close.addThemeVariants(ButtonVariant.LUMO_TERTIARY);
|
||||
|
||||
save.addClickShortcut(Key.ENTER);
|
||||
close.addClickShortcut(Key.ESCAPE);
|
||||
|
||||
save.addClickListener(event -> validateAndSave());
|
||||
delete.addClickListener(event -> fireEvent(new ModuleDataForm.DeleteEvent(this, binder.getBean())));
|
||||
close.addClickListener(event -> fireEvent(new ModuleDataForm.CloseEvent(this)));
|
||||
|
||||
binder.addStatusChangeListener(e -> save.setEnabled(binder.isValid()));
|
||||
return new HorizontalLayout(save, delete, close);
|
||||
}
|
||||
|
||||
private void validateAndSave() {
|
||||
if (binder.isValid()) {
|
||||
fireEvent(new ModuleDataForm.SaveEvent(this, binder.getBean()));
|
||||
}
|
||||
}
|
||||
|
||||
public void setModuleData(ModuleData moduleData) {
|
||||
binder.setBean(moduleData);
|
||||
}
|
||||
|
||||
public abstract static class ModuleDataFormEvent extends ComponentEvent<ModuleDataForm> {
|
||||
private ModuleData moduleData;
|
||||
|
||||
protected ModuleDataFormEvent(ModuleDataForm source, ModuleData moduleData) {
|
||||
super(source, false);
|
||||
this.moduleData = moduleData;
|
||||
}
|
||||
|
||||
public ModuleData getModuleData() {
|
||||
return moduleData;
|
||||
}
|
||||
}
|
||||
|
||||
public static class SaveEvent extends ModuleDataForm.ModuleDataFormEvent {
|
||||
SaveEvent(ModuleDataForm source, ModuleData moduleData) {
|
||||
super(source, moduleData);
|
||||
}
|
||||
}
|
||||
|
||||
public static class DeleteEvent extends ModuleDataForm.ModuleDataFormEvent {
|
||||
DeleteEvent(ModuleDataForm source, ModuleData moduleData) {
|
||||
super(source, moduleData);
|
||||
}
|
||||
}
|
||||
|
||||
public static class CloseEvent extends ModuleDataForm.ModuleDataFormEvent {
|
||||
CloseEvent(ModuleDataForm source) {
|
||||
super(source, null);
|
||||
}
|
||||
}
|
||||
|
||||
public void addDeleteListener(ComponentEventListener<ModuleDataForm.DeleteEvent> listener) {
|
||||
addListener(ModuleDataForm.DeleteEvent.class, listener);
|
||||
}
|
||||
|
||||
public void addSaveListener(ComponentEventListener<ModuleDataForm.SaveEvent> listener) {
|
||||
addListener(ModuleDataForm.SaveEvent.class, listener);
|
||||
}
|
||||
|
||||
public void addCloseListener(ComponentEventListener<ModuleDataForm.CloseEvent> listener) {
|
||||
addListener(ModuleDataForm.CloseEvent.class, listener);
|
||||
}
|
||||
}
|
||||
@@ -1,140 +0,0 @@
|
||||
package de.thpeetz.kontor.admin.views;
|
||||
|
||||
import com.vaadin.flow.component.Component;
|
||||
import com.vaadin.flow.component.button.Button;
|
||||
import com.vaadin.flow.component.button.ButtonVariant;
|
||||
import com.vaadin.flow.component.contextmenu.ContextMenu;
|
||||
import com.vaadin.flow.component.contextmenu.MenuItem;
|
||||
import com.vaadin.flow.component.grid.Grid;
|
||||
import com.vaadin.flow.component.icon.Icon;
|
||||
import com.vaadin.flow.component.icon.VaadinIcon;
|
||||
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
|
||||
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
|
||||
import com.vaadin.flow.component.textfield.TextField;
|
||||
import com.vaadin.flow.data.value.ValueChangeMode;
|
||||
import com.vaadin.flow.router.PageTitle;
|
||||
import com.vaadin.flow.router.Route;
|
||||
import com.vaadin.flow.spring.annotation.SpringComponent;
|
||||
import de.thpeetz.kontor.admin.data.ModuleData;
|
||||
import de.thpeetz.kontor.admin.services.ModuleService;
|
||||
import de.thpeetz.kontor.common.views.MainLayout;
|
||||
import de.thpeetz.kontor.common.views.ColumnToggleContextMenu;
|
||||
import de.thpeetz.kontor.common.views.StatusIcon;
|
||||
import jakarta.annotation.security.RolesAllowed;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
|
||||
@Slf4j
|
||||
@SpringComponent
|
||||
@Scope("prototype")
|
||||
@RolesAllowed("ROLE_ADMIN")
|
||||
@Route(value = "admin/module", layout = MainLayout.class)
|
||||
@PageTitle("Module Data | Admin | Kontor")
|
||||
public class ModuleDataView extends VerticalLayout {
|
||||
|
||||
Grid<ModuleData> grid = new Grid<>(ModuleData.class, false);
|
||||
Grid.Column<ModuleData> idColumn = grid.addColumn(ModuleData::getId)
|
||||
.setHeader("ID").setResizable(true).setSortable(true);
|
||||
Grid.Column<ModuleData> nameColumn = grid.addColumn(ModuleData::getModuleName)
|
||||
.setHeader("Name").setResizable(true).setSortable(true);
|
||||
Grid.Column<ModuleData> importColumn = grid.addComponentColumn(moduleData -> StatusIcon.create(moduleData.getImportData()))
|
||||
.setHeader("Import Data").setWidth("6rem").setSortable(true);
|
||||
|
||||
TextField filterText = new TextField();
|
||||
@Getter
|
||||
ModuleDataForm form;
|
||||
ModuleService service;
|
||||
|
||||
public ModuleDataView(ModuleService service) {
|
||||
this.service = service;
|
||||
addClassName("moduleData-view");
|
||||
setSizeFull();
|
||||
configureGrid();
|
||||
configureForm();
|
||||
|
||||
add(getToolbar(), getContent());
|
||||
updateList();
|
||||
}
|
||||
|
||||
private void configureGrid() {
|
||||
grid.addClassName("moduleData-grid");
|
||||
grid.setSizeFull();
|
||||
grid.getColumns().forEach(col -> col.setAutoWidth(true));
|
||||
grid.asSingleSelect().addValueChangeListener(event -> editModuleData(event.getValue()));
|
||||
}
|
||||
|
||||
private void configureForm() {
|
||||
form = new ModuleDataForm();
|
||||
form.setWidth("25em");
|
||||
form.setVisible(false);
|
||||
form.addSaveListener(this::saveModuleData);
|
||||
form.addDeleteListener(this::deleteModuleData);
|
||||
form.addCloseListener(e -> closeEditor());
|
||||
}
|
||||
|
||||
private void saveModuleData(ModuleDataForm.SaveEvent event) {
|
||||
ModuleData moduleData = event.getModuleData();
|
||||
service.saveModuleData(moduleData);
|
||||
updateList();
|
||||
closeEditor();
|
||||
}
|
||||
|
||||
private void deleteModuleData(ModuleDataForm.DeleteEvent event) {
|
||||
service.deleteModuleData(event.getModuleData());
|
||||
updateList();
|
||||
closeEditor();
|
||||
}
|
||||
|
||||
private Component getContent() {
|
||||
HorizontalLayout content = new HorizontalLayout(grid, form);
|
||||
content.setFlexGrow(2, grid);
|
||||
content.setFlexGrow(1, form);
|
||||
content.addClassName("content");
|
||||
content.setSizeFull();
|
||||
return content;
|
||||
}
|
||||
|
||||
private HorizontalLayout getToolbar() {
|
||||
filterText.setPlaceholder("Filter by module name...");
|
||||
filterText.setClearButtonVisible(true);
|
||||
filterText.setValueChangeMode(ValueChangeMode.LAZY);
|
||||
filterText.addValueChangeListener(e -> updateList());
|
||||
Button addModuleDataButton = new Button("Add module", click -> addModuleData());
|
||||
|
||||
Button menuButton = new Button("Show/Hide Columns");
|
||||
menuButton.addThemeVariants(ButtonVariant.LUMO_TERTIARY);
|
||||
ColumnToggleContextMenu<ModuleData> columnToggleContextMenu = new ColumnToggleContextMenu<>(menuButton);
|
||||
columnToggleContextMenu.addColumnToggleItem(idColumn);
|
||||
columnToggleContextMenu.addColumnToggleItem(nameColumn);
|
||||
columnToggleContextMenu.addColumnToggleItem(importColumn);
|
||||
HorizontalLayout toolbar = new HorizontalLayout(filterText, addModuleDataButton, menuButton);
|
||||
toolbar.addClassName("toolbar");
|
||||
return toolbar;
|
||||
}
|
||||
|
||||
public void editModuleData(ModuleData moduleData) {
|
||||
if (moduleData == null) {
|
||||
closeEditor();
|
||||
} else {
|
||||
form.setModuleData(moduleData);
|
||||
form.setVisible(true);
|
||||
addClassName("editing");
|
||||
}
|
||||
}
|
||||
|
||||
public void closeEditor() {
|
||||
form.setModuleData(null);
|
||||
form.setVisible(false);
|
||||
removeClassName("editing");
|
||||
}
|
||||
|
||||
private void addModuleData() {
|
||||
grid.asSingleSelect().clear();
|
||||
editModuleData(new ModuleData());
|
||||
}
|
||||
|
||||
private void updateList() {
|
||||
grid.setItems(service.findAll(filterText.getValue()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user