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