defaulteditcustomerservice.java

来自「Enjoy Web Dev With Tapestry 一书的源代码」· Java 代码 · 共 57 行

JAVA
57
字号
package com.ttdev.bank;

public class DefaultEditCustomerService implements EditCustomerService {
	private SystemTransaction transaction; 
	private SystemTransactionFactory transactionFactory;
	private CustomersSnapshotTaker snapshotTaker;

	public Customer getCustomerForEdit(String customerId) {
		startTransaction();
		try {
			Customer customer = loadCustomer(customerId);
			commit();
			return customer;
		} catch (Exception e) {
			rollback();
			throw new RuntimeException(e);
		}
	}
	public void saveCustomer(Customer updatedCustomer, Customer oldCustomer) {
		startTransaction();
		try {
			String customerId = oldCustomer.getId();
			Customer customerFromDB = loadCustomer(customerId);
			if (!oldCustomer.equals(customerFromDB)) {
				throw new RuntimeException("Data has changed");
			}
			saveCustomer(updatedCustomer);
			commit();
		} catch (RuntimeException e) {
			rollback();
			throw e;
		}
	}	
	private Customer loadCustomer(String customerId) {
		Customers customers = snapshotTaker.getCustomersSnapshot(transaction);
		return customers.loadCustomer(customerId);
	}
	private void startTransaction() {
		transaction = transactionFactory.start();
	}
	private void commit() {
		transaction.commit();
	}
	private void rollback() {
		transaction.rollback();
	}
	private void saveCustomer(Customer customer) {
		Customers customers = snapshotTaker.getCustomersSnapshot(transaction);
		customers.saveCustomer(customer);
	}
	public void setTransactionFactory(SystemTransactionFactory transactionFactory) {
		this.transactionFactory = transactionFactory;
	}
	public void setSnapshotTaker(CustomersSnapshotTaker snapshotTaker) {
		this.snapshotTaker = snapshotTaker;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?