Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
move QuoteRepository behind QuoteService.
  • Loading branch information
JaredGordon committed Sep 1, 2015
1 parent 2a53490 commit d47210f
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 40 deletions.
Expand Up @@ -16,28 +16,32 @@
package org.springframework.nanotrader.data.service;

import java.util.List;
import java.util.Set;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.nanotrader.data.domain.Quote;

public interface QuoteService {

public abstract long countAllQuotes();
long countAllQuotes();

void deleteQuote(Quote quote);

public abstract void deleteQuote(Quote quote);
Quote findQuote(Integer id);

public abstract Quote findQuote(Integer id);
List<Quote> findAllQuotes();

Page<Quote> findAllQuotes(PageRequest pageRequest);

public abstract List<Quote> findAllQuotes();
List<Quote> findBySymbolIn(Set<String> symbols);

List<Quote> findQuoteEntries(int firstResult, int maxResults);

public abstract List<Quote> findQuoteEntries(int firstResult, int maxResults);
void saveQuote(Quote quote);

Quote updateQuote(Quote quote);

public abstract void saveQuote(Quote quote);


public abstract Quote updateQuote(Quote quote);
Quote findBySymbol(String symbol);

}
Expand Up @@ -16,7 +16,11 @@
package org.springframework.nanotrader.data.service;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.nanotrader.data.domain.Quote;
import org.springframework.nanotrader.data.repository.QuoteRepository;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -58,4 +62,18 @@ public void saveQuote(Quote quote) {
public Quote updateQuote(Quote quote) {
return quoteRepository.save(quote);
}

public Quote findBySymbol(String symbol) {
return quoteRepository.findBySymbol(symbol);
}

@Override
public List<Quote> findBySymbolIn(Set<String> symbols) {
return quoteRepository.findBySymbolIn(symbols);
}

@Override
public Page<Quote> findAllQuotes(PageRequest pageRequest) {
return quoteRepository.findAll(pageRequest);
}
}
Expand Up @@ -48,7 +48,6 @@
import org.springframework.nanotrader.data.repository.MarketSummaryRepository;
import org.springframework.nanotrader.data.repository.OrderRepository;
import org.springframework.nanotrader.data.repository.PortfolioSummaryRepository;
import org.springframework.nanotrader.data.repository.QuoteRepository;
import org.springframework.nanotrader.data.util.FinancialUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -86,7 +85,7 @@ public class TradingServiceImpl implements TradingService {
private AccountRepository accountRepository;

@Autowired
private QuoteRepository quoteRepository;
private QuoteService quoteService;

@Autowired
private PortfolioSummaryRepository portfolioSummaryRepository;
Expand Down Expand Up @@ -283,7 +282,7 @@ public Order saveOrder(Order order) {
private Order buy(Order order) {

Account account = accountRepository.findOne(order.getAccountAccountid().getAccountid());
Quote quote = quoteRepository.findBySymbol(order.getQuote().getSymbol());
Quote quote = quoteService.findBySymbol(order.getQuote().getSymbol());
Holding holding = null;
// create order and persist
Order createdOrder = null;
Expand Down Expand Up @@ -314,7 +313,7 @@ private Order sell(Order order) {
throw new DataRetrievalFailureException("Attempted to sell holding"
+ order.getHoldingHoldingid().getHoldingid() + " which is already sold.");
}
Quote quote = quoteRepository.findBySymbol(holding.getQuoteSymbol());
Quote quote = quoteService.findBySymbol(holding.getQuoteSymbol());
// create order and persist

Order createdOrder = createOrder(order, account, holding, quote);
Expand Down Expand Up @@ -402,7 +401,7 @@ private void updateAccount(Order order) {
public void updateQuoteMarketData(String symbol, BigDecimal changeFactor, BigDecimal sharesTraded) {


Quote quote = quoteRepository.findBySymbol(symbol);
Quote quote = quoteService.findBySymbol(symbol);
Quote quoteToPublish = new Quote();
quoteToPublish.setCompanyname(quote.getCompanyname());
quoteToPublish.setQuoteid(quote.getQuoteid());
Expand Down Expand Up @@ -433,7 +432,7 @@ public void updateQuoteMarketData(String symbol, BigDecimal changeFactor, BigDec

@Transactional
public void updateQuote(Quote quote) {
quoteRepository.save(quote);
quoteService.saveQuote(quote);
}

@Override
Expand Down Expand Up @@ -538,22 +537,22 @@ private List<Order> processOrderResults(List<Order> orders, Integer accountId) {

@Override
public Quote findQuoteBySymbol(String symbol) {
return quoteRepository.findBySymbol(symbol);
return quoteService.findBySymbol(symbol);
}

@Override
public List<Quote> findQuotesBySymbols(Set<String> symbols) {
return quoteRepository.findBySymbolIn(symbols);
return quoteService.findBySymbolIn(symbols);
}

@Override
public List<Quote> findRandomQuotes(Integer count) {
return quoteRepository.findAll().subList(0, count.intValue());
return quoteService.findAllQuotes().subList(0, count.intValue());
}

@Override
public List<Quote> findAllQuotes() {
return quoteRepository.findAll();
return quoteService.findAllQuotes();
}

@Override
Expand All @@ -576,10 +575,10 @@ public PortfolioSummary findPortfolioSummary(Integer accountId) {
public MarketSummary findMarketSummary() {
MarketSummary marketSummary = marketSummaryRepository.findMarketSummary();
// get top losing stocks
Page<Quote> losers = quoteRepository.findAll(new PageRequest(0, TOP_N, new Sort(Direction.ASC, "change1")));
Page<Quote> losers = quoteService.findAllQuotes(new PageRequest(0, TOP_N, new Sort(Direction.ASC, "change1")));

// get top gaining stocks
Page<Quote> winners = quoteRepository.findAll(new PageRequest(0, TOP_N, new Sort(Direction.DESC, "change1")));
Page<Quote> winners = quoteService.findAllQuotes(new PageRequest(0, TOP_N, new Sort(Direction.DESC, "change1")));

List<Quote> topLosers = new ArrayList<Quote>(TOP_N);
for (Quote q : losers) {
Expand Down
Expand Up @@ -21,12 +21,13 @@
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.nanotrader.data.domain.Quote;
import org.springframework.nanotrader.data.repository.QuoteRepository;
import org.springframework.nanotrader.data.service.QuoteService;
import org.springframework.stereotype.Component;

Expand All @@ -42,9 +43,6 @@ public class QuoteDataOnDemand {
@Autowired
QuoteService quoteService;

@Autowired
QuoteRepository quoteRepository;

public Quote getNewTransientQuote(int index) {
Quote obj = new Quote();
setChange1(obj, index);
Expand Down Expand Up @@ -164,7 +162,6 @@ public void init() {
}
throw new RuntimeException(msg.toString(), e);
}
quoteRepository.flush();
data.add(obj);
}
}
Expand Down
@@ -1,14 +1,14 @@
package org.springframework.nanotrader.domain;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.nanotrader.data.domain.Quote;
import org.springframework.nanotrader.data.domain.test.QuoteDataOnDemand;
import org.springframework.nanotrader.data.repository.QuoteRepository;
import org.springframework.nanotrader.data.service.QuoteService;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Expand All @@ -31,9 +31,6 @@ public void testMarkerMethod() {
@Autowired
QuoteService quoteService;

@Autowired
QuoteRepository quoteRepository;

@Test
public void testCountAllQuotes() {
Assert.assertNotNull("Data on demand for 'Quote' failed to initialize correctly", dod.getRandomQuote());
Expand Down Expand Up @@ -81,7 +78,6 @@ public void testSaveQuote() {
Assert.assertNotNull("Data on demand for 'Quote' failed to provide a new transient entity", obj);
Assert.assertNull("Expected 'Quote' identifier to be null", obj.getQuoteid());
quoteService.saveQuote(obj);
quoteRepository.flush();
Assert.assertNotNull("Expected 'Quote' identifier to no longer be null", obj.getQuoteid());
}

Expand All @@ -93,7 +89,6 @@ public void testDeleteQuote() {
Assert.assertNotNull("Data on demand for 'Quote' failed to provide an identifier", id);
obj = quoteService.findQuote(id);
quoteService.deleteQuote(obj);
quoteRepository.flush();
Assert.assertNull("Failed to remove 'Quote' with identifier '" + id + "'", quoteService.findQuote(id));
}
}
Expand Up @@ -27,7 +27,7 @@
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.nanotrader.data.domain.Quote;
import org.springframework.nanotrader.data.repository.QuoteRepository;
import org.springframework.nanotrader.data.service.QuoteService;
import org.springframework.nanotrader.data.service.TradingService;
import org.springframework.nanotrader.data.service.TradingServiceImpl;
import org.springframework.test.context.ContextConfiguration;
Expand All @@ -48,7 +48,7 @@ public class QuotePublishingTests {
private TradingService tradingService;

@Autowired
QuoteRepository quoteRepository;
QuoteService quoteService;

@Before
public void setupMocks() {
Expand All @@ -59,7 +59,7 @@ public void setupMocks() {
quote.setOpen1(BigDecimal.valueOf(115.0));
quote.setHigh(BigDecimal.valueOf(130.0));
quote.setLow(BigDecimal.valueOf(1.0));
Mockito.when(quoteRepository.findBySymbol("VMW")).thenReturn(quote);
Mockito.when(quoteService.findBySymbol("VMW")).thenReturn(quote);
}

@Test
Expand Down
Expand Up @@ -45,7 +45,6 @@
import org.springframework.nanotrader.data.domain.test.OrderDataOnDemand;
import org.springframework.nanotrader.data.repository.AccountRepository;
import org.springframework.nanotrader.data.repository.HoldingRepository;
import org.springframework.nanotrader.data.repository.QuoteRepository;
import org.springframework.nanotrader.data.service.QuoteService;
import org.springframework.nanotrader.data.service.TradingService;
import org.springframework.test.context.ContextConfiguration;
Expand Down Expand Up @@ -82,9 +81,6 @@ public class TradingServiceTests {
@Autowired
HoldingRepository holdingRepository;

@Autowired
QuoteRepository quoteRepository;

@Autowired
QuoteService quoteService;

Expand Down
Expand Up @@ -36,13 +36,15 @@
<bean id="mockHARepo" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.nanotrader.data.repository.HoldingAggregateRepository"/>
</bean>

<bean id="mockChaosRepo" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.nanotrader.data.repository.ChaosProceduresRepository"/>
</bean>

<bean id="tradingService" class="org.springframework.nanotrader.data.service.TradingServiceImpl" />

<bean id="quoteService" class="org.springframework.nanotrader.data.service.QuoteServiceImpl" />

<import resource="classpath:/META-INF/spring/integration/amqp-data-outbound-context.xml" />

<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
Expand Down

0 comments on commit d47210f

Please sign in to comment.