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