Skip to content

Strategy pattern for multi-format document generation

The system employs the [[Strategy pattern]] to handle the generation of documents in various formats, such as CSV or PDF^[001-todo-code-copy.md]. This approach allows the core document generation logic to remain decoupled from the specific implementation details of each file type^[001-todo-code-copy.md].

Core Interface

The functionality relies on the ReportDocumentService interface^[001-todo-code-copy.md]. This contract defines the standard operations that all document strategies must support, ensuring consistency across different implementations^[001-todo-code-copy.md]. The primary methods include makeDocument for creating files from data and combineDocument for merging multiple byte arrays^[001-todo-code-copy.md].

Strategy Selection

A key method in the interface is accept(ReportDocumentType reportDocumentType)^[001-todo-code-copy.md]. Each implementation of this interface uses this method to determine if it can handle a specific ReportDocumentType enum value (e.g., CSV or PDF)^[001-todo-code-copy.md]. This effectively serves as the selection mechanism for the appropriate strategy at runtime^[001-todo-code-copy.md].

Implementations

CSV Strategy

The CSVDocumentServiceImpl class implements the interface to handle CSV generation^[001-todo-code-copy.md]. Its responsibilities include:

  • Validation: Checking for empty data or mismatched headers and throwing a BusinessException if validation fails^[001-todo-code-copy.md].
  • Formatting: Converting rows of data into comma-separated strings^[001-todo-code-copy.md].
  • Combination: Merging multiple CSV documents by removing headers from subsequent documents to prevent duplication^[001-todo-code-copy.md].

PDF Strategy

The PDFDocumentServiceImpl acts as a placeholder for future PDF support^[001-todo-code-copy.md]. Currently, it implements the accept method to identify PDF types, but the makeDocument and combineDocument methods throw an UnsupportedOperationException^[001-todo-code-copy.md].

File Storage and Retrieval

Generated documents are persisted as byte arrays using a file management service^[001-todo-code-copy.md]. The system interacts with a storage layer (e.g., Google Cloud Storage via GcsFileManageServiceImpl) to save these byte arrays to specific paths defined by the ReportDocumentType^[001-todo-code-copy.md]. The downloadDocument method retrieves these bytes to be returned to the client^[001-todo-code-copy.md].

  • [[Strategy pattern]]
  • [[Report processing workflow]]
  • [[File storage abstraction]]

Sources

^[001-todo-code-copy.md]