From 01fde2afc69cc6c7b005fd25d6fef53a81a79f5c Mon Sep 17 00:00:00 2001 From: tliero Date: Thu, 21 Jul 2016 15:20:08 +0200 Subject: [PATCH] Pattern processing, pt. 3 http://stackoverflow.com/a/23287265/3761783 http://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts.projections --- .../domain/{Pattern.java => Matcher.java} | 42 ++++----- .../transactions/repository/FullMatcher.java | 11 +++ ...Repository.java => MatcherRepository.java} | 12 +-- src/main/resources/data-h2.sql | 12 ++- src/main/resources/static/js/app.js | 2 +- src/main/resources/static/js/controllers.js | 91 +++++++++++++++---- src/main/resources/static/js/services.js | 26 ++++-- .../static/partials/import-list.html | 8 +- 8 files changed, 142 insertions(+), 62 deletions(-) rename src/main/java/de/tilman/transactions/domain/{Pattern.java => Matcher.java} (57%) create mode 100644 src/main/java/de/tilman/transactions/repository/FullMatcher.java rename src/main/java/de/tilman/transactions/repository/{PatternRepository.java => MatcherRepository.java} (65%) diff --git a/src/main/java/de/tilman/transactions/domain/Pattern.java b/src/main/java/de/tilman/transactions/domain/Matcher.java similarity index 57% rename from src/main/java/de/tilman/transactions/domain/Pattern.java rename to src/main/java/de/tilman/transactions/domain/Matcher.java index 4b35b43..4387b82 100644 --- a/src/main/java/de/tilman/transactions/domain/Pattern.java +++ b/src/main/java/de/tilman/transactions/domain/Matcher.java @@ -8,7 +8,7 @@ import javax.persistence.ManyToOne; import javax.persistence.OneToOne; @Entity -public class Pattern { +public class Matcher { @Id @GeneratedValue @@ -19,10 +19,10 @@ public class Pattern { private Integer position; - private String typePattern; - private String datePattern; - private String textPattern; - private String amountPattern; + private String typeMatcherCode; + private String dateMatcherCode; + private String textMatcherCode; + private String amountMatcherCode; @OneToOne private Category targetCategory; @@ -70,36 +70,36 @@ public class Pattern { this.position = position; } - public String getTypePattern() { - return typePattern; + public String getTypeMatcherCode() { + return typeMatcherCode; } - public void setTypePattern(String typePattern) { - this.typePattern = typePattern; + public void setTypeMatcherCode(String typeMatcherCode) { + this.typeMatcherCode = typeMatcherCode; } - public String getDatePattern() { - return datePattern; + public String getDateMatcherCode() { + return dateMatcherCode; } - public void setDatePattern(String datePattern) { - this.datePattern = datePattern; + public void setDateMatcherCode(String dateMatcherCode) { + this.dateMatcherCode = dateMatcherCode; } - public String getTextPattern() { - return textPattern; + public String getTextMatcherCode() { + return textMatcherCode; } - public void setTextPattern(String textPattern) { - this.textPattern = textPattern; + public void setTextMatcherCode(String textMatcherCode) { + this.textMatcherCode = textMatcherCode; } - public String getAmountPattern() { - return amountPattern; + public String getAmountMatcherCode() { + return amountMatcherCode; } - public void setAmountPattern(String amountPattern) { - this.amountPattern = amountPattern; + public void setAmountMatcherCode(String amountMatcherCode) { + this.amountMatcherCode = amountMatcherCode; } } diff --git a/src/main/java/de/tilman/transactions/repository/FullMatcher.java b/src/main/java/de/tilman/transactions/repository/FullMatcher.java new file mode 100644 index 0000000..ff96410 --- /dev/null +++ b/src/main/java/de/tilman/transactions/repository/FullMatcher.java @@ -0,0 +1,11 @@ +package de.tilman.transactions.repository; + +import org.springframework.data.rest.core.config.Projection; + +import de.tilman.transactions.domain.Category; +import de.tilman.transactions.domain.Matcher; + +@Projection(name = "fullMatchers", types = Matcher.class) +public interface FullMatcher { + Category getCategory(); +} diff --git a/src/main/java/de/tilman/transactions/repository/PatternRepository.java b/src/main/java/de/tilman/transactions/repository/MatcherRepository.java similarity index 65% rename from src/main/java/de/tilman/transactions/repository/PatternRepository.java rename to src/main/java/de/tilman/transactions/repository/MatcherRepository.java index 9031975..66c7550 100644 --- a/src/main/java/de/tilman/transactions/repository/PatternRepository.java +++ b/src/main/java/de/tilman/transactions/repository/MatcherRepository.java @@ -9,23 +9,23 @@ import org.springframework.data.repository.query.Param; import org.springframework.data.rest.core.annotation.RestResource; import org.springframework.security.access.prepost.PreAuthorize; -import de.tilman.transactions.domain.Pattern; +import de.tilman.transactions.domain.Matcher; -public interface PatternRepository extends PagingAndSortingRepository { +public interface MatcherRepository extends PagingAndSortingRepository { @RestResource(path = "listForAccount") - List findByAccountIdOrderByPositionAsc(@Param("accountId") Long accountId); + List findByAccountIdOrderByPositionAsc(@Param("accountId") Long accountId); @PreAuthorize("hasRole('ROLE_ADMIN')") @Override - Pattern save(Pattern pattern); + Matcher save(Matcher pattern); @PreAuthorize("hasRole('ROLE_ADMIN')") @Override - void delete(Pattern pattern); + void delete(Matcher pattern); @PreAuthorize("hasRole('ROLE_ADMIN')") @Override - Page findAll(Pageable pageable); + Page findAll(Pageable pageable); } diff --git a/src/main/resources/data-h2.sql b/src/main/resources/data-h2.sql index 216b491..4647cf7 100644 --- a/src/main/resources/data-h2.sql +++ b/src/main/resources/data-h2.sql @@ -11,10 +11,14 @@ INSERT INTO Category (id, name, account_id, position) VALUES INSERT INTO Account_User (account_id, user_id) VALUES (1, 1), (1, 2), (1, 3), (2, 1); -INSERT INTO Pattern (id, account_id, position, type_pattern, date_pattern, text_pattern, amount_pattern, target_category_id, target_description) VALUES - (NULL, 1, 1, '', '', '*.LIDL*.', '', 1, 'Lidl'), - (NULL, 1, 1, null, null, '*.Buchungstext: none*.', -40.0, 4, '(in Rücklage)'), - (NULL, 1, 1, 'Kontoübertrag', null, 'Auftraggeber: Tilman Liero Kto/IBAN: DE44200411330649163305*.', null, 4, '(Entnahme aus Rücklagen)'); +INSERT INTO Matcher (id, account_id, position, type_matcher_code, date_matcher_code, text_matcher_code, amount_matcher_code, target_category_id, target_description) VALUES + (NULL, 1, 1, null, null, 'return /\bnone\b/.test(x)', 'return (x == -40)', 4, 'Sparplan (in Rücklage)'), + (NULL, 1, 1, null, null, 'return /\bLIDL\b/.test(x)', null, 1, 'Lidl'); + +--INSERT INTO Pattern (id, account_id, position, type_pattern, date_pattern, text_pattern, amount_pattern, target_category_id, target_description) VALUES +-- (NULL, 1, 1, '', '', '*.LIDL*.', '', 1, 'Lidl'), +-- (NULL, 1, 1, null, null, '*.Buchungstext: none*.', -40.0, 4, '(in Rücklage)'), +-- (NULL, 1, 1, 'Kontoübertrag', null, 'Auftraggeber: Tilman Liero Kto/IBAN: DE44200411330649163305*.', null, 4, '(Entnahme aus Rücklagen)'); INSERT INTO Transaction (id, amount, account_id, category_id, date, description, creditor_id) VALUES (NULL, 1280.81, 1, 2, '2016-04-01 14:14:14', 'Gehalt', null), diff --git a/src/main/resources/static/js/app.js b/src/main/resources/static/js/app.js index 7bb7d8a..3c965a5 100644 --- a/src/main/resources/static/js/app.js +++ b/src/main/resources/static/js/app.js @@ -9,7 +9,7 @@ transactionsApp.config([ '$routeProvider', function($routeProvider) { controller : 'ImportCtrl' }).when('/importList', { templateUrl : 'partials/import-list.html', - controller : 'ImportCtrl' + controller : 'ImportListCtrl' }).otherwise({ redirectTo : '/list' }); diff --git a/src/main/resources/static/js/controllers.js b/src/main/resources/static/js/controllers.js index 6a8a33c..66a8c3a 100644 --- a/src/main/resources/static/js/controllers.js +++ b/src/main/resources/static/js/controllers.js @@ -367,20 +367,13 @@ transactionControllers.controller('OutstandingsModalCtrl', [ '$scope', '$uibModa transactionControllers.controller('ImportCtrl', [ '$scope', '$resource', '$location', 'ImportService', function($scope, $resource, $location, ImportService) { $scope.importCSV = function () { - ImportService.saveData($scope.csvInput); + ImportService.setAccount($scope.account); + ImportService.setCategories($scope.categories); + ImportService.setCSV($scope.csvInput); $location.path('importList'); } - $scope.processCSV = function () { - $scope.csv = Papa.parse("\"08.07.2016\";\"08.07.2016\";\"Übertrag/Überweisung\";\"Empfänger: Tilman Liero Kto/IBAN: DE44200411330649163305 BLZ/BIC: COBADEHD001 Buchungstext: none End-to-End-Ref.: nicht angegeben Ref. JK21619010302362/2\";\"-40,00\";\n\"07.07.2016\";\"07.07.2016\";\"Übertrag/Überweisung\";\"Empfänger: Tilman Liero Kto/IBAN: DE44200411330649163305 BLZ/BIC: COBADEHD001 Buchungstext: Uebertrag auf Tagesgeld PLUS-Konto End-to-End-Ref.: nicht angegeben Ref. J521618965322854/2\";\"-185,76\";\n\"06.07.2016\";\"06.07.2016\";\"Übertrag/Überweisung\";\"Auftraggeber: Bayer AG Buchungstext: /EDAM WORSHOP MIT/ Delaware (15.-16 End-to-End-Ref.: 8000012618 Ref. J321618880156700/705\";\"226,90\";\n\"01.07.2016\";\"01.07.2016\";\"Lastschrift/Belast.\";\"Auftraggeber: comdirect Visa-Monatsabrechnung Buchungstext: VISA-KARTE NR. 42635401XXXX5230 SUMME MONATSABRECHNUNG VISA TILMAN LIERO Ref. I421618341001491/75009\";\"-346,67\";\n"); -// $scope.csv = Papa.parse(ImportService.getData()); - console.log($scope.csv.meta.delimiter); - console.log($scope.csv); - } - - // COPIED CODE from TransactionListCtrl.initList, needs to be moved into a Service $scope.initPage = function () { - console.log('aloha'); var userinfo = $resource('/user').get(); userinfo.$promise.then(function(data) { $scope.userid = userinfo.name; @@ -392,17 +385,8 @@ transactionControllers.controller('ImportCtrl', [ '$scope', '$resource', '$locat var accountInfo = $resource('/accounts/search/findByUserName', { username: $scope.userid }).get(); accountInfo.$promise.then(function(data) { - console.log(accountInfo._embedded.accounts); $scope.accounts = accountInfo._embedded.accounts; - if ($scope.account == undefined) { - $scope.account = $scope.accounts[0]; - if ($routeParams.account != undefined) { - for (i = 0; i < $scope.accounts.length; i++) { - if ($scope.accounts[i].name == $routeParams.account) - $scope.account = $scope.accounts[i]; - } - } - } + $scope.account = $scope.accounts[0]; var categories = $resource('/categories/search/listForAccount', { accountId: $scope.account.id }).get(); categories.$promise.then(function(data) { @@ -415,8 +399,75 @@ transactionControllers.controller('ImportCtrl', [ '$scope', '$resource', '$locat }); }); }); + + // XXX + $scope.csvInput = "\"08.07.2016\";\"08.07.2016\";\"Übertrag/Überweisung\";\"Empfänger: Tilman Liero Kto/IBAN: DE44200411330649163305 BLZ/BIC: COBADEHD001 Buchungstext: none End-to-End-Ref.: nicht angegeben Ref. JK21619010302362/2\";\"-40,00\";\n\"27.06.2016\";\"27.06.2016\";\"Lastschrift/Belast.\";\"Auftraggeber: DANKE, IHR LIDL Kto/IBAN: DE61300500000008000119 Buchungstext: Kartenzahlung DANKE, IHR LIDL//Berlin/DE 2016-06-25T09:47:08 KFN 2 VJ 1912 Ref. 6GL16179A2640086/6763\";\"-70,17\";\n\"24.06.2016\";\"24.06.2016\";\"Übertrag/Überweisung\";\"Auftraggeber: Tilman Liero Buchungstext: Uebertrag comdirect bank End-to-End-Ref.: nicht angegeben Ref. IX216176F0205292/1\";\"200,00\";\n\"24.06.2016\";\"24.06.2016\";\"Übertrag/Überweisung\";\"Auftraggeber: Wohnungsbau-Verein Neukölln eG Buchungstext: Mg.-Nr. 35823, Walther, Tilman - 4, Dividende 2015 End-to-End-Ref.: nicht angegeben Ref. H221617674648332/35629\";\"4,42\";"; } $scope.initPage(); } ]); + +transactionControllers.controller('ImportListCtrl', [ '$scope', '$resource', '$location', 'ImportService', function($scope, $resource, $location, ImportService) { + + $scope.processCSV = function () { + $scope.account = ImportService.getAccount(); + $scope.categories = ImportService.getCategories(); + $scope.csv = Papa.parse(ImportService.getCSV()).data; + + var matcherData = $resource('/matchers/search/listForAccount', { accountId: $scope.account.id }).get(); + matcherData.$promise.then(function(data) { + + // XXX + console.log(matcherData); + + + matcherData = matcherData._embedded.matchers; + var matchers = new Array(matcherData.length); + + for (i = 0; i < matchers.length; i++) { + matchers[i] = {}; + + if (matcherData[i].dateMatcherCode != null) + matchers[i].dateMatcher = new Function('x', matcherData[i].dateMatcherCode); + if (matcherData[i].typeMatcherCode != null) + matchers[i].typeMatcher = new Function('x', matcherData[i].typeMatcherCode); + if (matcherData[i].textMatcherCode != null) + matchers[i].textMatcher = new Function('x', matcherData[i].textMatcherCode); + if (matcherData[i].amountMatcherCode != null) + matchers[i].amountMatcher = new Function('x', matcherData[i].amountMatcherCode); + + matchers[i].targetDescription = matcherData[i].targetDescription; + } + + // XXX + console.log(matchers); + + for (i = 0; i < $scope.csv.length; i++) { + var matched = false; + var j = 0; + + while (!matched && j < matchers.length) { + var match = true; + + if (match && matchers[j].dateMatcher) + match = matchers[j].dateMatcher($scope.csv[i][1]); + if (match && matchers[j].typeMatcher) + match = matchers[j].typeMatcher($scope.csv[i][2]); + if (match && matchers[j].textMatcher) + match = matchers[j].textMatcher($scope.csv[i][3]); + if (match && matchers[j].amountMatcher) + match = matchers[j].amountMatcher(amountToNumber($scope.csv[i][4])); + + if (match) { + $scope.csv[i].targetDescription = matchers[j].targetDescription; + } + + j++; + } + } + }); + } + +} ]); + diff --git a/src/main/resources/static/js/services.js b/src/main/resources/static/js/services.js index b993b6f..4c6749b 100644 --- a/src/main/resources/static/js/services.js +++ b/src/main/resources/static/js/services.js @@ -12,14 +12,28 @@ transactionServices.factory('Transaction', [ '$resource', function($resource) { } ]); transactionServices.factory('ImportService', function() { - var dataResponse = {}; + var account = {}; + var categories = {}; + var csvData = {}; + return { - saveData: function(data) { - dataResponse = data; - console.log(data); + setAccount: function(data) { + account = data; }, - getData : function() { - return dataResponse; + getAccount : function() { + return account; + }, + setCategories: function(data) { + categories = data; + }, + getCategories : function() { + return categories; + }, + setCSV: function(data) { + csvData = data; + }, + getCSV : function() { + return csvData; } }; }); diff --git a/src/main/resources/static/partials/import-list.html b/src/main/resources/static/partials/import-list.html index 438bda5..d2a5d9d 100644 --- a/src/main/resources/static/partials/import-list.html +++ b/src/main/resources/static/partials/import-list.html @@ -4,7 +4,7 @@ Wertstellung Typ - Text + Text Betrag Kategorie @@ -12,14 +12,14 @@ Pattern - + {{line[1]}} {{line[2]}} {{line[3]}} {{line[4]}} - - + {{line.targetCategory}} + {{line.targetDescription}}