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.party.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.server.control.CarrierControl;
022import com.echothree.model.control.contact.server.control.ContactControl;
023import com.echothree.model.control.core.server.control.CoreControl;
024import com.echothree.model.control.document.server.control.DocumentControl;
025import com.echothree.model.control.invoice.server.control.InvoiceControl;
026import com.echothree.model.control.party.common.PartyOptions;
027import com.echothree.model.control.party.common.transfer.CompanyTransfer;
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.data.accounting.server.entity.Currency;
038import com.echothree.model.data.invoice.server.factory.InvoiceFactory;
039import com.echothree.model.data.party.server.entity.DateTimeFormat;
040import com.echothree.model.data.party.server.entity.Language;
041import com.echothree.model.data.party.server.entity.Party;
042import com.echothree.model.data.party.server.entity.PartyCompany;
043import com.echothree.model.data.party.server.entity.PartyDetail;
044import com.echothree.model.data.party.server.entity.PartyGroup;
045import com.echothree.model.data.party.server.entity.Person;
046import com.echothree.model.data.party.server.entity.TimeZone;
047import com.echothree.model.data.user.server.entity.UserVisit;
048import com.echothree.util.common.transfer.ListWrapper;
049import com.echothree.util.server.persistence.Session;
050import java.util.Set;
051
052public class CompanyTransferCache
053        extends BasePartyTransferCache<Party, CompanyTransfer> {
054    
055    AccountingControl accountingControl = Session.getModelController(AccountingControl.class);
056    CarrierControl carrierControl = Session.getModelController(CarrierControl.class);
057    ContactControl contactControl = Session.getModelController(ContactControl.class);
058    CoreControl coreControl = Session.getModelController(CoreControl.class);
059    DocumentControl documentControl = Session.getModelController(DocumentControl.class);
060    InvoiceControl invoiceControl = Session.getModelController(InvoiceControl.class);
061    PrinterControl printerControl = Session.getModelController(PrinterControl.class);
062    ScaleControl scaleControl = Session.getModelController(ScaleControl.class);
063    boolean includePartyContactMechanisms;
064    boolean includePartyDocuments;
065    boolean includePartyPrinterGroupUses;
066    boolean includePartyScaleUses;
067    boolean includePartyCarriers;
068    boolean includePartyCarrierAccounts;
069    boolean includeBillingAccounts;
070    boolean includeInvoicesFrom;
071    boolean includeInvoicesTo;
072    boolean hasInvoiceLimits;
073    
074    /** Creates a new instance of CompanyTransferCache */
075    public CompanyTransferCache(UserVisit userVisit, PartyControl partyControl) {
076        super(userVisit, partyControl);
077        
078        var options = session.getOptions();
079        if(options != null) {
080            setIncludeKey(options.contains(PartyOptions.PartyIncludeKey) || options.contains(PartyOptions.CompanyIncludeKey));
081            setIncludeGuid(options.contains(PartyOptions.PartyIncludeGuid) || options.contains(PartyOptions.CompanyIncludeGuid));
082            includePartyContactMechanisms = options.contains(PartyOptions.PartyIncludePartyContactMechanisms);
083            includePartyDocuments = options.contains(PartyOptions.PartyIncludePartyDocuments);
084            includePartyPrinterGroupUses = options.contains(PartyOptions.PartyIncludePartyPrinterGroupUses);
085            includePartyScaleUses = options.contains(PartyOptions.PartyIncludePartyScaleUses);
086            includePartyCarriers = options.contains(PartyOptions.PartyIncludePartyCarriers);
087            includePartyCarrierAccounts = options.contains(PartyOptions.PartyIncludePartyCarrierAccounts);
088            includeBillingAccounts = options.contains(PartyOptions.CompanyIncludeBillingAccounts);
089            includeInvoicesFrom = options.contains(PartyOptions.CompanyIncludeInvoicesFrom);
090            includeInvoicesTo = options.contains(PartyOptions.CompanyIncludeInvoicesTo);
091            setIncludeEntityAttributeGroups(options.contains(PartyOptions.CompanyIncludeEntityAttributeGroups));
092            setIncludeTagScopes(options.contains(PartyOptions.CompanyIncludeTagScopes));
093        }
094        
095        setIncludeEntityInstance(true);
096        
097        hasInvoiceLimits = session.hasLimit(InvoiceFactory.class);
098    }
099    
100    public CompanyTransfer getCompanyTransfer(PartyCompany partyCompany) {
101        return getCompanyTransfer(partyCompany.getParty());
102    }
103    
104    public CompanyTransfer getCompanyTransfer(Party party) {
105        CompanyTransfer companyTransfer = get(party);
106        
107        if(companyTransfer == null) {
108            PartyDetail partyDetail = party.getLastDetail();
109            String partyName = partyDetail.getPartyName();
110            PartyTypeTransfer partyTypeTransfer = partyControl.getPartyTypeTransfer(userVisit, partyDetail.getPartyType());
111            Language preferredLanguage = partyDetail.getPreferredLanguage();
112            LanguageTransfer preferredLanguageTransfer = preferredLanguage == null ? null : partyControl.getLanguageTransfer(userVisit, preferredLanguage);
113            Currency preferredCurrency = partyDetail.getPreferredCurrency();
114            CurrencyTransfer preferredCurrencyTransfer = preferredCurrency == null ? null : accountingControl.getCurrencyTransfer(userVisit, preferredCurrency);
115            TimeZone preferredTimeZone = partyDetail.getPreferredTimeZone();
116            TimeZoneTransfer preferredTimeZoneTransfer = preferredTimeZone == null ? null : partyControl.getTimeZoneTransfer(userVisit, preferredTimeZone);
117            DateTimeFormat preferredDateTimeFormat = partyDetail.getPreferredDateTimeFormat();
118            DateTimeFormatTransfer preferredDateTimeFormatTransfer = preferredDateTimeFormat == null ? null : partyControl.getDateTimeFormatTransfer(userVisit, preferredDateTimeFormat);
119            Person person = partyControl.getPerson(party);
120            PersonTransfer personTransfer = person == null ? null : partyControl.getPersonTransfer(userVisit, person);
121            PartyGroup partyGroup = partyControl.getPartyGroup(party);
122            PartyGroupTransfer partyGroupTransfer = partyGroup == null ? null : partyControl.getPartyGroupTransfer(userVisit, partyGroup);
123            PartyCompany partyCompany = partyControl.getPartyCompany(party);
124            String companyName = partyCompany.getPartyCompanyName();
125            String isDefault = partyCompany.getIsDefault().toString();
126            String sortOrder = partyCompany.getSortOrder().toString();
127            
128            companyTransfer = new CompanyTransfer(partyName, partyTypeTransfer, preferredLanguageTransfer, preferredCurrencyTransfer, preferredTimeZoneTransfer, preferredDateTimeFormatTransfer,
129                    personTransfer, partyGroupTransfer, companyName, isDefault, sortOrder);
130            put(party, companyTransfer);
131            
132            if(includePartyContactMechanisms) {
133                companyTransfer.setPartyContactMechanisms(new ListWrapper<>(contactControl.getPartyContactMechanismTransfersByParty(userVisit, party)));
134            }
135
136            if(includePartyDocuments) {
137                companyTransfer.setPartyDocuments(new ListWrapper<>(documentControl.getPartyDocumentTransfersByParty(userVisit, party)));
138            }
139
140            if(includePartyPrinterGroupUses) {
141                companyTransfer.setPartyPrinterGroupUses(new ListWrapper<>(printerControl.getPartyPrinterGroupUseTransfersByParty(userVisit, party)));
142            }
143
144            if(includePartyScaleUses) {
145                companyTransfer.setPartyScaleUses(new ListWrapper<>(scaleControl.getPartyScaleUseTransfersByParty(userVisit, party)));
146            }
147
148            if(includePartyCarriers) {
149                companyTransfer.setPartyCarriers(new ListWrapper<>(carrierControl.getPartyCarrierTransfersByParty(userVisit, party)));
150            }
151
152            if(includePartyCarrierAccounts) {
153                companyTransfer.setPartyCarrierAccounts(new ListWrapper<>(carrierControl.getPartyCarrierAccountTransfersByParty(userVisit, party)));
154            }
155
156            if(includeInvoicesFrom) {
157                companyTransfer.setInvoicesFrom(new ListWrapper<>(invoiceControl.getInvoiceTransfersByInvoiceFrom(userVisit, party)));
158                
159                if(hasInvoiceLimits) {
160                    companyTransfer.setInvoicesFromCount(invoiceControl.countInvoicesByInvoiceFrom(party));
161                }
162            }
163            
164            if(includeInvoicesTo) {
165                companyTransfer.setInvoicesTo(new ListWrapper<>(invoiceControl.getInvoiceTransfersByInvoiceTo(userVisit, party)));
166                
167                if(hasInvoiceLimits) {
168                    companyTransfer.setInvoicesToCount(invoiceControl.countInvoicesByInvoiceTo(party));
169                }
170            }
171        }
172        
173        return companyTransfer;
174    }
175    
176}