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