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.item.server.transfer;
018
019import com.echothree.model.control.customer.common.transfer.CustomerTypeTransfer;
020import com.echothree.model.control.customer.server.control.CustomerControl;
021import com.echothree.model.control.item.common.ItemProperties;
022import com.echothree.model.control.item.common.transfer.ItemShippingTimeTransfer;
023import com.echothree.model.control.item.common.transfer.ItemTransfer;
024import com.echothree.model.control.item.server.control.ItemControl;
025import com.echothree.model.data.item.server.entity.ItemShippingTime;
026import com.echothree.model.data.user.server.entity.UserVisit;
027import com.echothree.util.common.form.TransferProperties;
028import com.echothree.util.server.persistence.Session;
029import java.util.Set;
030
031public class ItemShippingTimeTransferCache
032        extends BaseItemTransferCache<ItemShippingTime, ItemShippingTimeTransfer> {
033    
034    CustomerControl customerControl = Session.getModelController(CustomerControl.class);
035    
036    TransferProperties transferProperties;
037    boolean filterItem;
038    boolean filterCustomerType;
039    boolean filterUnformattedShippingStartTime;
040    boolean filterShippingStartTime;
041    boolean filterUnformattedShippingEndTime;
042    boolean filterShippingEndTime;
043
044    /** Creates a new instance of ItemShippingTimeTransferCache */
045    public ItemShippingTimeTransferCache(UserVisit userVisit, ItemControl itemControl) {
046        super(userVisit, itemControl);
047
048        transferProperties = session.getTransferProperties();
049        if(transferProperties != null) {
050            var properties = transferProperties.getProperties(ItemShippingTimeTransfer.class);
051            
052            if(properties != null) {
053                filterItem = !properties.contains(ItemProperties.ITEM);
054                filterCustomerType = !properties.contains(ItemProperties.CUSTOMER_TYPE);
055                filterUnformattedShippingStartTime = !properties.contains(ItemProperties.UNFORMATTED_SHIPPING_START_TIME);
056                filterShippingStartTime = !properties.contains(ItemProperties.SHIPPING_START_TIME);
057                filterUnformattedShippingEndTime = !properties.contains(ItemProperties.UNFORMATTED_SHIPPING_END_TIME);
058                filterShippingEndTime = !properties.contains(ItemProperties.SHIPPING_END_TIME);
059            }
060        }
061    }
062    
063    @Override
064    public ItemShippingTimeTransfer getTransfer(ItemShippingTime itemShippingTime) {
065        ItemShippingTimeTransfer itemShippingTimeTransfer = get(itemShippingTime);
066        
067        if(itemShippingTimeTransfer == null) {
068            ItemTransfer itemTransfer = filterItem ? null : itemControl.getItemTransfer(userVisit, itemShippingTime.getItem());
069            CustomerTypeTransfer customerTypeTransfer = filterCustomerType ? null : customerControl.getCustomerTypeTransfer(userVisit, itemShippingTime.getCustomerType());
070            Long unformattedShippingStartTime = itemShippingTime.getShippingStartTime();
071            String shippingStartTime = filterShippingStartTime ? null : formatTypicalDateTime(unformattedShippingStartTime);
072            Long unformattedShippingEndTime = itemShippingTime.getShippingEndTime();
073            String shippingEndTime = filterShippingEndTime ? null : formatTypicalDateTime(unformattedShippingEndTime);
074            
075            itemShippingTimeTransfer = new ItemShippingTimeTransfer(itemTransfer, customerTypeTransfer,
076                    filterUnformattedShippingStartTime ? null : unformattedShippingStartTime, shippingStartTime,
077                    filterUnformattedShippingEndTime ? null : unformattedShippingStartTime, shippingEndTime);
078            put(itemShippingTime, itemShippingTimeTransfer);
079        }
080        
081        return itemShippingTimeTransfer;
082    }
083    
084}