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