public static final String Q_FIND_BY_LOGISTIC_UNIT_REFERENCE = "QueueProcessTracker.Q_FIND_BY_LOGISTIC_UNIT_REFERENCE";
public static final String Q_FIND_BY_PROCESS_REFERENCE = "QueueProcessTracker.Q_FIND_BY_PROCESS_REFERENCE";
@Getter
public boolean isEnqueueProcessPending = false;
@Getter
public boolean isDequeueProcessPending = false;
/**
Retrieves a read-only list of tasks that are pending placement in the queue.
@return A list of {@link QueueProcessTrackerTask}.
*/
public List getTasksPendingQueuePlacement() {
return Collections.unmodifiableList(tasksPendingQueuePlacement);
}
/**
Retrieves a read-only list of tasks that are pending dequeue.
@return A list of {@link QueueProcessTrackerTask}.
*/
public List getTasksPendingDequeue() {
return Collections.unmodifiableList(tasksPendingDequeue);
}
/**
Adds a task to the list of tasks pending queue placement.
@param taskReference The task reference to be added.
@return true if the task was added successfully.
*/
protected boolean addTaskPendingQueuePlacement(final TaskReference taskReference){
return this.tasksPendingQueuePlacement.add(new QueueProcessTrackerTask(taskReference, this.toEntityReference()));
}
/**
Adds a task to the list of tasks pending dequeue.
@param taskReference The task reference to be added.
@return true if the task was added successfully.
*/
protected boolean addTaskPendingDequeue(final TaskReference taskReference){
return this.tasksPendingDequeue.add(new QueueProcessTrackerTask(taskReference, this.toEntityReference()));
}
/**
Checks if a given task is queued.
@param taskReferences The task reference to check.
@return true if the task is queued, false otherwise.
*/
public boolean isTaskQueued(final TaskReference taskReferences){
return this.tasksQueued.contains(new QueueProcessTrackerTask(taskReferences, this.toEntityReference()));
}
/**
Checks if a given task is pending queue placement.
@param taskReferences The task reference to check.
@return true if the task is pending queue placement, false otherwise.
*/
public boolean isTaskPendingQueuePlacement(final TaskReference taskReferences){
return this.tasksPendingQueuePlacement.contains(new QueueProcessTrackerTask(taskReferences, this.toEntityReference()));
}
/**
Retrieves a read-only list of tasks that have been placed in the queue.
@return A list of {@link TaskReference}.
*/
public List getTasksQueued() {
return Collections.unmodifiableList(tasksQueued);
}
public QueueProcessTracker(final EntityReference logisticUnitReference) {
this.logisticUnitReference = logisticUnitReference;
}
/**
Sets the process reference and flags the process as pending. It also fires a creation event for the element queue process.
@param processReference The process reference to set.
@param elementQueueProcessCreatedEventProducer Producer to fire the event.
*/
public void setExecutingEnqueueProcessReference(final ProcessReference processReference,
final Producer elementQueueProcessCreatedEventProducer) {
this.executingEnqueueProcessReference = processReference;
this.setEnqueueProcessPending(true);
elementQueueProcessCreatedEventProducer.fire(new ElementQueueProcessCreatedEvent(processReference));
}
/**
Sets the process reference and flags the process as pending. It also fires a creation event for the element dequeue process.
@param processReference The process reference to set.
@param elementQueueProcessCreatedEventProducer Producer to fire the event.
*/
public void setExecutingDequeueProcessReference(final ProcessReference processReference,
final Producer elementQueueProcessCreatedEventProducer) {
this.executingDequeueProcessReference = processReference;
this.setDequeueProcessPending(true);
elementQueueProcessCreatedEventProducer.fire(new ElementQueueProcessCreatedEvent(processReference));
}
Completes the current process, moves all pending tasks to the queued list, and clears pending tasks.
*/
public void completedEnqueueProcess(){
this.executingEnqueueProcessReference = null;
this.tasksQueued.addAll(this.getTasksPendingQueuePlacement());
this.tasksPendingQueuePlacement.clear();
this.setEnqueueProcessPending(false);
}
/**
Completes the current process, moves all pending tasks to the queued list, and clears pending tasks.
*/
public void completedDequeueProcess(){
this.executingDequeueProcessReference = null;
this.tasksQueued.removeAll(this.getTasksPendingDequeue());
this.tasksPendingDequeue.clear();
this.setDequeueProcessPending(false);
}
/**
Aborts the current queue process, clearing all pending tasks and resetting the process state.
*/
public void abortedEnqueueProcess(){
this.executingEnqueueProcessReference = null;
this.tasksPendingQueuePlacement.clear();
this.setEnqueueProcessPending(false);
}
/**
Creates a element queue process for the queueRequest, ensuring no process is currently pending.
@param queueRequest request that generates the element queue process.
@return {@link ElementEnqueueProcess} Newly created element queue process.
*/
public ElementEnqueueProcess createElementQueueProcess(final QueueRequest queueRequest) {
if (this.isEnqueueProcessPending()) {
throw new EnqueueProcessPendingException();
}
if (this.isDequeueProcessPending()) {
throw new DequeueProcessPendingException();
}
ElementEnqueueProcess elementEnqueueProcess = new ElementEnqueueProcess(logisticUnitReference);
boolean taskAdded = false;
for (QueueRequestInfo queueRequestInfo : queueRequest.getTaskInfos()){
if (isTaskQueued(queueRequestInfo.getTaskReference())){
continue;
}
taskAdded = true;
elementEnqueueProcess.addExecutionLocation(queueRequestInfo.getExecutionLocation(), queueRequestInfo.isScheduled());
this.addTaskPendingQueuePlacement(queueRequestInfo.getTaskReference());
}
if(!taskAdded){
throw new EmptyQueueRequestException();
}
return elementEnqueueProcess;
}
/**
Creates an element dequeue process for the next location destinations, ensuring no dequeue process is currently pending.
@param nextLocation List of next location destinations to dequeue from.
@return {@link ElementDequeueProcess} Newly created element dequeue process, or null if no locations to dequeue from.
@throws IllegalStateException if a dequeue process is already pending.
*/
public ElementDequeueProcess createElementDequeueProcess(final List nextLocation) {
if (this.isDequeueProcessPending()) {
throw new IllegalStateException("It's not possible to create a new dequeue process since one is still running.");
}
if (nextLocation.isEmpty()){ //No Location to dequeue from
return null;
}
ElementDequeueProcess elementDequeueProcess = new ElementDequeueProcess(logisticUnitReference, nextLocation);//TODO scheduled here is not appropriate
return elementDequeueProcess;
}
public EntityReference toEntityReference() {
return new EntityReference(MODULE_NAME, getClass().getSimpleName(), String.valueOf(this.getId()), null, "-");
}
@OneToMany(mappedBy = "elementQueueProcess", cascade = CascadeType.ALL)
private final List<QueueProcessItem> queueProcessItems = new ArrayList<>();
@Getter
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "module", column = @Column(name = "externalEntityRef_module")),
@AttributeOverride(name = "entityName", column = @Column(name = "externalEntityRef_entity_name")),
@AttributeOverride(name = "entityId", column = @Column(name = "externalEntityRef_entity_id")),
@AttributeOverride(name = "businessKey", column = @Column(name = "externalEntityRef_business_key")),
@AttributeOverride(name = "usageType", column = @Column(name = "externalEntityRef_usage_type"))
})
private EntityReference externalEntityReference;
protected ElementQueueProcess() {
super();
}
public ElementQueueProcess(final EntityReference externalEntityReference){
this.externalEntityReference = externalEntityReference;
}
public List<QueueProcessItem> getQueueProcessItems() {
return Collections.unmodifiableList(this.queueProcessItems);
}
/**
* Confirms the process at a specific location and fires events based on the process state.
*
* @param locationReference The location to confirm.
* @param elementQueueProcessPartialConfirmEventProducer Producer for firing partial confirmation events.
* @param elementQueueProcessCompletedEventProducer Producer for firing completion events.
* @throws IllegalStateException if the process has already been completed.
*/
public void confirm(final LocationReference locationReference,
final Producer<ElementQueueProcessPartialConfirmEvent> elementQueueProcessPartialConfirmEventProducer,
final Producer<ElementQueueProcessCompletedEvent> elementQueueProcessCompletedEventProducer) {
if (this.isCompleted()) {
throw new IllegalStateException("The process has already been completed");
}
boolean confirmed = this.confirmElement(locationReference);
this.completeProcess(ProcessCompletionType.CompletedNormally);
if (!this.isCompleted() && confirmed) {
elementQueueProcessPartialConfirmEventProducer.fire(
new ElementQueueProcessPartialConfirmEvent(new ProcessReference(this)));
}
if (this.isCompleted()) {
elementQueueProcessCompletedEventProducer.fire(
new ElementQueueProcessCompletedEvent(new ProcessReference(this), externalEntityReference,
this.getProcessCompletionType()));
}
}
/**
* Aborts the process at a specific location and fires a completion event if the process is completed.
*
* @param executionLocation The location where the process should be aborted.
* @param elementQueueProcessCompletedEventProducer Producer for firing completion events.
* @throws IllegalStateException if the process has already been completed.
*/
public void abort(final LocationReference executionLocation,
final Producer<ElementQueueProcessCompletedEvent> elementQueueProcessCompletedEventProducer) {
if (this.isCompleted()) {
throw new IllegalStateException("The process has already been completed");
}
this.queueProcessItems.stream().filter(
queueProcessItem -> queueProcessItem.getExecutionLocation().equals(executionLocation)).forEach(
queueProcessItem -> queueProcessItem.abort()
);
this.abortProcess(ProcessCompletionType.Aborted);
if (this.isCompleted()) {
elementQueueProcessCompletedEventProducer.fire(
new ElementQueueProcessCompletedEvent(new ProcessReference(this), externalEntityReference,
this.getProcessCompletionType()));
}
}
private boolean confirmElement(final LocationReference locationReference) {
Long numberOfInitialConfirmedElements = this.numberOfConfirmations();
this.queueProcessItems.stream().filter(
queueProcessItem -> queueProcessItem.getExecutionLocation().equals(locationReference)).forEach(
queueProcessItem -> queueProcessItem.confirm()
);
Long numberOfFinalConfirmedElements = this.numberOfConfirmations();
return numberOfFinalConfirmedElements.compareTo(numberOfInitialConfirmedElements) > 0;
}
private Long numberOfConfirmations() {
return this.queueProcessItems.stream().filter(QueueProcessItem::isConfirmed).count();
}
/**
* Add a new execution location.
*
* @param executionLocations List of locations to be added.
* @param scheduled boolean to indicate if the queueing was scheduled
* @return A read-only list of location statuses.
*/
public boolean addExecutionLocation(final LocationReference executionLocations,
final boolean scheduled) {
return queueProcessItems.add(new QueueProcessItem(this,
executionLocations, scheduled,
queueProcessItems.size()));
}
/**
* Verifies if all element locations within the process have been confirmed.
* It is a crucial part of the process lifecycle, ensuring that no further action is required for this process
* to be considered complete.
*
* @return true if all element location statuses are confirmed, false otherwise.
*/
@Override
protected boolean completenessVerified() {
return queueProcessItems.stream().allMatch(QueueProcessItem::isConfirmed);
}
@Override
protected boolean abortionVerified() {
return queueProcessItems.stream().allMatch(el -> el.isAborted() || !el.isStarted());
}
/**
* Retrieves the next Item containing information about the next queue location.
*
* @return The next QueueProcessItem to be confirmed, or null if all locations are confirmed.
*/
@JsonIgnore
public QueueProcessItem getNextQueueProcessItem() {
QueueProcessItem queueProcessItem = null;
for (QueueProcessItem status : this.queueProcessItems) {
if (!status.isConfirmed() && (queueProcessItem == null
|| status.getEnqueueOrder() < queueProcessItem.getEnqueueOrder())) {
queueProcessItem = status;
}
}
return queueProcessItem;
}
/**
* Marks the queueing process as started for a specific location.
*
* @param executionLocation The location where queueing has started.
*/
public void started(final LocationReference executionLocation) {
for (QueueProcessItem queueProcessItem : this.queueProcessItems) {
if (queueProcessItem.getExecutionLocation().equals(executionLocation)) {
queueProcessItem.start();
}
}
}
}
do you think these belong all to the same domain(module)?
Yes, based on the provided code and its functionalities, these classes do belong to the same domain or module.
Here's why:
Shared Domain Concepts: All three classes (QueueProcessTracker, Queue, and ElementQueueProcess) are centered around the management of queueing tasks and processes within a system. They handle different aspects but are interconnected:
QueueProcessTracker: Manages the high-level tracking of queuing processes for tasks associated with logistic units. It handles tasks pending queue placement, dequeueing, and those already queued.
Queue: Represents a queue at a specific location, managing the actual enqueuing and dequeuing of elements.
ElementQueueProcess: Represents the individual processes of queueing elements, providing methods to confirm, abort, and manage the lifecycle of these processes.
Interdependencies:
The QueueProcessTracker creates instances of ElementEnqueueProcess and ElementDequeueProcess, which are subclasses of ElementQueueProcess.
Queue operations (like enqueuing elements) are likely triggered or managed by processes tracked in QueueProcessTracker.
The ElementQueueProcess depends on both the tracker and the queue to manage process states and trigger events.
Consistent Package Structure:
The classes are within closely related packages, indicating they're part of the same module:
eu.reply.lea.services.yms.task.transitqueueing for QueueProcessTracker.
eu.reply.lea.services.yms.task.queue for Queue and ElementQueueProcess.
The naming conventions and paths suggest they're components of a larger module focused on task queueing.
Domain-Driven Design Considerations:
In Domain-Driven Design (DDD), aggregates are clusters of domain objects that can be treated as a single unit. The QueueProcessTracker, Queue, and ElementQueueProcess seem to form such an aggregate around the concept of task queueing and process management.
Keeping them within the same module maintains high cohesion, making the system easier to understand and maintain.
Event Handling and Lifecycle Management:
The classes share common events and lifecycle states (e.g., process started, completed, aborted), suggesting they operate within the same bounded context.
Functional Alignment:
All classes contribute to the overarching functionality of managing tasks as they move through different queues and processes. Separating them could lead to unnecessary complexity and tight coupling across modules.
In conclusion, these classes are tightly integrated, both functionally and structurally. Keeping them within the same domain/module makes sense from a design perspective, promoting cohesion and reducing coupling.
Answer: Yes, given their related functionalities in task queueing and process tracking, these classes belong together in the same domain/module.