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.shipping.server.logic;
018
019import com.echothree.model.control.selector.common.SelectorKinds;
020import com.echothree.model.control.selector.common.SelectorTypes;
021import com.echothree.model.control.selector.server.evaluator.CachedSelector;
022import com.echothree.model.control.selector.server.evaluator.SelectorCache;
023import com.echothree.model.control.selector.server.evaluator.SelectorCacheFactory;
024import com.echothree.model.control.selector.server.evaluator.ShippingMethodItemSelectorEvaluator;
025import com.echothree.model.control.shipping.common.exception.ItemNotAcceptibleForShippingMethodException;
026import com.echothree.model.control.shipping.common.exception.UnknownShippingMethodNameException;
027import com.echothree.model.control.shipping.server.control.ShippingControl;
028import com.echothree.model.data.item.server.entity.Item;
029import com.echothree.model.data.selector.server.entity.Selector;
030import com.echothree.model.data.shipping.server.entity.ShippingMethod;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.persistence.BasePK;
033import com.echothree.util.server.control.BaseLogic;
034import com.echothree.util.server.message.ExecutionErrorAccumulator;
035import com.echothree.util.server.persistence.EntityPermission;
036import com.echothree.util.server.persistence.Session;
037import java.util.Set;
038
039public class ShippingMethodLogic
040    extends BaseLogic {
041
042    private ShippingMethodLogic() {
043        super();
044    }
045
046    private static class ShippingMethodLogicHolder {
047        static ShippingMethodLogic instance = new ShippingMethodLogic();
048    }
049
050    public static ShippingMethodLogic getInstance() {
051        return ShippingMethodLogicHolder.instance;
052    }
053
054    private ShippingMethod getShippingMethodByName(final ExecutionErrorAccumulator eea, final String shippingMethodName, final EntityPermission entityPermission) {
055        var shippingControl = Session.getModelController(ShippingControl.class);
056        ShippingMethod shippingMethod = null;
057
058        shippingMethod = shippingControl.getShippingMethodByName(shippingMethodName, entityPermission);
059
060        if(shippingMethod == null) {
061            handleExecutionError(UnknownShippingMethodNameException.class, eea, ExecutionErrors.UnknownShippingMethodName.name(), shippingMethodName);
062        }
063
064        return shippingMethod;
065    }
066
067    public ShippingMethod getShippingMethodByName(final ExecutionErrorAccumulator eea, final String shippingMethodName) {
068        return getShippingMethodByName(eea, shippingMethodName, EntityPermission.READ_ONLY);
069    }
070
071    public ShippingMethod getShippingMethodByNameForUpdate(final ExecutionErrorAccumulator eea, final String shippingMethodName) {
072        return getShippingMethodByName(eea, shippingMethodName, EntityPermission.READ_WRITE);
073    }
074    
075    public void checkAcceptanceOfItem(final Session session, final ExecutionErrorAccumulator eea, final SelectorCache selectorCache,
076            final ShippingMethod shippingMethod, final Item item, final BasePK evaluatedBy) {
077        Selector selector = shippingMethod.getLastDetail().getItemSelector();
078        
079        if(selector != null) {
080            CachedSelector cachedSelector = selectorCache.getSelector(selector);
081            
082            if(!new ShippingMethodItemSelectorEvaluator(session, evaluatedBy).evaluate(cachedSelector, item)) {
083                handleExecutionError(ItemNotAcceptibleForShippingMethodException.class, eea, ExecutionErrors.ItemNotAcceptibleForShippingMethod.name(),
084                        shippingMethod.getLastDetail().getShippingMethodName(), item.getLastDetail().getItemName());
085            }
086        }
087    }
088    
089    public void checkAcceptanceOfItem(final Session session, final ExecutionErrorAccumulator eea, final ShippingMethod shippingMethod, final Item item,
090            final BasePK evaluatedBy) {
091        Selector selector = shippingMethod.getLastDetail().getItemSelector();
092        
093        if(selector != null) {
094            SelectorCache selectorCache = SelectorCacheFactory.getInstance().getSelectorCache(session, SelectorKinds.ITEM.name(),
095                    SelectorTypes.SHIPPING_METHOD.name());
096            
097            checkAcceptanceOfItem(session, eea, selectorCache, shippingMethod, item, evaluatedBy);
098        }
099    }
100    
101    public void checkAcceptanceOfItems(final Session session, final ExecutionErrorAccumulator eea, final ShippingMethod shippingMethod, final Set<Item> items,
102            final BasePK evaluatedBy) {
103        Selector selector = shippingMethod.getLastDetail().getItemSelector();
104        
105        if(selector != null) {
106            SelectorCache selectorCache = SelectorCacheFactory.getInstance().getSelectorCache(session, SelectorKinds.ITEM.name(),
107                    SelectorTypes.SHIPPING_METHOD.name());
108            
109            items.forEach((item) -> {
110                checkAcceptanceOfItem(session, eea, selectorCache, shippingMethod, item, evaluatedBy);
111            });
112        }
113    }
114    
115}