-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocumentLifecycleEngine.java
236 lines (204 loc) · 8.53 KB
/
DocumentLifecycleEngine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package com.example.documentlifecycle;
import co.elastic.clients.elasticsearch._types.ElasticsearchException;
import io.minio.errors.MinioException;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.TesseractException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.UUID;
/**
* Java 21 example for managing document lifecycle states.
*/
public class DocumentLifecycleEngine {
// --- 1) EXCEPTIONS ---
public static sealed class DocumentLifecycleException extends RuntimeException
permits DocumentLifecycleException.Infrastructure, DocumentLifecycleException.Domain {
public DocumentLifecycleException(String msg, Throwable cause) {
super(msg, cause);
}
public DocumentLifecycleException(String msg) {
super(msg);
}
public static DocumentLifecycleException create(String msg, Throwable cause) {
return new DocumentLifecycleException(msg, cause);
}
public static DocumentLifecycleException create(String msg) {
return new DocumentLifecycleException(msg);
}
public static sealed class Infrastructure extends DocumentLifecycleException
permits Infrastructure.Storage, Infrastructure.Search, Infrastructure.Ocr, Infrastructure.Messaging {
private Infrastructure(String msg, Throwable cause) {
super(msg, cause);
}
public static Infrastructure generic(String msg, Throwable cause) {
return new Infrastructure(msg, cause);
}
public static final class Storage extends Infrastructure {
public Storage(String msg, Throwable cause) {
super("Storage error: " + msg, cause);
}
public Storage(String msg) {
super("Storage error: " + msg, null);
}
}
public static final class Search extends Infrastructure {
public Search(String msg, Throwable cause) {
super("Search error: " + msg, cause);
}
public Search(String msg) {
super("Search error: " + msg, null);
}
}
public static final class Ocr extends Infrastructure {
public Ocr(String msg, Throwable cause) {
super("OCR error: " + msg, cause);
}
public Ocr(String msg) {
super("OCR error: " + msg, null);
}
}
public static final class Messaging extends Infrastructure {
public Messaging(String msg, Throwable cause) {
super("Messaging error: " + msg, cause);
}
public Messaging(String msg) {
super("Messaging error: " + msg, null);
}
}
}
public static sealed class Domain extends DocumentLifecycleException
permits Domain.NotFound, Domain.IllegalStateTransition, Domain.Validation {
private Domain(String msg) {
super(msg);
}
public static final class NotFound extends Domain {
public NotFound(String docId) {
super("Document not found: " + docId);
}
}
public static final class IllegalStateTransition extends Domain {
public IllegalStateTransition(String state, String event) {
super("Cannot transition from state '" + state + "' with event '" + event + "'");
}
}
public static final class Validation extends Domain {
public Validation(String msg) {
super("Validation error: " + msg);
}
public Validation(String field, String msg) {
super("Validation error: " + field + ": " + msg);
}
}
}
}
// --- 2) EVENTS & STATE ---
public enum DocumentEventType {
SAVE_TO_DATABASE, SAVE_TO_STORAGE, SAVE_COMPLETE,
PROCESS_START, PROCESS_COMPLETE, PROCESS_FAILED,
INDEX_COMPLETE
}
public record DocumentEvent(UUID documentId, DocumentEventType eventType, Map<String, Object> metadata) {
public static DocumentEvent of(UUID id, DocumentEventType t) {
return new DocumentEvent(id, t, Map.of());
}
public static DocumentEvent of(UUID id, DocumentEventType t, Map<String, Object> m) {
return new DocumentEvent(id, t, m);
}
}
public enum DocumentLifecycleState {
CREATED {
public DocumentLifecycleState transition(DocumentEventType e) {
return switch (e) {
case SAVE_TO_DATABASE -> PERSISTING_DATABASE;
case PROCESS_FAILED -> FAILED;
default -> fail(this, e);
};
}
},
PERSISTING_DATABASE {
public DocumentLifecycleState transition(DocumentEventType e) {
return switch (e) {
case SAVE_TO_STORAGE -> PERSISTING_STORAGE;
case PROCESS_FAILED -> FAILED;
default -> fail(this, e);
};
}
},
PERSISTING_STORAGE {
public DocumentLifecycleState transition(DocumentEventType e) {
return switch (e) {
case SAVE_COMPLETE -> SAVED;
case PROCESS_FAILED -> FAILED;
default -> fail(this, e);
};
}
},
SAVED {
public DocumentLifecycleState transition(DocumentEventType e) {
return switch (e) {
case PROCESS_START -> PROCESSING;
case PROCESS_FAILED -> FAILED;
default -> fail(this, e);
};
}
},
PROCESSING {
public DocumentLifecycleState transition(DocumentEventType e) {
return switch (e) {
case PROCESS_COMPLETE -> PROCESSED;
case PROCESS_FAILED -> FAILED;
default -> fail(this, e);
};
}
},
PROCESSED {
public DocumentLifecycleState transition(DocumentEventType e) {
return switch (e) {
case INDEX_COMPLETE -> INDEXED;
case PROCESS_FAILED -> FAILED;
default -> fail(this, e);
};
}
},
INDEXED {
public DocumentLifecycleState transition(DocumentEventType e) {
return fail(this, e); // Terminal state
}
},
FAILED {
public DocumentLifecycleState transition(DocumentEventType e) {
return fail(this, e); // Terminal state
}
};
public abstract DocumentLifecycleState transition(DocumentEventType e);
private static DocumentLifecycleState fail(DocumentLifecycleState s, DocumentEventType e) {
throw new DocumentLifecycleException.Domain.IllegalStateTransition(s.name(), e.name());
}
}
// --- 3) ASPECT FOR INFRA EXCEPTIONS ---
@Aspect
@Component
@Slf4j
public static class InfrastructureExceptionHandlerAspect {
@Around("within(com.example.infrastructure..*)")
public Object handleInfraExceptions(ProceedingJoinPoint pjp) throws Throwable {
try {
return pjp.proceed();
} catch (Exception ex) {
var op = pjp.getSignature().toShortString();
var translated = switch (ex) {
case MinioException m -> new DocumentLifecycleException.Infrastructure.Storage(op, m);
case ElasticsearchException es -> new DocumentLifecycleException.Infrastructure.Search(op, es);
case TesseractException t -> new DocumentLifecycleException.Infrastructure.Ocr(op, t);
case DocumentLifecycleException dle -> dle;
default -> DocumentLifecycleException.Infrastructure.generic("Unexpected error in " + op, ex);
};
log.error("Operation failed: {}", translated.getMessage(), translated);
throw translated;
}
}
}
}