generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add reset and rename SubmissionEntity
- Loading branch information
1 parent
3dd4caf
commit 67894da
Showing
7 changed files
with
88 additions
and
115 deletions.
There are no files selected for viewing
92 changes: 58 additions & 34 deletions
92
prime-router/src/main/kotlin/azure/SubmissionTableService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,99 @@ | ||
package gov.cdc.prime.router.azure | ||
|
||
import gov.cdc.prime.reportstream.shared.SubmissionEntity | ||
import gov.cdc.prime.reportstream.shared.Submission | ||
import org.apache.logging.log4j.kotlin.Logging | ||
|
||
/** | ||
* Service class responsible for handling operations related to the "submission" table in Azure Table Storage. | ||
* | ||
* This service uses the TableAccess singleton to interact with Azure Table Storage. | ||
* This service uses the TableAccess singleton to interact with Azure Table Storage for inserting and retrieving | ||
* submissions in the "submission" table. | ||
* | ||
* It allows inserting `Submission` objects as table entities and retrieving them based on the submission ID and status. | ||
*/ | ||
class SubmissionTableService private constructor() : Logging { | ||
|
||
companion object { | ||
/** | ||
* The singleton instance of SubmissionService, initialized lazily. | ||
* The singleton instance of SubmissionTableService, initialized lazily. | ||
* | ||
* This ensures that the instance is created only when it is first accessed, | ||
* providing thread-safe, lazy initialization. | ||
*/ | ||
val instance: SubmissionTableService by lazy { SubmissionTableService() } | ||
} | ||
|
||
private val tableName = "submission" | ||
|
||
/** | ||
* Lazily initialized TableClient to interact with the "submission" table. | ||
* The client is created once and reused for all operations on the "submission" table. | ||
* The `TableAccess` object used to interact with Azure Table Storage. | ||
* | ||
* This is marked as `@Volatile` to ensure thread visibility. It is initialized lazily | ||
* and resettable to allow refreshing the connection if necessary. | ||
*/ | ||
@Volatile | ||
private var tableAccess: TableAccess = TableAccess() | ||
|
||
/** | ||
* Resets the `TableAccess` instance used to interact with the "submission" table. | ||
* | ||
* This method ensures thread safety by synchronizing access to `tableAccess`, preventing multiple | ||
* threads from resetting the instance at the same time. | ||
*/ | ||
private val tableAccess: TableAccess by lazy { TableAccess.instance } | ||
fun reset() { | ||
synchronized(this) { | ||
tableAccess = TableAccess() // Re-initialize the TableAccess instance | ||
} | ||
} | ||
|
||
/** | ||
* Inserts a SubmissionEntity into the "submission" table. | ||
* Inserts a SubmissionEntity into the "submission" table in Azure Table Storage. | ||
* | ||
* Converts the provided SubmissionEntity into a TableEntity and attempts to insert it into the table. | ||
* Logs success or failure of the operation. | ||
* Converts the provided `Submission` object into a `TableEntity` and inserts it into the table. | ||
* Logs the outcome of the operation (success or failure). | ||
* | ||
* @param submission The SubmissionEntity to be inserted into the table. | ||
* @param submission The `Submission` object to be inserted into the table. | ||
*/ | ||
fun insertSubmission(submission: SubmissionEntity) { | ||
fun insertSubmission(submission: Submission) { | ||
try { | ||
// Convert Submission to TableEntity and insert into the table | ||
val entity = submission.toTableEntity() | ||
tableAccess.insertEntity(tableName, entity) | ||
logger | ||
.info( | ||
logger.info( | ||
"Submission entity insert succeeded: ${submission.submissionId} with status ${submission.status}" | ||
) | ||
) | ||
} catch (e: Exception) { | ||
logger | ||
.error( | ||
// Log the error if insertion fails | ||
logger.error( | ||
"Submission entity insert failed: ${submission.submissionId} with status ${submission.status}", | ||
e | ||
) | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* Reads a submission entity from the "submission" table. | ||
* Retrieves a SubmissionEntity from the "submission" table based on submission ID and status. | ||
* | ||
* Fetches the corresponding table entity from Azure Table Storage using the provided partition key | ||
* (submission ID) and row key (status). If the entity is found, it is converted back into a `Submission` object. | ||
* | ||
* @param partitionKey The partition key of the entity. | ||
* @param rowKey The row key of the entity. | ||
* @return The SubmissionEntity if found, otherwise null. | ||
* If the entity is not found, or if an error occurs during retrieval, the method returns `null` and logs the error. | ||
* | ||
* @param submissionID The partition key representing the submission ID. | ||
* @param status The row key representing the status of the submission. | ||
* @return The `Submission` object if found, otherwise `null`. | ||
*/ | ||
fun getSubmissionEntity(partitionKey: String, rowKey: String): SubmissionEntity? = try { | ||
val tableEntity = tableAccess.getEntity(tableName, partitionKey, rowKey) | ||
if (tableEntity != null) { | ||
SubmissionEntity.fromTableEntity(tableEntity) | ||
} else { | ||
null | ||
} | ||
} catch (e: Exception) { | ||
logger | ||
.error( | ||
"Failed to read submission entity: $partitionKey with status $rowKey", | ||
e | ||
) | ||
fun getSubmission(submissionID: String, status: String): Submission? = try { | ||
// Retrieve the TableEntity and convert it back to a Submission object if found | ||
val tableEntity = tableAccess.getEntity(tableName, submissionID, status) | ||
if (tableEntity != null) { | ||
Submission.fromTableEntity(tableEntity) | ||
} else { | ||
null | ||
} | ||
} catch (e: Exception) { | ||
// Log the error if retrieval fails and return null | ||
logger.error("Failed to read submission entity: $submissionID with status $status", e) | ||
null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.