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.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 com.echothree.util.server.persistence.Session; 039import javax.enterprise.context.ApplicationScoped; 040import javax.enterprise.inject.spi.CDI; 041 042@ApplicationScoped 043public class PartyCancellationPolicyLogic 044 extends BaseLogic { 045 046 protected PartyCancellationPolicyLogic() { 047 super(); 048 } 049 050 public static PartyCancellationPolicyLogic getInstance() { 051 return CDI.current().select(PartyCancellationPolicyLogic.class).get(); 052 } 053 054 public WorkflowEntrance insertPartyCancellationPolicyIntoWorkflow(EntityInstance entityInstance, BasePK createdBy) { 055 var workflowControl = Session.getModelController(WorkflowControl.class); 056 057 return workflowControl.addEntityToWorkflowUsingNames(null, PartyCancellationPolicyStatusConstants.Workflow_PARTY_CANCELLATION_POLICY_STATUS, 058 PartyCancellationPolicyStatusConstants.WorkflowEntrance_NEW_PARTY_CANCELLATION_POLICY, entityInstance, null, null, createdBy); 059 } 060 061 public WorkflowEntrance insertPartyCancellationPolicyIntoWorkflow(PartyCancellationPolicy partyCancellationPolicy, BasePK createdBy) { 062 var entityInstanceControl = Session.getModelController(EntityInstanceControl.class); 063 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(partyCancellationPolicy.getPrimaryKey()); 064 065 return insertPartyCancellationPolicyIntoWorkflow(entityInstance, createdBy); 066 } 067 068 public WorkflowEntityStatus getPartyCancellationPolicyStatus(EntityInstance entityInstance, BasePK createdBy) { 069 var workflowControl = Session.getModelController(WorkflowControl.class); 070 var partyCancellationPolicyStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceUsingNames( 071 PartyCancellationPolicyStatusConstants.Workflow_PARTY_CANCELLATION_POLICY_STATUS, entityInstance); 072 073 if(partyCancellationPolicyStatus == null) { 074 var workflowEntrance = PartyCancellationPolicyLogic.getInstance().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 entityInstanceControl = Session.getModelController(EntityInstanceControl.class); 086 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(partyCancellationPolicy.getPrimaryKey()); 087 088 return getPartyCancellationPolicyStatus(entityInstance, createdBy); 089 } 090 091 public WorkflowEntityStatusTransfer getPartyCancellationPolicyStatusTransfer(UserVisit userVisit, EntityInstance entityInstance, BasePK createdBy) { 092 var workflowControl = Session.getModelController(WorkflowControl.class); 093 var partyCancellationPolicyStatus = getPartyCancellationPolicyStatus(entityInstance, createdBy); 094 095 return partyCancellationPolicyStatus == null ? null : workflowControl.getWorkflowEntityStatusTransfer(userVisit, partyCancellationPolicyStatus); 096 } 097 098 public PartyCancellationPolicy createPartyCancellationPolicy(Party party, CancellationPolicy cancellationPolicy, BasePK createdBy) { 099 var cancellationPolicyControl = Session.getModelController(CancellationPolicyControl.class); 100 var partyCancellationPolicy = cancellationPolicyControl.createPartyCancellationPolicy(party, cancellationPolicy, createdBy); 101 102 insertPartyCancellationPolicyIntoWorkflow(partyCancellationPolicy, createdBy); 103 104 return partyCancellationPolicy; 105 } 106 107 public PartyCancellationPolicyStatusChoicesBean getPartyCancellationPolicyStatusChoices(final String defaultOrderStatusChoice, final Language language, final boolean allowNullChoice, 108 final PartyCancellationPolicy partyCancellationPolicy, final PartyPK partyPK) { 109 var workflowControl = Session.getModelController(WorkflowControl.class); 110 var partyCancellationPolicyStatusChoicesBean = new PartyCancellationPolicyStatusChoicesBean(); 111 112 if(partyCancellationPolicy == null) { 113 workflowControl.getWorkflowEntranceChoices(partyCancellationPolicyStatusChoicesBean, defaultOrderStatusChoice, language, allowNullChoice, 114 workflowControl.getWorkflowByName(PartyCancellationPolicyStatusConstants.Workflow_PARTY_CANCELLATION_POLICY_STATUS), partyPK); 115 } else { 116 var entityInstanceControl = Session.getModelController(EntityInstanceControl.class); 117 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(partyCancellationPolicy.getPrimaryKey()); 118 var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceUsingNames(PartyCancellationPolicyStatusConstants.Workflow_PARTY_CANCELLATION_POLICY_STATUS, entityInstance); 119 120 workflowControl.getWorkflowDestinationChoices(partyCancellationPolicyStatusChoicesBean, defaultOrderStatusChoice, language, allowNullChoice, workflowEntityStatus.getWorkflowStep(), partyPK); 121 } 122 123 return partyCancellationPolicyStatusChoicesBean; 124 } 125 126 public void setPartyCancellationPolicyStatus(final ExecutionErrorAccumulator eea, final PartyCancellationPolicy partyCancellationPolicy, final String orderStatusChoice, final PartyPK modifiedBy) { 127 var entityInstanceControl = Session.getModelController(EntityInstanceControl.class); 128 var workflowControl = Session.getModelController(WorkflowControl.class); 129 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(partyCancellationPolicy.getPrimaryKey()); 130 var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdateUsingNames(PartyCancellationPolicyStatusConstants.Workflow_PARTY_CANCELLATION_POLICY_STATUS, 131 entityInstance); 132 var workflowDestination = orderStatusChoice == null? null: workflowControl.getWorkflowDestinationByName(workflowEntityStatus.getWorkflowStep(), orderStatusChoice); 133 134 if(workflowDestination != null || orderStatusChoice == null) { 135 workflowControl.transitionEntityInWorkflow(eea, workflowEntityStatus, workflowDestination, null, modifiedBy); 136 } else { 137 eea.addExecutionError(ExecutionErrors.UnknownPartyCancellationPolicyStatusChoice.name(), orderStatusChoice); 138 } 139 } 140 141}