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.cancellationpolicy.server.logic;
018
019import com.echothree.model.control.cancellationpolicy.common.choice.PartyCancellationPolicyStatusChoicesBean;
020import com.echothree.model.control.cancellationpolicy.common.workflow.PartyCancellationPolicyStatusConstants;
021import com.echothree.model.control.cancellationpolicy.server.control.CancellationPolicyControl;
022import com.echothree.model.control.core.server.control.EntityInstanceControl;
023import com.echothree.model.control.workflow.common.transfer.WorkflowEntityStatusTransfer;
024import com.echothree.model.control.workflow.server.control.WorkflowControl;
025import com.echothree.model.data.cancellationpolicy.server.entity.CancellationPolicy;
026import com.echothree.model.data.cancellationpolicy.server.entity.PartyCancellationPolicy;
027import com.echothree.model.data.core.server.entity.EntityInstance;
028import com.echothree.model.data.party.common.pk.PartyPK;
029import com.echothree.model.data.party.server.entity.Language;
030import com.echothree.model.data.party.server.entity.Party;
031import com.echothree.model.data.user.server.entity.UserVisit;
032import com.echothree.model.data.workflow.server.entity.WorkflowEntityStatus;
033import com.echothree.model.data.workflow.server.entity.WorkflowEntrance;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.persistence.BasePK;
036import com.echothree.util.server.control.BaseLogic;
037import com.echothree.util.server.message.ExecutionErrorAccumulator;
038import javax.enterprise.context.ApplicationScoped;
039import javax.inject.Inject;
040
041@ApplicationScoped
042public class PartyCancellationPolicyLogic
043        extends BaseLogic {
044
045    @Inject
046    CancellationPolicyControl cancellationPolicyControl;
047
048    @Inject
049    EntityInstanceControl entityInstanceControl;
050
051    @Inject
052    WorkflowControl workflowControl;
053
054    protected PartyCancellationPolicyLogic() {
055        super();
056    }
057
058    public WorkflowEntrance insertPartyCancellationPolicyIntoWorkflow(EntityInstance entityInstance, BasePK createdBy) {
059        return workflowControl.addEntityToWorkflowUsingNames(null, PartyCancellationPolicyStatusConstants.Workflow_PARTY_CANCELLATION_POLICY_STATUS,
060                PartyCancellationPolicyStatusConstants.WorkflowEntrance_NEW_PARTY_CANCELLATION_POLICY, entityInstance, null, null, createdBy);
061    }
062
063    public WorkflowEntrance insertPartyCancellationPolicyIntoWorkflow(PartyCancellationPolicy partyCancellationPolicy, BasePK createdBy) {
064        var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(partyCancellationPolicy.getPrimaryKey());
065
066        return insertPartyCancellationPolicyIntoWorkflow(entityInstance, createdBy);
067    }
068
069    public WorkflowEntityStatus getPartyCancellationPolicyStatus(EntityInstance entityInstance, BasePK createdBy) {
070        var partyCancellationPolicyStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceUsingNames(
071                PartyCancellationPolicyStatusConstants.Workflow_PARTY_CANCELLATION_POLICY_STATUS, entityInstance);
072
073        if(partyCancellationPolicyStatus == null) {
074            var workflowEntrance = this.insertPartyCancellationPolicyIntoWorkflow(entityInstance, createdBy);
075
076            if(workflowEntrance != null) {
077                partyCancellationPolicyStatus = getPartyCancellationPolicyStatus(entityInstance, null);
078            }
079        }
080
081        return partyCancellationPolicyStatus;
082    }
083
084    public WorkflowEntityStatus getPartyCancellationPolicyStatus(PartyCancellationPolicy partyCancellationPolicy, BasePK createdBy) {
085        var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(partyCancellationPolicy.getPrimaryKey());
086
087        return getPartyCancellationPolicyStatus(entityInstance, createdBy);
088    }
089
090    public WorkflowEntityStatusTransfer getPartyCancellationPolicyStatusTransfer(UserVisit userVisit, EntityInstance entityInstance, BasePK createdBy) {
091        var partyCancellationPolicyStatus = getPartyCancellationPolicyStatus(entityInstance, createdBy);
092
093        return partyCancellationPolicyStatus == null ? null : workflowControl.getWorkflowEntityStatusTransfer(userVisit, partyCancellationPolicyStatus);
094    }
095
096    public PartyCancellationPolicy createPartyCancellationPolicy(Party party, CancellationPolicy cancellationPolicy, BasePK createdBy) {
097        var partyCancellationPolicy = cancellationPolicyControl.createPartyCancellationPolicy(party, cancellationPolicy, createdBy);
098
099        insertPartyCancellationPolicyIntoWorkflow(partyCancellationPolicy, createdBy);
100
101        return partyCancellationPolicy;
102    }
103
104    public PartyCancellationPolicyStatusChoicesBean getPartyCancellationPolicyStatusChoices(final String defaultOrderStatusChoice, final Language language, final boolean allowNullChoice,
105            final PartyCancellationPolicy partyCancellationPolicy, final PartyPK partyPK) {
106        var partyCancellationPolicyStatusChoicesBean = new PartyCancellationPolicyStatusChoicesBean();
107
108        if(partyCancellationPolicy == null) {
109            workflowControl.getWorkflowEntranceChoices(partyCancellationPolicyStatusChoicesBean, defaultOrderStatusChoice, language, allowNullChoice,
110                    workflowControl.getWorkflowByName(PartyCancellationPolicyStatusConstants.Workflow_PARTY_CANCELLATION_POLICY_STATUS), partyPK);
111        } else {
112            var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(partyCancellationPolicy.getPrimaryKey());
113            var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceUsingNames(PartyCancellationPolicyStatusConstants.Workflow_PARTY_CANCELLATION_POLICY_STATUS, entityInstance);
114
115            workflowControl.getWorkflowDestinationChoices(partyCancellationPolicyStatusChoicesBean, defaultOrderStatusChoice, language, allowNullChoice, workflowEntityStatus.getWorkflowStep(), partyPK);
116        }
117
118        return partyCancellationPolicyStatusChoicesBean;
119    }
120
121    public void setPartyCancellationPolicyStatus(final ExecutionErrorAccumulator eea, final PartyCancellationPolicy partyCancellationPolicy, final String orderStatusChoice, final PartyPK modifiedBy) {
122        var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(partyCancellationPolicy.getPrimaryKey());
123        var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdateUsingNames(PartyCancellationPolicyStatusConstants.Workflow_PARTY_CANCELLATION_POLICY_STATUS,
124                entityInstance);
125        var workflowDestination = orderStatusChoice == null? null: workflowControl.getWorkflowDestinationByName(workflowEntityStatus.getWorkflowStep(), orderStatusChoice);
126
127        if(workflowDestination != null || orderStatusChoice == null) {
128            workflowControl.transitionEntityInWorkflow(eea, workflowEntityStatus, workflowDestination, null, modifiedBy);
129        } else {
130            eea.addExecutionError(ExecutionErrors.UnknownPartyCancellationPolicyStatusChoice.name(), orderStatusChoice);
131        }
132    }
133
134}