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.order.server.trigger;
018
019import com.echothree.model.control.order.common.OrderTypes;
020import com.echothree.model.control.order.server.control.OrderControl;
021import com.echothree.model.control.sales.server.trigger.SalesOrderTrigger;
022import com.echothree.model.control.workflow.server.trigger.BaseTrigger;
023import com.echothree.model.control.workflow.server.trigger.EntityTypeTrigger;
024import com.echothree.model.data.order.server.entity.Order;
025import com.echothree.model.data.party.common.pk.PartyPK;
026import com.echothree.model.data.workflow.server.entity.WorkflowEntityStatus;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.server.message.ExecutionErrorAccumulator;
029import com.echothree.util.server.persistence.Session;
030
031public class OrderTrigger
032        extends BaseTrigger
033        implements EntityTypeTrigger {
034
035    // TODO: configure using a property file.
036    private OrderTypeTrigger locateTrigger(final ExecutionErrorAccumulator eea, final Order order) {
037        OrderTypeTrigger result = null;
038        String orderTypeName = order.getLastDetail().getOrderType().getLastDetail().getOrderTypeName();
039        
040        if(orderTypeName.equals(OrderTypes.SALES_ORDER.name())) {
041            result = new SalesOrderTrigger();
042        }
043        
044        if(result == null) {
045            eea.addExecutionError(ExecutionErrors.UnknownOrderTypeTrigger.name(), orderTypeName);
046        }
047        
048        return result;
049    }
050    
051    @Override
052    public void handleTrigger(final Session session, final ExecutionErrorAccumulator eea, final WorkflowEntityStatus workflowEntityStatus, final PartyPK triggeredBy) {
053        var orderControl = Session.getModelController(OrderControl.class);
054        Order order = orderControl.convertEntityInstanceToOrderForUpdate(getEntityInstance(workflowEntityStatus));
055        OrderTypeTrigger orderTypeTrigger = locateTrigger(eea, order);
056        
057        if(!eea.hasExecutionErrors()) {
058            orderTypeTrigger.handleTrigger(session, eea, workflowEntityStatus, order, triggeredBy);
059        }
060    }
061
062}