Certainly! Below are the Java classes you provided, enhanced with Javadoc comments to improve documentation and maintainability. Each class, interface, method, and significant field has been documented to explain its purpose and usage.
ChecklistCompletionType.java
package eu.reply.lea.services.yms.task.checklist;
/**
* Represents the completion status of a Checklist.
*/
public enum ChecklistCompletionType {
/**
* The checklist is not completed.
*/
NOT_COMPLETED,
/**
* The checklist has been partially completed.
*/
PARTIAL_COMPLETION,
/**
* The checklist has been fully completed.
*/
COMPLETED;
/**
* Checks if the checklist is fully completed.
*
* @return {@code true} if the checklist is completed, {@code false} otherwise.
*/
public boolean isCompleted(){
return this == COMPLETED;
}
/**
* Checks if the checklist has partial completion.
*
* @return {@code true} if the checklist is partially completed, {@code false} otherwise.
*/
public boolean isPartialCompletion(){
return this == PARTIAL_COMPLETION;
}
}
repository/ChecklistRepository.java
package eu.reply.lea.services.yms.task.checklist.repository;
import eu.reply.lea.services.yms.task.checklist.Checklist;
import eu.reply.lea.services.yms.task.checklist.EntityReference;
import eu.reply.lea.services.yms.task.checklist.ModuleConfiguration;
import eu.reply.lea.services.yms.task.checklist.logging.Logged;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import java.util.Optional;
/**
* Repository for managing {@link Checklist} entities.
*/
@ApplicationScoped
@Logged
public class ChecklistRepository {
@Inject
@ModuleConfiguration.Module(ModuleConfiguration.Module.CURRENT)
private EntityManager em;
@Inject
private EntityReferenceHandler entityReferenceHandler;
/**
* Finds a {@link Checklist} by its associated {@link EntityReference}.
*
* @param entityRef the entity reference of the checklist
* @return an {@link Optional} containing the found checklist, or empty if not found
*/
public Optional<Checklist> findByEntityRef(EntityReference entityRef) {
return Optional.ofNullable(entityReferenceHandler.getEntity(entityRef, em));
}
/**
* Finds a {@link Checklist} by its unique identifier.
*
* @param id the unique identifier of the checklist
* @return an {@link Optional} containing the found checklist, or empty if not found
*/
public Optional<Checklist> findById(Long id) {
return Optional.ofNullable(em.find(Checklist.class, id));
}
/**
* Saves a {@link Checklist} entity to the database.
*
* @param checklist the checklist to be saved
*/
public void save(Checklist checklist) {
em.persist(checklist);
}
}
repository/ChecklistConfigurationRepository.java
package eu.reply.lea.services.yms.task.checklist.repository;
import eu.reply.lea.services.yms.task.checklist.ChecklistConfiguration;
import eu.reply.lea.services.yms.task.checklist.EntityReference;
import eu.reply.lea.services.yms.task.checklist.ModuleConfiguration;
import eu.reply.lea.services.yms.task.checklist.logging.Logged;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import java.util.Optional;
/**
* Repository for managing {@link ChecklistConfiguration} entities.
*/
@ApplicationScoped
@Logged
public class ChecklistConfigurationRepository {
@Inject
@ModuleConfiguration.Module(ModuleConfiguration.Module.CURRENT)
private EntityManager em;
@Inject
private EntityReferenceHandler entityReferenceHandler;
/**
* Finds a {@link ChecklistConfiguration} by its associated {@link EntityReference}.
*
* @param entityRef the entity reference of the checklist configuration
* @return an {@link Optional} containing the found checklist configuration, or empty if not found
*/
public Optional<ChecklistConfiguration> findByEntityRef(EntityReference entityRef) {
return Optional.ofNullable(entityReferenceHandler.getEntity(entityRef, em));
}
/**
* Finds a {@link ChecklistConfiguration} by its unique identifier.
*
* @param id the unique identifier of the checklist configuration
* @return an {@link Optional} containing the found checklist configuration, or empty if not found
*/
public Optional<ChecklistConfiguration> findById(Long id) {
return Optional.ofNullable(em.find(ChecklistConfiguration.class, id));
}
/**
* Saves a {@link ChecklistConfiguration} entity to the database.
*
* @param checklistConfiguration the checklist configuration to be saved
*/
public void save(ChecklistConfiguration checklistConfiguration) {
em.persist(checklistConfiguration);
}
}
exception/NotesNotRequiredException.java
package eu.reply.lea.services.yms.task.checklist.exception;
/**
* Exception thrown when notes are not required for a checklist item but an attempt is made to add them.
*/
public class NotesNotRequiredException extends RuntimeException {
/**
* Constructs a new {@code NotesNotRequiredException} with the specified detail message.
*
* @param message the detail message
*/
public NotesNotRequiredException(String message) {
super(message);
}
}
exception/ChecklistConfigurationNotFoundException.java
package eu.reply.lea.services.yms.task.checklist.exception;
/**
* Exception thrown when a {@link ChecklistConfiguration} is not found.
*/
public class ChecklistConfigurationNotFoundException extends RuntimeException {
/**
* Constructs a new {@code ChecklistConfigurationNotFoundException} with the specified detail message.
*
* @param message the detail message
*/
public ChecklistConfigurationNotFoundException(String message) {
super(message);
}
}
exception/ChecklistBlockedException.java
package eu.reply.lea.services.yms.task.checklist.exception;
/**
* Exception thrown when an operation cannot be performed because the checklist is blocked.
*/
public class ChecklistBlockedException extends RuntimeException {
/**
* Constructs a new {@code ChecklistBlockedException} with the specified detail message.
*
* @param message the detail message
*/
public ChecklistBlockedException(String message) {
super(message);
}
}
exception/ValidationNotRequiredException.java
package eu.reply.lea.services.yms.task.checklist.exception;
/**
* Exception thrown when validation is not required for a checklist item but an attempt is made to perform it.
*/
public class ValidationNotRequiredException extends RuntimeException {
/**
* Constructs a new {@code ValidationNotRequiredException} with the specified detail message.
*
* @param message the detail message
*/
public ValidationNotRequiredException(String message) {
super(message);
}
}
exception/ChecklistNotFoundException.java
package eu.reply.lea.services.yms.task.checklist.exception;
/**
* Exception thrown when a {@link Checklist} is not found.
*/
public class ChecklistNotFoundException extends RuntimeException {
/**
* Constructs a new {@code ChecklistNotFoundException} with the specified detail message.
*
* @param message the detail message
*/
public ChecklistNotFoundException(String message) {
super(message);
}
}
exception/DocumentUploadNotRequiredException.java
package eu.reply.lea.services.yms.task.checklist.exception;
/**
* Exception thrown when document upload is not required for a checklist item but an attempt is made to upload one.
*/
public class DocumentUploadNotRequiredException extends RuntimeException {
/**
* Constructs a new {@code DocumentUploadNotRequiredException} with the specified detail message.
*
* @param message the detail message
*/
public DocumentUploadNotRequiredException(String message) {
super(message);
}
}
ChecklistItemConfiguration.java
package eu.reply.lea.services.yms.task.checklist;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
import java.math.BigDecimal;
/**
* Configuration entity for a checklist item.
*/
@Entity
@Getter
@Setter
public class ChecklistItemConfiguration extends StringCustomizableEntity {
/**
* The associated {@link ChecklistConfiguration}.
*/
@ManyToOne
private ChecklistConfiguration checklistConfiguration;
/**
* The name of the checklist item.
*/
@Column(nullable = false)
private String name;
/**
* The description of the checklist item.
*/
@Column(nullable = false)
private String description;
/**
* The order number determining the position of the checklist item.
*/
@Column(nullable = false)
private BigDecimal orderNumber;
/**
* Indicates if validation is required for this checklist item.
*/
private boolean checkValidation = false;
/**
* Indicates if document upload is required for this checklist item.
*/
private boolean documentUpload = false;
/**
* Indicates if adding notes is required for this checklist item.
*/
private boolean checkNotes = false;
}
ChecklistConfiguration.java
package eu.reply.lea.services.yms.task.checklist;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Configuration entity for a checklist, defining its name and associated items.
*/
@Entity
@StringifyAttributes(attributes = { "name"})
@Table(
uniqueConstraints = {@UniqueConstraint(columnNames = {"name"})},
indexes = {@Index(name = "idx_checklist_config_name", columnList = "name")}
)
@Getter
@Setter
public class ChecklistConfiguration extends StringCustomizableEntity {
/**
* The name of the checklist configuration.
*/
@NotNull
private String name;
/**
* The list of item configurations associated with this checklist configuration.
*/
@OneToMany(mappedBy = "checklistConfiguration", orphanRemoval = true, cascade = CascadeType.ALL)
private List<ChecklistItemConfiguration> itemsConfiguration = new ArrayList<>();
/**
* Retrieves an unmodifiable list of item configurations.
*
* @return an unmodifiable list of item configurations
*/
public List<ChecklistItemConfiguration> getItemsConfiguration() {
return Collections.unmodifiableList(itemsConfiguration);
}
/**
* Adds a new item configuration to the checklist configuration.
*
* @param itemConfiguration the item configuration to add
*/
public void addItem(ChecklistItemConfiguration itemConfiguration) {
this.addChild(itemConfiguration, itemsConfiguration);
itemConfiguration.setChecklistConfiguration(this);
}
/**
* Sets the name of the checklist configuration.
*
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* Retrieves the name of the checklist configuration.
*
* @return the name of the checklist configuration
*/
public String getName() {
return name;
}
}
ChecklistItem.java
package eu.reply.lea.services.yms.task.checklist;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
import java.math.BigDecimal;
/**
* Entity representing an item within a checklist.
*/
@Entity
@Getter
public class ChecklistItem extends StringCustomizableEntity {
/**
* The name of the checklist item.
*/
@Column(nullable = false)
private String name;
/**
* The description of the checklist item.
*/
@Column(nullable = false)
private String description;
/**
* The order number determining the position of the checklist item.
*/
@Column(nullable = false)
private BigDecimal orderNumber;
/**
* Indicates whether the checklist item has been completed.
*/
@Setter(AccessLevel.PROTECTED)
private boolean completed = false;
/**
* The associated {@link Checklist}.
*/
@ManyToOne
@Setter(AccessLevel.PACKAGE)
private Checklist checklist;
/**
* Embedded validation details for the checklist item.
*/
@Embedded
private CheckValidation checkValidation = new CheckValidation(false);
/**
* Embedded document upload details for the checklist item.
*/
@Embedded
private DocumentUpload documentUpload = new DocumentUpload(false);
/**
* Embedded notes details for the checklist item.
*/
@Embedded
private CheckNotes checkNotes = new CheckNotes(false);
/**
* Constructs a new {@link ChecklistItem} with the specified parameters.
*
* @param name the name of the checklist item
* @param description the description of the checklist item
* @param orderNumber the order number of the checklist item
* @param validationRequired indicates if validation is required
* @param documentRequired indicates if document upload is required
* @param notesRequired indicates if adding notes is required
*/
public ChecklistItem(String name, String description, BigDecimal orderNumber, boolean validationRequired, boolean documentRequired, boolean notesRequired) {
this.name = name;
this.description = description;
this.setOrderNumber(orderNumber);
this.checkValidation = new CheckValidation(validationRequired);
this.documentUpload = new DocumentUpload(documentRequired);
this.checkNotes = new CheckNotes(notesRequired);
}
/**
* Default constructor for JPA.
*/
public ChecklistItem() {
}
/**
* Checks if validation is required for this checklist item.
*
* @return {@code true} if validation is required, {@code false} otherwise
*/
public boolean isCheckValidationRequired(){
return checkValidation.isValidationRequired();
}
/**
* Checks if document upload is required for this checklist item.
*
* @return {@code true} if document upload is required, {@code false} otherwise
*/
public boolean isDocumentUploadRequired(){
return documentUpload.isDocumentRequired();
}
/**
* Checks if adding notes is required for this checklist item.
*
* @return {@code true} if adding notes is required, {@code false} otherwise
*/
public boolean isCheckNotesRequired(){
return checkNotes.isNotesRequired();
}
/**
* Determines if the checklist item is blocked from further actions.
*
* @return {@code true} if the item is blocked, {@code false} otherwise
*/
public boolean isBlocked(){
if (this.getOrderNumber().compareTo(BigDecimal.ZERO)<=0){
return false;
} else if (this.getChecklist().canProceedPastPartialConfirmation()){
return false;
}
return true;
}
/**
* Confirms that the validation for this checklist item is valid.
*
* @throws ChecklistBlockedException if the checklist item is blocked
* @throws ValidationNotRequiredException if validation is not required for this item
*/
public void confirmValid() {
if (isBlocked()){
throw new ChecklistBlockedException("Checklist item is blocked");
}
if (isCheckValidationRequired()) {
checkValidation.confirmValid();
} else {
throw new ValidationNotRequiredException("Validation is not required for this checklist item");
}
}
/**
* Checks if the validation status is valid.
*
* @return {@code true} if valid, {@code false} otherwise
*/
public boolean isValid() {
return checkValidation == null ? false : checkValidation.isValid();
}
/**
* Checks if the validation status is invalid.
*
* @return {@code true} if invalid, {@code false} otherwise
*/
public boolean isInvalid() {
return checkValidation == null ? false : checkValidation.isInvalid();
}
/**
* Checks if the validation has not been performed.
*
* @return {@code true} if not validated, {@code false} otherwise
*/
public boolean isNotValidated() {
return checkValidation == null ? true : checkValidation.isNotValidated();
}
/**
* Uploads a document for this checklist item.
*
* @param attachmentReference the reference to the uploaded attachment
* @throws ChecklistBlockedException if the checklist item is blocked
* @throws DocumentUploadNotRequiredException if document upload is not required for this item
*/
public void uploadDocument(AttachmentReference attachmentReference) {
if (isBlocked()){
throw new ChecklistBlockedException("Checklist item is blocked");
}
if (isDocumentUploadRequired()) {
documentUpload.uploadDocument(attachmentReference);
} else {
throw new DocumentUploadNotRequiredException("Document upload is not required for this checklist item");
}
}
/**
* Retrieves the uploaded document's attachment reference.
*
* @return the {@link AttachmentReference} if a document is uploaded, otherwise {@code null}
*/
public AttachmentReference getUploadedDocument() {
return documentUpload == null ? null : documentUpload.getAttachmentReference();
}
/**
* Adds notes to this checklist item.
*
* @param notesText the notes to add
* @throws ChecklistBlockedException if the checklist item is blocked
* @throws NotesNotRequiredException if adding notes is not required for this item
*/
public void addNotes(String notesText) {
if (isBlocked()){
throw new ChecklistBlockedException("Checklist item is blocked");
}
if (isCheckNotesRequired()) {
checkNotes.addNotes(notesText);
} else {
throw new NotesNotRequiredException("Adding notes is not required for this checklist item");
}
}
/**
* Retrieves the notes added to this checklist item.
*
* @return the notes if added, otherwise {@code null}
*/
public String getNotes() {
return checkNotes == null ? null : checkNotes.getNotes();
}
/**
* Checks if the document upload requirement has been fulfilled.
*
* @return {@code true} if document upload is completed or not required, {@code false} otherwise
*/
boolean isDocumentCompleted(){
if(isDocumentUploadRequired()){
return documentUpload.isDocumentUploaded();
} else {
return true;
}
}
/**
* Checks if the validation requirement has been fulfilled.
*
* @return {@code true} if validation is completed or not required, {@code false} otherwise
*/
boolean isCheckCompleted(){
if(isCheckValidationRequired()){
return checkValidation.isCheckCompleted();
} else {
return true;
}
}
/**
* Checks if the notes requirement has been fulfilled.
*
* @return {@code true} if notes are added or not required, {@code false} otherwise
*/
boolean isNotesCompleted(){
if(isCheckNotesRequired()){
return checkNotes.isNotesAdded();
} else {
return true;
}
}
/**
* Verifies the completeness of all required checks for this item.
*
* @return {@code true} if all required checks are completed, {@code false} otherwise
*/
boolean completenessVerified(){
return isCheckCompleted() && isNotesCompleted() && isDocumentCompleted();
}
/**
* Confirms that this checklist item is completed.
*
* @throws ChecklistBlockedException if the checklist item is blocked
* @throws IllegalStateException if completeness has not been verified
*/
public void confirmCompleted(){
if (isBlocked()){
throw new ChecklistBlockedException("Checklist item is blocked");
}
if (!completed && this.completenessVerified()){
this.completed = true;
checklist.confirmCompleted();
} else if (!completed){
throw new IllegalStateException("It's not possible to confirm, since there are still checks to do");
}
}
/**
* Sets the order number of the checklist item.
*
* @param orderNumber the order number to set
*/
public void setOrderNumber(BigDecimal orderNumber) {
this.orderNumber = orderNumber;
}
}
ChecklistFactory.java
package eu.reply.lea.services.yms.task.checklist;
import eu.reply.lea.services.yms.task.checklist.application.ChecklistApplicationService;
import lombok.extern.slf4j.Slf4j;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.List;
import java.util.stream.Collectors;
/**
* Factory class for creating {@link Checklist} instances based on configurations.
*/
@ApplicationScoped
@Slf4j
public class ChecklistFactory {
/**
* Generates a {@link Checklist} from the provided {@link ChecklistConfiguration}.
*
* @param configuration the checklist configuration to generate from
* @return the generated checklist
* @throws IllegalArgumentException if the provided configuration is {@code null}
*/
public Checklist generateChecklistFromConfiguration(ChecklistConfiguration configuration) {
if (configuration == null) {
throw new IllegalArgumentException("ChecklistConfiguration cannot be null");
}
Checklist checklist = new Checklist(configuration.getName());
// Loop through each item configuration and create checklist items
List<ChecklistItem> checklistItems = configuration.getItemsConfiguration().stream()
.map(this::createChecklistItem)
.collect(Collectors.toList());
checklistItems.forEach(checklist::addItem);
checklist.confirmCompleted();
return checklist;
}
/**
* Creates a {@link ChecklistItem} from a given {@link ChecklistItemConfiguration}.
*
* @param itemConfig the item configuration to create from
* @return the created checklist item
*/
private ChecklistItem createChecklistItem(ChecklistItemConfiguration itemConfig) {
return new ChecklistItem(
itemConfig.getName(),
itemConfig.getDescription(),
itemConfig.getOrderNumber(),
itemConfig.isCheckValidation(),
itemConfig.isDocumentUpload(),
itemConfig.isCheckNotes()
);
}
}
DocumentUpload.java
package eu.reply.lea.services.yms.task.checklist;
import javax.persistence.*;
/**
* Embeddable class representing the document upload details of a checklist item.
*/
@Embeddable
public class DocumentUpload {
/**
* Indicates if document upload is required.
*/
private boolean documentRequired = false;
/**
* The reference to the uploaded attachment.
*/
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "name", column = @Column(name = "document_name")),
@AttributeOverride(name = "uuid", column = @Column(name = "document_uuid"))
})
private AttachmentReference attachmentReference;
/**
* Constructs a new {@link DocumentUpload} with the specified requirement.
*
* @param documentRequired {@code true} if document upload is required, {@code false} otherwise
*/
public DocumentUpload(final boolean documentRequired) {
this.documentRequired = documentRequired;
}
/**
* Default constructor for JPA.
*/
public DocumentUpload() {
}
/**
* Checks if document upload is required.
*
* @return {@code true} if required, {@code false} otherwise
*/
public boolean isDocumentRequired() {
return documentRequired;
}
/**
* Sets whether document upload is required.
*
* @param documentRequired {@code true} if required, {@code false} otherwise
*/
private void setDocumentRequired(final boolean documentRequired) {
this.documentRequired = documentRequired;
}
/**
* Retrieves the attachment reference of the uploaded document.
*
* @return the {@link AttachmentReference} if uploaded, otherwise {@code null}
*/
public AttachmentReference getAttachmentReference() {
return attachmentReference;
}
/**
* Uploads a document by setting its attachment reference.
*
* @param attachmentReference the reference to the uploaded attachment
*/
public void uploadDocument(AttachmentReference attachmentReference) {
this.attachmentReference = attachmentReference;
}
/**
* Checks if a document has been uploaded.
*
* @return {@code true} if a document is uploaded, {@code false} otherwise
*/
public boolean isDocumentUploaded() {
return attachmentReference != null;
}
}
boundary/ChecklistConfigurationProcessor.java
package eu.reply.lea.services.yms.task.checklist.boundary;
import eu.reply.lea.services.yms.task.checklist.Checklist;
import eu.reply.lea.services.yms.task.checklist.ChecklistConfiguration;
import eu.reply.lea.services.yms.task.checklist.ChecklistFactory;
import eu.reply.lea.services.yms.task.checklist.logging.Logged;
import eu.reply.lea.services.yms.task.checklist.repository.ChecklistRepository;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
/**
* Processor for handling operations related to {@link ChecklistConfiguration}.
*/
@ApplicationScoped
@Logged
@Default
@EntityProcessor(entityClass= ChecklistConfiguration.class)
public class ChecklistConfigurationProcessor {
@Inject
private ChecklistFactory checklistFactory;
@Inject
private ChecklistRepository checklistRepository;
/**
* Creates a new checklist based on the provided configuration.
*
* @param checklistConfiguration the checklist configuration to create from
* @return the created checklist configuration
*/
public ChecklistConfiguration createCheckList(final ChecklistConfiguration checklistConfiguration) {
Checklist checklist = checklistFactory.generateChecklistFromConfiguration(checklistConfiguration);
checklistRepository.save(checklist);
return checklistConfiguration;
}
}
boundary/ChecklistItemProcessor.java
package eu.reply.lea.services.yms.task.checklist.boundary;
import eu.reply.lea.services.yms.task.checklist.ChecklistItem;
import eu.reply.lea.services.yms.task.checklist.action.AddDocumentAction;
import eu.reply.lea.services.yms.task.checklist.action.ChecklistItemAddNotesAction;
import eu.reply.lea.services.yms.task.checklist.logging.Logged;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
/**
* Processor for handling operations related to {@link ChecklistItem}.
*/
@ApplicationScoped
@Logged
@Default
@EntityProcessor(entityClass= ChecklistItem.class)
public class ChecklistItemProcessor {
/**
* Persists the provided checklist item.
*
* @param item the checklist item to persist
* @return the persisted checklist item
*/
public ChecklistItem persisting(ChecklistItem item){
return item;
}
/**
* Confirms that the provided checklist item is valid, based on validation requirements.
*
* @param item the checklist item to confirm
* @return the updated checklist item
*/
@Condition(value = {@Condition.Attribute(name = "checkValidation.validationRequired", values = {"true"})})
public ChecklistItem confirmValid(final ChecklistItem item) {
item.confirmValid();
return item;
}
/**
* Handles the action of uploading a document for the provided checklist item.
*
* @param item the checklist item to act upon
* @param action the action containing the attachment reference
* @return the updated checklist item
*/
@Condition(value = {@Condition.Attribute(name = "documentUpload.documentRequired", values = {"true"})})
public ChecklistItem action(final ChecklistItem item, final AddDocumentAction action) {
item.uploadDocument(action.getAttachmentReference());
return item;
}
/**
* Handles the action of adding notes to the provided checklist item.
*
* @param item the checklist item to act upon
* @param action the action containing the notes
* @return the updated checklist item
*/
@Condition(value = {@Condition.Attribute(name = "checkNotes.notesRequired", values = {"true"})})
public ChecklistItem action(final ChecklistItem item, final ChecklistItemAddNotesAction action) {
item.addNotes(action.getNotes());
return item;
}
/**
* Confirms that the provided checklist item is completed.
*
* @param item the checklist item to confirm
* @return the updated checklist item
*/
public ChecklistItem complete(final ChecklistItem item) {
item.confirmCompleted();
return item;
}
}
boundary/ChecklistProcessor.java
package eu.reply.lea.services.yms.task.checklist.boundary;
import eu.reply.lea.services.yms.task.checklist.Checklist;
import eu.reply.lea.services.yms.task.checklist.application.ChecklistApplicationService;
import eu.reply.lea.services.yms.task.checklist.logging.Logged;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
/**
* Processor for handling operations related to {@link Checklist}.
*/
@ApplicationScoped
@Logged
@Default
@EntityProcessor(entityClass= Checklist.class)
public class ChecklistProcessor {
@Inject
private ChecklistApplicationService checklistApplicationService;
/**
* Proceeds past a partial confirmation state for the provided checklist.
*
* @param checklist the checklist to update
* @return the updated checklist
*/
public Checklist proceedPastPartialConfirmation(final Checklist checklist) {
checklistApplicationService.proceedPastPartialConfirmation(checklist);
return checklist;
}
}
boundary/api/ChecklistApiImpl.java
package eu.reply.lea.services.yms.task.checklist.boundary.api;
import eu.reply.lea.services.yms.task.checklist.Checklist;
import eu.reply.lea.services.yms.task.checklist.EntityReference;
import eu.reply.lea.services.yms.task.checklist.application.ChecklistApplicationService;
import eu.reply.lea.services.yms.task.checklist.exception.ChecklistNotFoundException;
import eu.reply.lea.services.yms.task.checklist.repository.EntityReferenceHandler;
import javax.inject.Inject;
import javax.persistence.EntityManager;
/**
* Implementation of the {@link ChecklistApi} interface, providing APIs for checklist operations.
*/
public class ChecklistApiImpl implements ChecklistApi {
@Inject
private ChecklistApplicationService checklistApplicationService;
@Inject
private EntityReferenceHandler entityReferenceHandler;
@Inject
@ModuleConfiguration.Module(ModuleConfiguration.Module.CURRENT)
private EntityManager em;
/**
* Checks if the checklist with the specified ID is partially confirmed.
*
* @param checklistId the ID of the checklist
* @return {@code true} if partially confirmed, {@code false} otherwise
*/
@Override
public boolean isChecklistPartiallyConfirmed(final Long checklistId){
return checklistApplicationService.isChecklistPartiallyCompleted(checklistId);
}
/**
* Checks if the checklist with the specified ID is completed.
*
* @param checklistId the ID of the checklist
* @return {@code true} if completed, {@code false} otherwise
*/
@Override
public boolean isChecklistCompleted(final Long checklistId){
return checklistApplicationService.isChecklistCompleted(checklistId);
}
/**
* Proceeds past the partial confirmation state for the checklist with the specified ID.
*
* @param checklistId the ID of the checklist
*/
@Override
public void proceedPastPartialConfirmation(Long checklistId){
checklistApplicationService.proceedPastPartialConfirmation(checklistId);
}
/**
* Creates a new checklist based on the provided checklist configuration.
*
* @param checkListConfiguration the entity reference to the checklist configuration
* @return the entity reference to the created checklist
*/
@Override
public EntityReference createChecklist(final EntityReference checkListConfiguration) {
Checklist checklist = checklistApplicationService.createChecklist(checkListConfiguration);
return entityReferenceHandler.getEntityReference(em, checklist);
}
}
boundary/api/ChecklistApi.java
package eu.reply.lea.services.yms.task.checklist.boundary.api;
import eu.reply.lea.services.yms.task.checklist.EntityReference;
/**
* API interface for performing operations related to checklists.
*/
public interface ChecklistApi {
/**
* Determines if the checklist with the specified ID is partially confirmed.
*
* @param checklistId the ID of the checklist
* @return {@code true} if the checklist is partially confirmed, {@code false} otherwise
*/
boolean isChecklistPartiallyConfirmed(final Long checklistId);
/**
* Determines if the checklist with the specified ID is completed.
*
* @param checklistId the ID of the checklist
* @return {@code true} if the checklist is completed, {@code false} otherwise
*/
boolean isChecklistCompleted(final Long checklistId);
/**
* Proceeds past the partial confirmation state for the checklist with the specified ID.
*
* @param checklistId the ID of the checklist
*/
void proceedPastPartialConfirmation(Long checklistId);
/**
* Creates a new checklist based on the provided checklist configuration.
*
* @param checkListConfiguration the entity reference to the checklist configuration
* @return the entity reference to the created checklist
*/
EntityReference createChecklist(EntityReference checkListConfiguration);
}
Checklist.java
package eu.reply.lea.services.yms.task.checklist;
import lombok.Getter;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Entity representing a Checklist, containing multiple checklist items.
*/
@Entity
@Getter
public class Checklist extends StringCustomizableEntity {
/**
* The name of the checklist.
*/
@NotNull
private String name;
/**
* The list of items included in this checklist.
*/
@OneToMany(mappedBy = "checklist", orphanRemoval = true, cascade = CascadeType.ALL)
private List<ChecklistItem> items = new ArrayList<>();
/**
* The completion status of the checklist.
*/
private ChecklistCompletionType completionType = ChecklistCompletionType.NOT_COMPLETED;
/**
* Indicates if the checklist can proceed past partial confirmation.
*/
private boolean canProceedPastPartialConfirmation = false;
/**
* Constructs a new {@link Checklist} with the specified name.
*
* @param name the name of the checklist
*/
public Checklist(String name){
this.name = name;
}
/**
* Default constructor for JPA.
*/
public Checklist(){
}
/**
* Retrieves an unmodifiable list of checklist items.
*
* @return an unmodifiable list of checklist items
*/
public List<ChecklistItem> getItems() {
return Collections.unmodifiableList(items);
}
/**
* Adds a new checklist item to this checklist.
*
* @param item the checklist item to add
*/
public void addItem(ChecklistItem item) {
this.items.add(item);
item.setChecklist(this);
}
/**
* Enables the checklist to proceed past partial confirmation.
*/
public void setCanProceedPastPartialConfirmation() {
this.canProceedPastPartialConfirmation = true;
}
/**
* Checks if the checklist can proceed past partial confirmation.
*
* @return {@code true} if it can proceed, {@code false} otherwise
*/
public boolean canProceedPastPartialConfirmation() {
return canProceedPastPartialConfirmation;
}
/**
* Checks if the checklist is completed.
*
* @return {@code true} if completed, {@code false} otherwise
*/
public boolean isCompleted() {
return completionType.equals(ChecklistCompletionType.COMPLETED);
}
/**
* Checks if the checklist has partial completion.
*
* @return {@code true} if partially completed, {@code false} otherwise
*/
public boolean isPartialCompletion() {
return completionType.isPartialCompletion();
}
/**
* Verifies if all checklist items are completed.
*
* @return {@code true} if all items are completed, {@code false} otherwise
*/
private boolean completenessVerified() {
return this.items.stream().allMatch(ChecklistItem::isCompleted);
}
/**
* Verifies if all checklist items with a negative order number are completed.
*
* @return {@code true} if all such items are completed, {@code false} otherwise
*/
private boolean partialCompletenessVerified() {
return this.items.stream()
.filter(t -> t.getOrderNumber().compareTo(BigDecimal.ZERO) < 0)
.allMatch(ChecklistItem::isCompleted);
}
/**
* Confirms the completion status of the checklist based on the status of its items.
*/
public void confirmCompleted() {
if (!isCompleted() && this.completenessVerified()) {
this.setCompletionType(ChecklistCompletionType.COMPLETED);
} else if ((!isCompleted() || !isPartialCompletion()) && this.partialCompletenessVerified()) {
this.setCompletionType(ChecklistCompletionType.PARTIAL_COMPLETION);
}
}
/**
* Sets the completion type of the checklist.
*
* @param completionType the completion type to set
*/
private void setCompletionType(ChecklistCompletionType completionType) {
this.completionType = completionType;
}
/**
* Retrieves the completion type of the checklist.
*
* @return the {@link ChecklistCompletionType}
*/
public ChecklistCompletionType getCompletionType() {
return completionType;
}
}
CheckNotes.java
package eu.reply.lea.services.yms.task.checklist;
import javax.persistence.Embeddable;
/**
* Embeddable class representing the notes associated with a checklist item.
*/
@Embeddable
public class CheckNotes {
/**
* Indicates if adding notes is required.
*/
private boolean notesRequired = false;
/**
* The notes text added to the checklist item.
*/
private String notes;
/**
* Default constructor for JPA.
*/
public CheckNotes() {}
/**
* Constructs a new {@link CheckNotes} with the specified requirement.
*
* @param notesRequired {@code true} if adding notes is required, {@code false} otherwise
*/
public CheckNotes(boolean notesRequired) {
this.setNotesRequired(notesRequired);
}
/**
* Checks if adding notes is required.
*
* @return {@code true} if required, {@code false} otherwise
*/
public boolean isNotesRequired() {
return notesRequired;
}
/**
* Sets whether adding notes is required.
*
* @param notesRequired {@code true} if required, {@code false} otherwise
*/
private void setNotesRequired(boolean notesRequired){
this.notesRequired = notesRequired;
}
/**
* Retrieves the notes associated with this checklist item.
*
* @return the notes text, or {@code null} if none are added
*/
public String getNotes() {
return notes;
}
/**
* Adds notes to this checklist item.
*
* @param notes the notes text to add
*/
public void addNotes(String notes) {
this.notes = notes;
}
/**
* Checks if notes have been added.
*
* @return {@code true} if notes are added, {@code false} otherwise
*/
public boolean isNotesAdded() {
return notes != null;
}
}
CheckValidation.java
package eu.reply.lea.services.yms.task.checklist;
import javax.persistence.Embeddable;
/**
* Embeddable class representing the validation status of a checklist item.
*/
@Embeddable
public class CheckValidation {
/**
* Enum representing the validation status.
*/
public enum ValidationStatus {
NOT_VALIDATED,
VALID,
INVALID
}
/**
* The current validation status.
*/
private ValidationStatus status = ValidationStatus.NOT_VALIDATED;
/**
* Indicates if validation is required.
*/
private boolean validationRequired = false;
/**
* Constructs a new {@link CheckValidation} with the specified requirement.
*
* @param validationRequired {@code true} if validation is required, {@code false} otherwise
*/
public CheckValidation(boolean validationRequired) {
this.setValidationRequired(validationRequired);
}
/**
* Default constructor for JPA.
*/
public CheckValidation() {
}
/**
* Sets whether validation is required.
*
* @param validationRequired {@code true} if required, {@code false} otherwise
*/
private void setValidationRequired(boolean validationRequired) {
this.validationRequired = validationRequired;
}
/**
* Checks if validation is required.
*
* @return {@code true} if required, {@code false} otherwise
*/
public boolean isValidationRequired() {
return validationRequired;
}
/**
* Retrieves the current validation status.
*
* @return the current {@link ValidationStatus}
*/
public ValidationStatus getStatus() {
return status;
}
/**
* Confirms that the validation is valid.
*/
public void confirmValid() {
this.status = ValidationStatus.VALID;
}
/**
* Confirms that the validation is invalid.
*/
public void confirmInvalid() {
this.status = ValidationStatus.INVALID;
}
/**
* Checks if the validation check has been completed.
*
* @return {@code true} if completed, {@code false} otherwise
*/
public boolean isCheckCompleted(){
return status != ValidationStatus.NOT_VALIDATED;
}
/**
* Checks if the validation state is valid.
*
* @return {@code true} if valid, {@code false} otherwise
*/
public boolean isValid() {
return this.status == ValidationStatus.VALID;
}
/**
* Checks if the validation state is invalid.
*
* @return {@code true} if invalid, {@code false} otherwise
*/
public boolean isInvalid() {
return this.status == ValidationStatus.INVALID;
}
/**
* Checks if the validation has not been performed yet.
*
* @return {@code true} if not validated, {@code false} otherwise
*/
public boolean isNotValidated() {
return this.status == ValidationStatus.NOT_VALIDATED;
}
@Override
public String toString() {
return "CheckValidation{" +
"validationRequired=" + validationRequired +
", status=" + status +
'}';
}
}
action/ChecklistItemAddNotesAction.java
package eu.reply.lea.services.yms.task.checklist.action;
import lombok.NonNull;
import java.io.Serializable;
/**
* Action class representing the addition of notes to a {@link ChecklistItem}.
*/
public class ChecklistItemAddNotesAction implements Serializable {
private static final long serialVersionUID = -753133695422042391L;
/**
* The notes to be added.
*/
@NonNull
private String notes;
/**
* Default constructor.
*/
public ChecklistItemAddNotesAction() {
}
/**
* Retrieves the notes to be added.
*
* @return the notes text
*/
public @NonNull String getNotes() {
return notes;
}
/**
* Sets the notes to be added.
*
* @param notes the notes text
*/
public void setNotes(final @NonNull String notes) {
this.notes = notes;
}
}
action/AddDocumentAction.java
package eu.reply.lea.services.yms.task.checklist.action;
import eu.reply.lea.services.yms.task.checklist.AttachmentReference;
import lombok.NonNull;
/**
* Action class representing the addition of a document to a {@link ChecklistItem}.
*/
public class AddDocumentAction extends Stringable implements Serializable {
private static final long serialVersionUID = -753133695422042391L;
/**
* The reference to the attachment being uploaded.
*/
@NonNull
private AttachmentReference attachmentReference;
/**
* Default constructor.
*/
public AddDocumentAction() {
}
/**
* Retrieves the attachment reference.
*
* @return the {@link AttachmentReference}
*/
public AttachmentReference getAttachmentReference() {
return attachmentReference;
}
/**
* Sets the attachment reference.
*
* @param attachmentReference the {@link AttachmentReference} to set
*/
public void setAttachmentReference(final AttachmentReference attachmentReference) {
this.attachmentReference = attachmentReference;
}
@Override
public String toString() {
return new StringJoiner(", ", AddDocumentAction.class.getSimpleName() + "[", "]")
.add("attachmentReference=" + attachmentReference)
.toString();
}
}
application/ChecklistApplicationService.java
package eu.reply.lea.services.yms.task.checklist.application;
import eu.reply.lea.services.yms.task.checklist.Checklist;
import eu.reply.lea.services.yms.task.checklist.EntityReference;
/**
* Service interface for performing application-level operations on checklists.
*/
public interface ChecklistApplicationService {
/**
* Proceeds past the partial confirmation state for the checklist with the specified ID.
*
* @param checklistId the ID of the checklist
* @return the updated checklist
*/
Checklist proceedPastPartialConfirmation(Long checklistId);
/**
* Proceeds past the partial confirmation state for the provided checklist.
*
* @param checklist the checklist to update
* @return the updated checklist
*/
Checklist proceedPastPartialConfirmation(Checklist checklist);
/**
* Checks if the checklist with the specified ID is partially completed.
*
* @param checklistId the ID of the checklist
* @return {@code true} if partially completed, {@code false} otherwise
*/
boolean isChecklistPartiallyCompleted(final Long checklistId);
/**
* Checks if the checklist with the specified ID is completed.
*
* @param checklistId the ID of the checklist
* @return {@code true} if completed, {@code false} otherwise
*/
boolean isChecklistCompleted(final Long checklistId);
/**
* Creates a new checklist based on the provided checklist configuration reference.
*
* @param checkListConfigurationRef the entity reference to the checklist configuration
* @return the created checklist
*/
Checklist createChecklist(EntityReference checkListConfigurationRef);
}
application/ChecklistApplicationServiceImpl.java
package eu.reply.lea.services.yms.task.checklist.application;
import eu.reply.lea.services.yms.task.checklist.Checklist;
import eu.reply.lea.services.yms.task.checklist.ChecklistConfiguration;
import eu.reply.lea.services.yms.task.checklist.EntityReference;
import eu.reply.lea.services.yms.task.checklist.exception.ChecklistConfigurationNotFoundException;
import eu.reply.lea.services.yms.task.checklist.exception.ChecklistNotFoundException;
import eu.reply.lea.services.yms.task.checklist.logging.Logged;
import eu.reply.lea.services.yms.task.checklist.repository.ChecklistConfigurationRepository;
import eu.reply.lea.services.yms.task.checklist.repository.ChecklistRepository;
import eu.reply.lea.services.yms.task.checklist.ChecklistFactory;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
/**
* Implementation of the {@link ChecklistApplicationService} interface, providing business logic for checklist operations.
*/
@ApplicationScoped
@Logged
@Transactional
public class ChecklistApplicationServiceImpl implements ChecklistApplicationService {
@Inject
private ChecklistRepository checklistRepository;
@Inject
private ChecklistConfigurationRepository checklistConfigurationRepository;
@Inject
private ChecklistFactory checklistFactory;
@Inject
private Logger logger;
/**
* Proceeds past the partial confirmation state for the checklist with the specified ID.
*
* @param checklistId the ID of the checklist
* @return the updated checklist
* @throws ChecklistNotFoundException if the checklist is not found
*/
@Override
public Checklist proceedPastPartialConfirmation(final Long checklistId) {
logger.debug("Proceeding past partial confirmation for Checklist ID: {}", checklistId);
Checklist checklist = checklistRepository.findById(checklistId)
.orElseThrow(() -> new ChecklistNotFoundException("Checklist not found"));
return proceedPastPartialConfirmation(checklist);
}
/**
* Proceeds past the partial confirmation state for the provided checklist.
*
* @param checklist the checklist to update
* @return the updated checklist
* @throws IllegalArgumentException if the provided checklist is {@code null}
*/
@Override
public Checklist proceedPastPartialConfirmation(final Checklist checklist) {
if (checklist == null) {
logger.error("Checklist provided is null");
throw new IllegalArgumentException("Checklist provided is null");
}
// Call the processor for domain logic
checklist.setCanProceedPastPartialConfirmation();
// Persist the changes
checklistRepository.save(checklist);
return checklist;
}
/**
* Checks if the checklist with the specified ID is partially completed.
*
* @param checklistId the ID of the checklist
* @return {@code true} if partially completed, {@code false} otherwise
* @throws ChecklistNotFoundException if the checklist is not found
*/
@Override
public boolean isChecklistPartiallyCompleted(final Long checklistId) {
logger.debug("Checking if checklist is partially confirmed for Checklist ID: {}", checklistId);
Checklist checklist = checklistRepository.findById(checklistId)
.orElseThrow(() -> new ChecklistNotFoundException("Checklist not found"));
return checklist.isPartialCompletion();
}
/**
* Checks if the checklist with the specified ID is completed.
*
* @param checklistId the ID of the checklist
* @return {@code true} if completed, {@code false} otherwise
* @throws ChecklistNotFoundException if the checklist is not found
*/
@Override
public boolean isChecklistCompleted(final Long checklistId) {
logger.debug("Checking if checklist is completed for Checklist ID: {}", checklistId);
Checklist checklist = checklistRepository.findById(checklistId)
.orElseThrow(() -> new ChecklistNotFoundException("Checklist not found"));
return checklist.isCompleted();
}
/**
* Creates a new checklist based on the provided checklist configuration reference.
*
* @param checkListConfigurationRef the entity reference to the checklist configuration
* @return the created checklist
* @throws IllegalArgumentException if the configuration reference is {@code null}
* @throws ChecklistConfigurationNotFoundException if the checklist configuration is not found
*/
@Override
public Checklist createChecklist(final EntityReference checkListConfigurationRef) {
if (checkListConfigurationRef == null) {
throw new IllegalArgumentException("Checklist configuration ref is null");
}
logger.debug("Creating a new checklist from configuration with id ID: {}", checkListConfigurationRef);
ChecklistConfiguration checklistConfiguration = checklistConfigurationRepository.findByEntityRef(checkListConfigurationRef)
.orElseThrow(() -> new ChecklistConfigurationNotFoundException("ChecklistConfiguration not found"));
Checklist checklist = checklistFactory.generateChecklistFromConfiguration(checklistConfiguration);
checklistRepository.save(checklist);
return checklist;
}
}
Additional Notes
-
Lombok Annotations: The classes utilize Lombok annotations such as @Getter, @Setter, and @NonNull to reduce boilerplate code. Ensure that Lombok is correctly set up in your project to take advantage of these annotations.
-
Custom Annotations: Annotations like @Logged, @Default, and @EntityProcessor appear to be custom annotations. Ensure that these are properly defined and configured in your project.
-
Exception Handling: Custom exceptions have been defined to handle specific error scenarios, enhancing the readability and maintainability of error handling logic.
-
Entity Management: The repositories handle entity persistence and retrieval using JPA's EntityManager. Ensure that transactions are appropriately managed, especially if additional methods are added that modify the database state.
-
Service Layer: The ChecklistApplicationService and its implementation encapsulate the business logic, promoting a clean separation of concerns between the application's layers.
-
API Layer: The ChecklistApi interface and its implementation ChecklistApiImpl provide a clear contract for external interactions with the checklist functionalities.
-
Embeddable Classes: Classes like DocumentUpload, CheckNotes, and CheckValidation are marked as @Embeddable, indicating that they are value types embedded within other entities. Proper JPA mapping ensures their persistence within the parent entities.
-
Synchronization and Thread Safety: Depending on the application's concurrency requirements, consider thread safety aspects, especially when dealing with mutable shared data.
-
Validation: The use of @NotNull and other validation annotations helps in enforcing data integrity constraints at the entity level.
Ensure that all dependencies, such as JPA, Lombok, and any custom annotations or classes, are correctly configured in your project to support the functionalities demonstrated in these classes.