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.carrier.server.control.CarrierControl;
022import com.echothree.model.control.contact.server.control.ContactControl;
023import com.echothree.model.control.contactlist.server.ContactListControl;
024import com.echothree.model.control.document.server.control.DocumentControl;
025import com.echothree.model.control.employee.server.control.EmployeeControl;
026import com.echothree.model.control.party.common.PartyOptions;
027import com.echothree.model.control.party.common.transfer.DateTimeFormatTransfer;
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.PartyRelationshipTransfer;
031import com.echothree.model.control.party.common.transfer.PartyTransfer;
032import com.echothree.model.control.party.common.transfer.PartyTypeTransfer;
033import com.echothree.model.control.party.common.transfer.PersonTransfer;
034import com.echothree.model.control.party.common.transfer.ProfileTransfer;
035import com.echothree.model.control.party.common.transfer.TimeZoneTransfer;
036import com.echothree.model.control.party.server.control.PartyControl;
037import com.echothree.model.control.printer.server.control.PrinterControl;
038import com.echothree.model.control.scale.server.control.ScaleControl;
039import com.echothree.model.control.training.server.control.TrainingControl;
040import com.echothree.model.control.user.server.control.UserControl;
041import com.echothree.model.data.accounting.server.entity.Currency;
042import com.echothree.model.data.party.server.entity.DateTimeFormat;
043import com.echothree.model.data.party.server.entity.Language;
044import com.echothree.model.data.party.server.entity.Party;
045import com.echothree.model.data.party.server.entity.PartyDetail;
046import com.echothree.model.data.party.server.entity.PartyGroup;
047import com.echothree.model.data.party.server.entity.Person;
048import com.echothree.model.data.party.server.entity.Profile;
049import com.echothree.model.data.party.server.entity.TimeZone;
050import com.echothree.model.data.user.server.entity.UserVisit;
051import com.echothree.util.common.transfer.ListWrapper;
052import com.echothree.util.server.persistence.Session;
053import java.util.ArrayList;
054import java.util.HashSet;
055import java.util.Set;
056
057public class PartyTransferCache
058        extends BasePartyTransferCache<Party, PartyTransfer> {
059    
060    AccountingControl accountingControl = Session.getModelController(AccountingControl.class);
061    CarrierControl carrierControl = Session.getModelController(CarrierControl.class);
062    ContactControl contactControl = Session.getModelController(ContactControl.class);
063    ContactListControl contactListControl = Session.getModelController(ContactListControl.class);
064    DocumentControl documentControl = Session.getModelController(DocumentControl.class);
065    EmployeeControl employeeControl = Session.getModelController(EmployeeControl.class);
066    PrinterControl printerControl = Session.getModelController(PrinterControl.class);
067    ScaleControl scaleControl = Session.getModelController(ScaleControl.class);
068    TrainingControl trainingControl = Session.getModelController(TrainingControl.class);
069    UserControl userControl = Session.getModelController(UserControl.class);
070
071    boolean includeUserLogin;
072    boolean includeRecoveryAnswer;
073    boolean includeDescription;
074    boolean includePartyAliases;
075    boolean includePartyContactMechanisms;
076    boolean includePartyContactLists;
077    boolean includePartyDocuments;
078    boolean includePartyPrinterGroupUses;
079    boolean includePartyScaleUses;
080    boolean includePartyCarriers;
081    boolean includePartyCarrierAccounts;
082    boolean includePartyRelationships;
083    boolean includePartyRelationshipsByFromParty;
084    boolean includePartyRelationshipsByToParty;
085    boolean includeEmployments;
086    boolean includeLeaves;
087    boolean includePartyResponsibilities;
088    boolean includePartyTrainingClasses;
089    boolean includePartySkills;
090    
091    /** Creates a new instance of PartyTransferCache */
092    public PartyTransferCache(UserVisit userVisit, PartyControl partyControl) {
093        super(userVisit, partyControl);
094        
095        var options = session.getOptions();
096        if(options != null) {
097            setIncludeKey(options.contains(PartyOptions.PartyIncludeKey));
098            setIncludeGuid(options.contains(PartyOptions.PartyIncludeGuid));
099            includeUserLogin = options.contains(PartyOptions.PartyIncludeUserLogin);
100            includeRecoveryAnswer = options.contains(PartyOptions.PartyIncludeRecoveryAnswer);
101            includeDescription = options.contains(PartyOptions.PartyIncludeDescription);
102            includePartyAliases = options.contains(PartyOptions.PartyIncludePartyAliases);
103            includePartyContactMechanisms = options.contains(PartyOptions.PartyIncludePartyContactMechanisms);
104            includePartyContactLists = options.contains(PartyOptions.PartyIncludePartyContactLists);
105            includePartyDocuments = options.contains(PartyOptions.PartyIncludePartyDocuments);
106            includePartyPrinterGroupUses = options.contains(PartyOptions.PartyIncludePartyPrinterGroupUses);
107            includePartyScaleUses = options.contains(PartyOptions.PartyIncludePartyScaleUses);
108            includePartyCarriers = options.contains(PartyOptions.PartyIncludePartyCarriers);
109            includePartyCarrierAccounts = options.contains(PartyOptions.PartyIncludePartyCarrierAccounts);
110            includePartyRelationships = options.contains(PartyOptions.PartyIncludePartyRelationships);
111            includePartyRelationshipsByFromParty = options.contains(PartyOptions.PartyIncludePartyRelationshipsByFromParty);
112            includePartyRelationshipsByToParty = options.contains(PartyOptions.PartyIncludePartyRelationshipsByToParty);
113            includeEmployments = options.contains(PartyOptions.PartyIncludeEmployments);
114            includeLeaves = options.contains(PartyOptions.PartyIncludeLeaves);
115            includePartyResponsibilities = options.contains(PartyOptions.PartyIncludePartyResponsibilities);
116            includePartyTrainingClasses = options.contains(PartyOptions.PartyIncludePartyTrainingClasses);
117            includePartySkills = options.contains(PartyOptions.PartyIncludePartySkills);
118        }
119        
120        setIncludeEntityInstance(true);
121    }
122    
123    public PartyTransfer getTransfer(Party party) {
124        PartyTransfer partyTransfer = get(party);
125        
126        if(partyTransfer == null) {
127            PartyDetail partyDetail = party.getLastDetail();
128            String partyName = partyDetail.getPartyName();
129            PartyTypeTransfer partyTypeTransfer = partyControl.getPartyTypeTransfer(userVisit, partyDetail.getPartyType());
130            Language preferredLanguage = partyDetail.getPreferredLanguage();
131            LanguageTransfer preferredLanguageTransfer = preferredLanguage == null? null: partyControl.getLanguageTransfer(userVisit, preferredLanguage);
132            Currency preferredCurrency = partyDetail.getPreferredCurrency();
133            CurrencyTransfer preferredCurrencyTransfer = preferredCurrency == null? null: accountingControl.getCurrencyTransfer(userVisit, preferredCurrency);
134            TimeZone preferredTimeZone = partyDetail.getPreferredTimeZone();
135            TimeZoneTransfer preferredTimeZoneTransfer = preferredTimeZone == null? null: partyControl.getTimeZoneTransfer(userVisit, preferredTimeZone);
136            DateTimeFormat preferredDateTimeFormat = partyDetail.getPreferredDateTimeFormat();
137            DateTimeFormatTransfer preferredDateTimeFormatTransfer = preferredDateTimeFormat == null? null: partyControl.getDateTimeFormatTransfer(userVisit, preferredDateTimeFormat);
138            Person person = partyControl.getPerson(party);
139            PersonTransfer personTransfer = person == null? null: partyControl.getPersonTransfer(userVisit, person);
140            PartyGroup partyGroup = partyControl.getPartyGroup(party);
141            PartyGroupTransfer partyGroupTransfer = partyGroup == null? null: partyControl.getPartyGroupTransfer(userVisit, partyGroup);
142            Profile profile = partyControl.getProfile(party);
143            ProfileTransfer profileTransfer = profile == null? null: partyControl.getProfileTransfer(userVisit, profile);
144            
145            partyTransfer = new PartyTransfer(partyName, partyTypeTransfer, preferredLanguageTransfer, preferredCurrencyTransfer, preferredTimeZoneTransfer, preferredDateTimeFormatTransfer,
146                    personTransfer, partyGroupTransfer, profileTransfer);
147            put(party, partyTransfer);
148            
149            if(includeUserLogin) {
150                partyTransfer.setUserLogin(userControl.getUserLoginTransfer(userVisit, party));
151            }
152            
153            if(includeRecoveryAnswer) {
154                partyTransfer.setRecoveryAnswer(userControl.getRecoveryAnswerTransfer(userVisit, party));
155            }
156            
157            if(includeDescription) {
158                partyTransfer.setDescription(partyControl.getBestPartyDescription(party, getLanguage()));
159            }
160            
161            if(includePartyContactMechanisms) {
162                partyTransfer.setPartyContactMechanisms(new ListWrapper<>(contactControl.getPartyContactMechanismTransfersByParty(userVisit, party)));
163            }
164            
165            if(includePartyContactLists) {
166                partyTransfer.setPartyContactLists(new ListWrapper<>(contactListControl.getPartyContactListTransfersByParty(userVisit, party)));
167            }
168
169            if(includePartyAliases) {
170                partyTransfer.setPartyAliases(new ListWrapper<>(partyControl.getPartyAliasTransfersByParty(userVisit, party)));
171            }
172
173            if(includePartyDocuments) {
174                partyTransfer.setPartyDocuments(new ListWrapper<>(documentControl.getPartyDocumentTransfersByParty(userVisit, party)));
175            }
176
177            if(includePartyPrinterGroupUses) {
178                partyTransfer.setPartyPrinterGroupUses(new ListWrapper<>(printerControl.getPartyPrinterGroupUseTransfersByParty(userVisit, party)));
179            }
180
181            if(includePartyScaleUses) {
182                partyTransfer.setPartyScaleUses(new ListWrapper<>(scaleControl.getPartyScaleUseTransfersByParty(userVisit, party)));
183            }
184
185            if(includePartyCarriers) {
186                partyTransfer.setPartyCarriers(new ListWrapper<>(carrierControl.getPartyCarrierTransfersByParty(userVisit, party)));
187            }
188
189            if(includePartyCarrierAccounts) {
190                partyTransfer.setPartyCarrierAccounts(new ListWrapper<>(carrierControl.getPartyCarrierAccountTransfersByParty(userVisit, party)));
191            }
192
193            if(includePartyRelationships || includePartyRelationshipsByFromParty || includePartyRelationshipsByToParty) {
194                Set<PartyRelationshipTransfer> partyRelationships = new HashSet<>();
195
196                if(includePartyRelationships || includePartyRelationshipsByFromParty) {
197                    partyRelationships.addAll(partyControl.getPartyRelationshipTransfersByFromParty(userVisit, party));
198                }
199
200                if(includePartyRelationships || includePartyRelationshipsByToParty) {
201                    partyRelationships.addAll(partyControl.getPartyRelationshipTransfersByToParty(userVisit, party));
202                }
203
204                partyTransfer.setPartyRelationships(new ListWrapper<>(new ArrayList<>(partyRelationships)));
205            }
206
207            if(includeEmployments) {
208                partyTransfer.setEmployments(new ListWrapper<>(employeeControl.getEmploymentTransfersByParty(userVisit, party)));
209            }
210
211            if(includeLeaves) {
212                partyTransfer.setLeaves(new ListWrapper<>(employeeControl.getLeaveTransfersByParty(userVisit, party)));
213            }
214
215            if(includePartyResponsibilities) {
216                partyTransfer.setPartyResponsibilities(new ListWrapper<>(employeeControl.getPartyResponsibilityTransfersByParty(userVisit, party)));
217            }
218
219            if(includePartyTrainingClasses) {
220                partyTransfer.setPartyTrainingClasses(new ListWrapper<>(trainingControl.getPartyTrainingClassTransfersByParty(userVisit, party)));
221            }
222
223            if(includePartySkills) {
224                partyTransfer.setPartySkills(new ListWrapper<>(employeeControl.getPartySkillTransfersByParty(userVisit, party)));
225            }
226        }
227        
228        return partyTransfer;
229    }
230    
231}