001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.model.control.workflow.server.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.core.server.control.EntityInstanceControl;
021import com.echothree.model.control.workflow.common.WorkflowOptions;
022import com.echothree.model.control.workflow.common.transfer.WorkflowEntityStatusTransfer;
023import com.echothree.model.control.workflow.server.control.WorkflowControl;
024import com.echothree.model.data.user.server.entity.UserVisit;
025import com.echothree.model.data.workflow.server.entity.WorkflowEntityStatus;
026import javax.enterprise.context.RequestScoped;
027
028@RequestScoped
029public class WorkflowEntityStatusTransferCache
030        extends BaseWorkflowTransferCache<WorkflowEntityStatus, WorkflowEntityStatusTransfer> {
031
032    @Inject
033    WorkflowControl workflowControl;
034
035    boolean includeTriggerTime;
036
037    /** Creates a new instance of WorkflowEntityStatusTransferCache */
038    protected WorkflowEntityStatusTransferCache() {
039        super();
040
041        var options = session.getOptions();
042        if(options != null) {
043            includeTriggerTime = options.contains(WorkflowOptions.WorkflowEntityStatusIncludeTriggerTime);
044        }
045    }
046
047    public WorkflowEntityStatusTransfer getWorkflowEntityStatusTransfer(UserVisit userVisit, WorkflowEntityStatus workflowEntityStatus) {
048        var workflowEntityStatusTransfer = get(workflowEntityStatus);
049
050        if(workflowEntityStatusTransfer == null) {
051
052            var entityInstanceTransfer = entityInstanceControl.getEntityInstanceTransfer(userVisit, workflowEntityStatus.getEntityInstance(), false, false, false, false);
053            var workflowStepTransfer = workflowControl.getWorkflowStepTransfer(userVisit, workflowEntityStatus.getWorkflowStep());
054            var workEffortScope = workflowEntityStatus.getWorkEffortScope();
055            var workEffortScopeTransfer = workEffortScope == null ? null : workEffortControl.getWorkEffortScopeTransfer(userVisit, workEffortScope);
056            var unformattedFromTime = workflowEntityStatus.getFromTime();
057            var fromTime = formatTypicalDateTime(userVisit, unformattedFromTime);
058            var unformattedThruTime = workflowEntityStatus.getThruTime();
059            var thruTime = formatTypicalDateTime(userVisit, unformattedThruTime);
060            Long unformattedTriggerTime;
061            String triggerTime = null;
062
063            if(includeTriggerTime) {
064                var workflowTrigger = workflowControl.getWorkflowTrigger(workflowEntityStatus);
065
066                unformattedTriggerTime = workflowTrigger == null ? null : workflowTrigger.getTriggerTime();
067                triggerTime = unformattedTriggerTime == null ? null : formatTypicalDateTime(userVisit, unformattedTriggerTime);
068            }
069
070            workflowEntityStatusTransfer = new WorkflowEntityStatusTransfer(entityInstanceTransfer, workflowStepTransfer, workEffortScopeTransfer,
071                    unformattedFromTime, fromTime, unformattedThruTime, thruTime, unformattedThruTime, triggerTime);
072            put(userVisit, workflowEntityStatus, workflowEntityStatusTransfer);
073        }
074
075        return workflowEntityStatusTransfer;
076    }
077}