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.DepartmentTransfer;
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.PartyDepartment;
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 DepartmentTransferCache
036        extends BasePartyTransferCache<Party, DepartmentTransfer> {
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 DepartmentTransferCache */
050    protected DepartmentTransferCache() {
051        super();
052        
053        var options = session.getOptions();
054        if(options != null) {
055            setIncludeUuid(options.contains(PartyOptions.PartyIncludeUuid) || options.contains(PartyOptions.DepartmentIncludeUuid));
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.DepartmentIncludeEntityAttributeGroups));
061            setIncludeTagScopes(options.contains(PartyOptions.DepartmentIncludeTagScopes));
062        }
063        
064        setIncludeEntityInstance(true);
065    }
066    
067    public DepartmentTransfer getTransfer(UserVisit userVisit, PartyDepartment partyDepartment) {
068        return getTransfer(userVisit, partyDepartment.getParty());
069    }
070
071    @Override
072    public DepartmentTransfer getTransfer(UserVisit userVisit, Party party) {
073        var departmentTransfer = get(party);
074        
075        if(departmentTransfer == 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 partyDepartment = partyControl.getPartyDepartment(party);
092            var divisionParty = partyDepartment.getDivisionParty();
093            var division = partyControl.getDivisionTransfer(userVisit, divisionParty);
094            var departmentName = partyDepartment.getPartyDepartmentName();
095            var isDefault = partyDepartment.getIsDefault().toString();
096            var sortOrder = partyDepartment.getSortOrder().toString();
097            
098            departmentTransfer = new DepartmentTransfer(partyName, partyTypeTransfer, preferredLanguageTransfer, preferredCurrencyTransfer, preferredTimeZoneTransfer, preferredDateTimeFormatTransfer,
099                    personTransfer, partyGroupTransfer, division, departmentName, isDefault, sortOrder);
100            put(userVisit, party, departmentTransfer);
101            
102            if(includePartyContactMechanisms) {
103                departmentTransfer.setPartyContactMechanisms(new ListWrapper<>(contactControl.getPartyContactMechanismTransfersByParty(userVisit, party)));
104            }
105
106            if(includePartyDocuments) {
107                departmentTransfer.setPartyDocuments(new ListWrapper<>(documentControl.getPartyDocumentTransfersByParty(userVisit, party)));
108            }
109
110            if(includePartyPrinterGroupUses) {
111                departmentTransfer.setPartyPrinterGroupUses(new ListWrapper<>(printerControl.getPartyPrinterGroupUseTransfersByParty(userVisit, party)));
112            }
113
114            if(includePartyScaleUses) {
115                departmentTransfer.setPartyScaleUses(new ListWrapper<>(scaleControl.getPartyScaleUseTransfersByParty(userVisit, party)));
116            }
117        }
118        
119        return departmentTransfer;
120    }
121    
122}