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.carrier.server.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.accounting.server.control.AccountingControl;
021import com.echothree.model.control.carrier.common.CarrierOptions;
022import com.echothree.model.control.carrier.common.transfer.CarrierTransfer;
023import com.echothree.model.control.carrier.server.control.CarrierControl;
024import com.echothree.model.control.contact.server.control.ContactControl;
025import com.echothree.model.control.document.server.control.DocumentControl;
026import com.echothree.model.control.party.common.PartyOptions;
027import com.echothree.model.control.party.server.control.PartyControl;
028import com.echothree.model.control.printer.server.control.PrinterControl;
029import com.echothree.model.control.scale.server.control.ScaleControl;
030import com.echothree.model.control.selector.server.control.SelectorControl;
031import com.echothree.model.data.carrier.server.entity.Carrier;
032import com.echothree.model.data.party.server.entity.Party;
033import com.echothree.model.data.user.server.entity.UserVisit;
034import com.echothree.util.common.transfer.ListWrapper;
035import javax.enterprise.context.RequestScoped;
036
037@RequestScoped
038public class CarrierTransferCache
039        extends BaseCarrierTransferCache<Party, CarrierTransfer> {
040
041    @Inject
042    AccountingControl accountingControl;
043
044    @Inject
045    CarrierControl carrierControl;
046
047    @Inject
048    ContactControl contactControl;
049
050    @Inject
051    DocumentControl documentControl;
052
053    @Inject
054    PartyControl partyControl;
055
056    @Inject
057    PrinterControl printerControl;
058
059    @Inject
060    ScaleControl scaleControl;
061
062    @Inject
063    SelectorControl selectorControl;
064
065    boolean includePartyContactMechanisms;
066    boolean includePartyDocuments;
067    boolean includePartyPrinterGroupUses;
068    boolean includePartyScaleUses;
069    
070    /** Creates a new instance of CarrierTransferCache */
071    protected CarrierTransferCache() {
072        super();
073        
074        var options = session.getOptions();
075        if(options != null) {
076            setIncludeUuid(options.contains(PartyOptions.PartyIncludeUuid) || options.contains(CarrierOptions.CarrierIncludeUuid));
077            includePartyContactMechanisms = options.contains(PartyOptions.PartyIncludePartyContactMechanisms);
078            includePartyDocuments = options.contains(PartyOptions.PartyIncludePartyDocuments);
079            includePartyPrinterGroupUses = options.contains(PartyOptions.PartyIncludePartyPrinterGroupUses);
080            includePartyScaleUses = options.contains(PartyOptions.PartyIncludePartyScaleUses);
081            setIncludeEntityAttributeGroups(options.contains(CarrierOptions.CarrierIncludeEntityAttributeGroups));
082            setIncludeTagScopes(options.contains(CarrierOptions.CarrierIncludeTagScopes));
083        }
084        
085        setIncludeEntityInstance(true);
086    }
087    
088    public CarrierTransfer getCarrierTransfer(UserVisit userVisit, Carrier carrier) {
089        return getCarrierTransfer(userVisit, carrier.getParty());
090    }
091
092    public CarrierTransfer getCarrierTransfer(UserVisit userVisit, Party party) {
093        var carrierTransfer = get(party);
094        
095        if(carrierTransfer == null) {
096            var partyDetail = party.getLastDetail();
097            var partyName = partyDetail.getPartyName();
098            var partyTypeTransfer = partyControl.getPartyTypeTransfer(userVisit, partyDetail.getPartyType());
099            var preferredLanguage = partyDetail.getPreferredLanguage();
100            var preferredLanguageTransfer = preferredLanguage == null ? null : partyControl.getLanguageTransfer(userVisit, preferredLanguage);
101            var preferredCurrency = partyDetail.getPreferredCurrency();
102            var preferredCurrencyTransfer = preferredCurrency == null ? null : accountingControl.getCurrencyTransfer(userVisit, preferredCurrency);
103            var preferredTimeZone = partyDetail.getPreferredTimeZone();
104            var preferredTimeZoneTransfer = preferredTimeZone == null ? null : partyControl.getTimeZoneTransfer(userVisit, preferredTimeZone);
105            var preferredDateTimeFormat = partyDetail.getPreferredDateTimeFormat();
106            var preferredDateTimeFormatTransfer = preferredDateTimeFormat == null ? null : partyControl.getDateTimeFormatTransfer(userVisit, preferredDateTimeFormat);
107            var person = partyControl.getPerson(party);
108            var personTransfer = person == null ? null : partyControl.getPersonTransfer(userVisit, person);
109            var partyGroup = partyControl.getPartyGroup(party);
110            var partyGroupTransfer = partyGroup == null ? null : partyControl.getPartyGroupTransfer(userVisit, partyGroup);
111            var carrier = carrierControl.getCarrier(party);
112            var carrierName = carrier.getCarrierName();
113            var carrierType = carrierControl.getCarrierTypeTransfer(userVisit, carrier.getCarrierType());
114            var geoCodeSelector = carrier.getGeoCodeSelector();
115            var geoCodeSelectorTransfer = geoCodeSelector == null? null: selectorControl.getSelectorTransfer(userVisit, geoCodeSelector);
116            var itemSelector = carrier.getItemSelector();
117            var itemSelectorTransfer = itemSelector == null? null: selectorControl.getSelectorTransfer(userVisit, itemSelector);
118            var accountValidationPattern = carrier.getAccountValidationPattern();
119            var isDefault = carrier.getIsDefault();
120            var sortOrder = carrier.getSortOrder();
121            
122            carrierTransfer = new CarrierTransfer(partyName, partyTypeTransfer, preferredLanguageTransfer, preferredCurrencyTransfer, preferredTimeZoneTransfer,
123                    preferredDateTimeFormatTransfer, personTransfer, partyGroupTransfer, carrierName, carrierType, geoCodeSelectorTransfer, itemSelectorTransfer,
124                    accountValidationPattern, isDefault, sortOrder);
125            put(userVisit, party, carrierTransfer);
126            
127            if(includePartyContactMechanisms) {
128                carrierTransfer.setPartyContactMechanisms(new ListWrapper<>(contactControl.getPartyContactMechanismTransfersByParty(userVisit, party)));
129            }
130
131            if(includePartyDocuments) {
132                carrierTransfer.setPartyDocuments(new ListWrapper<>(documentControl.getPartyDocumentTransfersByParty(userVisit, party)));
133            }
134
135            if(includePartyPrinterGroupUses) {
136                carrierTransfer.setPartyPrinterGroupUses(new ListWrapper<>(printerControl.getPartyPrinterGroupUseTransfersByParty(userVisit, party)));
137            }
138
139            if(includePartyScaleUses) {
140                carrierTransfer.setPartyScaleUses(new ListWrapper<>(scaleControl.getPartyScaleUseTransfersByParty(userVisit, party)));
141            }
142        }
143        
144        return carrierTransfer;
145    }
146    
147}