@k2works/claude-code-booster 3.2.1 → 3.3.1

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.
Files changed (70) hide show
  1. package/lib/assets/.claude/skills/analyzing-business/SKILL.md +2 -2
  2. package/lib/assets/.claude/skills/analyzing-inception-deck/SKILL.md +5 -5
  3. package/lib/assets/.claude/skills/analyzing-requirements/SKILL.md +2 -2
  4. package/lib/assets/.claude/skills/generating-slides/SKILL.md +7 -7
  5. package/lib/assets/docs/article/index.md +4 -1
  6. package/lib/assets/docs/article/practical-database-design/index.md +121 -0
  7. package/lib/assets/docs/article/practical-database-design/part1/chapter01.md +288 -0
  8. package/lib/assets/docs/article/practical-database-design/part1/chapter02.md +518 -0
  9. package/lib/assets/docs/article/practical-database-design/part1/chapter03.md +557 -0
  10. package/lib/assets/docs/article/practical-database-design/part2/chapter04.md +924 -0
  11. package/lib/assets/docs/article/practical-database-design/part2/chapter05.md +1627 -0
  12. package/lib/assets/docs/article/practical-database-design/part2/chapter06.md +2716 -0
  13. package/lib/assets/docs/article/practical-database-design/part2/chapter07.md +2082 -0
  14. package/lib/assets/docs/article/practical-database-design/part2/chapter08.md +2105 -0
  15. package/lib/assets/docs/article/practical-database-design/part2/chapter09.md +2031 -0
  16. package/lib/assets/docs/article/practical-database-design/part2/chapter10.md +1387 -0
  17. package/lib/assets/docs/article/practical-database-design/part2/chapter11.md +1677 -0
  18. package/lib/assets/docs/article/practical-database-design/part2/chapter12.md +1417 -0
  19. package/lib/assets/docs/article/practical-database-design/part2/chapter13.md +1434 -0
  20. package/lib/assets/docs/article/practical-database-design/part3/chapter14.md +667 -0
  21. package/lib/assets/docs/article/practical-database-design/part3/chapter15.md +1625 -0
  22. package/lib/assets/docs/article/practical-database-design/part3/chapter16.md +1915 -0
  23. package/lib/assets/docs/article/practical-database-design/part3/chapter17.md +1708 -0
  24. package/lib/assets/docs/article/practical-database-design/part3/chapter18.md +2095 -0
  25. package/lib/assets/docs/article/practical-database-design/part3/chapter19.md +1123 -0
  26. package/lib/assets/docs/article/practical-database-design/part3/chapter20.md +1031 -0
  27. package/lib/assets/docs/article/practical-database-design/part3/chapter21.md +1382 -0
  28. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter14-orm.md +991 -0
  29. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter15-orm.md +1300 -0
  30. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter16-orm.md +1166 -0
  31. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter17-orm.md +1584 -0
  32. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter18-orm.md +1183 -0
  33. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter19-orm.md +1016 -0
  34. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter20-orm.md +1753 -0
  35. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter21-orm.md +1447 -0
  36. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter22-orm.md +1878 -0
  37. package/lib/assets/docs/article/practical-database-design/part4/chapter22.md +965 -0
  38. package/lib/assets/docs/article/practical-database-design/part4/chapter23.md +2069 -0
  39. package/lib/assets/docs/article/practical-database-design/part4/chapter24.md +2439 -0
  40. package/lib/assets/docs/article/practical-database-design/part4/chapter25.md +3661 -0
  41. package/lib/assets/docs/article/practical-database-design/part4/chapter26.md +2916 -0
  42. package/lib/assets/docs/article/practical-database-design/part4/chapter27.md +3105 -0
  43. package/lib/assets/docs/article/practical-database-design/part4/chapter28.md +2697 -0
  44. package/lib/assets/docs/article/practical-database-design/part4/chapter29.md +2544 -0
  45. package/lib/assets/docs/article/practical-database-design/part4/chapter30.md +2180 -0
  46. package/lib/assets/docs/article/practical-database-design/part4/chapter31.md +1192 -0
  47. package/lib/assets/docs/article/practical-database-design/part4/chapter32.md +2101 -0
  48. package/lib/assets/docs/article/practical-database-design/part5/chapter33.md +1032 -0
  49. package/lib/assets/docs/article/practical-database-design/part5/chapter34.md +1609 -0
  50. package/lib/assets/docs/article/practical-database-design/part5/chapter35.md +1453 -0
  51. package/lib/assets/docs/article/practical-database-design/part5/chapter36.md +1292 -0
  52. package/lib/assets/docs/article/practical-database-design/part5/chapter37.md +1470 -0
  53. package/lib/assets/docs/article/practical-database-design/part5/chapter38.md +1698 -0
  54. package/lib/assets/docs/article/practical-database-design/part5/chapter39.md +2334 -0
  55. package/lib/assets/docs/article/practical-database-design/study/study2-1.md +1693 -0
  56. package/lib/assets/docs/article/practical-database-design/study/study2-2.md +1347 -0
  57. package/lib/assets/docs/article/practical-database-design/study/study2-3.md +2044 -0
  58. package/lib/assets/docs/article/practical-database-design/study/study2-4.md +2229 -0
  59. package/lib/assets/docs/article/practical-database-design/study/study2-5.md +2418 -0
  60. package/lib/assets/docs/article/practical-database-design/study/study3-1.md +2205 -0
  61. package/lib/assets/docs/article/practical-database-design/study/study3-2.md +2221 -0
  62. package/lib/assets/docs/article/practical-database-design/study/study3-3.md +2253 -0
  63. package/lib/assets/docs/article/practical-database-design/study/study3-4.md +2106 -0
  64. package/lib/assets/docs/article/practical-database-design/study/study3-5.md +2507 -0
  65. package/lib/assets/docs/article/practical-database-design/study/study4-1.md +2587 -0
  66. package/lib/assets/docs/article/practical-database-design/study/study4-2.md +2075 -0
  67. package/lib/assets/docs/article/practical-database-design/study/study4-3.md +1805 -0
  68. package/lib/assets/docs/article/practical-database-design/study/study4-4.md +1895 -0
  69. package/lib/assets/docs/article/practical-database-design/study/study4-5.md +2878 -0
  70. package/package.json +1 -1
