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.warehouse.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.server.control.PartyControl;
024import com.echothree.model.control.printer.server.control.PrinterControl;
025import com.echothree.model.control.scale.server.control.ScaleControl;
026import com.echothree.model.control.warehouse.common.WarehouseOptions;
027import com.echothree.model.control.warehouse.common.transfer.WarehouseTransfer;
028import com.echothree.model.control.warehouse.server.control.WarehouseControl;
029import com.echothree.model.data.party.server.entity.Party;
030import com.echothree.model.data.user.server.entity.UserVisit;
031import com.echothree.model.data.warehouse.server.entity.Warehouse;
032import com.echothree.util.common.transfer.ListWrapper;
033import com.echothree.util.server.persistence.Session;
034import javax.enterprise.context.RequestScoped;
035
036@RequestScoped
037public class WarehouseTransferCache
038        extends BaseWarehouseTransferCache<Party, WarehouseTransfer> {
039    
040    AccountingControl accountingControl = Session.getModelController(AccountingControl.class);
041    ContactControl contactControl = Session.getModelController(ContactControl.class);
042    DocumentControl documentControl = Session.getModelController(DocumentControl.class);
043    PartyControl partyControl = Session.getModelController(PartyControl.class);
044    PrinterControl printerControl = Session.getModelController(PrinterControl.class);
045    ScaleControl scaleControl = Session.getModelController(ScaleControl.class);
046    WarehouseControl warehouseControl = Session.getModelController(WarehouseControl.class);
047
048    boolean includeLocationsCount;
049    boolean includeLocations;
050    boolean includePartyAliases;
051    boolean includePartyContactMechanisms;
052    boolean includePartyDocuments;
053    boolean includePartyPrinterGroupUses;
054    boolean includePartyScaleUses;
055    
056    /** Creates a new instance of WarehouseTransferCache */
057    protected WarehouseTransferCache() {
058        super();
059        
060        var options = session.getOptions();
061        if(options != null) {
062            includeLocationsCount = options.contains(WarehouseOptions.WarehouseIncludeLocationsCount);
063            includeLocations = options.contains(WarehouseOptions.WarehouseIncludeLocations);
064            setIncludeUuid(options.contains(PartyOptions.PartyIncludeUuid) || options.contains(WarehouseOptions.WarehouseIncludeUuid));
065            includePartyAliases = options.contains(PartyOptions.PartyIncludePartyAliases);
066            includePartyContactMechanisms = options.contains(PartyOptions.PartyIncludePartyContactMechanisms);
067            includePartyDocuments = options.contains(PartyOptions.PartyIncludePartyDocuments);
068            includePartyPrinterGroupUses = options.contains(PartyOptions.PartyIncludePartyPrinterGroupUses);
069            includePartyScaleUses = options.contains(PartyOptions.PartyIncludePartyScaleUses);
070            setIncludeEntityAttributeGroups(options.contains(WarehouseOptions.WarehouseIncludeEntityAttributeGroups));
071            setIncludeTagScopes(options.contains(WarehouseOptions.WarehouseIncludeTagScopes));
072        }
073        
074        setIncludeEntityInstance(true);
075    }
076    
077    public WarehouseTransfer getWarehouseTransfer(UserVisit userVisit, Warehouse warehouse) {
078        return getWarehouseTransfer(userVisit, warehouse.getParty());
079    }
080    
081    public WarehouseTransfer getWarehouseTransfer(UserVisit userVisit, Party party) {
082        var warehouseTransfer = get(party);
083        
084        if(warehouseTransfer == null) {
085            var partyDetail = party.getLastDetail();
086            var partyName = partyDetail.getPartyName();
087            var partyTypeTransfer = partyControl.getPartyTypeTransfer(userVisit, partyDetail.getPartyType());
088            var preferredLanguage = partyDetail.getPreferredLanguage();
089            var preferredLanguageTransfer = preferredLanguage == null ? null : partyControl.getLanguageTransfer(userVisit, preferredLanguage);
090            var preferredCurrency = partyDetail.getPreferredCurrency();
091            var preferredCurrencyTransfer = preferredCurrency == null ? null : accountingControl.getCurrencyTransfer(userVisit, preferredCurrency);
092            var preferredTimeZone = partyDetail.getPreferredTimeZone();
093            var preferredTimeZoneTransfer = preferredTimeZone == null ? null : partyControl.getTimeZoneTransfer(userVisit, preferredTimeZone);
094            var preferredDateTimeFormat = partyDetail.getPreferredDateTimeFormat();
095            var preferredDateTimeFormatTransfer = preferredDateTimeFormat == null ? null : partyControl.getDateTimeFormatTransfer(userVisit, preferredDateTimeFormat);
096            var person = partyControl.getPerson(party);
097            var personTransfer = person == null ? null : partyControl.getPersonTransfer(userVisit, person);
098            var partyGroup = partyControl.getPartyGroup(party);
099            var partyGroupTransfer = partyGroup == null ? null : partyControl.getPartyGroupTransfer(userVisit, partyGroup);
100            var warehouse = warehouseControl.getWarehouse(party);
101            var warehouseName = warehouse.getWarehouseName();
102            var warehouseTypeTransfer = warehouseControl.getWarehouseTypeTransfer(userVisit, warehouse.getWarehouseType());
103            var isDefault = warehouse.getIsDefault();
104            var sortOrder = warehouse.getSortOrder();
105            
106            warehouseTransfer = new WarehouseTransfer(partyName, partyTypeTransfer, preferredLanguageTransfer, preferredCurrencyTransfer,
107                    preferredTimeZoneTransfer, preferredDateTimeFormatTransfer, personTransfer, partyGroupTransfer, warehouseName,
108                    warehouseTypeTransfer, isDefault, sortOrder);
109            put(userVisit, party, warehouseTransfer);
110
111            if(includeLocationsCount) {
112                warehouseTransfer.setLocationsCount(warehouseControl.countLocationsByWarehouseParty(party));
113            }
114
115            if(includeLocations) {
116                warehouseTransfer.setLocations(new ListWrapper<>(warehouseControl.getLocationTransfersByWarehouseParty(userVisit, party)));
117            }
118
119            if(includePartyAliases) {
120                warehouseTransfer.setPartyAliases(new ListWrapper<>(partyControl.getPartyAliasTransfersByParty(userVisit, party)));
121            }
122
123            if(includePartyContactMechanisms) {
124                warehouseTransfer.setPartyContactMechanisms(new ListWrapper<>(contactControl.getPartyContactMechanismTransfersByParty(userVisit, party)));
125            }
126
127            if(includePartyDocuments) {
128                warehouseTransfer.setPartyDocuments(new ListWrapper<>(documentControl.getPartyDocumentTransfersByParty(userVisit, party)));
129            }
130
131            if(includePartyPrinterGroupUses) {
132                warehouseTransfer.setPartyPrinterGroupUses(new ListWrapper<>(printerControl.getPartyPrinterGroupUseTransfersByParty(userVisit, party)));
133            }
134
135            if(includePartyScaleUses) {
136                warehouseTransfer.setPartyScaleUses(new ListWrapper<>(scaleControl.getPartyScaleUseTransfersByParty(userVisit, party)));
137            }
138        }
139        
140        return warehouseTransfer;
141    }
142    
143}