logging of Transaction CRUD operations

This commit is contained in:
2016-07-10 21:13:42 +02:00
parent a8824c360d
commit 5d5a9c2bd5
5 changed files with 47 additions and 37 deletions
@@ -1,33 +0,0 @@
package de.tilman.transactions.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import de.tilman.transactions.domain.Transaction;
import de.tilman.transactions.repository.TransactionRepository;
//@RepositoryRestController
public class TransactionController {
// private final TransactionRepository repository;
//
// @Autowired
// public TransactionController(TransactionRepository repository) {
// this.repository = repository;
// }
//
// @RequestMapping(method = RequestMethod.DELETE, value = "/transactions/{id}")
// public @ResponseBody Iterable<Transaction> deleteTransaction() {
// Iterable<Transaction> producers = repository.findAll();
//
// //
// // do some intermediate processing, logging, etc. with the producers
// //
//
// return producers; // or some filtered/altered/mapped version
// }
}
@@ -1,6 +1,7 @@
package de.tilman.transactions.domain;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import javax.persistence.Entity;
@@ -11,6 +12,14 @@ import javax.persistence.ManyToOne;
@Entity
public class Transaction {
private static DecimalFormat df = new DecimalFormat();
static {
df.getDecimalFormatSymbols().setDecimalSeparator('.');
df.setMaximumFractionDigits(2);
df.setMinimumFractionDigits(0);
df.setGroupingUsed(false);
}
@Id
@GeneratedValue
private Long id;
@@ -92,4 +101,22 @@ public class Transaction {
this.category = category;
}
@Override
public String toString() {
String s = "{ id: " + id + ", amount: " + df.format(amount) + ", date: " + date + ", lastChange: " + lastChange + ", description: \"" + description + "\"";
if (creditor != null)
s += ", creditor: \"" + creditor.getName() + "\"";
else
s += ", creditor: null";
s += ", account: " + account.getId();
s += ", category: \"" + category.getName() + "\"";
s += "}";
return s;
}
}
@@ -3,7 +3,6 @@ package de.tilman.transactions.domain;
import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@@ -4,6 +4,9 @@ import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.rest.core.annotation.HandleAfterCreate;
import org.springframework.data.rest.core.annotation.HandleAfterDelete;
import org.springframework.data.rest.core.annotation.HandleAfterSave;
import org.springframework.data.rest.core.annotation.HandleBeforeCreate;
import org.springframework.data.rest.core.annotation.HandleBeforeSave;
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
@@ -22,7 +25,21 @@ public class SpringDataRestEventHandler {
@HandleBeforeSave
public void applyUserInformationUsingSecurityContext(Transaction transaction) {
Date timestamp = new Date();
log.info("Setting date '" + timestamp + "' on transaction: " + transaction.getId() + "(" + transaction.getDescription() + ")");
transaction.setLastChange(timestamp);
}
@HandleAfterCreate
public void logCreation(Transaction transaction) {
log.info("Created transaction: " + transaction.toString());
}
@HandleAfterSave
public void logUpdate(Transaction transaction) {
log.info("Updated transaction: " + transaction.toString());
}
@HandleAfterDelete
public void logDeletion(Transaction transaction) {
log.info("Deleted transaction: " + transaction.toString());
}
}
@@ -15,8 +15,8 @@ import de.tilman.transactions.domain.Transaction;
@RepositoryRestResource(excerptProjection = TransactionInlineProjection.class)
public interface TransactionRepository extends PagingAndSortingRepository<Transaction, Long> {
// @RestResource(exported = false)
// Page<Transaction> findAll(Pageable pageable);
@RestResource(exported = false)
Page<Transaction> findAll(Pageable pageable);
// @PreAuthorize("hasRole('ROLE_ADMIN')")
// @Override