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