Commit 9421b1d6 authored by Matti Henning's avatar Matti Henning
Browse files

new data_access-package

parent 16f3840e
import java.util.Date;
import java.util.List;
import data_access.DataAccess;
import model.Customer;
import model.Order;
import model.OrderItem;
import system.Builder;
import system.DataAccess;
import system.Logic;
import model.Article;
......
package data_access;
import java.util.Optional;
import java.util.function.Consumer;
import model.Article;
import model.Customer;
import model.Order;
import system.ComponentLifecycle;
public interface DataAccess extends ComponentLifecycle {
static DataAccess getInstance() {
return DataAccessImpl.getInstance();
}
DataRepository<Customer,String> customers();
DataRepository<Article,String> articles();
DataRepository<Order,Long> orders();
interface MockDataBuilder {
void buildCustomers();
void buildArticless();
void buildOrders( Consumer<Order> fillOrder );
Optional<MockDataBuilder> buildMockData();
}
}
package data_access;
import java.util.Optional;
import java.util.function.BiFunction;
import model.Article;
import model.Customer;
import model.Order;
/**
* Local singleton instance that implements the DataAccess interface.
*
* @author sgra64
*
*/
class DataAccessImpl implements DataAccess {
private static DataAccess instance = new DataAccessImpl();
private final DataRepository<Customer,String> customers;
private final DataRepository<Article,String> articles;
private final DataRepository<Order,Long> orders;
private Optional<MockDataBuilder> mockDataBuilder = Optional.empty();
/*
* Constructor.
*/
DataAccessImpl() {
//BiFunction<ID,ID,Boolean> compareId
BiFunction<Long,Long,Boolean> compareLongId = ( id1, id2 ) -> { return id1 == id2; };
BiFunction<String,String,Boolean> compareStringId =
( id1, id2 ) -> { return id1 != null && id1.equals( id2 ); };
this.customers = new DataRepositoryImpl<Customer,String>(
//Function<T,ID> getId, , BiFunction<ID,ID,Boolean> compareId
customer -> { return customer.getId(); }, compareStringId
);
this.articles = new DataRepositoryImpl<Article,String>(
article -> { return article.getId(); }, compareStringId
);
this.orders = new DataRepositoryImpl<Order,Long>(
order -> { return order.getId(); }, compareLongId
);
}
/*
* Public method to access singleton DataAccessImpl instance.
*/
public static DataAccess getInstance() {
return instance;
}
@Override
public void startup() {
this.customers.startup();
this.articles.startup();
this.orders.startup();
}
@Override
public void shutdown() {
this.orders.shutdown();
this.articles.shutdown();
this.customers.shutdown();
}
@Override
public DataRepository<Customer,String> customers() {
return this.customers;
}
@Override
public DataRepository<Article,String> articles() {
return this.articles;
}
@Override
public DataRepository<Order,Long> orders() {
return this.orders;
}
@Override
public Optional<MockDataBuilder> buildMockData() {
if( ! mockDataBuilder.isPresent() ) {
mockDataBuilder = Optional.of( new MockDataBuilderImpl( this ) );
}
return mockDataBuilder;
}
}
package data_access;
import java.util.Optional;
import system.ComponentLifecycle;
/**
* Interface of a generic entity repository modeled after Spring's CrudRepository. *
* @param <T> generic Type of objects stored in the repository
* @param <ID> generic Type of id‐attribute, e.g. String, Long, ...
*/
public interface DataRepository<T,ID> extends ComponentLifecycle {
<S extends T> S save( S entity );
<S extends T> Iterable<S> saveAll( Iterable<S> entities );
Optional<T> findById( ID id );
boolean existsById( ID id ); /* stub */
Iterable<T> findAll();
Iterable<T> findAllById( Iterable<ID> ids ); /* stub */
long count();
void deleteById( ID id ); /* stub */
void delete( T entity ); /* stub */
void deleteAll( Iterable<? extends T> entities ); /* stub */
void deleteAll();
}
package data_access;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;
class DataRepositoryImpl<T,ID> implements DataRepository<T,ID> {
private final List<T> list;
private final Function<T,ID> getId;
private final BiFunction<ID,ID,Boolean> compareId;
DataRepositoryImpl(
Function<T, ID> getId,
BiFunction<ID,ID,Boolean> compareId
){
this.list = new ArrayList<T>();
this.getId = getId;
this.compareId = compareId;
}
@Override
public <S extends T> S save(S entity) {
// TODO Auto-generated method stub
return null;
}
@Override
public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
// TODO Auto-generated method stub
return null;
}
@Override
public Optional<T> findById(ID id) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean existsById(ID id) {
// TODO Auto-generated method stub
return false;
}
@Override
public Iterable<T> findAll() {
// TODO Auto-generated method stub
return null;
}
@Override
public Iterable<T> findAllById(Iterable<ID> ids) {
// TODO Auto-generated method stub
return null;
}
@Override
public long count() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void deleteById(ID id) {
// TODO Auto-generated method stub
}
@Override
public void delete(T entity) {
// TODO Auto-generated method stub
}
@Override
public void deleteAll(Iterable<? extends T> entities) {
// TODO Auto-generated method stub
}
@Override
public void deleteAll() {
// TODO Auto-generated method stub
}
@Override
public void startup() {
// TODO Auto-generated method stub
}
@Override
public void shutdown() {
// TODO Auto-generated method stub
}
}
package data_access;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import data_access.DataAccess.MockDataBuilder;
import model.Article;
import model.Customer;
import model.Order;
import model.OrderItem;
/**
* Local singleton instance that implements the DataAccess.MockDataBuilder
* interface and that fills repositories with some mock data.
*
* @author sgra64
*
*/
class MockDataBuilderImpl implements DataAccess.MockDataBuilder {
private final DataAccess dataAccess;
private Customer cEric, cAnne, cNadine, cTimo, cSandra;
private Article a1, a2, a3, a4, a5, a6;
private static final Object[][] priceTable_Normal = new Object[][] {
{ "SKU-458362", 299 }, // Tasse
{ "SKU-693856", 149 }, // Becher
{ "SKU-518957", 2000 }, // Kanne
{ "SKU-638035", 649 }, // Teller
{ "SKU-386035", 2999 }, // Kaffeemaschine
{ "SKU-443803", 1999 }, // Teekocher
};
@SuppressWarnings("unused")
private static final Object[][] priceTable_Discount = new Object[][] {
{ "SKU-458362", 249 }, // Tasse
{ "SKU-693856", 99 }, // Becher
{ "SKU-518957", 1499 }, // Kanne
{ "SKU-638035", 499 }, // Teller
{ "SKU-386035", 2499 }, // Kaffeemaschine
{ "SKU-443803", 1499 }, // Teekocher
};
private Object[][] priceTable = priceTable_Normal;
/*
* Local constructor.
*/
public MockDataBuilderImpl( DataAccess dataAccess ) {
this.dataAccess = dataAccess;
}
@Override
public void buildCustomers() {
if( dataAccess.customers().count() <= 0 ) {
cEric = new Customer( "C86516", "Eric Schulz-Mueller", "eric2346@gmail.com" );
cAnne = new Customer( "C64327", "Meyer, Anne", "+4917223524" );
cNadine = new Customer( "C12396", "Nadine Ulla Blumenfeld", "+4915292454" );
/*
* Additional orders from task B1(4). Not part of handout.
*/
cTimo = new Customer( "C16865", "Timo Werner", "tw@gmail.com" );
cSandra = new Customer( "C66865", "Sandra Mueller", "samue62@gmx.de" );
dataAccess.customers().saveAll( List.of( cEric, cAnne, cNadine, cTimo, cSandra ) );
}
}
@Override
public void buildArticless() {
if( dataAccess.articles().count() <= 0 ) {
a1 = new Article( "SKU-458362", "Tasse", 299, 2000 );
a2 = new Article( "SKU-693856", "Becher", 149, 8400 );
a3 = new Article( "SKU-518957", "Kanne", 2000, 2400 );
a4 = new Article( "SKU-638035", "Teller", 649, 7000 );
a5 = new Article( "SKU-386035", "Kaffeemaschine", 2999, 500 );
a6 = new Article( "SKU-443803", "Teekocher", 1999, 2000 );
adjustPrice( a1, priceTable );
adjustPrice( a2, priceTable );
adjustPrice( a3, priceTable );
adjustPrice( a4, priceTable );
adjustPrice( a5, priceTable );
adjustPrice( a6, priceTable );
dataAccess.articles().saveAll( List.of( a1, a2, a3, a4, a5, a6 ) );
}
}
@Override
public void buildOrders( Consumer<Order> fillOrder ) {
// Eric's 1st order
Order o5234 = new Order( 5234968294L, new Date(), cEric );
OrderItem oi1 = new OrderItem( a3.getDescription(), a3, 1 ); // 1 Kanne (a3)
o5234.addItem( oi1 ); // add OrderItem to Order 5234968294L
// Eric's 2nd order
Order o8592 = new Order( 8592356245L, new Date(), cEric );
o8592.addItem( // add three OrderItems to Order 8592356245L
new OrderItem( a4.getDescription(), a4, 4 ) // 4 Teller (a4)
).addItem(
new OrderItem( a2.getDescription(), a2, 8 ) // 8 Becher (a2)
).addItem(
new OrderItem( "passende Tassen", a1, 4 ) // 4 passende Tassen (a1)
);
// Anne's order
Order o3563 = new Order( 3563561357L, new Date(), cAnne );
o3563.addItem(
new OrderItem( a3.getDescription() + " aus Porzellan", a3, 1 )
);
// Nadine's order
Order o6135 = new Order( 6135735635L, new Date(), cNadine );
o6135.addItem( // 12 Teller
new OrderItem( a4.getDescription() + " blau/weiss Keramik", a4, 12 )
);
Order o9235 = new Order( 9235856245L, new Date(), cTimo );
o9235.addItem(
new OrderItem( a5.getDescription(), a5, 1 ) // 1 Kaffeemaschine (a5)
).addItem(
new OrderItem( a1.getDescription(), a1, 6 ) // 4 Tassen (a1)
);
Order o7335 = new Order( 7335856245L, new Date(), cSandra );
o7335.addItem(
new OrderItem( a6.getDescription(), a6, 1 ) // 1 Teekocher (a6)
).addItem(
new OrderItem( a2.getDescription(), a2, 4 ) // 4 Becher (a2)
).addItem(
new OrderItem( a4.getDescription(), a4, 4 ) // 4 Teller (a4)
);
// Need to track orders
fillOrder.accept( o5234 );
fillOrder.accept( o8592 );
fillOrder.accept( o3563 );
fillOrder.accept( o6135 );
fillOrder.accept( o9235 );
fillOrder.accept( o7335 );
// build Nadine's 2nd order from existing data
Order o7356 = new Order( 7356613535L, new Date(), cNadine );
//Article teller = data.getArticle( 3 );
dataAccess.articles().findById( "SKU-638035" /*SKU for Teller*/ ).ifPresent( teller -> {
o7356.addItem( // 6 Teller
new OrderItem( teller.getDescription() + " blau/weiss Keramik", teller, 6 )
);
fillOrder.accept( o7356 );
});
dataAccess.orders().saveAll( List.of( o5234, o8592, o3563, o6135, o9235, o7335, o7356 ) );
}
/*
* Private methods.
*/
private void adjustPrice( Article article, Object[][] priceTable ) {
String id = article.getId();
for( Object[] line : priceTable ) {
String sku = (String)line[ 0 ];
if( sku.equals( id ) ) {
long price = (int)line[ 1 ];
article.setUnitPrice( price );
}
}
}
@Override
public Optional<MockDataBuilder> buildMockData() {
// TODO Auto-generated method stub
return null;
}
}
package system;
import data_access.DataAccess;
public class Builder implements ComponentLifecycle {
private static Builder instance = new Builder();
......
package system;
import java.util.List;
import model.Article;
import model.Customer;
import model.Order;
public interface DataAccess extends ComponentLifecycle {
public List<Customer> getCustomers();
public List<Article> getArticles();
public List<Order> getOrders();
public Customer getCustomer(int i);
public Article getArticle(int i);
public Order getOrder(int i);
public DataAccess add(Order order);
}
......@@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import data_access.DataAccess;
import model.Article;
import model.Customer;
import model.Order;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment