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