Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐞 fix autocategory functionality - 500 #50

Open
1 task
immarisabel opened this issue Jul 3, 2023 · 3 comments
Open
1 task

🐞 fix autocategory functionality - 500 #50

immarisabel opened this issue Jul 3, 2023 · 3 comments
Assignees
Labels
⏸ on hold to work on later, no priority

Comments

@immarisabel
Copy link
Owner

immarisabel commented Jul 3, 2023

java.lang.NullPointerException: Cannot invoke "java.lang.String.toLowerCase()" because the return value of "nl.marisabel.backend.transactions.entity.TransactionEntity.getDescription()" is null
at nl.marisabel.backend.categories.service.CategoryService.isTransactionMatch(CategoryService.java:57) ~[classes/:na]

TO DO:

  • need to handle transaction.getDescription() being null

officially, it should never be the case. But it is an edge case.

@immarisabel immarisabel self-assigned this Jul 3, 2023
@immarisabel immarisabel added the 🐞 bug Something isn't working label Jul 3, 2023
@immarisabel
Copy link
Owner Author

lot of code changed... possibly when I reverted one of the commits.

working code to be moved to a service:

@PostMapping("/autoCategorize")
    public String autoCategorize(Model model) {
        List<TransactionEntity> unCategorizedTransactions = transactionRepository.findByCategoriesEmpty();
        log.info("Number of uncategorized transactions: " + unCategorizedTransactions.size());
        List<AutoCategoryEntity> autoCategories = autoCategoryRepository.findAll();

        int numCategorized = 0;

        for (AutoCategoryEntity autoCategory : autoCategories) {
            // Try to find an existing CategoryEntity with this category
            CategoryEntity existingCategory = categoryRepository.findByCategory(autoCategory.getCategory());

            // If none exists, create a new one
            if (existingCategory == null) {
                existingCategory = new CategoryEntity();
                existingCategory.setCategory(autoCategory.getCategory());
                categoryRepository.save(existingCategory); // don't forget to save the new category
            }

            for (TransactionEntity transaction : unCategorizedTransactions) {
                for (String query : autoCategory.getQueries()) {
                    if (transaction.getEntity().toLowerCase().contains(query.toLowerCase()) ||
                            transaction.getDescription().toLowerCase().contains(query.toLowerCase())) {

                        transaction.addCategory(existingCategory);
                        transactionRepository.save(transaction);

                        numCategorized++;
                        break;
                    }
                }
            }
        }
        log.info("Transactions categorized: " + numCategorized);
        model.addAttribute("message", "Auto-categorized " + numCategorized + " transactions");
        return "redirect:/auto-category";
    }

@immarisabel
Copy link
Owner Author

java.lang.NullPointerException: Cannot invoke "java.lang.String.toLowerCase()" because the return value of "nl.marisabel.backend.transactions.entity.TransactionEntity.getDescription()" is null
at nl.marisabel.backend.categories.controller.AutoCategoryController.autoCategorize(AutoCategoryController.java:105) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]

@immarisabel
Copy link
Owner Author

immarisabel commented Jul 3, 2023

private final CategoryRepository categoryRepository;

// CURRENT NOT WORKING
//TransactionEntity.getDescription()" is null

@PostMapping("/autoCategorize")
    public String autoCategorize(Model model) {
        List<TransactionEntity> unCategorizedTransactions = transactionRepository.findByCategoriesEmpty();
        log.info("Number of uncategorized transactions: " + unCategorizedTransactions.size());
        List<AutoCategoryEntity> autoCategories = autoCategoryRepository.findAll();

        int numCategorized = 0;

        for (AutoCategoryEntity autoCategory : autoCategories) {
            // Try to find an existing CategoryEntity with this category
            CategoryEntity existingCategory = categoryRepository.findByCategory(autoCategory.getCategory());

            // If none exists, create a new one
            if (existingCategory == null) {
                existingCategory = new CategoryEntity();
                existingCategory.setCategory(autoCategory.getCategory());
                categoryRepository.save(existingCategory); // don't forget to save the new category
            }

            for (TransactionEntity transaction : unCategorizedTransactions) {
                for (String query : autoCategory.getQueries()) {
                    if (transaction.getEntity().toLowerCase().contains(query.toLowerCase()) ||
                            transaction.getDescription().toLowerCase().contains(query.toLowerCase())) {

                        transaction.addCategory(existingCategory);
                        transactionRepository.save(transaction);

                        numCategorized++;
                        break;
                    }
                }
            }
        }
        log.info("Transactions categorized: " + numCategorized);
        model.addAttribute("message", "Auto-categorized " + numCategorized + " transactions");
        return "redirect:/auto-category";
    }



// ORIGINAL WORKING


  @PostMapping("/autoCategorize")
    public String autoCategorize(Model model) {
        List<TransactionEntity> unCategorizedTransactions = transactionRepository.findByCategoriesEmpty();
        log.info("Number of uncategorized transactions: " + unCategorizedTransactions.size());
        List<AutoCategoryEntity> autoCategories = autoCategoryRepository.findAll();

        int numCategorized = 0;

        for (TransactionEntity transaction : unCategorizedTransactions) {
            for (AutoCategoryEntity autoCategory : autoCategories) {
                for (String query : autoCategory.getQueries()) {
                    if (transaction.getEntity().toLowerCase().contains(query.toLowerCase()) ||
                            transaction.getDescription().toLowerCase().contains(query.toLowerCase())) {
                        CategoryEntity newCategory = new CategoryEntity();
                        newCategory.setCategory(autoCategory.getCategory());

                        transaction.addCategory(newCategory);
                        transactionRepository.save(transaction);

                        numCategorized++;
                        break;

                    }
                }
            }
        }
        log.info("Transactions categorized: " + numCategorized);
        model.addAttribute("message", "Auto-categorized " + numCategorized + " transactions");
        return "redirect:/auto-category";
    }
 

immarisabel added a commit that referenced this issue Jul 3, 2023
…description being null. Will revisit later.
immarisabel added a commit that referenced this issue Jul 3, 2023
…description being null. Will revisit later.
@immarisabel immarisabel added ⏸ on hold to work on later, no priority and removed 🐞 bug Something isn't working labels Jul 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⏸ on hold to work on later, no priority
Development

No branches or pull requests

1 participant