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.shipping.server.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.comment.common.CommentConstants;
021import com.echothree.model.control.selector.server.control.SelectorControl;
022import com.echothree.model.control.shipping.common.ShippingOptions;
023import com.echothree.model.control.shipping.common.transfer.ShippingMethodTransfer;
024import com.echothree.model.control.shipping.server.control.ShippingControl;
025import com.echothree.model.data.shipping.server.entity.ShippingMethod;
026import com.echothree.model.data.user.server.entity.UserVisit;
027import javax.enterprise.context.RequestScoped;
028
029@RequestScoped
030public class ShippingMethodTransferCache
031        extends BaseShippingTransferCache<ShippingMethod, ShippingMethodTransfer> {
032
033    @Inject
034    SelectorControl selectorControl;
035
036    @Inject
037    ShippingControl shippingControl;
038
039    boolean includeComments;
040    
041    /** Creates a new instance of ShippingMethodTransferCache */
042    protected ShippingMethodTransferCache() {
043        super();
044        
045        var options = session.getOptions();
046        if(options != null) {
047            setIncludeUuid(options.contains(ShippingOptions.ShippingMethodIncludeUuid));
048            includeComments = options.contains(ShippingOptions.ShippingMethodIncludeComments);
049            setIncludeEntityAttributeGroups(options.contains(ShippingOptions.ShippingMethodIncludeEntityAttributeGroups));
050        }
051        
052        setIncludeEntityInstance(true);
053    }
054    
055    public ShippingMethodTransfer getShippingMethodTransfer(UserVisit userVisit, ShippingMethod shippingMethod) {
056        var shippingMethodTransfer = get(shippingMethod);
057
058        if(shippingMethodTransfer == null) {
059            var shippingMethodDetail = shippingMethod.getLastDetail();
060            var shippingMethodName = shippingMethodDetail.getShippingMethodName();
061            var geoCodeSelector = shippingMethodDetail.getGeoCodeSelector();
062            var geoCodeSelectorTransfer = geoCodeSelector == null ? null : selectorControl.getSelectorTransfer(userVisit, geoCodeSelector);
063            var itemSelector = shippingMethodDetail.getItemSelector();
064            var itemSelectorTransfer = itemSelector == null ? null : selectorControl.getSelectorTransfer(userVisit, itemSelector);
065            var sortOrder = shippingMethodDetail.getSortOrder();
066            var description = shippingControl.getBestShippingMethodDescription(shippingMethod, getLanguage(userVisit));
067
068            shippingMethodTransfer = new ShippingMethodTransfer(shippingMethodName, geoCodeSelectorTransfer, itemSelectorTransfer, sortOrder, description);
069            put(userVisit, shippingMethod, shippingMethodTransfer);
070
071            if(includeComments) {
072                setupComments(userVisit, shippingMethod, null, shippingMethodTransfer, CommentConstants.CommentType_SHIPPING_METHOD);
073            }
074        }
075
076        return shippingMethodTransfer;
077    }
078    
079}