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