primary ports moved to model, repository ports introduced to model

This commit is contained in:
Michał Michaluk
2018-02-18 12:33:18 +01:00
parent 95d6b1ed14
commit df64659038
14 changed files with 130 additions and 53 deletions

View File

@@ -0,0 +1,25 @@
package pl.com.bottega.factory.demand.forecasting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.transaction.Transactional;
@Configuration
class DemandForecastingConfiguration {
@Autowired
private ProductDemandRepository repository;
@Bean
@Transactional
DemandService demandService() {
return new DemandService(repository);
}
@Bean
ReviewPolicy reviewPolicy() {
return ReviewPolicy.BASIC;
}
}

View File

@@ -23,16 +23,24 @@ import static java.util.stream.Collectors.toMap;
@Component
@AllArgsConstructor
class DemandORMRepository {
class ProductDemandORMRepository implements ProductDemandRepository {
private final Clock clock;
private final DemandEvents events;
private final ReviewPolicy reviewPolicy = ReviewPolicy.BASIC;
private final ReviewPolicy reviewPolicy;
private final EntityManager em;
private final ProductDemandDao rootDao;
private final DemandDao demandDao;
ProductDemand get(String refNo) {
@Override
public void initDemandsFor(String refNo) {
if (rootDao.findByRefNo(refNo) == null) {
rootDao.save(new ProductDemandEntity(refNo));
}
}
@Override
public ProductDemand get(String refNo) {
ProductDemandEntity root = rootDao.findByRefNo(refNo);
RefNoId id = root.createId();
@@ -48,23 +56,8 @@ class DemandORMRepository {
return new ProductDemand(id, demands, clock, events);
}
private DailyDemand map(String refNo, LocalDate date,
Map<LocalDate, DemandEntity> data) {
return ofNullable(data.get(date))
.map(entity -> new DailyDemand(
entity.createId(),
reviewPolicy,
entity.getValue().getDocumented(),
entity.getValue().getAdjustment()))
.orElseGet(() -> new DailyDemand(
new DemandEntityId(refNo, date),
reviewPolicy,
null,
null
));
}
void save(ProductDemand model) {
@Override
public void save(ProductDemand model) {
ProductDemandEntity root = rootDao.findOne(TechnicalId.get(model.id));
if (model.updates.size() > 0) {
em.lock(root, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
@@ -87,9 +80,19 @@ class DemandORMRepository {
}
}
void initDemandsFor(String refNo) {
if (rootDao.findByRefNo(refNo) == null) {
rootDao.save(new ProductDemandEntity(refNo));
}
private DailyDemand map(String refNo, LocalDate date,
Map<LocalDate, DemandEntity> data) {
return ofNullable(data.get(date))
.map(entity -> new DailyDemand(
entity.createId(),
reviewPolicy,
entity.getValue().getDocumented(),
entity.getValue().getAdjustment()))
.orElseGet(() -> new DailyDemand(
new DemandEntityId(refNo, date),
reviewPolicy,
null,
null
));
}
}

View File

@@ -19,7 +19,7 @@ import java.time.ZoneId
@SpringBootTest
@Transactional
@Commit
class DemandORMRepositoryTest extends Specification {
class ProductDemandORMRepositoryTest extends Specification {
def clock = Clock.fixed(Instant.now(), ZoneId.systemDefault())
def events = Mock(DemandEvents)
@@ -30,7 +30,7 @@ class DemandORMRepositoryTest extends Specification {
@Autowired
DemandDao demandDao
DemandORMRepository repository
ProductDemandORMRepository repository
final def today = LocalDate.now(clock)
final def refNo = "3009000"
@@ -38,7 +38,14 @@ class DemandORMRepositoryTest extends Specification {
def setup() {
demandDao.deleteAllInBatch()
rootDao.deleteAllInBatch()
repository = new DemandORMRepository(clock, events, em, rootDao, demandDao)
repository = new ProductDemandORMRepository(
clock,
events,
ReviewPolicy.BASIC,
em,
rootDao,
demandDao
)
}
def "persists new demand"() {

View File

@@ -1,17 +1,12 @@
package pl.com.bottega.factory.demand.forecasting;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import pl.com.bottega.factory.demand.forecasting.ReviewRequired.ToReview;
import javax.transaction.Transactional;
@Service
@Transactional
@AllArgsConstructor
public class DemandService {
private final DemandORMRepository repository;
private final ProductDemandRepository repository;
public void init(String refNo) {
repository.initDemandsFor(refNo);

View File

@@ -0,0 +1,9 @@
package pl.com.bottega.factory.demand.forecasting;
interface ProductDemandRepository {
ProductDemand get(String refNo);
void save(ProductDemand model);
void initDemandsFor(String refNo);
}

View File

@@ -0,0 +1,32 @@
package pl.com.bottega.factory.shortages.prediction.monitoring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import pl.com.bottega.factory.shortages.prediction.ConfigurationParams;
import javax.transaction.Transactional;
@Configuration
class MonitoringConfiguration {
@Autowired
private ShortagePredictionProcessRepository repository;
@Bean
@Transactional
ShortagePredictionService shortagePredictionService() {
return new ShortagePredictionService(repository);
}
@Bean
ShortageDiffPolicy policy() {
return ShortageDiffPolicy.ValuesAreNotSame;
}
@Bean
ConfigurationParams configuration() {
return () -> 14;
}
}

View File

@@ -3,7 +3,7 @@ package pl.com.bottega.factory.shortages.prediction.monitoring;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import pl.com.bottega.factory.product.management.RefNoId;
import pl.com.bottega.factory.shortages.prediction.Configuration;
import pl.com.bottega.factory.shortages.prediction.ConfigurationParams;
import pl.com.bottega.factory.shortages.prediction.calculation.ShortageForecasts;
import pl.com.bottega.factory.shortages.prediction.monitoring.persistence.ShortagesDao;
import pl.com.bottega.factory.shortages.prediction.monitoring.persistence.ShortagesEntity;
@@ -13,15 +13,16 @@ import java.util.Optional;
@Component
@AllArgsConstructor
class ShortagePredictionProcessORMRepository {
class ShortagePredictionProcessORMRepository implements ShortagePredictionProcessRepository {
private final ShortagesDao dao;
private final ShortageDiffPolicy policy = ShortageDiffPolicy.ValuesAreNotSame;
private final ShortageForecasts forecasts;
private final Configuration configuration = () -> 14;
private final ConfigurationParams configuration = () -> 14;
private final ShortageEvents events;
ShortagePredictionProcess get(RefNoId refNo) {
@Override
public ShortagePredictionProcess get(RefNoId refNo) {
Optional<ShortagesEntity> entity = dao.findByRefNo(refNo.getRefNo());
return new ShortagePredictionProcess(
entity.map(ShortagesEntity::createId)
@@ -31,7 +32,8 @@ class ShortagePredictionProcessORMRepository {
);
}
void save(ShortagePredictionProcess model) {
@Override
public void save(ShortagePredictionProcess model) {
// persisted after event
}

View File

@@ -3,6 +3,6 @@ package pl.com.bottega.factory.shortages.prediction;
/**
* Created by michal on 02.02.2017.
*/
public interface Configuration {
public interface ConfigurationParams {
int shortagePredictionDaysAhead();
}

View File

@@ -2,7 +2,7 @@ package pl.com.bottega.factory.shortages.prediction.monitoring;
import lombok.AllArgsConstructor;
import pl.com.bottega.factory.product.management.RefNoId;
import pl.com.bottega.factory.shortages.prediction.Configuration;
import pl.com.bottega.factory.shortages.prediction.ConfigurationParams;
import pl.com.bottega.factory.shortages.prediction.Shortage;
import pl.com.bottega.factory.shortages.prediction.calculation.ShortageForecast;
import pl.com.bottega.factory.shortages.prediction.calculation.ShortageForecasts;
@@ -21,7 +21,7 @@ class ShortagePredictionProcess {
private final ShortageDiffPolicy diffPolicy;
private final ShortageForecasts forecasts;
private final Configuration configuration;
private final ConfigurationParams configuration;
private final ShortageEvents events;
void onDemandChanged() {

View File

@@ -0,0 +1,9 @@
package pl.com.bottega.factory.shortages.prediction.monitoring;
import pl.com.bottega.factory.product.management.RefNoId;
interface ShortagePredictionProcessRepository {
ShortagePredictionProcess get(RefNoId refNo);
void save(ShortagePredictionProcess model);
}

View File

@@ -1,17 +1,12 @@
package pl.com.bottega.factory.shortages.prediction.monitoring;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import pl.com.bottega.factory.demand.forecasting.DemandedLevelsChanged;
import javax.transaction.Transactional;
@Service
@Transactional
@AllArgsConstructor
public class ShortagePredictionService {
private final ShortagePredictionProcessORMRepository repository;
private final ShortagePredictionProcessRepository repository;
public void predictShortages(DemandedLevelsChanged event) {
ShortagePredictionProcess model = repository.get(event.getRefNo());

View File

@@ -5,7 +5,7 @@ import pl.com.bottega.factory.shortages.prediction.Shortage;
/**
* Created by michal on 02.02.2017.
*/
public interface Notifications {
interface Notifications {
void alertPlanner(Shortage shortage);
void softNotifyPlanner(Shortage shortage);

View File

@@ -1,8 +1,8 @@
package pl.com.bottega.factory.shortages.prediction.monitoring
import pl.com.bottega.factory.shortages.prediction.Configuration
import pl.com.bottega.factory.shortages.prediction.ConfigurationParams
class InMemoryConfiguration implements Configuration {
class InMemoryConfigurationParams implements ConfigurationParams {
int daysAhead;
@Override

View File

@@ -1,7 +1,7 @@
package pl.com.bottega.factory.shortages.prediction.monitoring
import pl.com.bottega.factory.product.management.RefNoId
import pl.com.bottega.factory.shortages.prediction.Configuration
import pl.com.bottega.factory.shortages.prediction.ConfigurationParams
import pl.com.bottega.factory.shortages.prediction.Shortage
import pl.com.bottega.factory.shortages.prediction.calculation.ShortageForecasts
import pl.com.bottega.factory.shortages.prediction.calculation.ShortagesCalculationAssembler
@@ -166,8 +166,8 @@ class ShortagePredictionProcessSpec extends Specification {
)
}
Configuration defaultConfig() {
new InMemoryConfiguration(daysAhead: 14)
ConfigurationParams defaultConfig() {
new InMemoryConfigurationParams(daysAhead: 14)
}
NewShortage newShortage(After after, Map<LocalDateTime, Long> missing) {