@k2works/claude-code-booster 3.2.0 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/assets/docs/article/index.md +4 -1
- package/lib/assets/docs/article/practical-database-design/index.md +121 -0
- package/lib/assets/docs/article/practical-database-design/part1/chapter01.md +288 -0
- package/lib/assets/docs/article/practical-database-design/part1/chapter02.md +518 -0
- package/lib/assets/docs/article/practical-database-design/part1/chapter03.md +557 -0
- package/lib/assets/docs/article/practical-database-design/part2/chapter04.md +924 -0
- package/lib/assets/docs/article/practical-database-design/part2/chapter05.md +1627 -0
- package/lib/assets/docs/article/practical-database-design/part2/chapter06.md +2716 -0
- package/lib/assets/docs/article/practical-database-design/part2/chapter07.md +2082 -0
- package/lib/assets/docs/article/practical-database-design/part2/chapter08.md +2105 -0
- package/lib/assets/docs/article/practical-database-design/part2/chapter09.md +2031 -0
- package/lib/assets/docs/article/practical-database-design/part2/chapter10.md +1387 -0
- package/lib/assets/docs/article/practical-database-design/part2/chapter11.md +1677 -0
- package/lib/assets/docs/article/practical-database-design/part2/chapter12.md +1417 -0
- package/lib/assets/docs/article/practical-database-design/part2/chapter13.md +1434 -0
- package/lib/assets/docs/article/practical-database-design/part3/chapter14.md +667 -0
- package/lib/assets/docs/article/practical-database-design/part3/chapter15.md +1625 -0
- package/lib/assets/docs/article/practical-database-design/part3/chapter16.md +1915 -0
- package/lib/assets/docs/article/practical-database-design/part3/chapter17.md +1708 -0
- package/lib/assets/docs/article/practical-database-design/part3/chapter18.md +2095 -0
- package/lib/assets/docs/article/practical-database-design/part3/chapter19.md +1123 -0
- package/lib/assets/docs/article/practical-database-design/part3/chapter20.md +1031 -0
- package/lib/assets/docs/article/practical-database-design/part3/chapter21.md +1382 -0
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter14-orm.md +991 -0
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter15-orm.md +1300 -0
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter16-orm.md +1166 -0
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter17-orm.md +1584 -0
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter18-orm.md +1183 -0
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter19-orm.md +1016 -0
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter20-orm.md +1753 -0
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter21-orm.md +1447 -0
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter22-orm.md +1878 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter22.md +965 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter23.md +2069 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter24.md +2439 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter25.md +3661 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter26.md +2916 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter27.md +3105 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter28.md +2697 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter29.md +2544 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter30.md +2180 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter31.md +1192 -0
- package/lib/assets/docs/article/practical-database-design/part4/chapter32.md +2101 -0
- package/lib/assets/docs/article/practical-database-design/part5/chapter33.md +1032 -0
- package/lib/assets/docs/article/practical-database-design/part5/chapter34.md +1609 -0
- package/lib/assets/docs/article/practical-database-design/part5/chapter35.md +1453 -0
- package/lib/assets/docs/article/practical-database-design/part5/chapter36.md +1292 -0
- package/lib/assets/docs/article/practical-database-design/part5/chapter37.md +1470 -0
- package/lib/assets/docs/article/practical-database-design/part5/chapter38.md +1698 -0
- package/lib/assets/docs/article/practical-database-design/part5/chapter39.md +2334 -0
- package/lib/assets/docs/article/practical-database-design/study/study2-1.md +1693 -0
- package/lib/assets/docs/article/practical-database-design/study/study2-2.md +1347 -0
- package/lib/assets/docs/article/practical-database-design/study/study2-3.md +2044 -0
- package/lib/assets/docs/article/practical-database-design/study/study2-4.md +2229 -0
- package/lib/assets/docs/article/practical-database-design/study/study2-5.md +2418 -0
- package/lib/assets/docs/article/practical-database-design/study/study3-1.md +2205 -0
- package/lib/assets/docs/article/practical-database-design/study/study3-2.md +2221 -0
- package/lib/assets/docs/article/practical-database-design/study/study3-3.md +2253 -0
- package/lib/assets/docs/article/practical-database-design/study/study3-4.md +2106 -0
- package/lib/assets/docs/article/practical-database-design/study/study3-5.md +2507 -0
- package/lib/assets/docs/article/practical-database-design/study/study4-1.md +2587 -0
- package/lib/assets/docs/article/practical-database-design/study/study4-2.md +2075 -0
- package/lib/assets/docs/article/practical-database-design/study/study4-3.md +1805 -0
- package/lib/assets/docs/article/practical-database-design/study/study4-4.md +1895 -0
- package/lib/assets/docs/article/practical-database-design/study/study4-5.md +2878 -0
- package/lib/assets/docs/reference//351/201/213/345/226/266/347/256/241/347/220/206.md +131 -39
- package/package.json +1 -1
|
@@ -0,0 +1,1878 @@
|
|
|
1
|
+
# 第22章:Axon CQRS/ES の実装【ORM版】
|
|
2
|
+
|
|
3
|
+
本章では、財務会計システムに **CQRS(Command Query Responsibility Segregation)** と **Event Sourcing** を実装します。Axon Framework を使用し、コマンド(書き込み)とクエリ(読み取り)を分離し、すべての状態変更をイベントとして記録するアーキテクチャを構築します。
|
|
4
|
+
|
|
5
|
+
研究 1 で構築したヘキサゴナルアーキテクチャの考え方を踏襲しつつ、**ドメインモデルをフレームワークから分離**し、Axon Aggregate Adapter を介して連携させます。Read Model の永続化には **Spring Data JPA** を使用します。
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 22.1 CQRS/ES アーキテクチャの基礎
|
|
10
|
+
|
|
11
|
+
### 22.1.1 CQRS とは
|
|
12
|
+
|
|
13
|
+
CQRS(Command Query Responsibility Segregation)は、コマンド(書き込み)とクエリ(読み取り)の責務を分離するアーキテクチャパターンです。
|
|
14
|
+
|
|
15
|
+
```plantuml
|
|
16
|
+
@startuml cqrs_architecture
|
|
17
|
+
skinparam componentStyle rectangle
|
|
18
|
+
skinparam backgroundColor #FEFEFE
|
|
19
|
+
|
|
20
|
+
title CQRS アーキテクチャ(財務会計システム JPA版)
|
|
21
|
+
|
|
22
|
+
package "Command Side (Write)" {
|
|
23
|
+
[REST Controller] --> [CommandGateway]
|
|
24
|
+
[CommandGateway] --> [Aggregate]
|
|
25
|
+
[Aggregate] --> [Event Store]
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
package "Query Side (Read)" {
|
|
29
|
+
[Event Store] --> [Projection]
|
|
30
|
+
[Projection] --> [Read Model DB]
|
|
31
|
+
[REST Controller] --> [JPA Repository]
|
|
32
|
+
[JPA Repository] --> [Read Model DB]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
@enduml
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
**CQRS の利点:**
|
|
39
|
+
|
|
40
|
+
| 観点 | 説明 |
|
|
41
|
+
|------|------|
|
|
42
|
+
| **スケーラビリティ** | 読み取りと書き込みを独立してスケール可能 |
|
|
43
|
+
| **パフォーマンス** | 読み取りに最適化されたモデルで高速クエリ |
|
|
44
|
+
| **複雑性の分離** | 書き込みロジックと読み取りロジックを独立して開発 |
|
|
45
|
+
| **監査対応** | Event Sourcing と組み合わせて完全な履歴を保持 |
|
|
46
|
+
|
|
47
|
+
**財務会計システムでの CQRS の意義:**
|
|
48
|
+
|
|
49
|
+
- **監査証跡**: すべての仕訳変更履歴を完全に保持
|
|
50
|
+
- **複雑なクエリ**: 試算表、財務諸表など多様なビューを効率的に生成
|
|
51
|
+
- **データ整合性**: イベントベースで一貫性を保証
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
### 22.1.2 Event Sourcing とは
|
|
56
|
+
|
|
57
|
+
Event Sourcing は、アプリケーションの状態をイベントの連続として保存するパターンです。
|
|
58
|
+
|
|
59
|
+
```plantuml
|
|
60
|
+
@startuml event_sourcing
|
|
61
|
+
skinparam componentStyle rectangle
|
|
62
|
+
skinparam backgroundColor #FEFEFE
|
|
63
|
+
|
|
64
|
+
[Command] --> [Aggregate] : 1. コマンド受信
|
|
65
|
+
[Aggregate] --> [Domain Model] : 2. ビジネスロジック実行
|
|
66
|
+
[Domain Model] --> [Event] : 3. イベント生成
|
|
67
|
+
[Event] --> [Event Store] : 4. イベント永続化
|
|
68
|
+
[Event Store] --> [Aggregate] : 5. イベント再生(状態復元)
|
|
69
|
+
|
|
70
|
+
@enduml
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
**Event Sourcing の利点:**
|
|
74
|
+
|
|
75
|
+
| 観点 | 説明 |
|
|
76
|
+
|------|------|
|
|
77
|
+
| **完全な履歴** | すべての状態変更が記録される |
|
|
78
|
+
| **監査証跡** | いつ、誰が、何を変更したかが明確 |
|
|
79
|
+
| **時間旅行** | 過去の任意の時点の状態を再構築可能 |
|
|
80
|
+
| **イベント駆動** | 他システムとの連携が容易 |
|
|
81
|
+
|
|
82
|
+
**財務会計システムでの Event Sourcing の意義:**
|
|
83
|
+
|
|
84
|
+
- **訂正仕訳の追跡**: 修正履歴を完全に保持
|
|
85
|
+
- **期末決算処理**: 任意の時点での残高を再計算可能
|
|
86
|
+
- **内部統制**: SOX 法対応の監査証跡を自動生成
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
### 22.1.3 ヘキサゴナルアーキテクチャとの統合
|
|
91
|
+
|
|
92
|
+
```plantuml
|
|
93
|
+
@startuml hexagonal_cqrs
|
|
94
|
+
!define RECTANGLE class
|
|
95
|
+
skinparam backgroundColor #FEFEFE
|
|
96
|
+
|
|
97
|
+
package "Hexagonal Architecture (CQRS/ES JPA版)" {
|
|
98
|
+
|
|
99
|
+
package "Inbound Adapters" {
|
|
100
|
+
[REST Controller]
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
package "Application Core" {
|
|
104
|
+
package "Application Layer" {
|
|
105
|
+
[Aggregate Adapter]
|
|
106
|
+
[Projection]
|
|
107
|
+
[Policy Handler]
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
package "Domain Layer" {
|
|
111
|
+
[Domain Model]
|
|
112
|
+
[Commands]
|
|
113
|
+
[Value Objects]
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
package "API Layer" {
|
|
117
|
+
[Events]
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
package "Outbound Adapters" {
|
|
122
|
+
[JPA Repository]
|
|
123
|
+
[Event Store]
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
[REST Controller] --> [Aggregate Adapter]
|
|
128
|
+
[Aggregate Adapter] --> [Domain Model]
|
|
129
|
+
[Projection] --> [JPA Repository]
|
|
130
|
+
|
|
131
|
+
note top of [Domain Model]
|
|
132
|
+
純粋なドメインモデル
|
|
133
|
+
Axon Framework に依存しない
|
|
134
|
+
ビジネスロジックのみ
|
|
135
|
+
end note
|
|
136
|
+
|
|
137
|
+
note left of [Aggregate Adapter]
|
|
138
|
+
Axon 用アダプター
|
|
139
|
+
@Aggregate, @CommandHandler
|
|
140
|
+
フレームワーク依存を吸収
|
|
141
|
+
end note
|
|
142
|
+
|
|
143
|
+
@enduml
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
**設計原則:**
|
|
147
|
+
|
|
148
|
+
1. **ドメインモデルの純粋性**: ドメインモデルは Axon に依存しない純粋な Java コード
|
|
149
|
+
2. **Aggregate Adapter**: Axon Framework 用のアダプターを Application Layer に配置
|
|
150
|
+
3. **イベントは公開 API**: イベントは他の Context から参照される公開 API として定義
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
## 22.2 技術スタック
|
|
155
|
+
|
|
156
|
+
| カテゴリ | 技術 | バージョン |
|
|
157
|
+
|---------|------|-----------|
|
|
158
|
+
| 言語 | Java | 21 |
|
|
159
|
+
| フレームワーク | Spring Boot | 3.4.1 |
|
|
160
|
+
| CQRS/ES | Axon Framework | 4.10.3 |
|
|
161
|
+
| ORM | Spring Data JPA | 3.4.1 |
|
|
162
|
+
| API ドキュメント | SpringDoc OpenAPI | 2.7.0 |
|
|
163
|
+
| データベース | H2 (開発) / PostgreSQL (本番) | - |
|
|
164
|
+
|
|
165
|
+
### 22.2.1 build.gradle.kts
|
|
166
|
+
|
|
167
|
+
<details>
|
|
168
|
+
<summary>コード例: build.gradle.kts</summary>
|
|
169
|
+
|
|
170
|
+
```kotlin
|
|
171
|
+
dependencies {
|
|
172
|
+
// Spring Boot
|
|
173
|
+
implementation("org.springframework.boot:spring-boot-starter-web")
|
|
174
|
+
implementation("org.springframework.boot:spring-boot-starter-validation")
|
|
175
|
+
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
|
|
176
|
+
|
|
177
|
+
// Axon Framework
|
|
178
|
+
implementation("org.axonframework:axon-spring-boot-starter:4.10.3") {
|
|
179
|
+
exclude(group = "org.axonframework", module = "axon-server-connector")
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// OpenAPI
|
|
183
|
+
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.7.0")
|
|
184
|
+
|
|
185
|
+
// Database
|
|
186
|
+
runtimeOnly("com.h2database:h2")
|
|
187
|
+
runtimeOnly("org.postgresql:postgresql")
|
|
188
|
+
|
|
189
|
+
// Test
|
|
190
|
+
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
|
191
|
+
testImplementation("org.axonframework:axon-test:4.10.3")
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
</details>
|
|
196
|
+
|
|
197
|
+
---
|
|
198
|
+
|
|
199
|
+
### 22.2.2 ディレクトリ構造
|
|
200
|
+
|
|
201
|
+
<details>
|
|
202
|
+
<summary>コード例: ディレクトリ構成</summary>
|
|
203
|
+
|
|
204
|
+
```
|
|
205
|
+
src/main/java/com/example/accounting/
|
|
206
|
+
├── app/ # アプリケーション共通
|
|
207
|
+
│ └── RootController.java
|
|
208
|
+
├── config/ # 設定クラス
|
|
209
|
+
│ ├── AxonConfig.java
|
|
210
|
+
│ ├── JpaConfig.java
|
|
211
|
+
│ └── OpenApiConfig.java
|
|
212
|
+
├── journal/ # Journal Bounded Context(仕訳)
|
|
213
|
+
│ ├── api/
|
|
214
|
+
│ │ └── events/ # 公開イベント API
|
|
215
|
+
│ │ ├── JournalEntryEvent.java # sealed interface
|
|
216
|
+
│ │ ├── JournalEntryCreatedEvent.java
|
|
217
|
+
│ │ ├── JournalEntryApprovedEvent.java
|
|
218
|
+
│ │ └── JournalEntryPostedEvent.java
|
|
219
|
+
│ ├── adapter/
|
|
220
|
+
│ │ ├── inbound/rest/journals/ # Inbound Adapter (REST)
|
|
221
|
+
│ │ │ ├── JournalsController.java
|
|
222
|
+
│ │ │ └── protocol/ # Request/Response DTO
|
|
223
|
+
│ │ └── outbound/persistence/ # Outbound Adapter (JPA)
|
|
224
|
+
│ │ ├── entity/
|
|
225
|
+
│ │ │ ├── JournalEntryEntity.java
|
|
226
|
+
│ │ │ └── JournalLineEntity.java
|
|
227
|
+
│ │ └── repository/
|
|
228
|
+
│ │ └── JournalEntryJpaRepository.java
|
|
229
|
+
│ ├── application/
|
|
230
|
+
│ │ ├── aggregate/ # Axon Aggregate Adapter
|
|
231
|
+
│ │ │ └── JournalEntryAggregateAdapter.java
|
|
232
|
+
│ │ ├── policy/ # イベントハンドラー(Choreography)
|
|
233
|
+
│ │ │ └── AccountEventHandler.java
|
|
234
|
+
│ │ └── query/ # Projection
|
|
235
|
+
│ │ └── JournalEntryProjection.java
|
|
236
|
+
│ └── domain/
|
|
237
|
+
│ └── model/aggregate/journal/ # 純粋なドメインモデル
|
|
238
|
+
│ ├── JournalEntry.java
|
|
239
|
+
│ ├── JournalEntryCommands.java
|
|
240
|
+
│ ├── JournalEntryStatus.java
|
|
241
|
+
│ └── JournalLine.java
|
|
242
|
+
└── account/ # Account Bounded Context(勘定科目)
|
|
243
|
+
├── api/events/
|
|
244
|
+
│ ├── AccountEvent.java
|
|
245
|
+
│ └── AccountBalanceUpdatedEvent.java
|
|
246
|
+
├── application/
|
|
247
|
+
│ ├── aggregate/
|
|
248
|
+
│ │ └── AccountAggregateAdapter.java
|
|
249
|
+
│ └── policy/
|
|
250
|
+
│ └── JournalEventHandler.java
|
|
251
|
+
└── domain/model/aggregate/account/
|
|
252
|
+
├── Account.java
|
|
253
|
+
└── AccountCommands.java
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
</details>
|
|
257
|
+
|
|
258
|
+
---
|
|
259
|
+
|
|
260
|
+
## 22.3 Axon 設定クラス(JPA版)
|
|
261
|
+
|
|
262
|
+
### 22.3.1 AxonConfig.java
|
|
263
|
+
|
|
264
|
+
<details>
|
|
265
|
+
<summary>コード例: AxonConfig.java</summary>
|
|
266
|
+
|
|
267
|
+
```java
|
|
268
|
+
package com.example.accounting.config;
|
|
269
|
+
|
|
270
|
+
import org.axonframework.common.jpa.EntityManagerProvider;
|
|
271
|
+
import org.axonframework.eventsourcing.eventstore.EmbeddedEventStore;
|
|
272
|
+
import org.axonframework.eventsourcing.eventstore.EventStorageEngine;
|
|
273
|
+
import org.axonframework.eventsourcing.eventstore.EventStore;
|
|
274
|
+
import org.axonframework.eventsourcing.eventstore.jpa.JpaEventStorageEngine;
|
|
275
|
+
import org.axonframework.serialization.Serializer;
|
|
276
|
+
import org.axonframework.serialization.json.JacksonSerializer;
|
|
277
|
+
import org.axonframework.spring.config.SpringAxonConfiguration;
|
|
278
|
+
import org.springframework.context.annotation.Bean;
|
|
279
|
+
import org.springframework.context.annotation.Configuration;
|
|
280
|
+
|
|
281
|
+
import javax.sql.DataSource;
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Axon Framework 設定(JPA版)
|
|
285
|
+
*/
|
|
286
|
+
@Configuration
|
|
287
|
+
public class AxonConfig {
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* JPA ベースの Event Storage Engine
|
|
291
|
+
*/
|
|
292
|
+
@Bean
|
|
293
|
+
public EventStorageEngine eventStorageEngine(
|
|
294
|
+
EntityManagerProvider entityManagerProvider,
|
|
295
|
+
Serializer serializer) {
|
|
296
|
+
return JpaEventStorageEngine.builder()
|
|
297
|
+
.entityManagerProvider(entityManagerProvider)
|
|
298
|
+
.snapshotSerializer(serializer)
|
|
299
|
+
.eventSerializer(serializer)
|
|
300
|
+
.build();
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Event Store
|
|
305
|
+
*/
|
|
306
|
+
@Bean
|
|
307
|
+
public EventStore eventStore(EventStorageEngine eventStorageEngine) {
|
|
308
|
+
return EmbeddedEventStore.builder()
|
|
309
|
+
.storageEngine(eventStorageEngine)
|
|
310
|
+
.build();
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* JSON シリアライザー
|
|
315
|
+
*/
|
|
316
|
+
@Bean
|
|
317
|
+
public Serializer eventSerializer() {
|
|
318
|
+
return JacksonSerializer.defaultSerializer();
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
</details>
|
|
324
|
+
|
|
325
|
+
### 22.3.2 JpaConfig.java
|
|
326
|
+
|
|
327
|
+
<details>
|
|
328
|
+
<summary>コード例: JpaConfig.java</summary>
|
|
329
|
+
|
|
330
|
+
```java
|
|
331
|
+
package com.example.accounting.config;
|
|
332
|
+
|
|
333
|
+
import org.springframework.context.annotation.Configuration;
|
|
334
|
+
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
|
335
|
+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* JPA 設定
|
|
339
|
+
*/
|
|
340
|
+
@Configuration
|
|
341
|
+
@EnableJpaRepositories(basePackages = {
|
|
342
|
+
"com.example.accounting.journal.adapter.outbound.persistence.repository",
|
|
343
|
+
"com.example.accounting.account.adapter.outbound.persistence.repository"
|
|
344
|
+
})
|
|
345
|
+
@EnableJpaAuditing
|
|
346
|
+
public class JpaConfig {
|
|
347
|
+
}
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
</details>
|
|
351
|
+
|
|
352
|
+
---
|
|
353
|
+
|
|
354
|
+
## 22.4 ドメインモデルとイベント設計
|
|
355
|
+
|
|
356
|
+
### 22.4.1 Journal Bounded Context
|
|
357
|
+
|
|
358
|
+
仕訳の作成から転記完了までのライフサイクルを管理します。
|
|
359
|
+
|
|
360
|
+
#### 状態遷移図
|
|
361
|
+
|
|
362
|
+
```plantuml
|
|
363
|
+
@startuml journal_state
|
|
364
|
+
skinparam backgroundColor #FEFEFE
|
|
365
|
+
|
|
366
|
+
title 仕訳(Journal Entry)の状態遷移
|
|
367
|
+
|
|
368
|
+
[*] --> DRAFT : JournalEntryCreated
|
|
369
|
+
DRAFT --> PENDING_APPROVAL : JournalEntrySubmitted
|
|
370
|
+
DRAFT --> CANCELLED : JournalEntryCancelled
|
|
371
|
+
PENDING_APPROVAL --> APPROVED : JournalEntryApproved
|
|
372
|
+
PENDING_APPROVAL --> REJECTED : JournalEntryRejected
|
|
373
|
+
PENDING_APPROVAL --> CANCELLED : JournalEntryCancelled
|
|
374
|
+
APPROVED --> POSTED : JournalEntryPosted
|
|
375
|
+
REJECTED --> DRAFT : JournalEntryRevised
|
|
376
|
+
|
|
377
|
+
@enduml
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
#### イベント一覧
|
|
381
|
+
|
|
382
|
+
| イベント | 説明 |
|
|
383
|
+
|---------|------|
|
|
384
|
+
| `JournalEntryCreatedEvent` | 仕訳が作成された |
|
|
385
|
+
| `JournalEntrySubmittedEvent` | 仕訳が承認申請された |
|
|
386
|
+
| `JournalEntryApprovedEvent` | 仕訳が承認された |
|
|
387
|
+
| `JournalEntryRejectedEvent` | 仕訳が却下された |
|
|
388
|
+
| `JournalEntryRevisedEvent` | 却下された仕訳が修正された |
|
|
389
|
+
| `JournalEntryPostedEvent` | 仕訳が転記された(元帳に反映) |
|
|
390
|
+
| `JournalEntryCancelledEvent` | 仕訳がキャンセルされた |
|
|
391
|
+
|
|
392
|
+
---
|
|
393
|
+
|
|
394
|
+
### 22.4.2 イベント定義(API Layer)
|
|
395
|
+
|
|
396
|
+
#### sealed interface によるイベントの型安全性
|
|
397
|
+
|
|
398
|
+
<details>
|
|
399
|
+
<summary>コード例: JournalEntryEvent.java</summary>
|
|
400
|
+
|
|
401
|
+
```java
|
|
402
|
+
package com.example.accounting.journal.api.events;
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* JournalEntry イベントの sealed interface
|
|
406
|
+
* すべての JournalEntry イベントの親インターフェース
|
|
407
|
+
*/
|
|
408
|
+
public sealed interface JournalEntryEvent permits
|
|
409
|
+
JournalEntryCreatedEvent,
|
|
410
|
+
JournalEntrySubmittedEvent,
|
|
411
|
+
JournalEntryApprovedEvent,
|
|
412
|
+
JournalEntryRejectedEvent,
|
|
413
|
+
JournalEntryRevisedEvent,
|
|
414
|
+
JournalEntryPostedEvent,
|
|
415
|
+
JournalEntryCancelledEvent {
|
|
416
|
+
|
|
417
|
+
String journalEntryId();
|
|
418
|
+
}
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
</details>
|
|
422
|
+
|
|
423
|
+
#### イベント record の実装
|
|
424
|
+
|
|
425
|
+
<details>
|
|
426
|
+
<summary>コード例: JournalEntryCreatedEvent.java</summary>
|
|
427
|
+
|
|
428
|
+
```java
|
|
429
|
+
package com.example.accounting.journal.api.events;
|
|
430
|
+
|
|
431
|
+
import java.math.BigDecimal;
|
|
432
|
+
import java.time.LocalDate;
|
|
433
|
+
import java.time.LocalDateTime;
|
|
434
|
+
import java.util.List;
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* 仕訳作成イベント
|
|
438
|
+
*/
|
|
439
|
+
public record JournalEntryCreatedEvent(
|
|
440
|
+
String journalEntryId,
|
|
441
|
+
String journalNumber,
|
|
442
|
+
LocalDate transactionDate,
|
|
443
|
+
String description,
|
|
444
|
+
List<JournalLineData> lines,
|
|
445
|
+
String createdBy,
|
|
446
|
+
LocalDateTime createdAt
|
|
447
|
+
) implements JournalEntryEvent {
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* 仕訳明細データ
|
|
451
|
+
*/
|
|
452
|
+
public record JournalLineData(
|
|
453
|
+
String accountCode,
|
|
454
|
+
String accountName,
|
|
455
|
+
BigDecimal debitAmount,
|
|
456
|
+
BigDecimal creditAmount
|
|
457
|
+
) {
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
</details>
|
|
463
|
+
|
|
464
|
+
<details>
|
|
465
|
+
<summary>コード例: JournalEntryApprovedEvent.java</summary>
|
|
466
|
+
|
|
467
|
+
```java
|
|
468
|
+
package com.example.accounting.journal.api.events;
|
|
469
|
+
|
|
470
|
+
import java.time.LocalDateTime;
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* 仕訳承認イベント
|
|
474
|
+
*/
|
|
475
|
+
public record JournalEntryApprovedEvent(
|
|
476
|
+
String journalEntryId,
|
|
477
|
+
String approvedBy,
|
|
478
|
+
String comment,
|
|
479
|
+
LocalDateTime approvedAt
|
|
480
|
+
) implements JournalEntryEvent {
|
|
481
|
+
}
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
</details>
|
|
485
|
+
|
|
486
|
+
<details>
|
|
487
|
+
<summary>コード例: JournalEntryPostedEvent.java</summary>
|
|
488
|
+
|
|
489
|
+
```java
|
|
490
|
+
package com.example.accounting.journal.api.events;
|
|
491
|
+
|
|
492
|
+
import java.time.LocalDateTime;
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* 仕訳転記イベント
|
|
496
|
+
*/
|
|
497
|
+
public record JournalEntryPostedEvent(
|
|
498
|
+
String journalEntryId,
|
|
499
|
+
String fiscalPeriod,
|
|
500
|
+
String postedBy,
|
|
501
|
+
LocalDateTime postedAt
|
|
502
|
+
) implements JournalEntryEvent {
|
|
503
|
+
}
|
|
504
|
+
```
|
|
505
|
+
|
|
506
|
+
</details>
|
|
507
|
+
|
|
508
|
+
---
|
|
509
|
+
|
|
510
|
+
### 22.4.3 コマンド定義(Domain Layer)
|
|
511
|
+
|
|
512
|
+
<details>
|
|
513
|
+
<summary>コード例: JournalEntryCommands.java</summary>
|
|
514
|
+
|
|
515
|
+
```java
|
|
516
|
+
package com.example.accounting.journal.domain.model.aggregate.journal;
|
|
517
|
+
|
|
518
|
+
import org.axonframework.modelling.command.TargetAggregateIdentifier;
|
|
519
|
+
import java.math.BigDecimal;
|
|
520
|
+
import java.time.LocalDate;
|
|
521
|
+
import java.util.List;
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* JournalEntry 集約へのコマンド定義
|
|
525
|
+
*/
|
|
526
|
+
public final class JournalEntryCommands {
|
|
527
|
+
|
|
528
|
+
private JournalEntryCommands() {
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* 仕訳作成コマンド
|
|
533
|
+
*/
|
|
534
|
+
public record CreateJournalEntryCommand(
|
|
535
|
+
@TargetAggregateIdentifier
|
|
536
|
+
String journalEntryId,
|
|
537
|
+
LocalDate transactionDate,
|
|
538
|
+
String description,
|
|
539
|
+
List<JournalLineInput> lines,
|
|
540
|
+
String createdBy
|
|
541
|
+
) {
|
|
542
|
+
public record JournalLineInput(
|
|
543
|
+
String accountCode,
|
|
544
|
+
String accountName,
|
|
545
|
+
BigDecimal debitAmount,
|
|
546
|
+
BigDecimal creditAmount
|
|
547
|
+
) {
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* 仕訳承認申請コマンド
|
|
553
|
+
*/
|
|
554
|
+
public record SubmitJournalEntryCommand(
|
|
555
|
+
@TargetAggregateIdentifier
|
|
556
|
+
String journalEntryId,
|
|
557
|
+
String submittedBy
|
|
558
|
+
) {
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* 仕訳承認コマンド
|
|
563
|
+
*/
|
|
564
|
+
public record ApproveJournalEntryCommand(
|
|
565
|
+
@TargetAggregateIdentifier
|
|
566
|
+
String journalEntryId,
|
|
567
|
+
String approvedBy,
|
|
568
|
+
String comment
|
|
569
|
+
) {
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* 仕訳却下コマンド
|
|
574
|
+
*/
|
|
575
|
+
public record RejectJournalEntryCommand(
|
|
576
|
+
@TargetAggregateIdentifier
|
|
577
|
+
String journalEntryId,
|
|
578
|
+
String rejectedBy,
|
|
579
|
+
String reason
|
|
580
|
+
) {
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* 仕訳転記コマンド
|
|
585
|
+
*/
|
|
586
|
+
public record PostJournalEntryCommand(
|
|
587
|
+
@TargetAggregateIdentifier
|
|
588
|
+
String journalEntryId,
|
|
589
|
+
String fiscalPeriod,
|
|
590
|
+
String postedBy
|
|
591
|
+
) {
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* 仕訳キャンセルコマンド
|
|
596
|
+
*/
|
|
597
|
+
public record CancelJournalEntryCommand(
|
|
598
|
+
@TargetAggregateIdentifier
|
|
599
|
+
String journalEntryId,
|
|
600
|
+
String cancelledBy,
|
|
601
|
+
String reason
|
|
602
|
+
) {
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
```
|
|
606
|
+
|
|
607
|
+
</details>
|
|
608
|
+
|
|
609
|
+
---
|
|
610
|
+
|
|
611
|
+
### 22.4.4 ドメインモデル(純粋な Java)
|
|
612
|
+
|
|
613
|
+
<details>
|
|
614
|
+
<summary>コード例: JournalEntry.java</summary>
|
|
615
|
+
|
|
616
|
+
```java
|
|
617
|
+
package com.example.accounting.journal.domain.model.aggregate.journal;
|
|
618
|
+
|
|
619
|
+
import com.example.accounting.journal.api.events.*;
|
|
620
|
+
import com.example.accounting.journal.api.events.JournalEntryCreatedEvent.JournalLineData;
|
|
621
|
+
import java.math.BigDecimal;
|
|
622
|
+
import java.time.LocalDate;
|
|
623
|
+
import java.time.LocalDateTime;
|
|
624
|
+
import java.util.List;
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* JournalEntry ドメインモデル(Axon 非依存)
|
|
628
|
+
* 純粋なビジネスロジックのみを含む
|
|
629
|
+
*/
|
|
630
|
+
public record JournalEntry(
|
|
631
|
+
String journalEntryId,
|
|
632
|
+
String journalNumber,
|
|
633
|
+
LocalDate transactionDate,
|
|
634
|
+
String description,
|
|
635
|
+
List<JournalLine> lines,
|
|
636
|
+
JournalEntryStatus status
|
|
637
|
+
) {
|
|
638
|
+
|
|
639
|
+
// ======== ファクトリメソッド ========
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* 仕訳作成
|
|
643
|
+
*/
|
|
644
|
+
public static JournalEntryCreatedEvent create(
|
|
645
|
+
String journalEntryId,
|
|
646
|
+
String journalNumber,
|
|
647
|
+
LocalDate transactionDate,
|
|
648
|
+
String description,
|
|
649
|
+
List<JournalLineData> lines,
|
|
650
|
+
String createdBy
|
|
651
|
+
) {
|
|
652
|
+
// バリデーション: 借方・貸方の合計が一致すること
|
|
653
|
+
BigDecimal totalDebit = lines.stream()
|
|
654
|
+
.map(JournalLineData::debitAmount)
|
|
655
|
+
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
656
|
+
BigDecimal totalCredit = lines.stream()
|
|
657
|
+
.map(JournalLineData::creditAmount)
|
|
658
|
+
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
659
|
+
|
|
660
|
+
if (totalDebit.compareTo(totalCredit) != 0) {
|
|
661
|
+
throw new IllegalArgumentException(
|
|
662
|
+
"Debit and credit amounts must be equal. Debit: " + totalDebit + ", Credit: " + totalCredit
|
|
663
|
+
);
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
if (lines.isEmpty()) {
|
|
667
|
+
throw new IllegalArgumentException("Journal entry must have at least one line");
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
return new JournalEntryCreatedEvent(
|
|
671
|
+
journalEntryId,
|
|
672
|
+
journalNumber,
|
|
673
|
+
transactionDate,
|
|
674
|
+
description,
|
|
675
|
+
lines,
|
|
676
|
+
createdBy,
|
|
677
|
+
LocalDateTime.now()
|
|
678
|
+
);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* イベントからの再構築
|
|
683
|
+
*/
|
|
684
|
+
public static JournalEntry from(JournalEntryCreatedEvent event) {
|
|
685
|
+
List<JournalLine> lines = event.lines().stream()
|
|
686
|
+
.map(data -> new JournalLine(
|
|
687
|
+
data.accountCode(),
|
|
688
|
+
data.accountName(),
|
|
689
|
+
data.debitAmount(),
|
|
690
|
+
data.creditAmount()
|
|
691
|
+
))
|
|
692
|
+
.toList();
|
|
693
|
+
|
|
694
|
+
return new JournalEntry(
|
|
695
|
+
event.journalEntryId(),
|
|
696
|
+
event.journalNumber(),
|
|
697
|
+
event.transactionDate(),
|
|
698
|
+
event.description(),
|
|
699
|
+
lines,
|
|
700
|
+
JournalEntryStatus.DRAFT
|
|
701
|
+
);
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
// ======== 状態遷移メソッド ========
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* 承認申請
|
|
708
|
+
*/
|
|
709
|
+
public JournalEntrySubmittedEvent submit(String submittedBy) {
|
|
710
|
+
if (status != JournalEntryStatus.DRAFT) {
|
|
711
|
+
throw new IllegalStateException("Only draft journal entries can be submitted");
|
|
712
|
+
}
|
|
713
|
+
return new JournalEntrySubmittedEvent(journalEntryId, submittedBy, LocalDateTime.now());
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
/**
|
|
717
|
+
* 承認
|
|
718
|
+
*/
|
|
719
|
+
public JournalEntryApprovedEvent approve(String approvedBy, String comment) {
|
|
720
|
+
if (status != JournalEntryStatus.PENDING_APPROVAL) {
|
|
721
|
+
throw new IllegalStateException("Only pending journal entries can be approved");
|
|
722
|
+
}
|
|
723
|
+
return new JournalEntryApprovedEvent(journalEntryId, approvedBy, comment, LocalDateTime.now());
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
* 却下
|
|
728
|
+
*/
|
|
729
|
+
public JournalEntryRejectedEvent reject(String rejectedBy, String reason) {
|
|
730
|
+
if (status != JournalEntryStatus.PENDING_APPROVAL) {
|
|
731
|
+
throw new IllegalStateException("Only pending journal entries can be rejected");
|
|
732
|
+
}
|
|
733
|
+
return new JournalEntryRejectedEvent(journalEntryId, rejectedBy, reason, LocalDateTime.now());
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
/**
|
|
737
|
+
* 転記
|
|
738
|
+
*/
|
|
739
|
+
public JournalEntryPostedEvent post(String fiscalPeriod, String postedBy) {
|
|
740
|
+
if (status != JournalEntryStatus.APPROVED) {
|
|
741
|
+
throw new IllegalStateException("Only approved journal entries can be posted");
|
|
742
|
+
}
|
|
743
|
+
return new JournalEntryPostedEvent(journalEntryId, fiscalPeriod, postedBy, LocalDateTime.now());
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
/**
|
|
747
|
+
* キャンセル
|
|
748
|
+
*/
|
|
749
|
+
public JournalEntryCancelledEvent cancel(String cancelledBy, String reason) {
|
|
750
|
+
if (status == JournalEntryStatus.POSTED) {
|
|
751
|
+
throw new IllegalStateException("Posted journal entries cannot be cancelled");
|
|
752
|
+
}
|
|
753
|
+
if (status == JournalEntryStatus.CANCELLED) {
|
|
754
|
+
throw new IllegalStateException("Journal entry is already cancelled");
|
|
755
|
+
}
|
|
756
|
+
return new JournalEntryCancelledEvent(journalEntryId, cancelledBy, reason, LocalDateTime.now());
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
// ======== イベント適用メソッド ========
|
|
760
|
+
|
|
761
|
+
/**
|
|
762
|
+
* イベントを適用して新しい状態を生成
|
|
763
|
+
*/
|
|
764
|
+
public JournalEntry apply(JournalEntryEvent event) {
|
|
765
|
+
return switch (event) {
|
|
766
|
+
case JournalEntryCreatedEvent e -> from(e);
|
|
767
|
+
case JournalEntrySubmittedEvent e -> withStatus(JournalEntryStatus.PENDING_APPROVAL);
|
|
768
|
+
case JournalEntryApprovedEvent e -> withStatus(JournalEntryStatus.APPROVED);
|
|
769
|
+
case JournalEntryRejectedEvent e -> withStatus(JournalEntryStatus.REJECTED);
|
|
770
|
+
case JournalEntryRevisedEvent e -> withStatus(JournalEntryStatus.DRAFT);
|
|
771
|
+
case JournalEntryPostedEvent e -> withStatus(JournalEntryStatus.POSTED);
|
|
772
|
+
case JournalEntryCancelledEvent e -> withStatus(JournalEntryStatus.CANCELLED);
|
|
773
|
+
};
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
/**
|
|
777
|
+
* 状態更新ヘルパー
|
|
778
|
+
*/
|
|
779
|
+
private JournalEntry withStatus(JournalEntryStatus newStatus) {
|
|
780
|
+
return new JournalEntry(journalEntryId, journalNumber, transactionDate, description, lines, newStatus);
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
```
|
|
784
|
+
|
|
785
|
+
</details>
|
|
786
|
+
|
|
787
|
+
<details>
|
|
788
|
+
<summary>コード例: JournalEntryStatus.java</summary>
|
|
789
|
+
|
|
790
|
+
```java
|
|
791
|
+
package com.example.accounting.journal.domain.model.aggregate.journal;
|
|
792
|
+
|
|
793
|
+
/**
|
|
794
|
+
* 仕訳ステータス
|
|
795
|
+
*/
|
|
796
|
+
public enum JournalEntryStatus {
|
|
797
|
+
DRAFT, // 下書き
|
|
798
|
+
PENDING_APPROVAL, // 承認待ち
|
|
799
|
+
APPROVED, // 承認済み
|
|
800
|
+
REJECTED, // 却下
|
|
801
|
+
POSTED, // 転記済み
|
|
802
|
+
CANCELLED // キャンセル
|
|
803
|
+
}
|
|
804
|
+
```
|
|
805
|
+
|
|
806
|
+
</details>
|
|
807
|
+
|
|
808
|
+
<details>
|
|
809
|
+
<summary>コード例: JournalLine.java</summary>
|
|
810
|
+
|
|
811
|
+
```java
|
|
812
|
+
package com.example.accounting.journal.domain.model.aggregate.journal;
|
|
813
|
+
|
|
814
|
+
import java.math.BigDecimal;
|
|
815
|
+
|
|
816
|
+
/**
|
|
817
|
+
* 仕訳明細(Value Object)
|
|
818
|
+
*/
|
|
819
|
+
public record JournalLine(
|
|
820
|
+
String accountCode,
|
|
821
|
+
String accountName,
|
|
822
|
+
BigDecimal debitAmount,
|
|
823
|
+
BigDecimal creditAmount
|
|
824
|
+
) {
|
|
825
|
+
public JournalLine {
|
|
826
|
+
if (debitAmount == null) debitAmount = BigDecimal.ZERO;
|
|
827
|
+
if (creditAmount == null) creditAmount = BigDecimal.ZERO;
|
|
828
|
+
|
|
829
|
+
if (debitAmount.compareTo(BigDecimal.ZERO) < 0 || creditAmount.compareTo(BigDecimal.ZERO) < 0) {
|
|
830
|
+
throw new IllegalArgumentException("Amounts cannot be negative");
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
```
|
|
835
|
+
|
|
836
|
+
</details>
|
|
837
|
+
|
|
838
|
+
---
|
|
839
|
+
|
|
840
|
+
## 22.5 Aggregate Adapter と Policy Handler
|
|
841
|
+
|
|
842
|
+
### 22.5.1 JournalEntry Aggregate Adapter
|
|
843
|
+
|
|
844
|
+
<details>
|
|
845
|
+
<summary>コード例: JournalEntryAggregateAdapter.java</summary>
|
|
846
|
+
|
|
847
|
+
```java
|
|
848
|
+
package com.example.accounting.journal.application.aggregate;
|
|
849
|
+
|
|
850
|
+
import com.example.accounting.journal.api.events.*;
|
|
851
|
+
import com.example.accounting.journal.api.events.JournalEntryCreatedEvent.JournalLineData;
|
|
852
|
+
import com.example.accounting.journal.domain.model.aggregate.journal.JournalEntry;
|
|
853
|
+
import com.example.accounting.journal.domain.model.aggregate.journal.JournalEntryCommands.*;
|
|
854
|
+
import org.axonframework.commandhandling.CommandHandler;
|
|
855
|
+
import org.axonframework.eventsourcing.EventSourcingHandler;
|
|
856
|
+
import org.axonframework.modelling.command.AggregateIdentifier;
|
|
857
|
+
import org.axonframework.modelling.command.AggregateLifecycle;
|
|
858
|
+
import org.axonframework.spring.stereotype.Aggregate;
|
|
859
|
+
|
|
860
|
+
import java.util.UUID;
|
|
861
|
+
|
|
862
|
+
/**
|
|
863
|
+
* JournalEntry Aggregate Adapter(Axon Framework 用)
|
|
864
|
+
*/
|
|
865
|
+
@Aggregate
|
|
866
|
+
public class JournalEntryAggregateAdapter {
|
|
867
|
+
|
|
868
|
+
@AggregateIdentifier
|
|
869
|
+
private String journalEntryId;
|
|
870
|
+
|
|
871
|
+
private JournalEntry journalEntry;
|
|
872
|
+
|
|
873
|
+
/**
|
|
874
|
+
* Axon が使用するデフォルトコンストラクタ
|
|
875
|
+
*/
|
|
876
|
+
protected JournalEntryAggregateAdapter() {
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
// ======== Command Handlers ========
|
|
880
|
+
|
|
881
|
+
/**
|
|
882
|
+
* 集約作成コマンドハンドラー(コンストラクタ)
|
|
883
|
+
*/
|
|
884
|
+
@CommandHandler
|
|
885
|
+
public JournalEntryAggregateAdapter(CreateJournalEntryCommand command) {
|
|
886
|
+
// 仕訳番号を生成
|
|
887
|
+
String journalNumber = generateJournalNumber();
|
|
888
|
+
|
|
889
|
+
// コマンドの明細をイベント用データに変換
|
|
890
|
+
var lineDataList = command.lines().stream()
|
|
891
|
+
.map(line -> new JournalLineData(
|
|
892
|
+
line.accountCode(),
|
|
893
|
+
line.accountName(),
|
|
894
|
+
line.debitAmount(),
|
|
895
|
+
line.creditAmount()
|
|
896
|
+
))
|
|
897
|
+
.toList();
|
|
898
|
+
|
|
899
|
+
// ドメインモデルのファクトリメソッドを呼び出し
|
|
900
|
+
var event = JournalEntry.create(
|
|
901
|
+
command.journalEntryId(),
|
|
902
|
+
journalNumber,
|
|
903
|
+
command.transactionDate(),
|
|
904
|
+
command.description(),
|
|
905
|
+
lineDataList,
|
|
906
|
+
command.createdBy()
|
|
907
|
+
);
|
|
908
|
+
|
|
909
|
+
// イベントを発行
|
|
910
|
+
AggregateLifecycle.apply(event);
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
/**
|
|
914
|
+
* 承認申請コマンドハンドラー
|
|
915
|
+
*/
|
|
916
|
+
@CommandHandler
|
|
917
|
+
public void handle(SubmitJournalEntryCommand command) {
|
|
918
|
+
var event = journalEntry.submit(command.submittedBy());
|
|
919
|
+
AggregateLifecycle.apply(event);
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
/**
|
|
923
|
+
* 承認コマンドハンドラー
|
|
924
|
+
*/
|
|
925
|
+
@CommandHandler
|
|
926
|
+
public void handle(ApproveJournalEntryCommand command) {
|
|
927
|
+
var event = journalEntry.approve(command.approvedBy(), command.comment());
|
|
928
|
+
AggregateLifecycle.apply(event);
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
/**
|
|
932
|
+
* 却下コマンドハンドラー
|
|
933
|
+
*/
|
|
934
|
+
@CommandHandler
|
|
935
|
+
public void handle(RejectJournalEntryCommand command) {
|
|
936
|
+
var event = journalEntry.reject(command.rejectedBy(), command.reason());
|
|
937
|
+
AggregateLifecycle.apply(event);
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
/**
|
|
941
|
+
* 転記コマンドハンドラー
|
|
942
|
+
*/
|
|
943
|
+
@CommandHandler
|
|
944
|
+
public void handle(PostJournalEntryCommand command) {
|
|
945
|
+
var event = journalEntry.post(command.fiscalPeriod(), command.postedBy());
|
|
946
|
+
AggregateLifecycle.apply(event);
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
/**
|
|
950
|
+
* キャンセルコマンドハンドラー
|
|
951
|
+
*/
|
|
952
|
+
@CommandHandler
|
|
953
|
+
public void handle(CancelJournalEntryCommand command) {
|
|
954
|
+
var event = journalEntry.cancel(command.cancelledBy(), command.reason());
|
|
955
|
+
AggregateLifecycle.apply(event);
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
// ======== Event Sourcing Handlers(状態復元)========
|
|
959
|
+
|
|
960
|
+
@EventSourcingHandler
|
|
961
|
+
public void on(JournalEntryCreatedEvent event) {
|
|
962
|
+
this.journalEntryId = event.journalEntryId();
|
|
963
|
+
this.journalEntry = JournalEntry.from(event);
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
@EventSourcingHandler
|
|
967
|
+
public void on(JournalEntrySubmittedEvent event) {
|
|
968
|
+
this.journalEntry = journalEntry.apply(event);
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
@EventSourcingHandler
|
|
972
|
+
public void on(JournalEntryApprovedEvent event) {
|
|
973
|
+
this.journalEntry = journalEntry.apply(event);
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
@EventSourcingHandler
|
|
977
|
+
public void on(JournalEntryRejectedEvent event) {
|
|
978
|
+
this.journalEntry = journalEntry.apply(event);
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
@EventSourcingHandler
|
|
982
|
+
public void on(JournalEntryPostedEvent event) {
|
|
983
|
+
this.journalEntry = journalEntry.apply(event);
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
@EventSourcingHandler
|
|
987
|
+
public void on(JournalEntryCancelledEvent event) {
|
|
988
|
+
this.journalEntry = journalEntry.apply(event);
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
// ======== Helper Methods ========
|
|
992
|
+
|
|
993
|
+
private String generateJournalNumber() {
|
|
994
|
+
// 実際には連番生成サービスを使用
|
|
995
|
+
return "JE-" + UUID.randomUUID().toString().substring(0, 8).toUpperCase();
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
```
|
|
999
|
+
|
|
1000
|
+
</details>
|
|
1001
|
+
|
|
1002
|
+
**Axon アノテーション解説:**
|
|
1003
|
+
|
|
1004
|
+
| アノテーション | 説明 |
|
|
1005
|
+
|--------------|------|
|
|
1006
|
+
| `@Aggregate` | Event Sourcing 集約であることを宣言 |
|
|
1007
|
+
| `@AggregateIdentifier` | 集約の識別子フィールド |
|
|
1008
|
+
| `@CommandHandler` | コマンドを処理するメソッド |
|
|
1009
|
+
| `@EventSourcingHandler` | イベントから状態を復元するメソッド |
|
|
1010
|
+
| `AggregateLifecycle.apply()` | イベントを発行するメソッド |
|
|
1011
|
+
|
|
1012
|
+
---
|
|
1013
|
+
|
|
1014
|
+
### 22.5.2 Policy Handler(Choreography パターン)
|
|
1015
|
+
|
|
1016
|
+
```plantuml
|
|
1017
|
+
@startuml choreography
|
|
1018
|
+
skinparam backgroundColor #FEFEFE
|
|
1019
|
+
|
|
1020
|
+
participant "Journal Context" as Journal
|
|
1021
|
+
participant "Event Bus" as Bus
|
|
1022
|
+
participant "Account Context" as Account
|
|
1023
|
+
|
|
1024
|
+
== 仕訳転記フロー ==
|
|
1025
|
+
Journal -> Bus : JournalEntryPostedEvent
|
|
1026
|
+
Bus -> Account : JournalEntryPostedEvent
|
|
1027
|
+
Account -> Account : updateBalance()
|
|
1028
|
+
Account -> Bus : AccountBalanceUpdatedEvent
|
|
1029
|
+
|
|
1030
|
+
@enduml
|
|
1031
|
+
```
|
|
1032
|
+
|
|
1033
|
+
<details>
|
|
1034
|
+
<summary>コード例: JournalEventHandler.java(Account Context 内)</summary>
|
|
1035
|
+
|
|
1036
|
+
```java
|
|
1037
|
+
package com.example.accounting.account.application.policy;
|
|
1038
|
+
|
|
1039
|
+
import com.example.accounting.account.domain.model.aggregate.account.AccountCommands.*;
|
|
1040
|
+
import com.example.accounting.journal.api.events.JournalEntryPostedEvent;
|
|
1041
|
+
import org.axonframework.commandhandling.gateway.CommandGateway;
|
|
1042
|
+
import org.axonframework.eventhandling.EventHandler;
|
|
1043
|
+
import org.springframework.stereotype.Component;
|
|
1044
|
+
|
|
1045
|
+
/**
|
|
1046
|
+
* Journal イベントに反応して勘定科目残高を更新する Policy Handler
|
|
1047
|
+
*/
|
|
1048
|
+
@Component
|
|
1049
|
+
public class JournalEventHandler {
|
|
1050
|
+
|
|
1051
|
+
private final CommandGateway commandGateway;
|
|
1052
|
+
|
|
1053
|
+
public JournalEventHandler(CommandGateway commandGateway) {
|
|
1054
|
+
this.commandGateway = commandGateway;
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
/**
|
|
1058
|
+
* Policy: "When a journal entry is posted, update account balances"
|
|
1059
|
+
*/
|
|
1060
|
+
@EventHandler
|
|
1061
|
+
public void on(JournalEntryPostedEvent event) {
|
|
1062
|
+
// 仕訳の各明細に対して残高更新コマンドを発行
|
|
1063
|
+
// 実際には JournalEntryPostedEvent に明細情報を含めるか、
|
|
1064
|
+
// Read Model から取得する
|
|
1065
|
+
var command = new UpdateAccountBalanceCommand(
|
|
1066
|
+
event.journalEntryId(),
|
|
1067
|
+
event.fiscalPeriod()
|
|
1068
|
+
);
|
|
1069
|
+
commandGateway.send(command);
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
```
|
|
1073
|
+
|
|
1074
|
+
</details>
|
|
1075
|
+
|
|
1076
|
+
---
|
|
1077
|
+
|
|
1078
|
+
## 22.6 Projection と Read Model(JPA版)
|
|
1079
|
+
|
|
1080
|
+
### 22.6.1 JournalEntry Projection
|
|
1081
|
+
|
|
1082
|
+
<details>
|
|
1083
|
+
<summary>コード例: JournalEntryProjection.java</summary>
|
|
1084
|
+
|
|
1085
|
+
```java
|
|
1086
|
+
package com.example.accounting.journal.application.query;
|
|
1087
|
+
|
|
1088
|
+
import com.example.accounting.journal.adapter.outbound.persistence.entity.JournalEntryEntity;
|
|
1089
|
+
import com.example.accounting.journal.adapter.outbound.persistence.entity.JournalLineEntity;
|
|
1090
|
+
import com.example.accounting.journal.adapter.outbound.persistence.repository.JournalEntryJpaRepository;
|
|
1091
|
+
import com.example.accounting.journal.api.events.*;
|
|
1092
|
+
import org.axonframework.eventhandling.EventHandler;
|
|
1093
|
+
import org.springframework.stereotype.Component;
|
|
1094
|
+
import org.springframework.transaction.annotation.Transactional;
|
|
1095
|
+
|
|
1096
|
+
import java.util.stream.Collectors;
|
|
1097
|
+
|
|
1098
|
+
/**
|
|
1099
|
+
* JournalEntry Projection(JPA を使用した Read Model の更新)
|
|
1100
|
+
*/
|
|
1101
|
+
@Component
|
|
1102
|
+
@Transactional
|
|
1103
|
+
public class JournalEntryProjection {
|
|
1104
|
+
|
|
1105
|
+
private final JournalEntryJpaRepository repository;
|
|
1106
|
+
|
|
1107
|
+
public JournalEntryProjection(JournalEntryJpaRepository repository) {
|
|
1108
|
+
this.repository = repository;
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
@EventHandler
|
|
1112
|
+
public void on(JournalEntryCreatedEvent event) {
|
|
1113
|
+
var entity = new JournalEntryEntity();
|
|
1114
|
+
entity.setJournalEntryId(event.journalEntryId());
|
|
1115
|
+
entity.setJournalNumber(event.journalNumber());
|
|
1116
|
+
entity.setTransactionDate(event.transactionDate());
|
|
1117
|
+
entity.setDescription(event.description());
|
|
1118
|
+
entity.setStatus("DRAFT");
|
|
1119
|
+
entity.setCreatedBy(event.createdBy());
|
|
1120
|
+
entity.setCreatedAt(event.createdAt());
|
|
1121
|
+
|
|
1122
|
+
// 明細をエンティティに変換
|
|
1123
|
+
var lineEntities = event.lines().stream()
|
|
1124
|
+
.map(line -> {
|
|
1125
|
+
var lineEntity = new JournalLineEntity();
|
|
1126
|
+
lineEntity.setAccountCode(line.accountCode());
|
|
1127
|
+
lineEntity.setAccountName(line.accountName());
|
|
1128
|
+
lineEntity.setDebitAmount(line.debitAmount());
|
|
1129
|
+
lineEntity.setCreditAmount(line.creditAmount());
|
|
1130
|
+
lineEntity.setJournalEntry(entity);
|
|
1131
|
+
return lineEntity;
|
|
1132
|
+
})
|
|
1133
|
+
.collect(Collectors.toList());
|
|
1134
|
+
|
|
1135
|
+
entity.setLines(lineEntities);
|
|
1136
|
+
repository.save(entity);
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
@EventHandler
|
|
1140
|
+
public void on(JournalEntrySubmittedEvent event) {
|
|
1141
|
+
repository.findById(event.journalEntryId()).ifPresent(entity -> {
|
|
1142
|
+
entity.setStatus("PENDING_APPROVAL");
|
|
1143
|
+
entity.setSubmittedBy(event.submittedBy());
|
|
1144
|
+
entity.setSubmittedAt(event.submittedAt());
|
|
1145
|
+
repository.save(entity);
|
|
1146
|
+
});
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
@EventHandler
|
|
1150
|
+
public void on(JournalEntryApprovedEvent event) {
|
|
1151
|
+
repository.findById(event.journalEntryId()).ifPresent(entity -> {
|
|
1152
|
+
entity.setStatus("APPROVED");
|
|
1153
|
+
entity.setApprovedBy(event.approvedBy());
|
|
1154
|
+
entity.setApprovalComment(event.comment());
|
|
1155
|
+
entity.setApprovedAt(event.approvedAt());
|
|
1156
|
+
repository.save(entity);
|
|
1157
|
+
});
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
@EventHandler
|
|
1161
|
+
public void on(JournalEntryRejectedEvent event) {
|
|
1162
|
+
repository.findById(event.journalEntryId()).ifPresent(entity -> {
|
|
1163
|
+
entity.setStatus("REJECTED");
|
|
1164
|
+
entity.setRejectedBy(event.rejectedBy());
|
|
1165
|
+
entity.setRejectionReason(event.reason());
|
|
1166
|
+
entity.setRejectedAt(event.rejectedAt());
|
|
1167
|
+
repository.save(entity);
|
|
1168
|
+
});
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
@EventHandler
|
|
1172
|
+
public void on(JournalEntryPostedEvent event) {
|
|
1173
|
+
repository.findById(event.journalEntryId()).ifPresent(entity -> {
|
|
1174
|
+
entity.setStatus("POSTED");
|
|
1175
|
+
entity.setFiscalPeriod(event.fiscalPeriod());
|
|
1176
|
+
entity.setPostedBy(event.postedBy());
|
|
1177
|
+
entity.setPostedAt(event.postedAt());
|
|
1178
|
+
repository.save(entity);
|
|
1179
|
+
});
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
@EventHandler
|
|
1183
|
+
public void on(JournalEntryCancelledEvent event) {
|
|
1184
|
+
repository.findById(event.journalEntryId()).ifPresent(entity -> {
|
|
1185
|
+
entity.setStatus("CANCELLED");
|
|
1186
|
+
entity.setCancelledBy(event.cancelledBy());
|
|
1187
|
+
entity.setCancellationReason(event.reason());
|
|
1188
|
+
entity.setCancelledAt(event.cancelledAt());
|
|
1189
|
+
repository.save(entity);
|
|
1190
|
+
});
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
```
|
|
1194
|
+
|
|
1195
|
+
</details>
|
|
1196
|
+
|
|
1197
|
+
---
|
|
1198
|
+
|
|
1199
|
+
### 22.6.2 Read Model Entity(JPA)
|
|
1200
|
+
|
|
1201
|
+
<details>
|
|
1202
|
+
<summary>コード例: JournalEntryEntity.java</summary>
|
|
1203
|
+
|
|
1204
|
+
```java
|
|
1205
|
+
package com.example.accounting.journal.adapter.outbound.persistence.entity;
|
|
1206
|
+
|
|
1207
|
+
import jakarta.persistence.*;
|
|
1208
|
+
import java.time.LocalDate;
|
|
1209
|
+
import java.time.LocalDateTime;
|
|
1210
|
+
import java.util.ArrayList;
|
|
1211
|
+
import java.util.List;
|
|
1212
|
+
|
|
1213
|
+
/**
|
|
1214
|
+
* JournalEntry Read Model Entity(JPA)
|
|
1215
|
+
*/
|
|
1216
|
+
@Entity
|
|
1217
|
+
@Table(name = "journal_entries")
|
|
1218
|
+
public class JournalEntryEntity {
|
|
1219
|
+
|
|
1220
|
+
@Id
|
|
1221
|
+
@Column(name = "journal_entry_id", length = 36)
|
|
1222
|
+
private String journalEntryId;
|
|
1223
|
+
|
|
1224
|
+
@Column(name = "journal_number", length = 20, nullable = false)
|
|
1225
|
+
private String journalNumber;
|
|
1226
|
+
|
|
1227
|
+
@Column(name = "transaction_date", nullable = false)
|
|
1228
|
+
private LocalDate transactionDate;
|
|
1229
|
+
|
|
1230
|
+
@Column(name = "description", length = 500)
|
|
1231
|
+
private String description;
|
|
1232
|
+
|
|
1233
|
+
@Column(name = "status", length = 20, nullable = false)
|
|
1234
|
+
private String status;
|
|
1235
|
+
|
|
1236
|
+
@Column(name = "fiscal_period", length = 10)
|
|
1237
|
+
private String fiscalPeriod;
|
|
1238
|
+
|
|
1239
|
+
@Column(name = "created_by", length = 100, nullable = false)
|
|
1240
|
+
private String createdBy;
|
|
1241
|
+
|
|
1242
|
+
@Column(name = "created_at", nullable = false)
|
|
1243
|
+
private LocalDateTime createdAt;
|
|
1244
|
+
|
|
1245
|
+
@Column(name = "submitted_by", length = 100)
|
|
1246
|
+
private String submittedBy;
|
|
1247
|
+
|
|
1248
|
+
@Column(name = "submitted_at")
|
|
1249
|
+
private LocalDateTime submittedAt;
|
|
1250
|
+
|
|
1251
|
+
@Column(name = "approved_by", length = 100)
|
|
1252
|
+
private String approvedBy;
|
|
1253
|
+
|
|
1254
|
+
@Column(name = "approval_comment", length = 500)
|
|
1255
|
+
private String approvalComment;
|
|
1256
|
+
|
|
1257
|
+
@Column(name = "approved_at")
|
|
1258
|
+
private LocalDateTime approvedAt;
|
|
1259
|
+
|
|
1260
|
+
@Column(name = "rejected_by", length = 100)
|
|
1261
|
+
private String rejectedBy;
|
|
1262
|
+
|
|
1263
|
+
@Column(name = "rejection_reason", length = 500)
|
|
1264
|
+
private String rejectionReason;
|
|
1265
|
+
|
|
1266
|
+
@Column(name = "rejected_at")
|
|
1267
|
+
private LocalDateTime rejectedAt;
|
|
1268
|
+
|
|
1269
|
+
@Column(name = "posted_by", length = 100)
|
|
1270
|
+
private String postedBy;
|
|
1271
|
+
|
|
1272
|
+
@Column(name = "posted_at")
|
|
1273
|
+
private LocalDateTime postedAt;
|
|
1274
|
+
|
|
1275
|
+
@Column(name = "cancelled_by", length = 100)
|
|
1276
|
+
private String cancelledBy;
|
|
1277
|
+
|
|
1278
|
+
@Column(name = "cancellation_reason", length = 500)
|
|
1279
|
+
private String cancellationReason;
|
|
1280
|
+
|
|
1281
|
+
@Column(name = "cancelled_at")
|
|
1282
|
+
private LocalDateTime cancelledAt;
|
|
1283
|
+
|
|
1284
|
+
@OneToMany(mappedBy = "journalEntry", cascade = CascadeType.ALL, orphanRemoval = true)
|
|
1285
|
+
private List<JournalLineEntity> lines = new ArrayList<>();
|
|
1286
|
+
|
|
1287
|
+
// Getters and Setters
|
|
1288
|
+
public String getJournalEntryId() { return journalEntryId; }
|
|
1289
|
+
public void setJournalEntryId(String journalEntryId) { this.journalEntryId = journalEntryId; }
|
|
1290
|
+
|
|
1291
|
+
public String getJournalNumber() { return journalNumber; }
|
|
1292
|
+
public void setJournalNumber(String journalNumber) { this.journalNumber = journalNumber; }
|
|
1293
|
+
|
|
1294
|
+
public LocalDate getTransactionDate() { return transactionDate; }
|
|
1295
|
+
public void setTransactionDate(LocalDate transactionDate) { this.transactionDate = transactionDate; }
|
|
1296
|
+
|
|
1297
|
+
public String getDescription() { return description; }
|
|
1298
|
+
public void setDescription(String description) { this.description = description; }
|
|
1299
|
+
|
|
1300
|
+
public String getStatus() { return status; }
|
|
1301
|
+
public void setStatus(String status) { this.status = status; }
|
|
1302
|
+
|
|
1303
|
+
public String getFiscalPeriod() { return fiscalPeriod; }
|
|
1304
|
+
public void setFiscalPeriod(String fiscalPeriod) { this.fiscalPeriod = fiscalPeriod; }
|
|
1305
|
+
|
|
1306
|
+
public String getCreatedBy() { return createdBy; }
|
|
1307
|
+
public void setCreatedBy(String createdBy) { this.createdBy = createdBy; }
|
|
1308
|
+
|
|
1309
|
+
public LocalDateTime getCreatedAt() { return createdAt; }
|
|
1310
|
+
public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }
|
|
1311
|
+
|
|
1312
|
+
public String getSubmittedBy() { return submittedBy; }
|
|
1313
|
+
public void setSubmittedBy(String submittedBy) { this.submittedBy = submittedBy; }
|
|
1314
|
+
|
|
1315
|
+
public LocalDateTime getSubmittedAt() { return submittedAt; }
|
|
1316
|
+
public void setSubmittedAt(LocalDateTime submittedAt) { this.submittedAt = submittedAt; }
|
|
1317
|
+
|
|
1318
|
+
public String getApprovedBy() { return approvedBy; }
|
|
1319
|
+
public void setApprovedBy(String approvedBy) { this.approvedBy = approvedBy; }
|
|
1320
|
+
|
|
1321
|
+
public String getApprovalComment() { return approvalComment; }
|
|
1322
|
+
public void setApprovalComment(String approvalComment) { this.approvalComment = approvalComment; }
|
|
1323
|
+
|
|
1324
|
+
public LocalDateTime getApprovedAt() { return approvedAt; }
|
|
1325
|
+
public void setApprovedAt(LocalDateTime approvedAt) { this.approvedAt = approvedAt; }
|
|
1326
|
+
|
|
1327
|
+
public String getRejectedBy() { return rejectedBy; }
|
|
1328
|
+
public void setRejectedBy(String rejectedBy) { this.rejectedBy = rejectedBy; }
|
|
1329
|
+
|
|
1330
|
+
public String getRejectionReason() { return rejectionReason; }
|
|
1331
|
+
public void setRejectionReason(String rejectionReason) { this.rejectionReason = rejectionReason; }
|
|
1332
|
+
|
|
1333
|
+
public LocalDateTime getRejectedAt() { return rejectedAt; }
|
|
1334
|
+
public void setRejectedAt(LocalDateTime rejectedAt) { this.rejectedAt = rejectedAt; }
|
|
1335
|
+
|
|
1336
|
+
public String getPostedBy() { return postedBy; }
|
|
1337
|
+
public void setPostedBy(String postedBy) { this.postedBy = postedBy; }
|
|
1338
|
+
|
|
1339
|
+
public LocalDateTime getPostedAt() { return postedAt; }
|
|
1340
|
+
public void setPostedAt(LocalDateTime postedAt) { this.postedAt = postedAt; }
|
|
1341
|
+
|
|
1342
|
+
public String getCancelledBy() { return cancelledBy; }
|
|
1343
|
+
public void setCancelledBy(String cancelledBy) { this.cancelledBy = cancelledBy; }
|
|
1344
|
+
|
|
1345
|
+
public String getCancellationReason() { return cancellationReason; }
|
|
1346
|
+
public void setCancellationReason(String cancellationReason) { this.cancellationReason = cancellationReason; }
|
|
1347
|
+
|
|
1348
|
+
public LocalDateTime getCancelledAt() { return cancelledAt; }
|
|
1349
|
+
public void setCancelledAt(LocalDateTime cancelledAt) { this.cancelledAt = cancelledAt; }
|
|
1350
|
+
|
|
1351
|
+
public List<JournalLineEntity> getLines() { return lines; }
|
|
1352
|
+
public void setLines(List<JournalLineEntity> lines) { this.lines = lines; }
|
|
1353
|
+
}
|
|
1354
|
+
```
|
|
1355
|
+
|
|
1356
|
+
</details>
|
|
1357
|
+
|
|
1358
|
+
<details>
|
|
1359
|
+
<summary>コード例: JournalLineEntity.java</summary>
|
|
1360
|
+
|
|
1361
|
+
```java
|
|
1362
|
+
package com.example.accounting.journal.adapter.outbound.persistence.entity;
|
|
1363
|
+
|
|
1364
|
+
import jakarta.persistence.*;
|
|
1365
|
+
import java.math.BigDecimal;
|
|
1366
|
+
|
|
1367
|
+
/**
|
|
1368
|
+
* JournalLine Read Model Entity(JPA)
|
|
1369
|
+
*/
|
|
1370
|
+
@Entity
|
|
1371
|
+
@Table(name = "journal_lines")
|
|
1372
|
+
public class JournalLineEntity {
|
|
1373
|
+
|
|
1374
|
+
@Id
|
|
1375
|
+
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
|
1376
|
+
@Column(name = "id")
|
|
1377
|
+
private Long id;
|
|
1378
|
+
|
|
1379
|
+
@ManyToOne(fetch = FetchType.LAZY)
|
|
1380
|
+
@JoinColumn(name = "journal_entry_id", nullable = false)
|
|
1381
|
+
private JournalEntryEntity journalEntry;
|
|
1382
|
+
|
|
1383
|
+
@Column(name = "account_code", length = 10, nullable = false)
|
|
1384
|
+
private String accountCode;
|
|
1385
|
+
|
|
1386
|
+
@Column(name = "account_name", length = 100, nullable = false)
|
|
1387
|
+
private String accountName;
|
|
1388
|
+
|
|
1389
|
+
@Column(name = "debit_amount", precision = 15, scale = 2, nullable = false)
|
|
1390
|
+
private BigDecimal debitAmount;
|
|
1391
|
+
|
|
1392
|
+
@Column(name = "credit_amount", precision = 15, scale = 2, nullable = false)
|
|
1393
|
+
private BigDecimal creditAmount;
|
|
1394
|
+
|
|
1395
|
+
// Getters and Setters
|
|
1396
|
+
public Long getId() { return id; }
|
|
1397
|
+
public void setId(Long id) { this.id = id; }
|
|
1398
|
+
|
|
1399
|
+
public JournalEntryEntity getJournalEntry() { return journalEntry; }
|
|
1400
|
+
public void setJournalEntry(JournalEntryEntity journalEntry) { this.journalEntry = journalEntry; }
|
|
1401
|
+
|
|
1402
|
+
public String getAccountCode() { return accountCode; }
|
|
1403
|
+
public void setAccountCode(String accountCode) { this.accountCode = accountCode; }
|
|
1404
|
+
|
|
1405
|
+
public String getAccountName() { return accountName; }
|
|
1406
|
+
public void setAccountName(String accountName) { this.accountName = accountName; }
|
|
1407
|
+
|
|
1408
|
+
public BigDecimal getDebitAmount() { return debitAmount; }
|
|
1409
|
+
public void setDebitAmount(BigDecimal debitAmount) { this.debitAmount = debitAmount; }
|
|
1410
|
+
|
|
1411
|
+
public BigDecimal getCreditAmount() { return creditAmount; }
|
|
1412
|
+
public void setCreditAmount(BigDecimal creditAmount) { this.creditAmount = creditAmount; }
|
|
1413
|
+
}
|
|
1414
|
+
```
|
|
1415
|
+
|
|
1416
|
+
</details>
|
|
1417
|
+
|
|
1418
|
+
---
|
|
1419
|
+
|
|
1420
|
+
### 22.6.3 JPA Repository インターフェース
|
|
1421
|
+
|
|
1422
|
+
<details>
|
|
1423
|
+
<summary>コード例: JournalEntryJpaRepository.java</summary>
|
|
1424
|
+
|
|
1425
|
+
```java
|
|
1426
|
+
package com.example.accounting.journal.adapter.outbound.persistence.repository;
|
|
1427
|
+
|
|
1428
|
+
import com.example.accounting.journal.adapter.outbound.persistence.entity.JournalEntryEntity;
|
|
1429
|
+
import org.springframework.data.jpa.repository.EntityGraph;
|
|
1430
|
+
import org.springframework.data.jpa.repository.JpaRepository;
|
|
1431
|
+
import org.springframework.data.jpa.repository.Query;
|
|
1432
|
+
import org.springframework.data.repository.query.Param;
|
|
1433
|
+
import org.springframework.stereotype.Repository;
|
|
1434
|
+
|
|
1435
|
+
import java.time.LocalDate;
|
|
1436
|
+
import java.util.List;
|
|
1437
|
+
import java.util.Optional;
|
|
1438
|
+
|
|
1439
|
+
/**
|
|
1440
|
+
* JournalEntry JPA Repository
|
|
1441
|
+
*/
|
|
1442
|
+
@Repository
|
|
1443
|
+
public interface JournalEntryJpaRepository extends JpaRepository<JournalEntryEntity, String> {
|
|
1444
|
+
|
|
1445
|
+
/**
|
|
1446
|
+
* ID で検索(明細を EAGER ロード)
|
|
1447
|
+
*/
|
|
1448
|
+
@EntityGraph(attributePaths = {"lines"})
|
|
1449
|
+
Optional<JournalEntryEntity> findWithLinesById(String journalEntryId);
|
|
1450
|
+
|
|
1451
|
+
/**
|
|
1452
|
+
* ステータスで検索
|
|
1453
|
+
*/
|
|
1454
|
+
List<JournalEntryEntity> findByStatus(String status);
|
|
1455
|
+
|
|
1456
|
+
/**
|
|
1457
|
+
* 期間で検索
|
|
1458
|
+
*/
|
|
1459
|
+
List<JournalEntryEntity> findByTransactionDateBetween(LocalDate startDate, LocalDate endDate);
|
|
1460
|
+
|
|
1461
|
+
/**
|
|
1462
|
+
* 会計期間で検索
|
|
1463
|
+
*/
|
|
1464
|
+
List<JournalEntryEntity> findByFiscalPeriod(String fiscalPeriod);
|
|
1465
|
+
|
|
1466
|
+
/**
|
|
1467
|
+
* 勘定科目コードを含む仕訳を検索
|
|
1468
|
+
*/
|
|
1469
|
+
@Query("SELECT DISTINCT je FROM JournalEntryEntity je JOIN je.lines l WHERE l.accountCode = :accountCode")
|
|
1470
|
+
List<JournalEntryEntity> findByAccountCode(@Param("accountCode") String accountCode);
|
|
1471
|
+
|
|
1472
|
+
/**
|
|
1473
|
+
* 仕訳番号で検索
|
|
1474
|
+
*/
|
|
1475
|
+
Optional<JournalEntryEntity> findByJournalNumber(String journalNumber);
|
|
1476
|
+
}
|
|
1477
|
+
```
|
|
1478
|
+
|
|
1479
|
+
</details>
|
|
1480
|
+
|
|
1481
|
+
---
|
|
1482
|
+
|
|
1483
|
+
## 22.7 REST API と統合テスト
|
|
1484
|
+
|
|
1485
|
+
### 22.7.1 REST Controller(Inbound Adapter)
|
|
1486
|
+
|
|
1487
|
+
<details>
|
|
1488
|
+
<summary>コード例: JournalsController.java</summary>
|
|
1489
|
+
|
|
1490
|
+
```java
|
|
1491
|
+
package com.example.accounting.journal.adapter.inbound.rest.journals;
|
|
1492
|
+
|
|
1493
|
+
import com.example.accounting.journal.adapter.inbound.rest.journals.protocol.*;
|
|
1494
|
+
import com.example.accounting.journal.adapter.outbound.persistence.repository.JournalEntryJpaRepository;
|
|
1495
|
+
import com.example.accounting.journal.api.events.JournalEntryCreatedEvent.JournalLineData;
|
|
1496
|
+
import com.example.accounting.journal.domain.model.aggregate.journal.JournalEntryCommands.*;
|
|
1497
|
+
import io.swagger.v3.oas.annotations.Operation;
|
|
1498
|
+
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
1499
|
+
import jakarta.validation.Valid;
|
|
1500
|
+
import org.axonframework.commandhandling.gateway.CommandGateway;
|
|
1501
|
+
import org.springframework.http.HttpStatus;
|
|
1502
|
+
import org.springframework.http.ResponseEntity;
|
|
1503
|
+
import org.springframework.web.bind.annotation.*;
|
|
1504
|
+
|
|
1505
|
+
import java.util.UUID;
|
|
1506
|
+
import java.util.concurrent.CompletableFuture;
|
|
1507
|
+
|
|
1508
|
+
/**
|
|
1509
|
+
* Journal REST Controller(Inbound Adapter)
|
|
1510
|
+
*/
|
|
1511
|
+
@RestController
|
|
1512
|
+
@RequestMapping("/api/journals")
|
|
1513
|
+
@Tag(name = "Journals", description = "Journal Entry management API")
|
|
1514
|
+
public class JournalsController {
|
|
1515
|
+
|
|
1516
|
+
private final CommandGateway commandGateway;
|
|
1517
|
+
private final JournalEntryJpaRepository repository;
|
|
1518
|
+
|
|
1519
|
+
public JournalsController(CommandGateway commandGateway, JournalEntryJpaRepository repository) {
|
|
1520
|
+
this.commandGateway = commandGateway;
|
|
1521
|
+
this.repository = repository;
|
|
1522
|
+
}
|
|
1523
|
+
|
|
1524
|
+
// ========== Command Side (Write) ==========
|
|
1525
|
+
|
|
1526
|
+
@PostMapping
|
|
1527
|
+
@Operation(summary = "Create a new journal entry")
|
|
1528
|
+
public CompletableFuture<ResponseEntity<JournalCreateResponse>> create(
|
|
1529
|
+
@Valid @RequestBody JournalCreateRequest request
|
|
1530
|
+
) {
|
|
1531
|
+
var journalEntryId = UUID.randomUUID().toString();
|
|
1532
|
+
|
|
1533
|
+
var lineInputs = request.lines().stream()
|
|
1534
|
+
.map(line -> new JournalLineInput(
|
|
1535
|
+
line.accountCode(),
|
|
1536
|
+
line.accountName(),
|
|
1537
|
+
line.debitAmount(),
|
|
1538
|
+
line.creditAmount()
|
|
1539
|
+
))
|
|
1540
|
+
.toList();
|
|
1541
|
+
|
|
1542
|
+
var command = new CreateJournalEntryCommand(
|
|
1543
|
+
journalEntryId,
|
|
1544
|
+
request.transactionDate(),
|
|
1545
|
+
request.description(),
|
|
1546
|
+
lineInputs,
|
|
1547
|
+
request.createdBy()
|
|
1548
|
+
);
|
|
1549
|
+
|
|
1550
|
+
return commandGateway.send(command)
|
|
1551
|
+
.thenApply(result -> ResponseEntity
|
|
1552
|
+
.status(HttpStatus.CREATED)
|
|
1553
|
+
.body(new JournalCreateResponse(journalEntryId)));
|
|
1554
|
+
}
|
|
1555
|
+
|
|
1556
|
+
@PostMapping("/{id}/submit")
|
|
1557
|
+
@Operation(summary = "Submit journal entry for approval")
|
|
1558
|
+
public CompletableFuture<ResponseEntity<Void>> submit(
|
|
1559
|
+
@PathVariable String id,
|
|
1560
|
+
@Valid @RequestBody JournalSubmitRequest request
|
|
1561
|
+
) {
|
|
1562
|
+
var command = new SubmitJournalEntryCommand(id, request.submittedBy());
|
|
1563
|
+
|
|
1564
|
+
return commandGateway.send(command)
|
|
1565
|
+
.thenApply(result -> ResponseEntity.ok().<Void>build());
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
@PostMapping("/{id}/approve")
|
|
1569
|
+
@Operation(summary = "Approve journal entry")
|
|
1570
|
+
public CompletableFuture<ResponseEntity<Void>> approve(
|
|
1571
|
+
@PathVariable String id,
|
|
1572
|
+
@Valid @RequestBody JournalApproveRequest request
|
|
1573
|
+
) {
|
|
1574
|
+
var command = new ApproveJournalEntryCommand(id, request.approvedBy(), request.comment());
|
|
1575
|
+
|
|
1576
|
+
return commandGateway.send(command)
|
|
1577
|
+
.thenApply(result -> ResponseEntity.ok().<Void>build());
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1580
|
+
@PostMapping("/{id}/reject")
|
|
1581
|
+
@Operation(summary = "Reject journal entry")
|
|
1582
|
+
public CompletableFuture<ResponseEntity<Void>> reject(
|
|
1583
|
+
@PathVariable String id,
|
|
1584
|
+
@Valid @RequestBody JournalRejectRequest request
|
|
1585
|
+
) {
|
|
1586
|
+
var command = new RejectJournalEntryCommand(id, request.rejectedBy(), request.reason());
|
|
1587
|
+
|
|
1588
|
+
return commandGateway.send(command)
|
|
1589
|
+
.thenApply(result -> ResponseEntity.ok().<Void>build());
|
|
1590
|
+
}
|
|
1591
|
+
|
|
1592
|
+
@PostMapping("/{id}/post")
|
|
1593
|
+
@Operation(summary = "Post journal entry to general ledger")
|
|
1594
|
+
public CompletableFuture<ResponseEntity<Void>> post(
|
|
1595
|
+
@PathVariable String id,
|
|
1596
|
+
@Valid @RequestBody JournalPostRequest request
|
|
1597
|
+
) {
|
|
1598
|
+
var command = new PostJournalEntryCommand(id, request.fiscalPeriod(), request.postedBy());
|
|
1599
|
+
|
|
1600
|
+
return commandGateway.send(command)
|
|
1601
|
+
.thenApply(result -> ResponseEntity.ok().<Void>build());
|
|
1602
|
+
}
|
|
1603
|
+
|
|
1604
|
+
@PostMapping("/{id}/cancel")
|
|
1605
|
+
@Operation(summary = "Cancel journal entry")
|
|
1606
|
+
public CompletableFuture<ResponseEntity<Void>> cancel(
|
|
1607
|
+
@PathVariable String id,
|
|
1608
|
+
@Valid @RequestBody JournalCancelRequest request
|
|
1609
|
+
) {
|
|
1610
|
+
var command = new CancelJournalEntryCommand(id, request.cancelledBy(), request.reason());
|
|
1611
|
+
|
|
1612
|
+
return commandGateway.send(command)
|
|
1613
|
+
.thenApply(result -> ResponseEntity.ok().<Void>build());
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1616
|
+
// ========== Query Side (Read) ==========
|
|
1617
|
+
|
|
1618
|
+
@GetMapping("/{id}")
|
|
1619
|
+
@Operation(summary = "Get a journal entry by ID")
|
|
1620
|
+
public ResponseEntity<JournalGetResponse> get(@PathVariable String id) {
|
|
1621
|
+
return repository.findWithLinesById(id)
|
|
1622
|
+
.map(entity -> ResponseEntity.ok(JournalGetResponse.from(entity)))
|
|
1623
|
+
.orElse(ResponseEntity.notFound().build());
|
|
1624
|
+
}
|
|
1625
|
+
|
|
1626
|
+
@GetMapping
|
|
1627
|
+
@Operation(summary = "Get all journal entries")
|
|
1628
|
+
public ResponseEntity<JournalListResponse> getAll() {
|
|
1629
|
+
var entities = repository.findAll();
|
|
1630
|
+
var items = entities.stream()
|
|
1631
|
+
.map(JournalGetResponse::from)
|
|
1632
|
+
.toList();
|
|
1633
|
+
return ResponseEntity.ok(new JournalListResponse(items));
|
|
1634
|
+
}
|
|
1635
|
+
|
|
1636
|
+
@GetMapping("/status/{status}")
|
|
1637
|
+
@Operation(summary = "Get journal entries by status")
|
|
1638
|
+
public ResponseEntity<JournalListResponse> getByStatus(@PathVariable String status) {
|
|
1639
|
+
var entities = repository.findByStatus(status);
|
|
1640
|
+
var items = entities.stream()
|
|
1641
|
+
.map(JournalGetResponse::from)
|
|
1642
|
+
.toList();
|
|
1643
|
+
return ResponseEntity.ok(new JournalListResponse(items));
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
```
|
|
1647
|
+
|
|
1648
|
+
</details>
|
|
1649
|
+
|
|
1650
|
+
---
|
|
1651
|
+
|
|
1652
|
+
### 22.7.2 API エンドポイント一覧
|
|
1653
|
+
|
|
1654
|
+
| メソッド | パス | 説明 | 種別 |
|
|
1655
|
+
|---------|------|------|------|
|
|
1656
|
+
| POST | `/api/journals` | 仕訳作成 | Command |
|
|
1657
|
+
| POST | `/api/journals/{id}/submit` | 承認申請 | Command |
|
|
1658
|
+
| POST | `/api/journals/{id}/approve` | 承認 | Command |
|
|
1659
|
+
| POST | `/api/journals/{id}/reject` | 却下 | Command |
|
|
1660
|
+
| POST | `/api/journals/{id}/post` | 転記 | Command |
|
|
1661
|
+
| POST | `/api/journals/{id}/cancel` | キャンセル | Command |
|
|
1662
|
+
| GET | `/api/journals` | 仕訳一覧取得 | Query |
|
|
1663
|
+
| GET | `/api/journals/{id}` | 仕訳詳細取得 | Query |
|
|
1664
|
+
| GET | `/api/journals/status/{status}` | ステータス別取得 | Query |
|
|
1665
|
+
|
|
1666
|
+
---
|
|
1667
|
+
|
|
1668
|
+
### 22.7.3 Axon Test による単体テスト
|
|
1669
|
+
|
|
1670
|
+
<details>
|
|
1671
|
+
<summary>コード例: JournalEntryAggregateTest.java</summary>
|
|
1672
|
+
|
|
1673
|
+
```java
|
|
1674
|
+
package com.example.accounting.journal.application.aggregate;
|
|
1675
|
+
|
|
1676
|
+
import com.example.accounting.journal.api.events.*;
|
|
1677
|
+
import com.example.accounting.journal.api.events.JournalEntryCreatedEvent.JournalLineData;
|
|
1678
|
+
import com.example.accounting.journal.domain.model.aggregate.journal.JournalEntryCommands.*;
|
|
1679
|
+
import org.axonframework.test.aggregate.AggregateTestFixture;
|
|
1680
|
+
import org.axonframework.test.aggregate.FixtureConfiguration;
|
|
1681
|
+
import org.junit.jupiter.api.BeforeEach;
|
|
1682
|
+
import org.junit.jupiter.api.DisplayName;
|
|
1683
|
+
import org.junit.jupiter.api.Nested;
|
|
1684
|
+
import org.junit.jupiter.api.Test;
|
|
1685
|
+
|
|
1686
|
+
import java.math.BigDecimal;
|
|
1687
|
+
import java.time.LocalDate;
|
|
1688
|
+
import java.time.LocalDateTime;
|
|
1689
|
+
import java.util.List;
|
|
1690
|
+
|
|
1691
|
+
class JournalEntryAggregateTest {
|
|
1692
|
+
|
|
1693
|
+
private FixtureConfiguration<JournalEntryAggregateAdapter> fixture;
|
|
1694
|
+
|
|
1695
|
+
@BeforeEach
|
|
1696
|
+
void setUp() {
|
|
1697
|
+
fixture = new AggregateTestFixture<>(JournalEntryAggregateAdapter.class);
|
|
1698
|
+
}
|
|
1699
|
+
|
|
1700
|
+
@Nested
|
|
1701
|
+
@DisplayName("仕訳作成")
|
|
1702
|
+
class CreateJournalEntry {
|
|
1703
|
+
|
|
1704
|
+
@Test
|
|
1705
|
+
@DisplayName("貸借一致の仕訳を作成できる")
|
|
1706
|
+
void shouldCreateBalancedJournalEntry() {
|
|
1707
|
+
var command = new CreateJournalEntryCommand(
|
|
1708
|
+
"je-1",
|
|
1709
|
+
LocalDate.now(),
|
|
1710
|
+
"売上計上",
|
|
1711
|
+
List.of(
|
|
1712
|
+
new JournalLineInput("1100", "売掛金", BigDecimal.valueOf(10000), BigDecimal.ZERO),
|
|
1713
|
+
new JournalLineInput("4100", "売上高", BigDecimal.ZERO, BigDecimal.valueOf(10000))
|
|
1714
|
+
),
|
|
1715
|
+
"user-1"
|
|
1716
|
+
);
|
|
1717
|
+
|
|
1718
|
+
fixture.givenNoPriorActivity()
|
|
1719
|
+
.when(command)
|
|
1720
|
+
.expectSuccessfulHandlerExecution()
|
|
1721
|
+
.expectEventsMatching(events ->
|
|
1722
|
+
events.getPayload() instanceof JournalEntryCreatedEvent
|
|
1723
|
+
);
|
|
1724
|
+
}
|
|
1725
|
+
|
|
1726
|
+
@Test
|
|
1727
|
+
@DisplayName("貸借不一致の場合はエラー")
|
|
1728
|
+
void shouldRejectUnbalancedJournalEntry() {
|
|
1729
|
+
var command = new CreateJournalEntryCommand(
|
|
1730
|
+
"je-1",
|
|
1731
|
+
LocalDate.now(),
|
|
1732
|
+
"不正な仕訳",
|
|
1733
|
+
List.of(
|
|
1734
|
+
new JournalLineInput("1100", "売掛金", BigDecimal.valueOf(10000), BigDecimal.ZERO),
|
|
1735
|
+
new JournalLineInput("4100", "売上高", BigDecimal.ZERO, BigDecimal.valueOf(5000))
|
|
1736
|
+
),
|
|
1737
|
+
"user-1"
|
|
1738
|
+
);
|
|
1739
|
+
|
|
1740
|
+
fixture.givenNoPriorActivity()
|
|
1741
|
+
.when(command)
|
|
1742
|
+
.expectException(IllegalArgumentException.class);
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1745
|
+
|
|
1746
|
+
@Nested
|
|
1747
|
+
@DisplayName("仕訳承認")
|
|
1748
|
+
class ApproveJournalEntry {
|
|
1749
|
+
|
|
1750
|
+
@Test
|
|
1751
|
+
@DisplayName("承認待ちの仕訳を承認できる")
|
|
1752
|
+
void shouldApprovePendingJournalEntry() {
|
|
1753
|
+
var createdEvent = new JournalEntryCreatedEvent(
|
|
1754
|
+
"je-1",
|
|
1755
|
+
"JE-001",
|
|
1756
|
+
LocalDate.now(),
|
|
1757
|
+
"売上計上",
|
|
1758
|
+
List.of(
|
|
1759
|
+
new JournalLineData("1100", "売掛金", BigDecimal.valueOf(10000), BigDecimal.ZERO),
|
|
1760
|
+
new JournalLineData("4100", "売上高", BigDecimal.ZERO, BigDecimal.valueOf(10000))
|
|
1761
|
+
),
|
|
1762
|
+
"user-1",
|
|
1763
|
+
LocalDateTime.now()
|
|
1764
|
+
);
|
|
1765
|
+
var submittedEvent = new JournalEntrySubmittedEvent("je-1", "user-1", LocalDateTime.now());
|
|
1766
|
+
|
|
1767
|
+
fixture.given(createdEvent, submittedEvent)
|
|
1768
|
+
.when(new ApproveJournalEntryCommand("je-1", "manager-1", "承認します"))
|
|
1769
|
+
.expectSuccessfulHandlerExecution()
|
|
1770
|
+
.expectEventsMatching(events ->
|
|
1771
|
+
events.getPayload() instanceof JournalEntryApprovedEvent
|
|
1772
|
+
);
|
|
1773
|
+
}
|
|
1774
|
+
}
|
|
1775
|
+
|
|
1776
|
+
@Nested
|
|
1777
|
+
@DisplayName("仕訳転記")
|
|
1778
|
+
class PostJournalEntry {
|
|
1779
|
+
|
|
1780
|
+
@Test
|
|
1781
|
+
@DisplayName("承認済みの仕訳を転記できる")
|
|
1782
|
+
void shouldPostApprovedJournalEntry() {
|
|
1783
|
+
var createdEvent = new JournalEntryCreatedEvent(
|
|
1784
|
+
"je-1",
|
|
1785
|
+
"JE-001",
|
|
1786
|
+
LocalDate.now(),
|
|
1787
|
+
"売上計上",
|
|
1788
|
+
List.of(
|
|
1789
|
+
new JournalLineData("1100", "売掛金", BigDecimal.valueOf(10000), BigDecimal.ZERO),
|
|
1790
|
+
new JournalLineData("4100", "売上高", BigDecimal.ZERO, BigDecimal.valueOf(10000))
|
|
1791
|
+
),
|
|
1792
|
+
"user-1",
|
|
1793
|
+
LocalDateTime.now()
|
|
1794
|
+
);
|
|
1795
|
+
var submittedEvent = new JournalEntrySubmittedEvent("je-1", "user-1", LocalDateTime.now());
|
|
1796
|
+
var approvedEvent = new JournalEntryApprovedEvent("je-1", "manager-1", "OK", LocalDateTime.now());
|
|
1797
|
+
|
|
1798
|
+
fixture.given(createdEvent, submittedEvent, approvedEvent)
|
|
1799
|
+
.when(new PostJournalEntryCommand("je-1", "2024-01", "system"))
|
|
1800
|
+
.expectSuccessfulHandlerExecution()
|
|
1801
|
+
.expectEventsMatching(events ->
|
|
1802
|
+
events.getPayload() instanceof JournalEntryPostedEvent
|
|
1803
|
+
);
|
|
1804
|
+
}
|
|
1805
|
+
|
|
1806
|
+
@Test
|
|
1807
|
+
@DisplayName("未承認の仕訳は転記できない")
|
|
1808
|
+
void shouldNotPostUnapprovedJournalEntry() {
|
|
1809
|
+
var createdEvent = new JournalEntryCreatedEvent(
|
|
1810
|
+
"je-1",
|
|
1811
|
+
"JE-001",
|
|
1812
|
+
LocalDate.now(),
|
|
1813
|
+
"売上計上",
|
|
1814
|
+
List.of(
|
|
1815
|
+
new JournalLineData("1100", "売掛金", BigDecimal.valueOf(10000), BigDecimal.ZERO),
|
|
1816
|
+
new JournalLineData("4100", "売上高", BigDecimal.ZERO, BigDecimal.valueOf(10000))
|
|
1817
|
+
),
|
|
1818
|
+
"user-1",
|
|
1819
|
+
LocalDateTime.now()
|
|
1820
|
+
);
|
|
1821
|
+
|
|
1822
|
+
fixture.given(createdEvent)
|
|
1823
|
+
.when(new PostJournalEntryCommand("je-1", "2024-01", "system"))
|
|
1824
|
+
.expectException(IllegalStateException.class);
|
|
1825
|
+
}
|
|
1826
|
+
}
|
|
1827
|
+
}
|
|
1828
|
+
```
|
|
1829
|
+
|
|
1830
|
+
</details>
|
|
1831
|
+
|
|
1832
|
+
---
|
|
1833
|
+
|
|
1834
|
+
## 22.8 まとめ
|
|
1835
|
+
|
|
1836
|
+
### 22.8.1 実装した機能一覧
|
|
1837
|
+
|
|
1838
|
+
| 項目 | 内容 | 主要技術 |
|
|
1839
|
+
|---|---|---|
|
|
1840
|
+
| **CQRS/ES アーキテクチャ** | コマンドとクエリの分離 | Axon Framework, Event Sourcing |
|
|
1841
|
+
| **ドメイン設計** | イベント、コマンド、ドメインモデル | sealed interface, record |
|
|
1842
|
+
| **Aggregate Adapter** | フレームワーク依存の分離 | @Aggregate, @CommandHandler |
|
|
1843
|
+
| **Projection** | Read Model の更新 | @EventHandler, Spring Data JPA |
|
|
1844
|
+
| **REST API** | Inbound Adapter, テスト | CommandGateway, Axon Test |
|
|
1845
|
+
|
|
1846
|
+
### 22.8.2 MyBatis 版と JPA 版の比較
|
|
1847
|
+
|
|
1848
|
+
| 観点 | MyBatis 版 | JPA 版 |
|
|
1849
|
+
|------|-----------|--------|
|
|
1850
|
+
| **設定** | MapperScan, XML マッピング | EnableJpaRepositories |
|
|
1851
|
+
| **Event Store** | JdbcEventStorageEngine | JpaEventStorageEngine |
|
|
1852
|
+
| **Repository** | Mapper インターフェース + XML | JpaRepository インターフェース |
|
|
1853
|
+
| **Projection** | Mapper.insert/update | Repository.save |
|
|
1854
|
+
| **エンティティ** | POJO | @Entity アノテーション |
|
|
1855
|
+
| **リレーション** | 手動結合 | @OneToMany/@ManyToOne |
|
|
1856
|
+
| **クエリ** | XML で SQL 定義 | メソッド名規約 + @Query |
|
|
1857
|
+
|
|
1858
|
+
### 22.8.3 JPA 版の利点
|
|
1859
|
+
|
|
1860
|
+
| 利点 | 説明 |
|
|
1861
|
+
|------|------|
|
|
1862
|
+
| **コード量削減** | Spring Data JPA による自動実装 |
|
|
1863
|
+
| **型安全性** | コンパイル時にクエリメソッドをチェック |
|
|
1864
|
+
| **リレーション管理** | @OneToMany によるカスケード操作 |
|
|
1865
|
+
| **監査機能** | @EnableJpaAuditing による自動タイムスタンプ |
|
|
1866
|
+
| **N+1 対策** | @EntityGraph による EAGER ロード制御 |
|
|
1867
|
+
|
|
1868
|
+
### 22.8.4 財務会計システムにおける CQRS/ES の意義
|
|
1869
|
+
|
|
1870
|
+
| 観点 | 説明 |
|
|
1871
|
+
|------|------|
|
|
1872
|
+
| **監査証跡** | すべての仕訳変更履歴を完全に保持 |
|
|
1873
|
+
| **内部統制** | SOX 法対応の監査ログを自動生成 |
|
|
1874
|
+
| **訂正仕訳** | 修正履歴を完全に追跡可能 |
|
|
1875
|
+
| **期末決算** | 任意の時点での残高を再計算可能 |
|
|
1876
|
+
| **試算表生成** | Read Model を使った高速クエリ |
|
|
1877
|
+
|
|
1878
|
+
CQRS/ES は導入コストが高いですが、監査要件が厳しい財務会計システムでは、その恩恵は大きくなります。JPA を使用することで、Read Model の管理が簡潔になり、Spring Data JPA の豊富な機能を活用できます。
|