Skip to content

Commit

Permalink
Take SearchOptions into separate configuration class
Browse files Browse the repository at this point in the history
  • Loading branch information
TudorOrban committed Apr 24, 2024
1 parent 1d9ef47 commit e088157
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
import org.chainoptim.desktop.shared.confirmdialog.controller.GenericConfirmDialogController;
import org.chainoptim.desktop.shared.confirmdialog.controller.RunnableConfirmDialogActionListener;
import org.chainoptim.desktop.shared.confirmdialog.model.ConfirmDialogInput;
import org.chainoptim.desktop.shared.enums.Feature;
import org.chainoptim.desktop.shared.enums.FilterType;
import org.chainoptim.desktop.shared.enums.OperationOutcome;
import org.chainoptim.desktop.shared.enums.OrderStatus;
import org.chainoptim.desktop.shared.fallback.FallbackManager;
import org.chainoptim.desktop.shared.search.controller.PageSelectorController;
import org.chainoptim.desktop.shared.search.filters.FilterOption;
import org.chainoptim.desktop.shared.search.model.PaginatedResults;
import org.chainoptim.desktop.shared.search.model.SearchOptions;
import org.chainoptim.desktop.shared.search.model.SearchOptionsConfiguration;
import org.chainoptim.desktop.shared.search.model.SearchParams;
import org.chainoptim.desktop.shared.table.TableToolbarController;
import org.chainoptim.desktop.shared.table.edit.cell.ComboBoxEditableCell;
Expand Down Expand Up @@ -68,29 +71,7 @@ public class SupplierOrdersController implements DataReceiver<Supplier> {
// State
private final FallbackManager fallbackManager;
private final SearchParams searchParams;
private static final List<FilterOption> filterOptions = List.of(
new FilterOption(
new UIItem("Order Date Start", "orderDateStart"),
new ArrayList<>(),
FilterType.DATE
),
new FilterOption(
new UIItem("Quantity", "greaterThanQuantity"),
new ArrayList<>(),
FilterType.NUMBER
),
new FilterOption(
new UIItem("Status", "status"),
List.of(new UIItem("Delivered", "DELIVERED"), new UIItem("Pending", "PENDING")),
FilterType.ENUM
)
);
private final Map<String, String> sortOptions = Map.of(
"orderDate", "Order Date",
"estimatedDeliveryDate", "Estimated Delivery Date",
"deliveryDate", "Delivery Date",
"quantity", "Quantity"
);

private Supplier supplier;
private final List<OrderStatus> statusOptions = Arrays.asList(OrderStatus.values());
private long totalRowsCount;
Expand Down Expand Up @@ -164,10 +145,15 @@ public void setData(Supplier supplier) {
this.supplier = supplier;

searchParams.setItemsPerPage(20);
SearchOptions searchOptions = SearchOptionsConfiguration.getSearchOptions(Feature.SUPPLIER_ORDER);

pageSelectorController = commonViewsLoader.loadPageSelector(pageSelectorContainer);
tableToolbarController = commonViewsLoader.initializeTableToolbar(tableToolbarContainer);
tableToolbarController.initialize(searchParams, filterOptions, sortOptions, () -> loadSupplierOrders(supplier.getId()));
tableToolbarController.initialize(
searchParams,
searchOptions.getFilterOptions(),
searchOptions.getSortOptions(),
() -> loadSupplierOrders(supplier.getId()));
pageSelectorController = commonViewsLoader.loadPageSelector(pageSelectorContainer);
selectComponentLoader.initialize();

TableConfigurer.configureTableView(tableView, selectRowColumn);
Expand Down Expand Up @@ -282,7 +268,9 @@ private void setUpSearchListeners() {
searchParams.getSearchQueryProperty().addListener((observable, oldValue, newValue) -> loadSupplierOrders(supplier.getId()));
searchParams.getFiltersProperty().addListener((MapChangeListener.Change<? extends String, ? extends String> change) -> {
System.out.println("Filter changed: " + change.getKey() + " -> " + change.getValueAdded());
loadSupplierOrders(supplier.getId());
if (searchParams.getFiltersProperty().entrySet().size() == 1) { // Allow only one filter at a time
loadSupplierOrders(supplier.getId());
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ public static String buildAdvancedSearchKey(String feature, String secondaryFeat
"&itemsPerPage=" + searchParams.getItemsPerPage();

if (!searchParams.getFiltersProperty().isEmpty()) {
String filtersJson = null;
String filtersJson;
try {
filtersJson = JsonUtil.getObjectMapper().writeValueAsString(searchParams.getFiltersProperty());
filtersJson = URLEncoder.encode(filtersJson, StandardCharsets.UTF_8);
} catch (Exception e) {
throw new RuntimeException("Error serializing filters to JSON", e);
throw new RuntimeException("Error encoding filters to JSON", e);
}


key += "&filters=" + filtersJson;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@
import org.chainoptim.desktop.shared.enums.FilterType;
import org.chainoptim.desktop.shared.search.model.SearchParams;

import javafx.beans.property.SimpleMapProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import jfxtras.scene.control.LocalDateTimeTextField;
import lombok.Getter;

import java.util.HashMap;
import java.util.List;

public class FilterBar extends HBox {
Expand Down Expand Up @@ -77,7 +72,6 @@ private void setUpEnumPicker() {
if (newVal != null) {
searchParams.getFiltersProperty().clear();
searchParams.getFiltersProperty().put(keyComboBox.getValue().getValue(), newVal.getValue());
System.out.println("Enum set to: " + newVal.getValue());
}
});
}
Expand All @@ -91,7 +85,6 @@ private void setUpNumberPicker() {
applyNumberPicker.setOnAction(event -> {
searchParams.getFiltersProperty().clear();
searchParams.getFiltersProperty().put(keyComboBox.getValue().getValue(), textField.getText());
System.out.println("Number set to: " + textField.getText());
});

numberPicker.getChildren().addAll(textField, applyNumberPicker);
Expand All @@ -102,7 +95,6 @@ private void setUpDatePicker() {
if (newValue != null) {
searchParams.getFiltersProperty().clear();
searchParams.getFiltersProperty().put(keyComboBox.getValue().getValue(), dateTimePicker.getLocalDateTime().toString());
System.out.println("Date/time set to: " + newValue);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
package org.chainoptim.desktop.shared.search.model;

import org.chainoptim.desktop.shared.search.filters.FilterOption;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SearchOptions {

private List<FilterOption> filterOptions;
private Map<String, String> sortOptions;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.chainoptim.desktop.shared.search.model;

import org.chainoptim.desktop.shared.common.uielements.UIItem;
import org.chainoptim.desktop.shared.enums.Feature;
import org.chainoptim.desktop.shared.enums.FilterType;
import org.chainoptim.desktop.shared.search.filters.FilterOption;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SearchOptionsConfiguration {

private SearchOptionsConfiguration() {
}

private static final SearchOptions SUPPLIER_ORDER_OPTIONS = new SearchOptions(
List.of(
new FilterOption(
new UIItem("Order Date Start", "orderDateStart"),
new ArrayList<>(),
FilterType.DATE
),
new FilterOption(
new UIItem("Quantity", "greaterThanQuantity"),
new ArrayList<>(),
FilterType.NUMBER
),
new FilterOption(
new UIItem("Status", "status"),
List.of(new UIItem("Delivered", "DELIVERED"), new UIItem("Pending", "PENDING")),
FilterType.ENUM
)
),
Map.of(
"orderDate", "Order Date",
"estimatedDeliveryDate", "Estimated Delivery Date",
"deliveryDate", "Delivery Date",
"quantity", "Quantity"
)
);

private static final Map<Feature, SearchOptions> SEARCH_OPTIONS_MAP = Map.of(
Feature.SUPPLIER_ORDER, SUPPLIER_ORDER_OPTIONS
);

public static SearchOptions getSearchOptions(Feature feature) {
return SEARCH_OPTIONS_MAP.get(feature);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
<TextField fx:id="searchBar" onAction="#handleSearch" promptText="Search..." prefWidth="240" styleClass="search-bar"/>
<Button fx:id="searchButton" onAction="#handleSearch" styleClass="search-button"/>
</StackPane>

<FilterBar fx:id="filterBar"/>

<HBox alignment="CENTER_LEFT" spacing="6">
<ComboBox fx:id="sortOptions" promptText="Sort By..." onAction="#handleSortOption"/>
<Button fx:id="orderingButton" onAction="#handleOrdering" styleClass="ordering-button"/>
Expand Down

0 comments on commit e088157

Please sign in to comment.