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

@@ -1,37 +0,0 @@
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;
public void init(String refNo) {
repository.initDemandsFor(refNo);
}
public void process(Document document) {
ProductDemand model = repository.get(document.getRefNo());
model.process(document);
repository.save(model);
}
public void adjust(AdjustDemand adjustDemand) {
ProductDemand model = repository.get(adjustDemand.getRefNo());
model.adjust(adjustDemand);
repository.save(model);
}
public void review(ToReview review, ReviewDecision decision) {
ProductDemand model = repository.get(review.getRefNo());
model.review(review, decision);
repository.save(model);
}
}

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"() {