3
\$\begingroup\$

I have method that fetches all orders from Database and filtering, sorting that data(I know better to do It by specific query to Database but I dont know how). I have Order entity that has following

public class Order {
    private int orderId;
    private int userId;
    private int carId;
    private String userAddress;
    private String userDestination;
    private double orderCost;
    @JsonSerialize(using = CustomLocalDateTimeJsonSerializer.class)
    private LocalDateTime orderDate;

// getters/setters
}

public class OrdersProcessor {
    private final MySQLOrderDao orderDao;
    private final DateTimeFormatter formatter;
    public OrdersProcessor(MySQLOrderDao orderDao){
        this.orderDao=orderDao;
        formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
    }
    public List<Order> findFilSortOrders(Map<String,String> filters, String sortedColumn, boolean descending, int startRow, int rowsPerPage){
        List<Order> fetchedOrders = orderDao.findAllOrders();
        List<Order>orders = new LinkedList<>(fetchedOrders.subList(startRow, fetchedOrders.size()));
        if(filters!=null){
            filters.entrySet().iterator().forEachRemaining(element->{
                switch (element.getKey()){
                    case "orderId":{
                        orders.removeIf(order -> order.getOrderId()!=Integer.parseInt(element.getValue()));
                        break;
                    }
                    case "userId":{
                        orders.removeIf(order -> order.getUserId()!=Integer.parseInt(element.getValue()));
                        break;
                    }
                    case "carId":{
                        orders.removeIf(order -> order.getCarId()!=Integer.parseInt(element.getValue()));
                        break;
                    }
                    case "userAddress":{
                        orders.removeIf(order -> !order.getUserAddress().equals(element.getValue()));
                        break;
                    }
                    case "userDestination":{
                        orders.removeIf(order -> !order.getUserDestination().equals(element.getValue()));
                        break;
                    }
                    case "orderCost":{
                        orders.removeIf(order -> order.getOrderCost()!=Double.parseDouble(element.getValue()));
                        break;
                    }
                    case "orderDate":{
                        orders.removeIf(order -> !order.getOrderDate().format(formatter).equals(element.getValue()));
                        break;
                    }
                }
            });
        }
        switch (sortedColumn){
            case "orderId":{
                if(descending){
                    orders.sort(Comparator.comparing(Order::getOrderId).reversed());
                }else {
                    orders.sort(Comparator.comparing(Order::getOrderId));
                }
                break;
            }
            case "userId":{
                if(descending){
                    orders.sort(Comparator.comparing(Order::getUserId).reversed());
                }else {
                    orders.sort(Comparator.comparing(Order::getUserId));
                }

                break;
            }
            case "carId":{
                if(descending){
                    orders.sort(Comparator.comparing(Order::getCarId).reversed());
                }else {
                    orders.sort(Comparator.comparing(Order::getCarId));
                }
                break;
            }
            case "userAddress":{
                if(descending){
                    orders.sort(Comparator.comparing(Order::getUserAddress).reversed());
                }else {
                    orders.sort(Comparator.comparing(Order::getUserAddress));
                }
                break;
            }
            case "userDestination":{
                if(descending){
                    orders.sort(Comparator.comparing(Order::getUserDestination).reversed());
                }else {
                    orders.sort(Comparator.comparing(Order::getUserDestination));
                }
                break;
            }
            case "orderCost":{
                if(descending){
                    orders.sort(Comparator.comparing(Order::getOrderCost).reversed());
                }else {
                    orders.sort(Comparator.comparing(Order::getOrderCost));
                }
                break;
            }
            case "orderDate":{
                if(descending){
                    orders.sort(Comparator.comparing(Order::getOrderDate).reversed());
                }else {
                    orders.sort(Comparator.comparing(Order::getOrderDate));
                }
                break;
            }
        }
     return orders.subList(0, Math.min(orders.size(), rowsPerPage));
    }
}

And my question Is how can I do it at least more readable?Thanks for answering.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Maybe you want to have a look at Javas Streams-API. Also you might want to distinguish between foreward and reverse order before iterating over the entries. \$\endgroup\$ May 24 '21 at 17:56
  • 1
    \$\begingroup\$ Leeching the complete database into memory to filter and sort there is such a bad idea, that the resulting code should not even be reviewed. If you don't know how to do this via database queries, I recommend that you grab a tutorial and learn it, as this is basically the only serious approach to the problem. \$\endgroup\$
    – mtj
    May 26 '21 at 7:00
  • \$\begingroup\$ @mtj I have Map that contains column name as key and column value as value. It all about order entity. I know how to work with jdbc but I dont know how to do it, If I have column name as variables. Can you help me with that? \$\endgroup\$ May 26 '21 at 8:29
  • \$\begingroup\$ Not really. As a starter, you might look into hibernate and the criteria API. This is a lot to tackle at a time, but definitely worthwile. \$\endgroup\$
    – mtj
    May 26 '21 at 9:22
  • \$\begingroup\$ @mtj I have just done It by using Jdbc. Can you review my code and tell what should i improve? \$\endgroup\$ May 28 '21 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.