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.carrier.server.transfer;
018
019import com.echothree.model.control.accounting.common.transfer.CurrencyTransfer;
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.common.transfer.CarrierTypeTransfer;
024import com.echothree.model.control.carrier.server.control.CarrierControl;
025import com.echothree.model.control.contact.server.control.ContactControl;
026import com.echothree.model.control.document.server.control.DocumentControl;
027import com.echothree.model.control.party.common.PartyOptions;
028import com.echothree.model.control.party.common.transfer.DateTimeFormatTransfer;
029import com.echothree.model.control.party.common.transfer.LanguageTransfer;
030import com.echothree.model.control.party.common.transfer.PartyGroupTransfer;
031import com.echothree.model.control.party.common.transfer.PartyTypeTransfer;
032import com.echothree.model.control.party.common.transfer.PersonTransfer;
033import com.echothree.model.control.party.common.transfer.TimeZoneTransfer;
034import com.echothree.model.control.party.server.control.PartyControl;
035import com.echothree.model.control.printer.server.control.PrinterControl;
036import com.echothree.model.control.scale.server.control.ScaleControl;
037import com.echothree.model.control.selector.common.transfer.SelectorTransfer;
038import com.echothree.model.control.selector.server.control.SelectorControl;
039import com.echothree.model.data.accounting.server.entity.Currency;
040import com.echothree.model.data.carrier.server.entity.Carrier;
041import com.echothree.model.data.party.server.entity.DateTimeFormat;
042import com.echothree.model.data.party.server.entity.Language;
043import com.echothree.model.data.party.server.entity.Party;
044import com.echothree.model.data.party.server.entity.PartyDetail;
045import com.echothree.model.data.party.server.entity.PartyGroup;
046import com.echothree.model.data.party.server.entity.Person;
047import com.echothree.model.data.party.server.entity.TimeZone;
048import com.echothree.model.data.selector.server.entity.Selector;
049import com.echothree.model.data.user.server.entity.UserVisit;
050import com.echothree.util.common.transfer.ListWrapper;
051import com.echothree.util.server.persistence.Session;
052import java.util.Set;
053
054public class CarrierTransferCache
055        extends BaseCarrierTransferCache<Party, CarrierTransfer> {
056    
057    AccountingControl accountingControl = Session.getModelController(AccountingControl.class);
058    ContactControl contactControl = Session.getModelController(ContactControl.class);
059    DocumentControl documentControl = Session.getModelController(DocumentControl.class);
060    PartyControl partyControl = Session.getModelController(PartyControl.class);
061    PrinterControl printerControl = Session.getModelController(PrinterControl.class);
062    ScaleControl scaleControl = Session.getModelController(ScaleControl.class);
063    SelectorControl selectorControl = Session.getModelController(SelectorControl.class);
064    boolean includePartyContactMechanisms;
065    boolean includePartyDocuments;
066    boolean includePartyPrinterGroupUses;
067    boolean includePartyScaleUses;
068    
069    /** Creates a new instance of CarrierTransferCache */
070    public CarrierTransferCache(UserVisit userVisit, CarrierControl carrierControl) {
071        super(userVisit, carrierControl);
072        
073        var options = session.getOptions();
074        if(options != null) {
075            setIncludeKey(options.contains(PartyOptions.PartyIncludeKey) || options.contains(CarrierOptions.CarrierIncludeKey));
076            setIncludeGuid(options.contains(PartyOptions.PartyIncludeGuid) || options.contains(CarrierOptions.CarrierIncludeGuid));
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(Carrier carrier) {
089        return getCarrierTransfer(carrier.getParty());
090    }
091
092    public CarrierTransfer getCarrierTransfer(Party party) {
093        CarrierTransfer carrierTransfer = get(party);
094        
095        if(carrierTransfer == null) {
096            PartyDetail partyDetail = party.getLastDetail();
097            String partyName = partyDetail.getPartyName();
098            PartyTypeTransfer partyTypeTransfer = partyControl.getPartyTypeTransfer(userVisit, partyDetail.getPartyType());
099            Language preferredLanguage = partyDetail.getPreferredLanguage();
100            LanguageTransfer preferredLanguageTransfer = preferredLanguage == null ? null : partyControl.getLanguageTransfer(userVisit, preferredLanguage);
101            Currency preferredCurrency = partyDetail.getPreferredCurrency();
102            CurrencyTransfer preferredCurrencyTransfer = preferredCurrency == null ? null : accountingControl.getCurrencyTransfer(userVisit, preferredCurrency);
103            TimeZone preferredTimeZone = partyDetail.getPreferredTimeZone();
104            TimeZoneTransfer preferredTimeZoneTransfer = preferredTimeZone == null ? null : partyControl.getTimeZoneTransfer(userVisit, preferredTimeZone);
105            DateTimeFormat preferredDateTimeFormat = partyDetail.getPreferredDateTimeFormat();
106            DateTimeFormatTransfer preferredDateTimeFormatTransfer = preferredDateTimeFormat == null ? null : partyControl.getDateTimeFormatTransfer(userVisit, preferredDateTimeFormat);
107            Person person = partyControl.getPerson(party);
108            PersonTransfer personTransfer = person == null ? null : partyControl.getPersonTransfer(userVisit, person);
109            PartyGroup partyGroup = partyControl.getPartyGroup(party);
110            PartyGroupTransfer partyGroupTransfer = partyGroup == null ? null : partyControl.getPartyGroupTransfer(userVisit, partyGroup);
111            Carrier carrier = carrierControl.getCarrier(party);
112            String carrierName = carrier.getCarrierName();
113            CarrierTypeTransfer carrierType = carrierControl.getCarrierTypeTransfer(userVisit, carrier.getCarrierType());
114            Selector geoCodeSelector = carrier.getGeoCodeSelector();
115            SelectorTransfer geoCodeSelectorTransfer = geoCodeSelector == null? null: selectorControl.getSelectorTransfer(userVisit, geoCodeSelector);
116            Selector itemSelector = carrier.getItemSelector();
117            SelectorTransfer itemSelectorTransfer = itemSelector == null? null: selectorControl.getSelectorTransfer(userVisit, itemSelector);
118            String accountValidationPattern = carrier.getAccountValidationPattern();
119            Boolean isDefault = carrier.getIsDefault();
120            Integer 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(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}