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