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.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.enterprise.inject.spi.CDI;
036
037@ApplicationScoped
038public class TrackLogic
039        extends BaseLogic {
040
041    protected TrackLogic() {
042        super();
043    }
044
045    public static TrackLogic getInstance() {
046        return CDI.current().select(TrackLogic.class).get();
047    }
048    
049    public Track getTrackByName(final ExecutionErrorAccumulator eea, final String trackName) {
050        var trackControl = Session.getModelController(TrackControl.class);
051        var track = trackControl.getTrackByName(trackName);
052
053        if(track == null) {
054            handleExecutionError(UnknownTrackNameException.class, eea, ExecutionErrors.UnknownTrackName.name(), trackName);
055        }
056
057        return track;
058    }
059    
060    public Track getTrackByValue(final ExecutionErrorAccumulator eea, final String trackValue) {
061        var trackControl = Session.getModelController(TrackControl.class);
062        var track = trackControl.getTrackByValue(trackValue);
063
064        if(track == null) {
065            handleExecutionError(UnknownTrackValueException.class, eea, ExecutionErrors.UnknownTrackValue.name(), trackValue);
066        }
067
068        return track;
069    }
070    
071    public void setTrackStatus(final Session session, ExecutionErrorAccumulator eea, Track track, String trackStatusChoice, PartyPK modifiedBy) {
072        var entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
073        var workflowControl = Session.getModelController(WorkflowControl.class);
074        var workflow = WorkflowLogic.getInstance().getWorkflowByName(eea, TrackStatusConstants.Workflow_TRACK_STATUS);
075        var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(track.getPrimaryKey());
076        var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdate(workflow, entityInstance);
077        var workflowDestination = trackStatusChoice == null ? null : workflowControl.getWorkflowDestinationByName(workflowEntityStatus.getWorkflowStep(), trackStatusChoice);
078
079        if(workflowDestination != null || trackStatusChoice == null) {
080            var workflowDestinationLogic = WorkflowDestinationLogic.getInstance();
081            var currentWorkflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName();
082            var map = workflowDestinationLogic.getWorkflowDestinationsAsMap(workflowDestination);
083            Long triggerTime = null;
084
085            if(currentWorkflowStepName.equals(TrackStatusConstants.WorkflowStep_ACTIVE)) {
086                if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, TrackStatusConstants.Workflow_TRACK_STATUS, TrackStatusConstants.WorkflowStep_INACTIVE)) {
087                    // Nothing at this time.
088                }
089            } else if(currentWorkflowStepName.equals(TrackStatusConstants.WorkflowStep_INACTIVE)) {
090                if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, TrackStatusConstants.Workflow_TRACK_STATUS, TrackStatusConstants.WorkflowStep_ACTIVE)) {
091                    // Nothing at this time.
092                }
093            }
094
095            if(eea == null || !eea.hasExecutionErrors()) {
096                workflowControl.transitionEntityInWorkflow(eea, workflowEntityStatus, workflowDestination, triggerTime, modifiedBy);
097            }
098        } else {
099            handleExecutionError(UnknownTrackStatusChoiceException.class, eea, ExecutionErrors.UnknownTrackStatusChoice.name(), trackStatusChoice);
100        }
101    }
102    
103}