001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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 com.echothree.model.control.core.server.control.EntityInstanceControl;
020import com.echothree.model.control.workeffort.server.control.WorkEffortControl;
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 com.echothree.util.server.persistence.Session;
027import javax.enterprise.context.RequestScoped;
028
029@RequestScoped
030public class WorkflowEntityStatusTransferCache
031        extends BaseWorkflowTransferCache<WorkflowEntityStatus, WorkflowEntityStatusTransfer> {
032
033    WorkEffortControl workEffortControl = Session.getModelController(WorkEffortControl.class);
034    WorkflowControl workflowControl = Session.getModelController(WorkflowControl.class);
035
036    boolean includeTriggerTime;
037
038    /** Creates a new instance of WorkflowEntityStatusTransferCache */
039    protected WorkflowEntityStatusTransferCache() {
040        super();
041
042        var options = session.getOptions();
043        if(options != null) {
044            includeTriggerTime = options.contains(WorkflowOptions.WorkflowEntityStatusIncludeTriggerTime);
045        }
046    }
047
048    public WorkflowEntityStatusTransfer getWorkflowEntityStatusTransfer(UserVisit userVisit, WorkflowEntityStatus workflowEntityStatus) {
049        var workflowEntityStatusTransfer = get(workflowEntityStatus);
050
051        if(workflowEntityStatusTransfer == null) {
052            var entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
053            var entityInstanceTransfer = entityInstanceControl.getEntityInstanceTransfer(userVisit, workflowEntityStatus.getEntityInstance(), false, false, false, false);
054            var workflowStepTransfer = workflowControl.getWorkflowStepTransfer(userVisit, workflowEntityStatus.getWorkflowStep());
055            var workEffortScope = workflowEntityStatus.getWorkEffortScope();
056            var workEffortScopeTransfer = workEffortScope == null ? null : workEffortControl.getWorkEffortScopeTransfer(userVisit, workEffortScope);
057            var unformattedFromTime = workflowEntityStatus.getFromTime();
058            var fromTime = formatTypicalDateTime(userVisit, unformattedFromTime);
059            var unformattedThruTime = workflowEntityStatus.getThruTime();
060            var thruTime = formatTypicalDateTime(userVisit, unformattedThruTime);
061            Long unformattedTriggerTime;
062            String triggerTime = null;
063
064            if(includeTriggerTime) {
065                var workflowTrigger = workflowControl.getWorkflowTrigger(workflowEntityStatus);
066
067                unformattedTriggerTime = workflowTrigger == null ? null : workflowTrigger.getTriggerTime();
068                triggerTime = unformattedTriggerTime == null ? null : formatTypicalDateTime(userVisit, unformattedTriggerTime);
069            }
070
071            workflowEntityStatusTransfer = new WorkflowEntityStatusTransfer(entityInstanceTransfer, workflowStepTransfer, workEffortScopeTransfer,
072                    unformattedFromTime, fromTime, unformattedThruTime, thruTime, unformattedThruTime, triggerTime);
073            put(userVisit, workflowEntityStatus, workflowEntityStatusTransfer);
074        }
075
076        return workflowEntityStatusTransfer;
077    }
078}