Skip to content

Commit dc1ca55

Browse files
committed
archetypes: contracts module - better
1 parent 72ce9f0 commit dc1ca55

File tree

13 files changed

+94
-27
lines changed

13 files changed

+94
-27
lines changed

src/main/java/io/legacyfighter/cabs/contracts/application/acme/dynamic/DocumentResourceManager.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public DocumentOperationResult createDocument(Long authorId){
4848
return generateDocumentOperationResult(DocumentOperationResult.Result.SUCCESS, state);
4949
}
5050

51-
public DocumentOperationResult changeState(Long documentId, String desiredState, Map<String, Object> params){
51+
public DocumentOperationResult changeState(Long documentId, String desiredState,
52+
Map<String, Object> params){
5253
DocumentHeader documentHeader = documentHeaderRepository.getOne(documentId);
5354
StateConfig stateConfig = assembler.assemble();
5455
State state = stateConfig.recreate(documentHeader);
@@ -70,9 +71,11 @@ public DocumentOperationResult changeContent(Long headerId, ContentId contentVer
7071
return generateDocumentOperationResult(DocumentOperationResult.Result.SUCCESS, state);
7172
}
7273

73-
private DocumentOperationResult generateDocumentOperationResult(DocumentOperationResult.Result result, State state) {
74+
private DocumentOperationResult generateDocumentOperationResult(
75+
DocumentOperationResult.Result result, State state) {
7476
return new DocumentOperationResult(result, state.getDocumentHeader().getId(),
75-
state.getDocumentHeader().getDocumentNumber(), state.getStateDescriptor(), state.getDocumentHeader().getContentId(),
77+
state.getDocumentHeader().getDocumentNumber(), state.getStateDescriptor(),
78+
state.getDocumentHeader().getContentId(),
7679
extractPossibleTransitionsAndRules(state),
7780
state.isContentEditable(),
7881
extractContentChangePredicate(state));
@@ -88,13 +91,15 @@ private String extractContentChangePredicate(State state) {
8891
private Map<String, List<String>> extractPossibleTransitionsAndRules(State state) {
8992
Map<String, List<String>> transitionsAndRules = new HashMap<>();
9093

91-
Map<State, List<BiFunction<State, ChangeCommand, Boolean>>> stateChangePredicates = state.getStateChangePredicates();
94+
Map<State, List<BiFunction<State, ChangeCommand, Boolean>>> stateChangePredicates =
95+
state.getStateChangePredicates();
9296
for (State s : stateChangePredicates.keySet()){
9397
//transition to self is not important
9498
if (s.equals(state))
9599
continue;
96100

97-
List<BiFunction<State, ChangeCommand, Boolean>> predicates = stateChangePredicates.get(s);
101+
List<BiFunction<State, ChangeCommand, Boolean>> predicates =
102+
stateChangePredicates.get(s);
98103
List<String> ruleNames = new ArrayList<>();
99104
for (BiFunction<State, ChangeCommand, Boolean> predicate : predicates){
100105
ruleNames.add(predicate.getClass().getTypeName());

src/main/java/io/legacyfighter/cabs/contracts/model/state/dynamic/acme/AcmeContractStateAssembler.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,22 @@ public AcmeContractStateAssembler(ApplicationEventPublisher publisher) {
3434

3535
public StateConfig assemble(){
3636
StateBuilder builder = new StateBuilder();
37-
builder.beginWith(DRAFT).check(new ContentNotEmptyVerifier()).check(new AuthorIsNotAVerifier()).to(VERIFIED).action(new ChangeVerifier());
38-
builder.from(DRAFT).whenContentChanged().to(DRAFT);
37+
builder.beginWith(DRAFT)
38+
.check(new ContentNotEmptyVerifier())
39+
.check(new AuthorIsNotAVerifier())
40+
.to(VERIFIED).action(new ChangeVerifier());
41+
builder.from(DRAFT)
42+
.whenContentChanged()
43+
.to(DRAFT);
3944
//name of the "published" state and name of the DocumentPublished event are NOT correlated. These are two different domains, name similarity is just a coincidence
40-
builder.from(VERIFIED).check(new ContentNotEmptyVerifier()).to(PUBLISHED).action(new PublishEvent(DocumentPublished.class, publisher));
45+
builder.from(VERIFIED)
46+
.check(new ContentNotEmptyVerifier())
47+
.to(PUBLISHED).action(new PublishEvent(DocumentPublished.class, publisher));
4148
builder.from(VERIFIED).whenContentChanged().to(DRAFT);
4249
builder.from(DRAFT).to(ARCHIVED);
4350
builder.from(VERIFIED).to(ARCHIVED);
44-
builder.from(PUBLISHED).to(ARCHIVED).action(new PublishEvent(DocumentUnpublished.class, publisher));
51+
builder.from(PUBLISHED)
52+
.to(ARCHIVED).action(new PublishEvent(DocumentUnpublished.class, publisher));
4553
return builder;
4654
}
4755
}

src/main/java/io/legacyfighter/cabs/contracts/model/state/dynamic/config/predicates/statechange/AuthorIsNotAVerifier.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class AuthorIsNotAVerifier implements BiFunction<State, ChangeCommand, Bo
1212

1313
@Override
1414
public Boolean apply(State state, ChangeCommand command) {
15-
return ! command.getParam(PARAM_VERIFIER, Long.class).equals(state.getDocumentHeader().getAuthorId());
15+
return ! command.getParam(PARAM_VERIFIER, Long.class)
16+
.equals(state.getDocumentHeader().getAuthorId());
1617
}
1718
}

src/main/java/io/legacyfighter/cabs/party/model/party/PartyRelationshipRepository.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.util.Optional;
66

77
public interface PartyRelationshipRepository {
8-
PartyRelationship put(String partyRelationship, String partyARole, Party partyA, String partyBRole, Party partyB);
8+
PartyRelationship put(String partyRelationship,
9+
String partyARole, Party partyA,
10+
String partyBRole, Party partyB);
911
Optional<PartyRelationship> findRelationshipFor(PartyId id, String relationshipName);
1012
}

src/main/java/io/legacyfighter/cabs/repair/api/RepairProcess.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,33 @@ public RepairProcess(PartyMapper partyMapper){
2222
}
2323

2424
public ResolveResult resolve(RepairRequest repairRequest) {
25-
return partyMapper.mapRelation(repairRequest.getVehicle(), PartyRelationshipsDictionary.REPAIR.name())
25+
return partyMapper.mapRelation(repairRequest.getVehicle(),
26+
PartyRelationshipsDictionary.REPAIR.name())
2627
.map(RoleObjectFactory::from)
2728
.flatMap(rof -> rof.getRole(RoleForRepairer.class))
2829
.map(role -> role.handle(repairRequest))
29-
.map(repairingResult -> new ResolveResult(ResolveResult.Status.SUCCESS, repairingResult.getHandlingParty(), repairingResult.getTotalCost(), repairingResult.getHandledParts()))
30+
.map(repairingResult -> new ResolveResult(ResolveResult.Status.SUCCESS,
31+
repairingResult.getHandlingParty(), repairingResult.getTotalCost(),
32+
repairingResult.getHandledParts()))
3033
.orElseGet(() -> new ResolveResult(ResolveResult.Status.ERROR));
3134
}
3235

3336
public ResolveResult resolve_oldschool_version(RepairRequest repairRequest) {
3437
//who is responsible for repairing the vehicle
35-
Optional<PartyRelationship> relationship = partyMapper.mapRelation(repairRequest.getVehicle(), PartyRelationshipsDictionary.REPAIR.name());
38+
Optional<PartyRelationship> relationship = partyMapper.mapRelation(
39+
repairRequest.getVehicle(),
40+
PartyRelationshipsDictionary.REPAIR.name());
3641
if (relationship.isPresent()){
3742
RoleObjectFactory roleObjectFactory = RoleObjectFactory.from(relationship.get());
3843
//dynamically assigned rules
3944
Optional<RoleForRepairer> role = roleObjectFactory.getRole(RoleForRepairer.class);
4045
if (role.isPresent()) {
4146
//actual repair request handling
4247
RepairingResult repairingResult = role.get().handle(repairRequest);
43-
return new ResolveResult(ResolveResult.Status.SUCCESS, repairingResult.getHandlingParty(), repairingResult.getTotalCost(), repairingResult.getHandledParts());
48+
return new ResolveResult(ResolveResult.Status.SUCCESS,
49+
repairingResult.getHandlingParty(),
50+
repairingResult.getTotalCost(),
51+
repairingResult.getHandledParts());
4452
}
4553
}
4654
return new ResolveResult(ResolveResult.Status.ERROR);

src/main/java/io/legacyfighter/cabs/repair/legacy/user/CommonBaseAbstractUser.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ public JobResult doJob(CommonBaseAbstractJob job){
1515
if (job instanceof MaintenanceJob){
1616
return handle((MaintenanceJob) job);
1717
}
18+
if (job instanceof XJob){
19+
return handle((XJob) job);
20+
}
21+
1822
return defaultHandler(job);
1923
}
2024

25+
26+
2127
protected JobResult handle(RepairJob job) {
2228
return defaultHandler(job);
2329
}
@@ -29,4 +35,8 @@ protected JobResult handle(MaintenanceJob job) {
2935
protected JobResult defaultHandler(CommonBaseAbstractJob job){
3036
throw new IllegalArgumentException(getClass().getName() + " can not handle " + job.getClass().getName());
3137
}
38+
39+
protected JobResult handle(XJob job) {
40+
throw new IllegalArgumentException(getClass().getName() + " can not handle " + job.getClass().getName());
41+
}
3242
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
package io.legacyfighter.cabs.repair.legacy.user;
22

3+
import io.legacyfighter.cabs.repair.legacy.job.JobResult;
4+
35
public class EmployeeDriver extends CommonBaseAbstractUser{
6+
7+
@Override
8+
protected JobResult handle(XJob job) {
9+
return super.handle(job);
10+
}
411
}

src/main/java/io/legacyfighter/cabs/repair/legacy/user/EmployeeDriverWithLeasedCar.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ public class EmployeeDriverWithLeasedCar extends EmployeeDriver{
99

1010
@Override
1111
protected JobResult handle(RepairJob job) {
12-
return new JobResult(JobResult.Decision.REDIRECTION).addParam("shouldHandleBy", lasingCompanyId);
12+
return new JobResult(JobResult.Decision.REDIRECTION)
13+
.addParam("shouldHandleBy", lasingCompanyId);
14+
}
15+
16+
@Override
17+
protected JobResult handle(XJob job) {
18+
return super.handle(job);
1319
}
1420

1521
public Long getLasingCompanyId() {

src/main/java/io/legacyfighter/cabs/repair/legacy/user/EmployeeDriverWithOwnCar.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,17 @@ protected JobResult handle(RepairJob job) {
2121
Set<Parts> acceptedParts = new HashSet<>(job.getPartsToRepair());
2222
acceptedParts.retainAll(contract.getCoveredParts());
2323

24-
Money coveredCost = job.getEstimatedValue().percentage(contract.getCoverageRatio());
24+
Money coveredCost = job.getEstimatedValue()
25+
.percentage(contract.getCoverageRatio());
2526
Money totalCost = job.getEstimatedValue().subtract(coveredCost);
2627

27-
return new JobResult(JobResult.Decision.ACCEPTED).addParam("totalCost", totalCost).addParam("acceptedParts", acceptedParts);
28+
return new JobResult(JobResult.Decision.ACCEPTED)
29+
.addParam("totalCost", totalCost).addParam("acceptedParts", acceptedParts);
30+
}
31+
32+
@Override
33+
protected JobResult handle(XJob job) {
34+
return super.handle(job);
2835
}
2936

3037
public void setContract(SignedContract contract) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package io.legacyfighter.cabs.repair.legacy.user;
2+
3+
import io.legacyfighter.cabs.repair.legacy.job.CommonBaseAbstractJob;
4+
5+
public class XJob extends CommonBaseAbstractJob {
6+
}

0 commit comments

Comments
 (0)