@@ -0,0 +1,2253 @@
1
+ # 研究 3:gRPC サービスの実装
2
+
3
+ ## はじめに
4
+
5
+ 本パートでは、API サーバー構成とは異なるアプローチとして、**gRPC** による財務会計システムを実装します。Protocol Buffers による高効率なバイナリシリアライゼーションと、HTTP/2 によるストリーミング通信を活用し、高性能なマイクロサービス間通信を実現します。
6
+
7
+ ヘキサゴナルアーキテクチャ(ドメイン層・アプリケーション層)はそのまま共有し、**Input Adapter として gRPC サービス層のみを追加**します。
8
+
9
+ ---
10
+
11
+ ## 第 22 章:gRPC サーバーの基礎
12
+
13
+ ### 22.1 gRPC とは
14
+
15
+ gRPC は Google が開発したオープンソースの高性能 RPC(Remote Procedure Call)フレームワークです。HTTP/2 プロトコルと Protocol Buffers を基盤とし、マイクロサービス間通信に最適化されています。
16
+
17
+ ```plantuml
18
+ @startuml grpc_architecture
19
+ !define RECTANGLE class
20
+
21
+ skinparam backgroundColor #FEFEFE
22
+
23
+ package "gRPC Architecture (財務会計システム)" {
24
+
25
+ package "Client Side" {
26
+ RECTANGLE "gRPC Client\n(Generated Stub)" as client {
27
+ - 単項 RPC
28
+ - サーバーストリーミング
29
+ - クライアントストリーミング
30
+ - 双方向ストリーミング
31
+ }
32
+ }
33
+
34
+ package "Server Side" {
35
+ RECTANGLE "gRPC Server\n(grpc-spring-boot-starter)" as server {
36
+ - AccountService
37
+ - JournalService
38
+ - BalanceService
39
+ - ReportService
40
+ }
41
+ }
42
+
43
+ package "Shared" {
44
+ RECTANGLE "Protocol Buffers\n(.proto files)" as proto {
45
+ - account.proto
46
+ - journal.proto
47
+ - balance.proto
48
+ - common.proto
49
+ }
50
+ }
51
+ }
52
+
53
+ client --> proto : ".proto からスタブ生成"
54
+ server --> proto : ".proto からスケルトン生成"
55
+ client <--> server : "HTTP/2\n(Binary Protocol)"
56
+
57
+ note bottom of proto
58
+ スキーマ駆動開発
59
+ 言語中立なインターフェース定義
60
+ 高効率なバイナリシリアライゼーション
61
+ end note
62
+
63
+ @enduml
64
+ ```
65
+
66
+ **gRPC の主な特徴:**
67
+
68
+ | 特徴 | 説明 |
69
+ |------|------|
70
+ | HTTP/2 | 多重化、ヘッダー圧縮、バイナリフレーミング |
71
+ | Protocol Buffers | 高速なバイナリシリアライゼーション |
72
+ | 多言語サポート | Java, Go, Python, C#, Node.js 等 |
73
+ | ストリーミング | 4 つの RPC パターンをサポート |
74
+ | デッドライン | タイムアウト管理の組み込みサポート |
75
+ | 認証 | TLS/SSL、トークンベース認証のサポート |
76
+
77
+ ### 22.2 REST API / GraphQL との比較
78
+
79
+ ```plantuml
80
+ @startuml api_comparison
81
+ skinparam backgroundColor #FEFEFE
82
+
83
+ rectangle "REST API" as rest {
84
+ (HTTP/1.1 or 2)
85
+ (JSON/XML)
86
+ (OpenAPI)
87
+ }
88
+
89
+ rectangle "gRPC" as grpc {
90
+ (HTTP/2)
91
+ (Protocol Buffers)
92
+ (.proto)
93
+ }
94
+
95
+ rectangle "GraphQL" as graphql {
96
+ (HTTP/1.1 or 2)
97
+ (JSON)
98
+ (.graphqls)
99
+ }
100
+
101
+ note bottom of rest
102
+ 汎用性が高い
103
+ ブラウザ直接アクセス可能
104
+ 人間が読みやすい
105
+ end note
106
+
107
+ note bottom of grpc
108
+ 高性能
109
+ マイクロサービス向け
110
+ 型安全
111
+ end note
112
+
113
+ note bottom of graphql
114
+ 柔軟なクエリ
115
+ フロントエンド向け
116
+ 単一エンドポイント
117
+ end note
118
+
119
+ @enduml
120
+ ```
121
+
122
+ **詳細比較表:**
123
+
124
+ | 特徴 | REST API | gRPC | GraphQL |
125
+ |------|----------|------|---------|
126
+ | プロトコル | HTTP/1.1 | HTTP/2 | HTTP/1.1 or HTTP/2 |
127
+ | データ形式 | JSON | Protocol Buffers | JSON |
128
+ | スキーマ | OpenAPI (任意) | .proto (必須) | .graphqls (必須) |
129
+ | シリアライゼーション | テキスト | バイナリ | テキスト |
130
+ | パフォーマンス | 中 | 高 | 中 |
131
+ | ストリーミング | WebSocket 別実装 | ネイティブサポート | Subscription |
132
+ | ブラウザ対応 | ◎ | △ (gRPC-Web) | ◎ |
133
+ | 学習コスト | 低 | 中 | 中 |
134
+ | 主な用途 | 汎用 API | マイクロサービス | フロントエンド向け |
135
+
136
+ **財務会計システムで gRPC を選択する場面:**
137
+
138
+ 1. **基幹システム間連携**: 販売管理・購買管理との高性能な内部通信
139
+ 2. **リアルタイム残高更新**: ストリーミングによる即時反映通知
140
+ 3. **一括仕訳処理**: クライアントストリーミングによる大量データ転送
141
+ 4. **月次締め処理**: 長時間処理の進捗通知
142
+
143
+ ### 22.3 4 つの RPC パターン
144
+
145
+ gRPC は 4 つの RPC パターンをサポートします。
146
+
147
+ ```plantuml
148
+ @startuml grpc_patterns
149
+ skinparam backgroundColor #FEFEFE
150
+
151
+ rectangle "1. 単項 RPC (Unary)" as unary {
152
+ (Client) -right-> (Server) : "Request"
153
+ (Server) -left-> (Client) : "Response"
154
+ }
155
+
156
+ rectangle "2. サーバーストリーミング RPC" as server_stream {
157
+ (Client2) -right-> (Server2) : "Request"
158
+ (Server2) -left-> (Client2) : "Response 1"
159
+ (Server2) -left-> (Client2) : "Response 2"
160
+ (Server2) -left-> (Client2) : "Response N"
161
+ }
162
+
163
+ rectangle "3. クライアントストリーミング RPC" as client_stream {
164
+ (Client3) -right-> (Server3) : "Request 1"
165
+ (Client3) -right-> (Server3) : "Request 2"
166
+ (Client3) -right-> (Server3) : "Request N"
167
+ (Server3) -left-> (Client3) : "Response"
168
+ }
169
+
170
+ rectangle "4. 双方向ストリーミング RPC" as bidirectional {
171
+ (Client4) <-right-> (Server4) : "Request/Response Stream"
172
+ }
173
+
174
+ @enduml
175
+ ```
176
+
177
+ **各パターンの用途(財務会計システム):**
178
+
179
+ | パターン | 用途例 |
180
+ |----------|---------------------------|
181
+ | 単項 RPC | 勘定科目取得、仕訳登録 |
182
+ | サーバーストリーミング | 仕訳一覧取得、残高照会 |
183
+ | クライアントストリーミング | 仕訳明細の一括登録 |
184
+ | 双方向ストリーミング | リアルタイム残高同期 |
185
+
186
+ ### 22.4 gRPC におけるヘキサゴナルアーキテクチャ
187
+
188
+ gRPC を導入しても、ヘキサゴナルアーキテクチャ(ドメイン層・アプリケーション層)はそのまま共有し、**Input Adapter として gRPC サービス層のみを追加**します。
189
+
190
+ ```plantuml
191
+ @startuml hexagonal_grpc
192
+ !define RECTANGLE class
193
+
194
+ package "Hexagonal Architecture (gRPC版)" {
195
+
196
+ RECTANGLE "Application Core\n(Domain + Use Cases)" as core {
197
+ - Account (勘定科目)
198
+ - Journal (仕訳)
199
+ - DailyBalance (日次残高)
200
+ - MonthlyBalance (月次残高)
201
+ - AccountUseCase
202
+ - JournalUseCase
203
+ - BalanceUseCase
204
+ }
205
+
206
+ RECTANGLE "Input Adapters\n(Driving Side)" as input {
207
+ - REST Controller(既存)
208
+ - gRPC Service(新規追加)
209
+ - StreamObserver
210
+ - ServerInterceptor
211
+ }
212
+
213
+ RECTANGLE "Output Adapters\n(Driven Side)" as output {
214
+ - MyBatis Repository
215
+ - Database Access
216
+ - Entity Mapping
217
+ }
218
+ }
219
+
220
+ input --> core : "Input Ports\n(Use Cases)"
221
+ core --> output : "Output Ports\n(Repository Interfaces)"
222
+
223
+ note top of core
224
+ 既存のビジネスロジック
225
+ REST API 版と完全に共有
226
+ gRPC 固有のコードは含まない
227
+ end note
228
+
229
+ note left of input
230
+ gRPC サービスを
231
+ Input Adapter として追加
232
+ 既存の REST と共存可能
233
+ end note
234
+
235
+ note right of output
236
+ 既存の Repository を
237
+ そのまま使用
238
+ 変更不要
239
+ end note
240
+
241
+ @enduml
242
+ ```
243
+
244
+ ### 22.5 ディレクトリ構成
245
+
246
+ 既存の構成に `infrastructure/in/grpc/` を追加するだけです。
247
+
248
+ ```
249
+ src/main/java/com/example/accounting/
250
+ ├── domain/ # ドメイン層(API版と共通)
251
+ │ ├── model/
252
+ │ │ ├── account/ # 勘定科目ドメイン
253
+ │ │ ├── journal/ # 仕訳ドメイン
254
+ │ │ ├── balance/ # 残高ドメイン
255
+ │ │ └── tax/ # 課税取引ドメイン
256
+ │ └── exception/
257
+
258
+ ├── application/ # アプリケーション層(API版と共通)
259
+ │ ├── port/
260
+ │ │ ├── in/ # Input Port(ユースケース)
261
+ │ │ └── out/ # Output Port(リポジトリ)
262
+ │ └── service/
263
+
264
+ ├── infrastructure/
265
+ │ ├── in/
266
+ │ │ ├── api/ # Input Adapter(REST実装)- 既存
267
+ │ │ └── grpc/ # Input Adapter(gRPC実装)- 新規追加
268
+ │ │ ├── service/ # gRPC サービス実装
269
+ │ │ ├── interceptor/ # インターセプター
270
+ │ │ └── converter/ # ドメイン ↔ Proto 変換
271
+ │ └── out/
272
+ │ └── persistence/ # Output Adapter(DB実装)- 既存
273
+ │ ├── mapper/
274
+ │ └── repository/
275
+
276
+ ├── config/
277
+
278
+ └── src/main/proto/ # Protocol Buffers 定義
279
+ ├── common.proto
280
+ ├── account.proto
281
+ ├── journal.proto
282
+ ├── balance.proto
283
+ └── report.proto
284
+ ```
285
+
286
+ ### 22.6 技術スタックの追加
287
+
288
+ 既存の `build.gradle.kts` に gRPC 関連の依存関係を追加します。
289
+
290
+ <details>
291
+ <summary>build.gradle.kts(差分)</summary>
292
+
293
+ ```kotlin
294
+ import com.google.protobuf.gradle.*
295
+
296
+ plugins {
297
+ // 既存のプラグイン
298
+ id("java")
299
+ id("org.springframework.boot") version "3.2.0"
300
+ id("io.spring.dependency-management") version "1.1.4"
301
+
302
+ // gRPC 関連を追加
303
+ id("com.google.protobuf") version "0.9.4"
304
+ }
305
+
306
+ dependencies {
307
+ // 既存の依存関係(Spring Boot, MyBatis, PostgreSQL等)はそのまま
308
+
309
+ // gRPC 関連を追加
310
+ implementation("net.devh:grpc-spring-boot-starter:3.1.0.RELEASE")
311
+ implementation("io.grpc:grpc-protobuf:1.62.2")
312
+ implementation("io.grpc:grpc-stub:1.62.2")
313
+ implementation("io.grpc:grpc-services:1.62.2") // ヘルスチェック、リフレクション
314
+
315
+ // Protocol Buffers
316
+ implementation("com.google.protobuf:protobuf-java:3.25.3")
317
+ implementation("com.google.protobuf:protobuf-java-util:3.25.3") // JSON 変換
318
+
319
+ // Jakarta Annotation (gRPC generated code で必要)
320
+ compileOnly("jakarta.annotation:jakarta.annotation-api:2.1.1")
321
+
322
+ // Test
323
+ testImplementation("io.grpc:grpc-testing:1.62.2")
324
+ testImplementation("net.devh:grpc-client-spring-boot-starter:3.1.0.RELEASE")
325
+ }
326
+
327
+ protobuf {
328
+ protoc {
329
+ artifact = "com.google.protobuf:protoc:3.25.3"
330
+ }
331
+ plugins {
332
+ create("grpc") {
333
+ artifact = "io.grpc:protoc-gen-grpc-java:1.62.2"
334
+ }
335
+ }
336
+ generateProtoTasks {
337
+ all().forEach { task ->
338
+ task.plugins {
339
+ create("grpc")
340
+ }
341
+ }
342
+ }
343
+ }
344
+
345
+ // 生成コードのソースパス追加
346
+ sourceSets {
347
+ main {
348
+ java {
349
+ srcDirs(
350
+ "build/generated/source/proto/main/java",
351
+ "build/generated/source/proto/main/grpc"
352
+ )
353
+ }
354
+ }
355
+ }
356
+ ```
357
+
358
+ </details>
359
+
360
+ <details>
361
+ <summary>application.yml(差分)</summary>
362
+
363
+ ```yaml
364
+ # 既存の設定はそのまま
365
+
366
+ # gRPC サーバー設定
367
+ grpc:
368
+ server:
369
+ port: 9090
370
+ reflection-service-enabled: true # gRPC リフレクション有効化
371
+ health-service-enabled: true # ヘルスチェック有効化
372
+ max-inbound-message-size: 4194304 # 4MB
373
+ max-inbound-metadata-size: 8192 # 8KB
374
+
375
+ # ログ設定
376
+ logging:
377
+ level:
378
+ net.devh.boot.grpc: DEBUG
379
+ io.grpc: INFO
380
+ ```
381
+
382
+ </details>
383
+
384
+ ---
385
+
386
+ ## 第 23 章:Protocol Buffers 定義
387
+
388
+ ### 23.1 共通メッセージ定義
389
+
390
+ <details>
391
+ <summary>src/main/proto/common.proto</summary>
392
+
393
+ ```protobuf
394
+ syntax = "proto3";
395
+
396
+ package com.example.accounting;
397
+
398
+ option java_package = "com.example.accounting.infrastructure.in.grpc.proto";
399
+ option java_outer_classname = "CommonProto";
400
+ option java_multiple_files = true;
401
+
402
+ import "google/protobuf/timestamp.proto";
403
+ import "google/protobuf/wrappers.proto";
404
+
405
+ // 共通のページネーション
406
+ message PageRequest {
407
+ int32 page = 1;
408
+ int32 size = 2;
409
+ }
410
+
411
+ message PageInfo {
412
+ int32 page = 1;
413
+ int32 size = 2;
414
+ int32 total_elements = 3;
415
+ int32 total_pages = 4;
416
+ bool has_next = 5;
417
+ bool has_previous = 6;
418
+ }
419
+
420
+ // 共通のエラー応答
421
+ message ErrorDetail {
422
+ string field = 1;
423
+ string message = 2;
424
+ }
425
+
426
+ message ErrorResponse {
427
+ string code = 1;
428
+ string message = 2;
429
+ repeated ErrorDetail details = 3;
430
+ }
431
+
432
+ // 金額型(Decimal 相当)
433
+ message Money {
434
+ int64 units = 1; // 整数部
435
+ int32 nanos = 2; // 小数部(10億分の1単位)
436
+ string currency = 3; // 通貨コード(JPY等)
437
+ }
438
+
439
+ // 日付型(Date 相当)
440
+ message Date {
441
+ int32 year = 1;
442
+ int32 month = 2;
443
+ int32 day = 3;
444
+ }
445
+
446
+ // 年月型
447
+ message YearMonth {
448
+ int32 year = 1;
449
+ int32 month = 2;
450
+ }
451
+
452
+ // 決算期
453
+ message FiscalPeriod {
454
+ int32 fiscal_year = 1; // 決算年度
455
+ int32 fiscal_month = 2; // 決算月度(1-12)
456
+ }
457
+
458
+ // 監査情報
459
+ message AuditInfo {
460
+ google.protobuf.Timestamp created_at = 1;
461
+ string created_by = 2;
462
+ google.protobuf.Timestamp updated_at = 3;
463
+ string updated_by = 4;
464
+ }
465
+ ```
466
+
467
+ </details>
468
+
469
+ ### 23.2 勘定科目 Protocol Buffers
470
+
471
+ <details>
472
+ <summary>src/main/proto/account.proto</summary>
473
+
474
+ ```protobuf
475
+ syntax = "proto3";
476
+
477
+ package com.example.accounting;
478
+
479
+ option java_package = "com.example.accounting.infrastructure.in.grpc.proto";
480
+ option java_outer_classname = "AccountProto";
481
+ option java_multiple_files = true;
482
+
483
+ import "common.proto";
484
+ import "google/protobuf/empty.proto";
485
+
486
+ // BSPL区分
487
+ enum BsplType {
488
+ BSPL_TYPE_UNSPECIFIED = 0;
489
+ BSPL_TYPE_BS = 1; // 貸借対照表
490
+ BSPL_TYPE_PL = 2; // 損益計算書
491
+ }
492
+
493
+ // 貸借区分
494
+ enum DebitCreditType {
495
+ DEBIT_CREDIT_TYPE_UNSPECIFIED = 0;
496
+ DEBIT_CREDIT_TYPE_DEBIT = 1; // 借方
497
+ DEBIT_CREDIT_TYPE_CREDIT = 2; // 貸方
498
+ }
499
+
500
+ // 取引要素区分
501
+ enum ElementType {
502
+ ELEMENT_TYPE_UNSPECIFIED = 0;
503
+ ELEMENT_TYPE_ASSET = 1; // 資産
504
+ ELEMENT_TYPE_LIABILITY = 2; // 負債
505
+ ELEMENT_TYPE_EQUITY = 3; // 資本
506
+ ELEMENT_TYPE_REVENUE = 4; // 収益
507
+ ELEMENT_TYPE_EXPENSE = 5; // 費用
508
+ }
509
+
510
+ // 集計区分
511
+ enum AggregationType {
512
+ AGGREGATION_TYPE_UNSPECIFIED = 0;
513
+ AGGREGATION_TYPE_HEADING = 1; // 見出科目
514
+ AGGREGATION_TYPE_SUMMARY = 2; // 集計科目
515
+ AGGREGATION_TYPE_POSTING = 3; // 計上科目
516
+ }
517
+
518
+ // 勘定科目メッセージ
519
+ message Account {
520
+ string account_code = 1; // 勘定科目コード
521
+ string account_name = 2; // 勘定科目名
522
+ BsplType bspl_type = 3; // BSPL区分
523
+ DebitCreditType debit_credit_type = 4; // 貸借区分
524
+ ElementType element_type = 5; // 取引要素区分
525
+ AggregationType aggregation_type = 6; // 集計区分
526
+ string parent_account_code = 7; // 親勘定科目コード
527
+ string account_path = 8; // 勘定科目パス
528
+ int32 display_order = 9; // 表示順
529
+ bool is_active = 10; // 有効フラグ
530
+ AuditInfo audit = 11; // 監査情報
531
+ }
532
+
533
+ // 勘定科目構成メッセージ
534
+ message AccountStructure {
535
+ string account_code = 1; // 勘定科目コード
536
+ string account_path = 2; // 勘定科目パス(~区切り)
537
+ int32 level = 3; // 階層レベル
538
+ }
539
+
540
+ // 課税取引マスタ
541
+ message TaxTransaction {
542
+ string tax_code = 1; // 課税取引コード
543
+ string tax_name = 2; // 課税取引名
544
+ int32 tax_rate = 3; // 税率(パーセント * 100)
545
+ Date effective_from = 4; // 適用開始日
546
+ Date effective_to = 5; // 適用終了日
547
+ bool is_active = 6; // 有効フラグ
548
+ }
549
+
550
+ // === リクエスト/レスポンス ===
551
+
552
+ // 勘定科目取得
553
+ message GetAccountRequest {
554
+ string account_code = 1;
555
+ }
556
+
557
+ message GetAccountResponse {
558
+ Account account = 1;
559
+ }
560
+
561
+ // 勘定科目一覧取得
562
+ message ListAccountsRequest {
563
+ PageRequest page = 1;
564
+ BsplType bspl_type = 2; // フィルタ(オプション)
565
+ ElementType element_type = 3; // フィルタ(オプション)
566
+ AggregationType aggregation_type = 4; // フィルタ(オプション)
567
+ string keyword = 5; // 検索キーワード
568
+ bool active_only = 6; // 有効のみ
569
+ }
570
+
571
+ message ListAccountsResponse {
572
+ repeated Account accounts = 1;
573
+ PageInfo page_info = 2;
574
+ }
575
+
576
+ // 勘定科目登録
577
+ message CreateAccountRequest {
578
+ string account_code = 1;
579
+ string account_name = 2;
580
+ BsplType bspl_type = 3;
581
+ DebitCreditType debit_credit_type = 4;
582
+ ElementType element_type = 5;
583
+ AggregationType aggregation_type = 6;
584
+ string parent_account_code = 7;
585
+ int32 display_order = 8;
586
+ }
587
+
588
+ message CreateAccountResponse {
589
+ Account account = 1;
590
+ }
591
+
592
+ // 勘定科目更新
593
+ message UpdateAccountRequest {
594
+ string account_code = 1;
595
+ string account_name = 2;
596
+ BsplType bspl_type = 3;
597
+ DebitCreditType debit_credit_type = 4;
598
+ ElementType element_type = 5;
599
+ AggregationType aggregation_type = 6;
600
+ string parent_account_code = 7;
601
+ int32 display_order = 8;
602
+ bool is_active = 9;
603
+ }
604
+
605
+ message UpdateAccountResponse {
606
+ Account account = 1;
607
+ }
608
+
609
+ // 勘定科目削除
610
+ message DeleteAccountRequest {
611
+ string account_code = 1;
612
+ }
613
+
614
+ message DeleteAccountResponse {
615
+ bool success = 1;
616
+ }
617
+
618
+ // 勘定科目一括登録(クライアントストリーミング)
619
+ message BulkCreateAccountRequest {
620
+ CreateAccountRequest account = 1;
621
+ }
622
+
623
+ message BulkCreateAccountResponse {
624
+ int32 success_count = 1;
625
+ int32 failure_count = 2;
626
+ repeated ErrorDetail errors = 3;
627
+ }
628
+
629
+ // 勘定科目ツリー取得
630
+ message GetAccountTreeRequest {
631
+ BsplType bspl_type = 1; // BS または PL
632
+ }
633
+
634
+ message GetAccountTreeResponse {
635
+ repeated AccountNode nodes = 1;
636
+ }
637
+
638
+ message AccountNode {
639
+ Account account = 1;
640
+ repeated AccountNode children = 2;
641
+ }
642
+
643
+ // 課税取引マスタ取得
644
+ message GetTaxTransactionRequest {
645
+ string tax_code = 1;
646
+ }
647
+
648
+ message GetTaxTransactionResponse {
649
+ TaxTransaction tax_transaction = 1;
650
+ }
651
+
652
+ // 課税取引マスタ一覧
653
+ message ListTaxTransactionsRequest {
654
+ Date as_of_date = 1; // 適用日(オプション)
655
+ bool active_only = 2;
656
+ }
657
+
658
+ message ListTaxTransactionsResponse {
659
+ repeated TaxTransaction tax_transactions = 1;
660
+ }
661
+
662
+ // === サービス定義 ===
663
+
664
+ service AccountService {
665
+ // 単項 RPC
666
+ rpc GetAccount(GetAccountRequest) returns (GetAccountResponse);
667
+ rpc CreateAccount(CreateAccountRequest) returns (CreateAccountResponse);
668
+ rpc UpdateAccount(UpdateAccountRequest) returns (UpdateAccountResponse);
669
+ rpc DeleteAccount(DeleteAccountRequest) returns (DeleteAccountResponse);
670
+
671
+ // サーバーストリーミング RPC(大量データ取得)
672
+ rpc ListAccounts(ListAccountsRequest) returns (stream Account);
673
+
674
+ // クライアントストリーミング RPC(一括登録)
675
+ rpc BulkCreateAccounts(stream BulkCreateAccountRequest) returns (BulkCreateAccountResponse);
676
+
677
+ // 勘定科目構成
678
+ rpc GetAccountTree(GetAccountTreeRequest) returns (GetAccountTreeResponse);
679
+
680
+ // 課税取引マスタ
681
+ rpc GetTaxTransaction(GetTaxTransactionRequest) returns (GetTaxTransactionResponse);
682
+ rpc ListTaxTransactions(ListTaxTransactionsRequest) returns (ListTaxTransactionsResponse);
683
+ }
684
+ ```
685
+
686
+ </details>
687
+
688
+ ### 23.3 仕訳 Protocol Buffers
689
+
690
+ <details>
691
+ <summary>src/main/proto/journal.proto</summary>
692
+
693
+ ```protobuf
694
+ syntax = "proto3";
695
+
696
+ package com.example.accounting;
697
+
698
+ option java_package = "com.example.accounting.infrastructure.in.grpc.proto";
699
+ option java_outer_classname = "JournalProto";
700
+ option java_multiple_files = true;
701
+
702
+ import "common.proto";
703
+ import "account.proto";
704
+ import "google/protobuf/empty.proto";
705
+
706
+ // 仕訳伝票区分
707
+ enum JournalType {
708
+ JOURNAL_TYPE_UNSPECIFIED = 0;
709
+ JOURNAL_TYPE_NORMAL = 1; // 通常仕訳
710
+ JOURNAL_TYPE_CLOSING = 2; // 決算仕訳
711
+ JOURNAL_TYPE_ADJUSTMENT = 3; // 調整仕訳
712
+ JOURNAL_TYPE_REVERSAL = 4; // 取消仕訳
713
+ }
714
+
715
+ // 仕訳ステータス
716
+ enum JournalStatus {
717
+ JOURNAL_STATUS_UNSPECIFIED = 0;
718
+ JOURNAL_STATUS_DRAFT = 1; // 下書き
719
+ JOURNAL_STATUS_PENDING = 2; // 承認待ち
720
+ JOURNAL_STATUS_APPROVED = 3; // 承認済み
721
+ JOURNAL_STATUS_POSTED = 4; // 転記済み
722
+ JOURNAL_STATUS_CANCELED = 5; // 取消済み
723
+ }
724
+
725
+ // 仕訳伝票メッセージ
726
+ message Journal {
727
+ string slip_number = 1; // 伝票番号
728
+ Date journal_date = 2; // 起票日
729
+ Date input_date = 3; // 入力日
730
+ string summary = 4; // 伝票摘要
731
+ JournalType journal_type = 5; // 仕訳伝票区分
732
+ JournalStatus status = 6; // ステータス
733
+ bool is_closing_journal = 7; // 決算仕訳フラグ
734
+ FiscalPeriod fiscal_period = 8; // 決算期
735
+ Money total_debit = 9; // 借方合計
736
+ Money total_credit = 10; // 貸方合計
737
+ repeated JournalDetail details = 11; // 仕訳明細
738
+ string reversal_slip_number = 12; // 取消元伝票番号
739
+ AuditInfo audit = 13; // 監査情報
740
+ }
741
+
742
+ // 仕訳明細メッセージ
743
+ message JournalDetail {
744
+ int32 line_number = 1; // 行番号
745
+ string line_summary = 2; // 行摘要
746
+ repeated JournalDebitCreditDetail debit_credit_details = 3; // 貸借明細
747
+ }
748
+
749
+ // 仕訳貸借明細メッセージ
750
+ message JournalDebitCreditDetail {
751
+ DebitCreditType debit_credit_type = 1; // 借方/貸方
752
+ string account_code = 2; // 勘定科目コード
753
+ string account_name = 3; // 勘定科目名(参照用)
754
+ string sub_account_code = 4; // 補助科目コード
755
+ string department_code = 5; // 部門コード
756
+ string project_code = 6; // プロジェクトコード
757
+ Money amount = 7; // 金額
758
+ Money base_currency_amount = 8; // 基軸通貨金額
759
+ string tax_code = 9; // 課税取引コード
760
+ int32 tax_rate = 10; // 税率
761
+ Money tax_amount = 11; // 消費税額
762
+ }
763
+
764
+ // === リクエスト/レスポンス ===
765
+
766
+ // 仕訳取得
767
+ message GetJournalRequest {
768
+ string slip_number = 1;
769
+ }
770
+
771
+ message GetJournalResponse {
772
+ Journal journal = 1;
773
+ }
774
+
775
+ // 仕訳一覧取得
776
+ message ListJournalsRequest {
777
+ PageRequest page = 1;
778
+ Date date_from = 2; // 起票日From
779
+ Date date_to = 3; // 起票日To
780
+ FiscalPeriod fiscal_period = 4; // 決算期
781
+ JournalType journal_type = 5; // 仕訳伝票区分
782
+ JournalStatus status = 6; // ステータス
783
+ string account_code = 7; // 勘定科目コード
784
+ string keyword = 8; // 検索キーワード
785
+ }
786
+
787
+ message ListJournalsResponse {
788
+ repeated Journal journals = 1;
789
+ PageInfo page_info = 2;
790
+ }
791
+
792
+ // 仕訳登録
793
+ message CreateJournalRequest {
794
+ Date journal_date = 1;
795
+ string summary = 2;
796
+ JournalType journal_type = 3;
797
+ bool is_closing_journal = 4;
798
+ repeated CreateJournalDetailRequest details = 5;
799
+ }
800
+
801
+ message CreateJournalDetailRequest {
802
+ int32 line_number = 1;
803
+ string line_summary = 2;
804
+ repeated CreateJournalDebitCreditRequest debit_credit_details = 3;
805
+ }
806
+
807
+ message CreateJournalDebitCreditRequest {
808
+ DebitCreditType debit_credit_type = 1;
809
+ string account_code = 2;
810
+ string sub_account_code = 3;
811
+ string department_code = 4;
812
+ string project_code = 5;
813
+ Money amount = 6;
814
+ string tax_code = 7;
815
+ }
816
+
817
+ message CreateJournalResponse {
818
+ Journal journal = 1;
819
+ }
820
+
821
+ // 仕訳更新
822
+ message UpdateJournalRequest {
823
+ string slip_number = 1;
824
+ Date journal_date = 2;
825
+ string summary = 3;
826
+ JournalType journal_type = 4;
827
+ bool is_closing_journal = 5;
828
+ repeated CreateJournalDetailRequest details = 6;
829
+ }
830
+
831
+ message UpdateJournalResponse {
832
+ Journal journal = 1;
833
+ }
834
+
835
+ // 仕訳取消(赤黒処理)
836
+ message CancelJournalRequest {
837
+ string slip_number = 1;
838
+ string cancel_reason = 2;
839
+ }
840
+
841
+ message CancelJournalResponse {
842
+ Journal original_journal = 1; // 元の仕訳
843
+ Journal reversal_journal = 2; // 取消仕訳
844
+ }
845
+
846
+ // 仕訳承認
847
+ message ApproveJournalRequest {
848
+ string slip_number = 1;
849
+ string approver_comment = 2;
850
+ }
851
+
852
+ message ApproveJournalResponse {
853
+ Journal journal = 1;
854
+ }
855
+
856
+ // 仕訳一括登録(クライアントストリーミング)
857
+ message BulkCreateJournalRequest {
858
+ CreateJournalRequest journal = 1;
859
+ }
860
+
861
+ message BulkCreateJournalResponse {
862
+ int32 success_count = 1;
863
+ int32 failure_count = 2;
864
+ repeated ErrorDetail errors = 3;
865
+ repeated string created_slip_numbers = 4;
866
+ }
867
+
868
+ // 貸借バランスチェック
869
+ message ValidateBalanceRequest {
870
+ repeated CreateJournalDetailRequest details = 1;
871
+ }
872
+
873
+ message ValidateBalanceResponse {
874
+ bool is_balanced = 1;
875
+ Money total_debit = 2;
876
+ Money total_credit = 3;
877
+ Money difference = 4;
878
+ }
879
+
880
+ // === サービス定義 ===
881
+
882
+ service JournalService {
883
+ // 単項 RPC
884
+ rpc GetJournal(GetJournalRequest) returns (GetJournalResponse);
885
+ rpc CreateJournal(CreateJournalRequest) returns (CreateJournalResponse);
886
+ rpc UpdateJournal(UpdateJournalRequest) returns (UpdateJournalResponse);
887
+ rpc CancelJournal(CancelJournalRequest) returns (CancelJournalResponse);
888
+ rpc ApproveJournal(ApproveJournalRequest) returns (ApproveJournalResponse);
889
+ rpc ValidateBalance(ValidateBalanceRequest) returns (ValidateBalanceResponse);
890
+
891
+ // サーバーストリーミング RPC
892
+ rpc ListJournals(ListJournalsRequest) returns (stream Journal);
893
+
894
+ // クライアントストリーミング RPC
895
+ rpc BulkCreateJournals(stream BulkCreateJournalRequest) returns (BulkCreateJournalResponse);
896
+
897
+ // 双方向ストリーミング RPC(リアルタイム仕訳入力)
898
+ rpc StreamJournalEntry(stream CreateJournalRequest) returns (stream CreateJournalResponse);
899
+ }
900
+ ```
901
+
902
+ </details>
903
+
904
+ ### 23.4 残高 Protocol Buffers
905
+
906
+ <details>
907
+ <summary>src/main/proto/balance.proto</summary>
908
+
909
+ ```protobuf
910
+ syntax = "proto3";
911
+
912
+ package com.example.accounting;
913
+
914
+ option java_package = "com.example.accounting.infrastructure.in.grpc.proto";
915
+ option java_outer_classname = "BalanceProto";
916
+ option java_multiple_files = true;
917
+
918
+ import "common.proto";
919
+ import "account.proto";
920
+
921
+ // 日次勘定科目残高
922
+ message DailyBalance {
923
+ Date balance_date = 1; // 残高日
924
+ string account_code = 2; // 勘定科目コード
925
+ string account_name = 3; // 勘定科目名
926
+ string sub_account_code = 4; // 補助科目コード
927
+ string department_code = 5; // 部門コード
928
+ Money previous_balance = 6; // 前日残高
929
+ Money debit_amount = 7; // 借方金額
930
+ Money credit_amount = 8; // 貸方金額
931
+ Money current_balance = 9; // 当日残高
932
+ }
933
+
934
+ // 月次勘定科目残高
935
+ message MonthlyBalance {
936
+ FiscalPeriod fiscal_period = 1; // 決算期
937
+ string account_code = 2; // 勘定科目コード
938
+ string account_name = 3; // 勘定科目名
939
+ string sub_account_code = 4; // 補助科目コード
940
+ string department_code = 5; // 部門コード
941
+ Money opening_balance = 6; // 月初残高
942
+ Money debit_amount = 7; // 借方金額
943
+ Money credit_amount = 8; // 貸方金額
944
+ Money closing_balance = 9; // 月末残高
945
+ Money closing_debit_balance = 10; // 決算借方金額
946
+ Money closing_credit_balance = 11; // 決算貸方金額
947
+ }
948
+
949
+ // 残高サマリー
950
+ message BalanceSummary {
951
+ string account_code = 1;
952
+ string account_name = 2;
953
+ BsplType bspl_type = 3;
954
+ ElementType element_type = 4;
955
+ Money balance = 5;
956
+ int32 level = 6;
957
+ bool is_leaf = 7;
958
+ }
959
+
960
+ // === リクエスト/レスポンス ===
961
+
962
+ // 日次残高取得
963
+ message GetDailyBalanceRequest {
964
+ Date balance_date = 1;
965
+ string account_code = 2;
966
+ string sub_account_code = 3;
967
+ string department_code = 4;
968
+ }
969
+
970
+ message GetDailyBalanceResponse {
971
+ DailyBalance balance = 1;
972
+ }
973
+
974
+ // 日次残高一覧取得
975
+ message ListDailyBalancesRequest {
976
+ Date balance_date = 1;
977
+ BsplType bspl_type = 2;
978
+ ElementType element_type = 3;
979
+ string department_code = 4;
980
+ bool posting_accounts_only = 5; // 計上科目のみ
981
+ }
982
+
983
+ message ListDailyBalancesResponse {
984
+ repeated DailyBalance balances = 1;
985
+ Money total_debit = 2;
986
+ Money total_credit = 3;
987
+ }
988
+
989
+ // 月次残高取得
990
+ message GetMonthlyBalanceRequest {
991
+ FiscalPeriod fiscal_period = 1;
992
+ string account_code = 2;
993
+ string sub_account_code = 3;
994
+ string department_code = 4;
995
+ }
996
+
997
+ message GetMonthlyBalanceResponse {
998
+ MonthlyBalance balance = 1;
999
+ }
1000
+
1001
+ // 月次残高一覧取得
1002
+ message ListMonthlyBalancesRequest {
1003
+ FiscalPeriod fiscal_period = 1;
1004
+ BsplType bspl_type = 2;
1005
+ ElementType element_type = 3;
1006
+ string department_code = 4;
1007
+ bool include_closing = 5; // 決算仕訳含む
1008
+ }
1009
+
1010
+ message ListMonthlyBalancesResponse {
1011
+ repeated MonthlyBalance balances = 1;
1012
+ Money total_debit = 2;
1013
+ Money total_credit = 3;
1014
+ }
1015
+
1016
+ // 勘定科目別残高照会
1017
+ message GetAccountBalanceRequest {
1018
+ string account_code = 1;
1019
+ Date date_from = 2;
1020
+ Date date_to = 3;
1021
+ string sub_account_code = 4;
1022
+ string department_code = 5;
1023
+ }
1024
+
1025
+ message GetAccountBalanceResponse {
1026
+ string account_code = 1;
1027
+ string account_name = 2;
1028
+ Money opening_balance = 3; // 期首残高
1029
+ Money total_debit = 4; // 期間借方合計
1030
+ Money total_credit = 5; // 期間貸方合計
1031
+ Money closing_balance = 6; // 期末残高
1032
+ repeated DailyBalance daily_movements = 7; // 日別推移
1033
+ }
1034
+
1035
+ // 残高サマリー取得(試算表用)
1036
+ message GetBalanceSummaryRequest {
1037
+ Date as_of_date = 1;
1038
+ BsplType bspl_type = 2;
1039
+ bool include_sub_accounts = 3;
1040
+ }
1041
+
1042
+ message GetBalanceSummaryResponse {
1043
+ repeated BalanceSummary summaries = 1;
1044
+ Money bs_total_debit = 2; // BS借方合計
1045
+ Money bs_total_credit = 3; // BS貸方合計
1046
+ Money pl_total_debit = 4; // PL借方合計
1047
+ Money pl_total_credit = 5; // PL貸方合計
1048
+ }
1049
+
1050
+ // 残高更新(仕訳転記時)
1051
+ message UpdateBalanceRequest {
1052
+ string slip_number = 1;
1053
+ }
1054
+
1055
+ message UpdateBalanceResponse {
1056
+ bool success = 1;
1057
+ int32 updated_daily_records = 2;
1058
+ int32 updated_monthly_records = 3;
1059
+ }
1060
+
1061
+ // === サービス定義 ===
1062
+
1063
+ service BalanceService {
1064
+ // 単項 RPC
1065
+ rpc GetDailyBalance(GetDailyBalanceRequest) returns (GetDailyBalanceResponse);
1066
+ rpc GetMonthlyBalance(GetMonthlyBalanceRequest) returns (GetMonthlyBalanceResponse);
1067
+ rpc GetAccountBalance(GetAccountBalanceRequest) returns (GetAccountBalanceResponse);
1068
+ rpc GetBalanceSummary(GetBalanceSummaryRequest) returns (GetBalanceSummaryResponse);
1069
+ rpc UpdateBalance(UpdateBalanceRequest) returns (UpdateBalanceResponse);
1070
+
1071
+ // サーバーストリーミング RPC
1072
+ rpc ListDailyBalances(ListDailyBalancesRequest) returns (stream DailyBalance);
1073
+ rpc ListMonthlyBalances(ListMonthlyBalancesRequest) returns (stream MonthlyBalance);
1074
+
1075
+ // 双方向ストリーミング(リアルタイム残高監視)
1076
+ rpc WatchBalance(stream GetAccountBalanceRequest) returns (stream GetAccountBalanceResponse);
1077
+ }
1078
+ ```
1079
+
1080
+ </details>
1081
+
1082
+ ---
1083
+
1084
+ ## 第 24 章:gRPC サービス実装
1085
+
1086
+ ### 24.1 勘定科目サービス実装
1087
+
1088
+ <details>
1089
+ <summary>GrpcAccountService.java</summary>
1090
+
1091
+ ```java
1092
+ package com.example.accounting.infrastructure.in.grpc.service;
1093
+
1094
+ import com.example.accounting.application.port.in.AccountUseCase;
1095
+ import com.example.accounting.domain.model.account.Account;
1096
+ import com.example.accounting.domain.model.account.AccountCode;
1097
+ import com.example.accounting.domain.exception.ResourceNotFoundException;
1098
+ import com.example.accounting.infrastructure.in.grpc.converter.AccountConverter;
1099
+ import com.example.accounting.infrastructure.in.grpc.proto.*;
1100
+ import io.grpc.Status;
1101
+ import io.grpc.stub.StreamObserver;
1102
+ import net.devh.boot.grpc.server.service.GrpcService;
1103
+ import org.slf4j.Logger;
1104
+ import org.slf4j.LoggerFactory;
1105
+
1106
+ import java.util.List;
1107
+ import java.util.concurrent.atomic.AtomicInteger;
1108
+
1109
+ /**
1110
+ * 勘定科目 gRPC サービス実装
1111
+ */
1112
+ @GrpcService
1113
+ public class GrpcAccountService extends AccountServiceGrpc.AccountServiceImplBase {
1114
+
1115
+ private static final Logger log = LoggerFactory.getLogger(GrpcAccountService.class);
1116
+
1117
+ private final AccountUseCase accountUseCase;
1118
+ private final AccountConverter converter;
1119
+
1120
+ public GrpcAccountService(AccountUseCase accountUseCase, AccountConverter converter) {
1121
+ this.accountUseCase = accountUseCase;
1122
+ this.converter = converter;
1123
+ }
1124
+
1125
+ /**
1126
+ * 単項 RPC: 勘定科目取得
1127
+ */
1128
+ @Override
1129
+ public void getAccount(GetAccountRequest request,
1130
+ StreamObserver<GetAccountResponse> responseObserver) {
1131
+ log.info("getAccount: {}", request.getAccountCode());
1132
+
1133
+ try {
1134
+ AccountCode code = new AccountCode(request.getAccountCode());
1135
+ Account account = accountUseCase.findByCode(code)
1136
+ .orElseThrow(() -> new ResourceNotFoundException(
1137
+ "勘定科目", request.getAccountCode()));
1138
+
1139
+ GetAccountResponse response = GetAccountResponse.newBuilder()
1140
+ .setAccount(converter.toProto(account))
1141
+ .build();
1142
+
1143
+ responseObserver.onNext(response);
1144
+ responseObserver.onCompleted();
1145
+
1146
+ } catch (ResourceNotFoundException e) {
1147
+ log.warn("Account not found: {}", request.getAccountCode());
1148
+ responseObserver.onError(
1149
+ Status.NOT_FOUND
1150
+ .withDescription("勘定科目が見つかりません: " + request.getAccountCode())
1151
+ .asRuntimeException()
1152
+ );
1153
+ } catch (Exception e) {
1154
+ log.error("Error getting account", e);
1155
+ responseObserver.onError(
1156
+ Status.INTERNAL
1157
+ .withDescription("内部エラーが発生しました")
1158
+ .withCause(e)
1159
+ .asRuntimeException()
1160
+ );
1161
+ }
1162
+ }
1163
+
1164
+ /**
1165
+ * 単項 RPC: 勘定科目登録
1166
+ */
1167
+ @Override
1168
+ public void createAccount(CreateAccountRequest request,
1169
+ StreamObserver<CreateAccountResponse> responseObserver) {
1170
+ log.info("createAccount: {}", request.getAccountCode());
1171
+
1172
+ try {
1173
+ Account account = converter.toDomain(request);
1174
+ Account created = accountUseCase.create(account);
1175
+
1176
+ CreateAccountResponse response = CreateAccountResponse.newBuilder()
1177
+ .setAccount(converter.toProto(created))
1178
+ .build();
1179
+
1180
+ responseObserver.onNext(response);
1181
+ responseObserver.onCompleted();
1182
+
1183
+ } catch (IllegalArgumentException e) {
1184
+ log.warn("Invalid request: {}", e.getMessage());
1185
+ responseObserver.onError(
1186
+ Status.INVALID_ARGUMENT
1187
+ .withDescription(e.getMessage())
1188
+ .asRuntimeException()
1189
+ );
1190
+ } catch (Exception e) {
1191
+ log.error("Error creating account", e);
1192
+ responseObserver.onError(
1193
+ Status.INTERNAL
1194
+ .withDescription("内部エラーが発生しました")
1195
+ .asRuntimeException()
1196
+ );
1197
+ }
1198
+ }
1199
+
1200
+ /**
1201
+ * サーバーストリーミング RPC: 勘定科目一覧取得
1202
+ */
1203
+ @Override
1204
+ public void listAccounts(ListAccountsRequest request,
1205
+ StreamObserver<com.example.accounting.infrastructure.in.grpc.proto.Account> responseObserver) {
1206
+ log.info("listAccounts: bsplType={}, keyword={}",
1207
+ request.getBsplType(), request.getKeyword());
1208
+
1209
+ try {
1210
+ List<Account> accounts = accountUseCase.findAll(
1211
+ converter.toCriteria(request)
1212
+ );
1213
+
1214
+ // ストリーミングで勘定科目を1件ずつ送信
1215
+ for (Account account : accounts) {
1216
+ responseObserver.onNext(converter.toProto(account));
1217
+ }
1218
+
1219
+ responseObserver.onCompleted();
1220
+ log.info("listAccounts completed: {} items", accounts.size());
1221
+
1222
+ } catch (Exception e) {
1223
+ log.error("Error listing accounts", e);
1224
+ responseObserver.onError(
1225
+ Status.INTERNAL
1226
+ .withDescription("内部エラーが発生しました")
1227
+ .asRuntimeException()
1228
+ );
1229
+ }
1230
+ }
1231
+
1232
+ /**
1233
+ * クライアントストリーミング RPC: 勘定科目一括登録
1234
+ */
1235
+ @Override
1236
+ public StreamObserver<BulkCreateAccountRequest> bulkCreateAccounts(
1237
+ StreamObserver<BulkCreateAccountResponse> responseObserver) {
1238
+
1239
+ log.info("bulkCreateAccounts: started");
1240
+
1241
+ AtomicInteger successCount = new AtomicInteger(0);
1242
+ AtomicInteger failureCount = new AtomicInteger(0);
1243
+ List<ErrorDetail> errors = new java.util.ArrayList<>();
1244
+
1245
+ return new StreamObserver<BulkCreateAccountRequest>() {
1246
+ @Override
1247
+ public void onNext(BulkCreateAccountRequest request) {
1248
+ try {
1249
+ CreateAccountRequest accountRequest = request.getAccount();
1250
+ Account account = converter.toDomain(accountRequest);
1251
+ accountUseCase.create(account);
1252
+ successCount.incrementAndGet();
1253
+ log.debug("Created account: {}", accountRequest.getAccountCode());
1254
+
1255
+ } catch (Exception e) {
1256
+ failureCount.incrementAndGet();
1257
+ errors.add(ErrorDetail.newBuilder()
1258
+ .setField(request.getAccount().getAccountCode())
1259
+ .setMessage(e.getMessage())
1260
+ .build());
1261
+ log.warn("Failed to create account: {}",
1262
+ request.getAccount().getAccountCode(), e);
1263
+ }
1264
+ }
1265
+
1266
+ @Override
1267
+ public void onError(Throwable t) {
1268
+ log.error("bulkCreateAccounts error", t);
1269
+ }
1270
+
1271
+ @Override
1272
+ public void onCompleted() {
1273
+ BulkCreateAccountResponse response = BulkCreateAccountResponse.newBuilder()
1274
+ .setSuccessCount(successCount.get())
1275
+ .setFailureCount(failureCount.get())
1276
+ .addAllErrors(errors)
1277
+ .build();
1278
+
1279
+ responseObserver.onNext(response);
1280
+ responseObserver.onCompleted();
1281
+
1282
+ log.info("bulkCreateAccounts completed: success={}, failure={}",
1283
+ successCount.get(), failureCount.get());
1284
+ }
1285
+ };
1286
+ }
1287
+
1288
+ /**
1289
+ * 勘定科目ツリー取得
1290
+ */
1291
+ @Override
1292
+ public void getAccountTree(GetAccountTreeRequest request,
1293
+ StreamObserver<GetAccountTreeResponse> responseObserver) {
1294
+ log.info("getAccountTree: bsplType={}", request.getBsplType());
1295
+
1296
+ try {
1297
+ var tree = accountUseCase.getAccountTree(
1298
+ converter.toDomainBsplType(request.getBsplType())
1299
+ );
1300
+
1301
+ GetAccountTreeResponse response = GetAccountTreeResponse.newBuilder()
1302
+ .addAllNodes(converter.toProtoTree(tree))
1303
+ .build();
1304
+
1305
+ responseObserver.onNext(response);
1306
+ responseObserver.onCompleted();
1307
+
1308
+ } catch (Exception e) {
1309
+ log.error("Error getting account tree", e);
1310
+ responseObserver.onError(
1311
+ Status.INTERNAL
1312
+ .withDescription("内部エラーが発生しました")
1313
+ .asRuntimeException()
1314
+ );
1315
+ }
1316
+ }
1317
+ }
1318
+ ```
1319
+
1320
+ </details>
1321
+
1322
+ ### 24.2 仕訳サービス実装
1323
+
1324
+ <details>
1325
+ <summary>GrpcJournalService.java</summary>
1326
+
1327
+ ```java
1328
+ package com.example.accounting.infrastructure.in.grpc.service;
1329
+
1330
+ import com.example.accounting.application.port.in.JournalUseCase;
1331
+ import com.example.accounting.domain.model.journal.Journal;
1332
+ import com.example.accounting.domain.model.journal.SlipNumber;
1333
+ import com.example.accounting.domain.exception.ResourceNotFoundException;
1334
+ import com.example.accounting.domain.exception.BalanceNotMatchException;
1335
+ import com.example.accounting.infrastructure.in.grpc.converter.JournalConverter;
1336
+ import com.example.accounting.infrastructure.in.grpc.proto.*;
1337
+ import io.grpc.Status;
1338
+ import io.grpc.stub.StreamObserver;
1339
+ import net.devh.boot.grpc.server.service.GrpcService;
1340
+ import org.slf4j.Logger;
1341
+ import org.slf4j.LoggerFactory;
1342
+
1343
+ import java.util.List;
1344
+ import java.util.concurrent.atomic.AtomicInteger;
1345
+
1346
+ /**
1347
+ * 仕訳 gRPC サービス実装
1348
+ */
1349
+ @GrpcService
1350
+ public class GrpcJournalService extends JournalServiceGrpc.JournalServiceImplBase {
1351
+
1352
+ private static final Logger log = LoggerFactory.getLogger(GrpcJournalService.class);
1353
+
1354
+ private final JournalUseCase journalUseCase;
1355
+ private final JournalConverter converter;
1356
+
1357
+ public GrpcJournalService(JournalUseCase journalUseCase, JournalConverter converter) {
1358
+ this.journalUseCase = journalUseCase;
1359
+ this.converter = converter;
1360
+ }
1361
+
1362
+ /**
1363
+ * 単項 RPC: 仕訳取得
1364
+ */
1365
+ @Override
1366
+ public void getJournal(GetJournalRequest request,
1367
+ StreamObserver<GetJournalResponse> responseObserver) {
1368
+ log.info("getJournal: {}", request.getSlipNumber());
1369
+
1370
+ try {
1371
+ SlipNumber slipNumber = new SlipNumber(request.getSlipNumber());
1372
+ Journal journal = journalUseCase.findBySlipNumber(slipNumber)
1373
+ .orElseThrow(() -> new ResourceNotFoundException(
1374
+ "仕訳", request.getSlipNumber()));
1375
+
1376
+ GetJournalResponse response = GetJournalResponse.newBuilder()
1377
+ .setJournal(converter.toProto(journal))
1378
+ .build();
1379
+
1380
+ responseObserver.onNext(response);
1381
+ responseObserver.onCompleted();
1382
+
1383
+ } catch (ResourceNotFoundException e) {
1384
+ log.warn("Journal not found: {}", request.getSlipNumber());
1385
+ responseObserver.onError(
1386
+ Status.NOT_FOUND
1387
+ .withDescription("仕訳が見つかりません: " + request.getSlipNumber())
1388
+ .asRuntimeException()
1389
+ );
1390
+ } catch (Exception e) {
1391
+ log.error("Error getting journal", e);
1392
+ responseObserver.onError(
1393
+ Status.INTERNAL
1394
+ .withDescription("内部エラーが発生しました")
1395
+ .asRuntimeException()
1396
+ );
1397
+ }
1398
+ }
1399
+
1400
+ /**
1401
+ * 単項 RPC: 仕訳登録
1402
+ */
1403
+ @Override
1404
+ public void createJournal(CreateJournalRequest request,
1405
+ StreamObserver<CreateJournalResponse> responseObserver) {
1406
+ log.info("createJournal: date={}", request.getJournalDate());
1407
+
1408
+ try {
1409
+ Journal journal = converter.toDomain(request);
1410
+ Journal created = journalUseCase.create(journal);
1411
+
1412
+ CreateJournalResponse response = CreateJournalResponse.newBuilder()
1413
+ .setJournal(converter.toProto(created))
1414
+ .build();
1415
+
1416
+ responseObserver.onNext(response);
1417
+ responseObserver.onCompleted();
1418
+
1419
+ } catch (BalanceNotMatchException e) {
1420
+ log.warn("Balance not match: {}", e.getMessage());
1421
+ responseObserver.onError(
1422
+ Status.FAILED_PRECONDITION
1423
+ .withDescription("貸借が一致しません: " + e.getMessage())
1424
+ .asRuntimeException()
1425
+ );
1426
+ } catch (IllegalArgumentException e) {
1427
+ log.warn("Invalid request: {}", e.getMessage());
1428
+ responseObserver.onError(
1429
+ Status.INVALID_ARGUMENT
1430
+ .withDescription(e.getMessage())
1431
+ .asRuntimeException()
1432
+ );
1433
+ } catch (Exception e) {
1434
+ log.error("Error creating journal", e);
1435
+ responseObserver.onError(
1436
+ Status.INTERNAL
1437
+ .withDescription("内部エラーが発生しました")
1438
+ .asRuntimeException()
1439
+ );
1440
+ }
1441
+ }
1442
+
1443
+ /**
1444
+ * 単項 RPC: 仕訳取消(赤黒処理)
1445
+ */
1446
+ @Override
1447
+ public void cancelJournal(CancelJournalRequest request,
1448
+ StreamObserver<CancelJournalResponse> responseObserver) {
1449
+ log.info("cancelJournal: {}", request.getSlipNumber());
1450
+
1451
+ try {
1452
+ SlipNumber slipNumber = new SlipNumber(request.getSlipNumber());
1453
+ var result = journalUseCase.cancel(slipNumber, request.getCancelReason());
1454
+
1455
+ CancelJournalResponse response = CancelJournalResponse.newBuilder()
1456
+ .setOriginalJournal(converter.toProto(result.getOriginal()))
1457
+ .setReversalJournal(converter.toProto(result.getReversal()))
1458
+ .build();
1459
+
1460
+ responseObserver.onNext(response);
1461
+ responseObserver.onCompleted();
1462
+
1463
+ } catch (ResourceNotFoundException e) {
1464
+ log.warn("Journal not found for cancel: {}", request.getSlipNumber());
1465
+ responseObserver.onError(
1466
+ Status.NOT_FOUND
1467
+ .withDescription("取消対象の仕訳が見つかりません: " + request.getSlipNumber())
1468
+ .asRuntimeException()
1469
+ );
1470
+ } catch (Exception e) {
1471
+ log.error("Error canceling journal", e);
1472
+ responseObserver.onError(
1473
+ Status.INTERNAL
1474
+ .withDescription("内部エラーが発生しました")
1475
+ .asRuntimeException()
1476
+ );
1477
+ }
1478
+ }
1479
+
1480
+ /**
1481
+ * サーバーストリーミング RPC: 仕訳一覧取得
1482
+ */
1483
+ @Override
1484
+ public void listJournals(ListJournalsRequest request,
1485
+ StreamObserver<com.example.accounting.infrastructure.in.grpc.proto.Journal> responseObserver) {
1486
+ log.info("listJournals: dateFrom={}, dateTo={}",
1487
+ request.getDateFrom(), request.getDateTo());
1488
+
1489
+ try {
1490
+ List<Journal> journals = journalUseCase.findAll(
1491
+ converter.toCriteria(request)
1492
+ );
1493
+
1494
+ // ストリーミングで仕訳を1件ずつ送信
1495
+ for (Journal journal : journals) {
1496
+ responseObserver.onNext(converter.toProto(journal));
1497
+ }
1498
+
1499
+ responseObserver.onCompleted();
1500
+ log.info("listJournals completed: {} items", journals.size());
1501
+
1502
+ } catch (Exception e) {
1503
+ log.error("Error listing journals", e);
1504
+ responseObserver.onError(
1505
+ Status.INTERNAL
1506
+ .withDescription("内部エラーが発生しました")
1507
+ .asRuntimeException()
1508
+ );
1509
+ }
1510
+ }
1511
+
1512
+ /**
1513
+ * クライアントストリーミング RPC: 仕訳一括登録
1514
+ */
1515
+ @Override
1516
+ public StreamObserver<BulkCreateJournalRequest> bulkCreateJournals(
1517
+ StreamObserver<BulkCreateJournalResponse> responseObserver) {
1518
+
1519
+ log.info("bulkCreateJournals: started");
1520
+
1521
+ AtomicInteger successCount = new AtomicInteger(0);
1522
+ AtomicInteger failureCount = new AtomicInteger(0);
1523
+ List<ErrorDetail> errors = new java.util.ArrayList<>();
1524
+ List<String> createdSlipNumbers = new java.util.ArrayList<>();
1525
+
1526
+ return new StreamObserver<BulkCreateJournalRequest>() {
1527
+ @Override
1528
+ public void onNext(BulkCreateJournalRequest request) {
1529
+ try {
1530
+ CreateJournalRequest journalRequest = request.getJournal();
1531
+ Journal journal = converter.toDomain(journalRequest);
1532
+ Journal created = journalUseCase.create(journal);
1533
+ successCount.incrementAndGet();
1534
+ createdSlipNumbers.add(created.getSlipNumber().getValue());
1535
+ log.debug("Created journal: {}", created.getSlipNumber().getValue());
1536
+
1537
+ } catch (Exception e) {
1538
+ failureCount.incrementAndGet();
1539
+ errors.add(ErrorDetail.newBuilder()
1540
+ .setField("journal")
1541
+ .setMessage(e.getMessage())
1542
+ .build());
1543
+ log.warn("Failed to create journal", e);
1544
+ }
1545
+ }
1546
+
1547
+ @Override
1548
+ public void onError(Throwable t) {
1549
+ log.error("bulkCreateJournals error", t);
1550
+ }
1551
+
1552
+ @Override
1553
+ public void onCompleted() {
1554
+ BulkCreateJournalResponse response = BulkCreateJournalResponse.newBuilder()
1555
+ .setSuccessCount(successCount.get())
1556
+ .setFailureCount(failureCount.get())
1557
+ .addAllErrors(errors)
1558
+ .addAllCreatedSlipNumbers(createdSlipNumbers)
1559
+ .build();
1560
+
1561
+ responseObserver.onNext(response);
1562
+ responseObserver.onCompleted();
1563
+
1564
+ log.info("bulkCreateJournals completed: success={}, failure={}",
1565
+ successCount.get(), failureCount.get());
1566
+ }
1567
+ };
1568
+ }
1569
+
1570
+ /**
1571
+ * 単項 RPC: 貸借バランスチェック
1572
+ */
1573
+ @Override
1574
+ public void validateBalance(ValidateBalanceRequest request,
1575
+ StreamObserver<ValidateBalanceResponse> responseObserver) {
1576
+ log.info("validateBalance");
1577
+
1578
+ try {
1579
+ var result = journalUseCase.validateBalance(
1580
+ converter.toDetailDomainList(request.getDetailsList())
1581
+ );
1582
+
1583
+ ValidateBalanceResponse response = ValidateBalanceResponse.newBuilder()
1584
+ .setIsBalanced(result.isBalanced())
1585
+ .setTotalDebit(converter.toProtoMoney(result.getTotalDebit()))
1586
+ .setTotalCredit(converter.toProtoMoney(result.getTotalCredit()))
1587
+ .setDifference(converter.toProtoMoney(result.getDifference()))
1588
+ .build();
1589
+
1590
+ responseObserver.onNext(response);
1591
+ responseObserver.onCompleted();
1592
+
1593
+ } catch (Exception e) {
1594
+ log.error("Error validating balance", e);
1595
+ responseObserver.onError(
1596
+ Status.INTERNAL
1597
+ .withDescription("内部エラーが発生しました")
1598
+ .asRuntimeException()
1599
+ );
1600
+ }
1601
+ }
1602
+ }
1603
+ ```
1604
+
1605
+ </details>
1606
+
1607
+ ### 24.3 コンバーター実装
1608
+
1609
+ <details>
1610
+ <summary>AccountConverter.java</summary>
1611
+
1612
+ ```java
1613
+ package com.example.accounting.infrastructure.in.grpc.converter;
1614
+
1615
+ import com.example.accounting.domain.model.account.*;
1616
+ import com.example.accounting.infrastructure.in.grpc.proto.*;
1617
+ import org.springframework.stereotype.Component;
1618
+
1619
+ import java.util.List;
1620
+ import java.util.stream.Collectors;
1621
+
1622
+ /**
1623
+ * 勘定科目ドメインモデル ⇔ Protocol Buffers 変換
1624
+ */
1625
+ @Component
1626
+ public class AccountConverter {
1627
+
1628
+ /**
1629
+ * ドメインモデル → Proto
1630
+ */
1631
+ public com.example.accounting.infrastructure.in.grpc.proto.Account toProto(Account domain) {
1632
+ return com.example.accounting.infrastructure.in.grpc.proto.Account.newBuilder()
1633
+ .setAccountCode(domain.getAccountCode().getValue())
1634
+ .setAccountName(domain.getAccountName())
1635
+ .setBsplType(toProtoBsplType(domain.getBsplType()))
1636
+ .setDebitCreditType(toProtoDebitCreditType(domain.getDebitCreditType()))
1637
+ .setElementType(toProtoElementType(domain.getElementType()))
1638
+ .setAggregationType(toProtoAggregationType(domain.getAggregationType()))
1639
+ .setParentAccountCode(nullToEmpty(domain.getParentAccountCode()))
1640
+ .setAccountPath(nullToEmpty(domain.getAccountPath()))
1641
+ .setDisplayOrder(domain.getDisplayOrder())
1642
+ .setIsActive(domain.isActive())
1643
+ .build();
1644
+ }
1645
+
1646
+ /**
1647
+ * CreateAccountRequest → ドメインモデル
1648
+ */
1649
+ public Account toDomain(CreateAccountRequest request) {
1650
+ return Account.builder()
1651
+ .accountCode(new AccountCode(request.getAccountCode()))
1652
+ .accountName(request.getAccountName())
1653
+ .bsplType(toDomainBsplType(request.getBsplType()))
1654
+ .debitCreditType(toDomainDebitCreditType(request.getDebitCreditType()))
1655
+ .elementType(toDomainElementType(request.getElementType()))
1656
+ .aggregationType(toDomainAggregationType(request.getAggregationType()))
1657
+ .parentAccountCode(emptyToNull(request.getParentAccountCode()))
1658
+ .displayOrder(request.getDisplayOrder())
1659
+ .isActive(true)
1660
+ .build();
1661
+ }
1662
+
1663
+ /**
1664
+ * ListAccountsRequest → 検索条件
1665
+ */
1666
+ public AccountSearchCriteria toCriteria(ListAccountsRequest request) {
1667
+ return AccountSearchCriteria.builder()
1668
+ .bsplType(request.hasBsplType()
1669
+ ? toDomainBsplType(request.getBsplType()) : null)
1670
+ .elementType(request.hasElementType()
1671
+ ? toDomainElementType(request.getElementType()) : null)
1672
+ .aggregationType(request.hasAggregationType()
1673
+ ? toDomainAggregationType(request.getAggregationType()) : null)
1674
+ .keyword(emptyToNull(request.getKeyword()))
1675
+ .activeOnly(request.getActiveOnly())
1676
+ .page(request.hasPage() ? request.getPage().getPage() : 0)
1677
+ .size(request.hasPage() ? request.getPage().getSize() : 20)
1678
+ .build();
1679
+ }
1680
+
1681
+ /**
1682
+ * ツリー構造を Proto に変換
1683
+ */
1684
+ public List<AccountNode> toProtoTree(List<AccountTreeNode> tree) {
1685
+ return tree.stream()
1686
+ .map(this::toProtoNode)
1687
+ .collect(Collectors.toList());
1688
+ }
1689
+
1690
+ private AccountNode toProtoNode(AccountTreeNode node) {
1691
+ AccountNode.Builder builder = AccountNode.newBuilder()
1692
+ .setAccount(toProto(node.getAccount()));
1693
+
1694
+ if (node.getChildren() != null && !node.getChildren().isEmpty()) {
1695
+ builder.addAllChildren(
1696
+ node.getChildren().stream()
1697
+ .map(this::toProtoNode)
1698
+ .collect(Collectors.toList())
1699
+ );
1700
+ }
1701
+
1702
+ return builder.build();
1703
+ }
1704
+
1705
+ // === 区分変換 ===
1706
+
1707
+ public BsplType toProtoBsplType(
1708
+ com.example.accounting.domain.model.account.BsplType domain) {
1709
+ return switch (domain) {
1710
+ case BS -> BsplType.BSPL_TYPE_BS;
1711
+ case PL -> BsplType.BSPL_TYPE_PL;
1712
+ };
1713
+ }
1714
+
1715
+ public com.example.accounting.domain.model.account.BsplType toDomainBsplType(
1716
+ BsplType proto) {
1717
+ return switch (proto) {
1718
+ case BSPL_TYPE_BS -> com.example.accounting.domain.model.account.BsplType.BS;
1719
+ case BSPL_TYPE_PL -> com.example.accounting.domain.model.account.BsplType.PL;
1720
+ default -> throw new IllegalArgumentException("Unknown BSPL type: " + proto);
1721
+ };
1722
+ }
1723
+
1724
+ private DebitCreditType toProtoDebitCreditType(
1725
+ com.example.accounting.domain.model.account.DebitCreditType domain) {
1726
+ return switch (domain) {
1727
+ case DEBIT -> DebitCreditType.DEBIT_CREDIT_TYPE_DEBIT;
1728
+ case CREDIT -> DebitCreditType.DEBIT_CREDIT_TYPE_CREDIT;
1729
+ };
1730
+ }
1731
+
1732
+ private com.example.accounting.domain.model.account.DebitCreditType toDomainDebitCreditType(
1733
+ DebitCreditType proto) {
1734
+ return switch (proto) {
1735
+ case DEBIT_CREDIT_TYPE_DEBIT ->
1736
+ com.example.accounting.domain.model.account.DebitCreditType.DEBIT;
1737
+ case DEBIT_CREDIT_TYPE_CREDIT ->
1738
+ com.example.accounting.domain.model.account.DebitCreditType.CREDIT;
1739
+ default -> throw new IllegalArgumentException("Unknown debit/credit type: " + proto);
1740
+ };
1741
+ }
1742
+
1743
+ private ElementType toProtoElementType(
1744
+ com.example.accounting.domain.model.account.ElementType domain) {
1745
+ return switch (domain) {
1746
+ case ASSET -> ElementType.ELEMENT_TYPE_ASSET;
1747
+ case LIABILITY -> ElementType.ELEMENT_TYPE_LIABILITY;
1748
+ case EQUITY -> ElementType.ELEMENT_TYPE_EQUITY;
1749
+ case REVENUE -> ElementType.ELEMENT_TYPE_REVENUE;
1750
+ case EXPENSE -> ElementType.ELEMENT_TYPE_EXPENSE;
1751
+ };
1752
+ }
1753
+
1754
+ private com.example.accounting.domain.model.account.ElementType toDomainElementType(
1755
+ ElementType proto) {
1756
+ return switch (proto) {
1757
+ case ELEMENT_TYPE_ASSET ->
1758
+ com.example.accounting.domain.model.account.ElementType.ASSET;
1759
+ case ELEMENT_TYPE_LIABILITY ->
1760
+ com.example.accounting.domain.model.account.ElementType.LIABILITY;
1761
+ case ELEMENT_TYPE_EQUITY ->
1762
+ com.example.accounting.domain.model.account.ElementType.EQUITY;
1763
+ case ELEMENT_TYPE_REVENUE ->
1764
+ com.example.accounting.domain.model.account.ElementType.REVENUE;
1765
+ case ELEMENT_TYPE_EXPENSE ->
1766
+ com.example.accounting.domain.model.account.ElementType.EXPENSE;
1767
+ default -> throw new IllegalArgumentException("Unknown element type: " + proto);
1768
+ };
1769
+ }
1770
+
1771
+ private AggregationType toProtoAggregationType(
1772
+ com.example.accounting.domain.model.account.AggregationType domain) {
1773
+ return switch (domain) {
1774
+ case HEADING -> AggregationType.AGGREGATION_TYPE_HEADING;
1775
+ case SUMMARY -> AggregationType.AGGREGATION_TYPE_SUMMARY;
1776
+ case POSTING -> AggregationType.AGGREGATION_TYPE_POSTING;
1777
+ };
1778
+ }
1779
+
1780
+ private com.example.accounting.domain.model.account.AggregationType toDomainAggregationType(
1781
+ AggregationType proto) {
1782
+ return switch (proto) {
1783
+ case AGGREGATION_TYPE_HEADING ->
1784
+ com.example.accounting.domain.model.account.AggregationType.HEADING;
1785
+ case AGGREGATION_TYPE_SUMMARY ->
1786
+ com.example.accounting.domain.model.account.AggregationType.SUMMARY;
1787
+ case AGGREGATION_TYPE_POSTING ->
1788
+ com.example.accounting.domain.model.account.AggregationType.POSTING;
1789
+ default -> throw new IllegalArgumentException("Unknown aggregation type: " + proto);
1790
+ };
1791
+ }
1792
+
1793
+ // === ユーティリティ ===
1794
+
1795
+ private String nullToEmpty(String value) {
1796
+ return value == null ? "" : value;
1797
+ }
1798
+
1799
+ private String emptyToNull(String value) {
1800
+ return value == null || value.isEmpty() ? null : value;
1801
+ }
1802
+ }
1803
+ ```
1804
+
1805
+ </details>
1806
+
1807
+ ---
1808
+
1809
+ ## 第 25 章:gRPC インターセプター
1810
+
1811
+ ### 25.1 ログインターセプター
1812
+
1813
+ <details>
1814
+ <summary>LoggingInterceptor.java</summary>
1815
+
1816
+ ```java
1817
+ package com.example.accounting.infrastructure.in.grpc.interceptor;
1818
+
1819
+ import io.grpc.*;
1820
+ import net.devh.boot.grpc.server.interceptor.GrpcGlobalServerInterceptor;
1821
+ import org.slf4j.Logger;
1822
+ import org.slf4j.LoggerFactory;
1823
+ import org.slf4j.MDC;
1824
+
1825
+ import java.util.UUID;
1826
+
1827
+ /**
1828
+ * gRPC ログインターセプター
1829
+ */
1830
+ @GrpcGlobalServerInterceptor
1831
+ public class LoggingInterceptor implements ServerInterceptor {
1832
+
1833
+ private static final Logger log = LoggerFactory.getLogger(LoggingInterceptor.class);
1834
+
1835
+ @Override
1836
+ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
1837
+ ServerCall<ReqT, RespT> call,
1838
+ Metadata headers,
1839
+ ServerCallHandler<ReqT, RespT> next) {
1840
+
1841
+ String requestId = UUID.randomUUID().toString();
1842
+ String methodName = call.getMethodDescriptor().getFullMethodName();
1843
+ long startTime = System.currentTimeMillis();
1844
+
1845
+ MDC.put("requestId", requestId);
1846
+ log.info("gRPC Request: method={}", methodName);
1847
+
1848
+ return new ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(
1849
+ next.startCall(
1850
+ new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
1851
+ @Override
1852
+ public void close(Status status, Metadata trailers) {
1853
+ long duration = System.currentTimeMillis() - startTime;
1854
+ log.info("gRPC Response: method={}, status={}, duration={}ms",
1855
+ methodName, status.getCode(), duration);
1856
+ MDC.clear();
1857
+ super.close(status, trailers);
1858
+ }
1859
+ },
1860
+ headers
1861
+ )
1862
+ ) {
1863
+ @Override
1864
+ public void onMessage(ReqT message) {
1865
+ log.debug("gRPC Request message: {}", message);
1866
+ super.onMessage(message);
1867
+ }
1868
+ };
1869
+ }
1870
+ }
1871
+ ```
1872
+
1873
+ </details>
1874
+
1875
+ ### 25.2 例外インターセプター
1876
+
1877
+ <details>
1878
+ <summary>ExceptionInterceptor.java</summary>
1879
+
1880
+ ```java
1881
+ package com.example.accounting.infrastructure.in.grpc.interceptor;
1882
+
1883
+ import com.example.accounting.domain.exception.BusinessException;
1884
+ import com.example.accounting.domain.exception.ResourceNotFoundException;
1885
+ import com.example.accounting.domain.exception.ValidationException;
1886
+ import com.example.accounting.domain.exception.BalanceNotMatchException;
1887
+ import io.grpc.*;
1888
+ import net.devh.boot.grpc.server.interceptor.GrpcGlobalServerInterceptor;
1889
+ import org.slf4j.Logger;
1890
+ import org.slf4j.LoggerFactory;
1891
+
1892
+ /**
1893
+ * gRPC 例外インターセプター
1894
+ */
1895
+ @GrpcGlobalServerInterceptor
1896
+ public class ExceptionInterceptor implements ServerInterceptor {
1897
+
1898
+ private static final Logger log = LoggerFactory.getLogger(ExceptionInterceptor.class);
1899
+
1900
+ @Override
1901
+ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
1902
+ ServerCall<ReqT, RespT> call,
1903
+ Metadata headers,
1904
+ ServerCallHandler<ReqT, RespT> next) {
1905
+
1906
+ return new ExceptionHandlingListener<>(
1907
+ next.startCall(call, headers),
1908
+ call
1909
+ );
1910
+ }
1911
+
1912
+ private class ExceptionHandlingListener<ReqT, RespT>
1913
+ extends ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT> {
1914
+
1915
+ private final ServerCall<ReqT, RespT> call;
1916
+
1917
+ protected ExceptionHandlingListener(
1918
+ ServerCall.Listener<ReqT> delegate,
1919
+ ServerCall<ReqT, RespT> call) {
1920
+ super(delegate);
1921
+ this.call = call;
1922
+ }
1923
+
1924
+ @Override
1925
+ public void onHalfClose() {
1926
+ try {
1927
+ super.onHalfClose();
1928
+ } catch (Exception e) {
1929
+ handleException(e);
1930
+ }
1931
+ }
1932
+
1933
+ private void handleException(Exception e) {
1934
+ Status status;
1935
+ String message;
1936
+
1937
+ if (e instanceof ResourceNotFoundException) {
1938
+ status = Status.NOT_FOUND;
1939
+ message = e.getMessage();
1940
+ log.warn("Resource not found: {}", message);
1941
+
1942
+ } else if (e instanceof ValidationException) {
1943
+ status = Status.INVALID_ARGUMENT;
1944
+ message = e.getMessage();
1945
+ log.warn("Validation error: {}", message);
1946
+
1947
+ } else if (e instanceof BalanceNotMatchException) {
1948
+ status = Status.FAILED_PRECONDITION;
1949
+ message = e.getMessage();
1950
+ log.warn("Balance not match: {}", message);
1951
+
1952
+ } else if (e instanceof BusinessException) {
1953
+ status = Status.FAILED_PRECONDITION;
1954
+ message = e.getMessage();
1955
+ log.warn("Business rule violation: {}", message);
1956
+
1957
+ } else if (e instanceof IllegalArgumentException) {
1958
+ status = Status.INVALID_ARGUMENT;
1959
+ message = e.getMessage();
1960
+ log.warn("Invalid argument: {}", message);
1961
+
1962
+ } else {
1963
+ status = Status.INTERNAL;
1964
+ message = "内部エラーが発生しました";
1965
+ log.error("Internal error", e);
1966
+ }
1967
+
1968
+ call.close(status.withDescription(message), new Metadata());
1969
+ }
1970
+ }
1971
+ }
1972
+ ```
1973
+
1974
+ </details>
1975
+
1976
+ ---
1977
+
1978
+ ## 第 26 章:動作確認とテスト
1979
+
1980
+ ### 26.1 grpcurl による動作確認
1981
+
1982
+ ```bash
1983
+ # サーバー起動確認(リフレクションサービス経由)
1984
+ grpcurl -plaintext localhost:9090 list
1985
+
1986
+ # 勘定科目サービスのメソッド一覧
1987
+ grpcurl -plaintext localhost:9090 list com.example.accounting.AccountService
1988
+
1989
+ # 勘定科目取得
1990
+ grpcurl -plaintext -d '{"account_code": "1110"}' \
1991
+ localhost:9090 com.example.accounting.AccountService/GetAccount
1992
+
1993
+ # 勘定科目登録
1994
+ grpcurl -plaintext -d '{
1995
+ "account_code": "1111",
1996
+ "account_name": "普通預金(みずほ銀行)",
1997
+ "bspl_type": "BSPL_TYPE_BS",
1998
+ "debit_credit_type": "DEBIT_CREDIT_TYPE_DEBIT",
1999
+ "element_type": "ELEMENT_TYPE_ASSET",
2000
+ "aggregation_type": "AGGREGATION_TYPE_POSTING",
2001
+ "parent_account_code": "1110",
2002
+ "display_order": 10
2003
+ }' localhost:9090 com.example.accounting.AccountService/CreateAccount
2004
+
2005
+ # 勘定科目一覧取得(ストリーミング)
2006
+ grpcurl -plaintext -d '{
2007
+ "bspl_type": "BSPL_TYPE_BS",
2008
+ "active_only": true
2009
+ }' localhost:9090 com.example.accounting.AccountService/ListAccounts
2010
+
2011
+ # 仕訳登録
2012
+ grpcurl -plaintext -d '{
2013
+ "journal_date": {"year": 2025, "month": 1, "day": 15},
2014
+ "summary": "売上計上",
2015
+ "journal_type": "JOURNAL_TYPE_NORMAL",
2016
+ "details": [{
2017
+ "line_number": 1,
2018
+ "line_summary": "A社への売上",
2019
+ "debit_credit_details": [
2020
+ {
2021
+ "debit_credit_type": "DEBIT_CREDIT_TYPE_DEBIT",
2022
+ "account_code": "1310",
2023
+ "amount": {"units": 110000, "currency": "JPY"}
2024
+ },
2025
+ {
2026
+ "debit_credit_type": "DEBIT_CREDIT_TYPE_CREDIT",
2027
+ "account_code": "4100",
2028
+ "amount": {"units": 100000, "currency": "JPY"}
2029
+ },
2030
+ {
2031
+ "debit_credit_type": "DEBIT_CREDIT_TYPE_CREDIT",
2032
+ "account_code": "2110",
2033
+ "amount": {"units": 10000, "currency": "JPY"}
2034
+ }
2035
+ ]
2036
+ }]
2037
+ }' localhost:9090 com.example.accounting.JournalService/CreateJournal
2038
+
2039
+ # ヘルスチェック
2040
+ grpcurl -plaintext localhost:9090 grpc.health.v1.Health/Check
2041
+ ```
2042
+
2043
+ ### 26.2 統合テスト
2044
+
2045
+ <details>
2046
+ <summary>GrpcAccountServiceTest.java</summary>
2047
+
2048
+ ```java
2049
+ package com.example.accounting.infrastructure.in.grpc.service;
2050
+
2051
+ import com.example.accounting.application.port.in.AccountUseCase;
2052
+ import com.example.accounting.domain.model.account.*;
2053
+ import com.example.accounting.infrastructure.in.grpc.converter.AccountConverter;
2054
+ import com.example.accounting.infrastructure.in.grpc.proto.*;
2055
+ import io.grpc.StatusRuntimeException;
2056
+ import net.devh.boot.grpc.client.inject.GrpcClient;
2057
+ import org.junit.jupiter.api.*;
2058
+ import org.springframework.beans.factory.annotation.Autowired;
2059
+ import org.springframework.boot.test.context.SpringBootTest;
2060
+ import org.springframework.boot.test.mock.mockito.MockBean;
2061
+ import org.springframework.test.context.ActiveProfiles;
2062
+
2063
+ import java.util.Iterator;
2064
+ import java.util.List;
2065
+ import java.util.Optional;
2066
+
2067
+ import static org.assertj.core.api.Assertions.*;
2068
+ import static org.mockito.ArgumentMatchers.any;
2069
+ import static org.mockito.Mockito.*;
2070
+
2071
+ @SpringBootTest(properties = {
2072
+ "grpc.server.port=9091",
2073
+ "grpc.server.in-process-name=test"
2074
+ })
2075
+ @ActiveProfiles("test")
2076
+ class GrpcAccountServiceTest {
2077
+
2078
+ @MockBean
2079
+ private AccountUseCase accountUseCase;
2080
+
2081
+ @GrpcClient("inProcess")
2082
+ private AccountServiceGrpc.AccountServiceBlockingStub blockingStub;
2083
+
2084
+ private Account sampleAccount;
2085
+
2086
+ @BeforeEach
2087
+ void setUp() {
2088
+ sampleAccount = Account.builder()
2089
+ .accountCode(new AccountCode("1110"))
2090
+ .accountName("現金")
2091
+ .bsplType(com.example.accounting.domain.model.account.BsplType.BS)
2092
+ .debitCreditType(
2093
+ com.example.accounting.domain.model.account.DebitCreditType.DEBIT)
2094
+ .elementType(
2095
+ com.example.accounting.domain.model.account.ElementType.ASSET)
2096
+ .aggregationType(
2097
+ com.example.accounting.domain.model.account.AggregationType.POSTING)
2098
+ .isActive(true)
2099
+ .build();
2100
+ }
2101
+
2102
+ @Test
2103
+ @DisplayName("勘定科目取得 - 存在する科目を取得できること")
2104
+ void getAccount_found() {
2105
+ // Given
2106
+ when(accountUseCase.findByCode(any(AccountCode.class)))
2107
+ .thenReturn(Optional.of(sampleAccount));
2108
+
2109
+ GetAccountRequest request = GetAccountRequest.newBuilder()
2110
+ .setAccountCode("1110")
2111
+ .build();
2112
+
2113
+ // When
2114
+ GetAccountResponse response = blockingStub.getAccount(request);
2115
+
2116
+ // Then
2117
+ assertThat(response.getAccount().getAccountCode()).isEqualTo("1110");
2118
+ assertThat(response.getAccount().getAccountName()).isEqualTo("現金");
2119
+ assertThat(response.getAccount().getBsplType())
2120
+ .isEqualTo(BsplType.BSPL_TYPE_BS);
2121
+ }
2122
+
2123
+ @Test
2124
+ @DisplayName("勘定科目取得 - 存在しない科目は NOT_FOUND")
2125
+ void getAccount_notFound() {
2126
+ // Given
2127
+ when(accountUseCase.findByCode(any(AccountCode.class)))
2128
+ .thenReturn(Optional.empty());
2129
+
2130
+ GetAccountRequest request = GetAccountRequest.newBuilder()
2131
+ .setAccountCode("9999")
2132
+ .build();
2133
+
2134
+ // When & Then
2135
+ assertThatThrownBy(() -> blockingStub.getAccount(request))
2136
+ .isInstanceOf(StatusRuntimeException.class)
2137
+ .satisfies(e -> {
2138
+ StatusRuntimeException sre = (StatusRuntimeException) e;
2139
+ assertThat(sre.getStatus().getCode())
2140
+ .isEqualTo(io.grpc.Status.NOT_FOUND.getCode());
2141
+ });
2142
+ }
2143
+
2144
+ @Test
2145
+ @DisplayName("勘定科目一覧取得 - ストリーミングで取得できること")
2146
+ void listAccounts_streaming() {
2147
+ // Given
2148
+ List<Account> accounts = List.of(
2149
+ sampleAccount,
2150
+ Account.builder()
2151
+ .accountCode(new AccountCode("1120"))
2152
+ .accountName("普通預金")
2153
+ .bsplType(com.example.accounting.domain.model.account.BsplType.BS)
2154
+ .debitCreditType(
2155
+ com.example.accounting.domain.model.account.DebitCreditType.DEBIT)
2156
+ .elementType(
2157
+ com.example.accounting.domain.model.account.ElementType.ASSET)
2158
+ .aggregationType(
2159
+ com.example.accounting.domain.model.account.AggregationType.POSTING)
2160
+ .isActive(true)
2161
+ .build()
2162
+ );
2163
+ when(accountUseCase.findAll(any())).thenReturn(accounts);
2164
+
2165
+ ListAccountsRequest request = ListAccountsRequest.newBuilder()
2166
+ .setBsplType(BsplType.BSPL_TYPE_BS)
2167
+ .setActiveOnly(true)
2168
+ .build();
2169
+
2170
+ // When
2171
+ Iterator<com.example.accounting.infrastructure.in.grpc.proto.Account> iterator =
2172
+ blockingStub.listAccounts(request);
2173
+
2174
+ // Then
2175
+ int count = 0;
2176
+ while (iterator.hasNext()) {
2177
+ com.example.accounting.infrastructure.in.grpc.proto.Account account = iterator.next();
2178
+ assertThat(account.getAccountCode()).matches("11\\d+");
2179
+ count++;
2180
+ }
2181
+ assertThat(count).isEqualTo(2);
2182
+ }
2183
+ }
2184
+ ```
2185
+
2186
+ </details>
2187
+
2188
+ ---
2189
+
2190
+ ## 第 27 章:まとめ
2191
+
2192
+ ### 27.1 学習した内容
2193
+
2194
+ 1. **gRPC の基本概念**: Protocol Buffers、HTTP/2、4 つの RPC パターン
2195
+ 2. **REST/GraphQL との比較**: 用途に応じた適切な選択
2196
+ 3. **ヘキサゴナルアーキテクチャとの統合**: Input Adapter として gRPC サービスを追加
2197
+ 4. **技術スタック**: grpc-spring-boot-starter、Protocol Buffers
2198
+ 5. **Protocol Buffers 定義**: 共通型、勘定科目、仕訳、残高のメッセージとサービス定義
2199
+ 6. **gRPC サービス実装**: 単項 RPC、ストリーミング RPC
2200
+ 7. **インターセプター**: ログ、例外ハンドリング
2201
+ 8. **テスト**: gRPC クライアントを使用した統合テスト
2202
+
2203
+ ### 27.2 gRPC が適するユースケース
2204
+
2205
+ | ユースケース | 理由 |
2206
+ |-------------|------|
2207
+ | 基幹システム連携 | 高性能なバイナリ通信 |
2208
+ | 大量仕訳一括登録 | クライアントストリーミング |
2209
+ | リアルタイム残高監視 | 双方向ストリーミング |
2210
+ | 月次締め進捗通知 | サーバーストリーミング |
2211
+ | マイクロサービス間通信 | 型安全な API 定義 |
2212
+
2213
+ ### 27.3 API アーキテクチャ選択ガイド
2214
+
2215
+ ```plantuml
2216
+ @startuml api_selection
2217
+ !define RECTANGLE class
2218
+
2219
+ skinparam backgroundColor #FEFEFE
2220
+
2221
+ start
2222
+
2223
+ :API が必要;
2224
+
2225
+ if (ブラウザから直接アクセス?) then (はい)
2226
+ if (柔軟なクエリが必要?) then (はい)
2227
+ :GraphQL;
2228
+ else (いいえ)
2229
+ :REST API;
2230
+ endif
2231
+ else (いいえ)
2232
+ if (高性能が必要?) then (はい)
2233
+ :gRPC;
2234
+ else (いいえ)
2235
+ if (ストリーミングが必要?) then (はい)
2236
+ :gRPC;
2237
+ else (いいえ)
2238
+ :REST API;
2239
+ endif
2240
+ endif
2241
+ endif
2242
+
2243
+ stop
2244
+
2245
+ @enduml
2246
+ ```
2247
+
2248
+ ### 27.4 次のステップ
2249
+
2250
+ - gRPC-Web による Web フロントエンド対応
2251
+ - 認証・認可(JWT トークン検証)
2252
+ - ロードバランシングとサービスメッシュ
2253
+ - Observability(メトリクス、トレーシング)