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.track.server.logic;
018
019import com.echothree.model.control.core.server.control.EntityInstanceControl;
020import com.echothree.model.control.track.common.exception.UnknownTrackNameException;
021import com.echothree.model.control.track.common.exception.UnknownTrackStatusChoiceException;
022import com.echothree.model.control.track.common.exception.UnknownTrackValueException;
023import com.echothree.model.control.track.common.workflow.TrackStatusConstants;
024import com.echothree.model.control.track.server.control.TrackControl;
025import com.echothree.model.control.workflow.server.control.WorkflowControl;
026import com.echothree.model.control.workflow.server.logic.WorkflowDestinationLogic;
027import com.echothree.model.control.workflow.server.logic.WorkflowLogic;
028import com.echothree.model.data.party.common.pk.PartyPK;
029import com.echothree.model.data.track.server.entity.Track;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.server.control.BaseLogic;
032import com.echothree.util.server.message.ExecutionErrorAccumulator;
033import com.echothree.util.server.persistence.Session;
034import javax.enterprise.context.ApplicationScoped;
035import javax.inject.Inject;
036
037@ApplicationScoped
038public class TrackLogic
039        extends BaseLogic {
040
041    @Inject
042    EntityInstanceControl entityInstanceControl;
043
044    @Inject
045    TrackControl trackControl;
046
047    @Inject
048    WorkflowControl workflowControl;
049
050    @Inject
051    WorkflowDestinationLogic workflowDestinationLogic;
052
053    @Inject
054    WorkflowLogic workflowLogic;
055
056    protected TrackLogic() {
057        super();
058    }
059    
060    public Track getTrackByName(final ExecutionErrorAccumulator eea, final String trackName) {
061        var track = trackControl.getTrackByName(trackName);
062
063        if(track == null) {
064            handleExecutionError(UnknownTrackNameException.class, eea, ExecutionErrors.UnknownTrackName.name(), trackName);
065        }
066
067        return track;
068    }
069    
070    public Track getTrackByValue(final ExecutionErrorAccumulator eea, final String trackValue) {
071        var track = trackControl.getTrackByValue(trackValue);
072
073        if(track == null) {
074            handleExecutionError(UnknownTrackValueException.class, eea, ExecutionErrors.UnknownTrackValue.name(), trackValue);
075        }
076
077        return track;
078    }
079    
080    public void setTrackStatus(final Session session, ExecutionErrorAccumulator eea, Track track, String trackStatusChoice, PartyPK modifiedBy) {
081        var workflow = workflowLogic.getWorkflowByName(eea, TrackStatusConstants.Workflow_TRACK_STATUS);
082        var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(track.getPrimaryKey());
083        var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdate(workflow, entityInstance);
084        var workflowDestination = trackStatusChoice == null ? null : workflowControl.getWorkflowDestinationByName(workflowEntityStatus.getWorkflowStep(), trackStatusChoice);
085
086        if(workflowDestination != null || trackStatusChoice == null) {
087            var currentWorkflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName();
088            var map = workflowDestinationLogic.getWorkflowDestinationsAsMap(workflowDestination);
089            Long triggerTime = null;
090
091            if(currentWorkflowStepName.equals(TrackStatusConstants.WorkflowStep_ACTIVE)) {
092                if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, TrackStatusConstants.Workflow_TRACK_STATUS, TrackStatusConstants.WorkflowStep_INACTIVE)) {
093                    // Nothing at this time.
094                }
095            } else if(currentWorkflowStepName.equals(TrackStatusConstants.WorkflowStep_INACTIVE)) {
096                if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, TrackStatusConstants.Workflow_TRACK_STATUS, TrackStatusConstants.WorkflowStep_ACTIVE)) {
097                    // Nothing at this time.
098                }
099            }
100
101            if(eea == null || !eea.hasExecutionErrors()) {
102                workflowControl.transitionEntityInWorkflow(eea, workflowEntityStatus, workflowDestination, triggerTime, modifiedBy);
103            }
104        } else {
105            handleExecutionError(UnknownTrackStatusChoiceException.class, eea, ExecutionErrors.UnknownTrackStatusChoice.name(), trackStatusChoice);
106        }
107    }
108    
109}