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