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.DateTimeFormatTransfer;
026import com.echothree.model.control.party.common.transfer.DepartmentTransfer;
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.PartyDepartment;
041import com.echothree.model.data.party.server.entity.PartyDetail;
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 DepartmentTransferCache
051        extends BasePartyTransferCache<Party, DepartmentTransfer> {
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 DepartmentTransferCache */
065    public DepartmentTransferCache(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.DepartmentIncludeKey));
071            setIncludeGuid(options.contains(PartyOptions.PartyIncludeGuid) || options.contains(PartyOptions.DepartmentIncludeGuid));
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.DepartmentIncludeEntityAttributeGroups));
077            setIncludeTagScopes(options.contains(PartyOptions.DepartmentIncludeTagScopes));
078        }
079        
080        setIncludeEntityInstance(true);
081    }
082    
083    public DepartmentTransfer getDepartmentTransfer(PartyDepartment partyDepartment) {
084        return getDepartmentTransfer(partyDepartment.getParty());
085    }
086    
087    public DepartmentTransfer getDepartmentTransfer(Party party) {
088        DepartmentTransfer departmentTransfer = get(party);
089        
090        if(departmentTransfer == 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            PartyDepartment partyDepartment = partyControl.getPartyDepartment(party);
107            Party divisionParty = partyDepartment.getDivisionParty();
108            DivisionTransfer division = partyControl.getDivisionTransfer(userVisit, divisionParty);
109            String departmentName = partyDepartment.getPartyDepartmentName();
110            String isDefault = partyDepartment.getIsDefault().toString();
111            String sortOrder = partyDepartment.getSortOrder().toString();
112            
113            departmentTransfer = new DepartmentTransfer(partyName, partyTypeTransfer, preferredLanguageTransfer, preferredCurrencyTransfer, preferredTimeZoneTransfer, preferredDateTimeFormatTransfer,
114                    personTransfer, partyGroupTransfer, division, departmentName, isDefault, sortOrder);
115            put(party, departmentTransfer);
116            
117            if(includePartyContactMechanisms) {
118                departmentTransfer.setPartyContactMechanisms(new ListWrapper<>(contactControl.getPartyContactMechanismTransfersByParty(userVisit, party)));
119            }
120
121            if(includePartyDocuments) {
122                departmentTransfer.setPartyDocuments(new ListWrapper<>(documentControl.getPartyDocumentTransfersByParty(userVisit, party)));
123            }
124
125            if(includePartyPrinterGroupUses) {
126                departmentTransfer.setPartyPrinterGroupUses(new ListWrapper<>(printerControl.getPartyPrinterGroupUseTransfersByParty(userVisit, party)));
127            }
128
129            if(includePartyScaleUses) {
130                departmentTransfer.setPartyScaleUses(new ListWrapper<>(scaleControl.getPartyScaleUseTransfersByParty(userVisit, party)));
131            }
132        }
133        
134        return departmentTransfer;
135    }
136    
137}