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// --------------------------------------------------------------------------------
016package com.echothree.model.control.employee.server.transfer;
017
018import com.echothree.model.control.accounting.common.transfer.CurrencyTransfer;
019import com.echothree.model.control.accounting.server.control.AccountingControl;
020import com.echothree.model.control.contact.server.control.ContactControl;
021import com.echothree.model.control.contactlist.server.ContactListControl;
022import com.echothree.model.control.core.server.control.CoreControl;
023import com.echothree.model.control.document.server.control.DocumentControl;
024import com.echothree.model.control.employee.common.EmployeeOptions;
025import com.echothree.model.control.employee.common.transfer.EmployeeTransfer;
026import com.echothree.model.control.employee.common.transfer.EmployeeTypeTransfer;
027import com.echothree.model.control.employee.common.workflow.EmployeeAvailabilityConstants;
028import com.echothree.model.control.employee.common.workflow.EmployeeStatusConstants;
029import com.echothree.model.control.employee.server.control.EmployeeControl;
030import com.echothree.model.control.party.common.PartyOptions;
031import com.echothree.model.control.party.common.transfer.DateTimeFormatTransfer;
032import com.echothree.model.control.party.common.transfer.LanguageTransfer;
033import com.echothree.model.control.party.common.transfer.PartyRelationshipTransfer;
034import com.echothree.model.control.party.common.transfer.PartyTypeTransfer;
035import com.echothree.model.control.party.common.transfer.PersonTransfer;
036import com.echothree.model.control.party.common.transfer.ProfileTransfer;
037import com.echothree.model.control.party.common.transfer.TimeZoneTransfer;
038import com.echothree.model.control.party.server.control.PartyControl;
039import com.echothree.model.control.printer.server.control.PrinterControl;
040import com.echothree.model.control.scale.server.control.ScaleControl;
041import com.echothree.model.control.training.server.control.TrainingControl;
042import com.echothree.model.control.user.server.control.UserControl;
043import com.echothree.model.control.workflow.common.transfer.WorkflowEntityStatusTransfer;
044import com.echothree.model.control.workflow.server.control.WorkflowControl;
045import com.echothree.model.data.accounting.server.entity.Currency;
046import com.echothree.model.data.core.server.entity.EntityInstance;
047import com.echothree.model.data.employee.server.entity.PartyEmployee;
048import com.echothree.model.data.party.server.entity.DateTimeFormat;
049import com.echothree.model.data.party.server.entity.Language;
050import com.echothree.model.data.party.server.entity.Party;
051import com.echothree.model.data.party.server.entity.PartyDetail;
052import com.echothree.model.data.party.server.entity.Person;
053import com.echothree.model.data.party.server.entity.Profile;
054import com.echothree.model.data.party.server.entity.TimeZone;
055import com.echothree.model.data.user.server.entity.UserVisit;
056import com.echothree.util.common.transfer.ListWrapper;
057import com.echothree.util.server.persistence.Session;
058import java.util.ArrayList;
059import java.util.HashSet;
060import java.util.Set;
061
062public class EmployeeTransferCache
063        extends BaseEmployeeTransferCache<Party, EmployeeTransfer> {
064
065    AccountingControl accountingControl = Session.getModelController(AccountingControl.class);
066    ContactControl contactControl = Session.getModelController(ContactControl.class);
067    ContactListControl contactListControl = Session.getModelController(ContactListControl.class);
068    CoreControl coreControl = Session.getModelController(CoreControl.class);
069    DocumentControl documentControl = Session.getModelController(DocumentControl.class);
070    PartyControl partyControl = Session.getModelController(PartyControl.class);
071    PrinterControl printerControl = Session.getModelController(PrinterControl.class);
072    ScaleControl scaleControl = Session.getModelController(ScaleControl.class);
073    TrainingControl trainingControl = Session.getModelController(TrainingControl.class);
074    UserControl userControl = Session.getModelController(UserControl.class);
075    WorkflowControl workflowControl = Session.getModelController(WorkflowControl.class);
076    
077    boolean includeUserLogin;
078    boolean includeRecoveryAnswer;
079    boolean includePartyAliases;
080    boolean includePartyContactMechanisms;
081    boolean includePartyContactLists;
082    boolean includePartyDocuments;
083    boolean includePartyPrinterGroupUses;
084    boolean includePartyScaleUses;
085    boolean includePartyEntityTypes;
086    boolean includePartyApplicationEditorUses;
087    boolean includePartyRelationships;
088    boolean includePartyRelationshipsByFromParty;
089    boolean includePartyRelationshipsByToParty;
090    boolean includeEmployments;
091    boolean includeLeaves;
092    boolean includePartyResponsibilities;
093    boolean includePartyTrainingClasses;
094    boolean includePartySkills;
095
096    /** Creates a new instance of EmployeeTransferCache */
097    public EmployeeTransferCache(UserVisit userVisit, EmployeeControl employeeControl) {
098        super(userVisit, employeeControl);
099
100        var options = session.getOptions();
101        if(options != null) {
102            setIncludeKey(options.contains(PartyOptions.PartyIncludeKey) || options.contains(EmployeeOptions.EmployeeIncludeKey));
103            setIncludeGuid(options.contains(PartyOptions.PartyIncludeGuid) || options.contains(EmployeeOptions.EmployeeIncludeGuid));
104            includeUserLogin = options.contains(PartyOptions.PartyIncludeUserLogin);
105            includeRecoveryAnswer = options.contains(PartyOptions.PartyIncludeRecoveryAnswer);
106            includePartyAliases = options.contains(PartyOptions.PartyIncludePartyAliases);
107            includePartyContactMechanisms = options.contains(PartyOptions.PartyIncludePartyContactMechanisms);
108            includePartyContactLists = options.contains(PartyOptions.PartyIncludePartyContactLists);
109            includePartyDocuments = options.contains(PartyOptions.PartyIncludePartyDocuments);
110            includePartyPrinterGroupUses = options.contains(PartyOptions.PartyIncludePartyPrinterGroupUses);
111            includePartyScaleUses = options.contains(PartyOptions.PartyIncludePartyScaleUses);
112            includePartyEntityTypes = options.contains(PartyOptions.PartyIncludePartyEntityTypes);
113            includePartyApplicationEditorUses = options.contains(PartyOptions.PartyIncludePartyApplicationEditorUses);
114            includePartyRelationships = options.contains(EmployeeOptions.EmployeeIncludePartyRelationships);
115            includePartyRelationshipsByFromParty = options.contains(EmployeeOptions.EmployeeIncludePartyRelationshipsByFromParty);
116            includePartyRelationshipsByToParty = options.contains(EmployeeOptions.EmployeeIncludePartyRelationshipsByToParty);
117            includeEmployments = options.contains(EmployeeOptions.EmployeeIncludeEmployments);
118            includeLeaves = options.contains(EmployeeOptions.EmployeeIncludeLeaves);
119            includePartyResponsibilities = options.contains(EmployeeOptions.EmployeeIncludePartyResponsibilities);
120            includePartyTrainingClasses = options.contains(EmployeeOptions.EmployeeIncludePartyTrainingClasses);
121            includePartySkills = options.contains(EmployeeOptions.EmployeeIncludePartySkills);
122            setIncludeEntityAttributeGroups(options.contains(EmployeeOptions.EmployeeIncludeEntityAttributeGroups));
123            setIncludeTagScopes(options.contains(EmployeeOptions.EmployeeIncludeTagScopes));
124        }
125        
126        setIncludeEntityInstance(true);
127    }
128
129    public EmployeeTransfer getTransfer(PartyEmployee partyEmployee) {
130        return getTransfer(partyEmployee.getParty());
131    }
132
133    public EmployeeTransfer getTransfer(Party party) {
134        EmployeeTransfer employeeTransfer = get(party);
135
136        if(employeeTransfer == null) {
137            PartyDetail partyDetail = party.getLastDetail();
138            String partyName = partyDetail.getPartyName();
139            PartyTypeTransfer partyTypeTransfer = partyControl.getPartyTypeTransfer(userVisit, partyDetail.getPartyType());
140            Language preferredLanguage = partyDetail.getPreferredLanguage();
141            LanguageTransfer preferredLanguageTransfer = preferredLanguage == null ? null : partyControl.getLanguageTransfer(userVisit, preferredLanguage);
142            Currency preferredCurrency = partyDetail.getPreferredCurrency();
143            CurrencyTransfer preferredCurrencyTransfer = preferredCurrency == null ? null : accountingControl.getCurrencyTransfer(userVisit, preferredCurrency);
144            TimeZone preferredTimeZone = partyDetail.getPreferredTimeZone();
145            TimeZoneTransfer preferredTimeZoneTransfer = preferredTimeZone == null ? null : partyControl.getTimeZoneTransfer(userVisit, preferredTimeZone);
146            DateTimeFormat preferredDateTimeFormat = partyDetail.getPreferredDateTimeFormat();
147            DateTimeFormatTransfer preferredDateTimeFormatTransfer = preferredDateTimeFormat == null ? null : partyControl.getDateTimeFormatTransfer(userVisit, preferredDateTimeFormat);
148            PartyEmployee partyEmployee = employeeControl.getPartyEmployee(party);
149            Person person = partyControl.getPerson(party);
150            PersonTransfer personTransfer = person == null ? null : partyControl.getPersonTransfer(userVisit, person);
151            Profile profile = partyControl.getProfile(party);
152            ProfileTransfer profileTransfer = profile == null ? null : partyControl.getProfileTransfer(userVisit, profile);
153
154            String employeeName = partyEmployee.getPartyEmployeeName();
155            EmployeeTypeTransfer employeeType = employeeControl.getEmployeeTypeTransfer(userVisit, partyEmployee.getEmployeeType());
156
157            EntityInstance entityInstance = coreControl.getEntityInstanceByBasePK(party.getPrimaryKey());
158            WorkflowEntityStatusTransfer employeeStatusTransfer = workflowControl.getWorkflowEntityStatusTransferByEntityInstanceUsingNames(userVisit,
159                    EmployeeStatusConstants.Workflow_EMPLOYEE_STATUS, entityInstance);
160            WorkflowEntityStatusTransfer employeeAvailabilityTransfer = workflowControl.getWorkflowEntityStatusTransferByEntityInstanceUsingNames(userVisit,
161                    EmployeeAvailabilityConstants.Workflow_EMPLOYEE_AVAILABILITY, entityInstance);
162
163            employeeTransfer = new EmployeeTransfer(partyName, partyTypeTransfer, preferredLanguageTransfer, preferredCurrencyTransfer, preferredTimeZoneTransfer, preferredDateTimeFormatTransfer,
164                    personTransfer, profileTransfer, employeeName, employeeType, employeeStatusTransfer, employeeAvailabilityTransfer);
165            put(party, employeeTransfer, entityInstance);
166
167            if(includeUserLogin) {
168                employeeTransfer.setUserLogin(userControl.getUserLoginTransfer(userVisit, party));
169            }
170
171            if(includeRecoveryAnswer) {
172                employeeTransfer.setRecoveryAnswer(userControl.getRecoveryAnswerTransfer(userVisit, party));
173            }
174
175            if(includePartyAliases) {
176                employeeTransfer.setPartyAliases(new ListWrapper<>(partyControl.getPartyAliasTransfersByParty(userVisit, party)));
177            }
178
179            if(includePartyContactMechanisms) {
180                employeeTransfer.setPartyContactMechanisms(new ListWrapper<>(contactControl.getPartyContactMechanismTransfersByParty(userVisit, party)));
181            }
182
183            if(includePartyContactLists) {
184                employeeTransfer.setPartyContactLists(new ListWrapper<>(contactListControl.getPartyContactListTransfersByParty(userVisit, party)));
185            }
186
187            if(includePartyDocuments) {
188                employeeTransfer.setPartyDocuments(new ListWrapper<>(documentControl.getPartyDocumentTransfersByParty(userVisit, party)));
189            }
190
191            if(includePartyPrinterGroupUses) {
192                employeeTransfer.setPartyPrinterGroupUses(new ListWrapper<>(printerControl.getPartyPrinterGroupUseTransfersByParty(userVisit, party)));
193            }
194
195            if(includePartyScaleUses) {
196                employeeTransfer.setPartyScaleUses(new ListWrapper<>(scaleControl.getPartyScaleUseTransfersByParty(userVisit, party)));
197            }
198            
199            if(includePartyEntityTypes) {
200                employeeTransfer.setPartyEntityTypes(new ListWrapper<>(coreControl.getPartyEntityTypeTransfersByParty(userVisit, party)));
201            }
202            
203            if(includePartyApplicationEditorUses) {
204                employeeTransfer.setPartyApplicationEditorUses(new ListWrapper<>(coreControl.getPartyApplicationEditorUseTransfersByParty(userVisit, party)));
205            }
206
207            if(includePartyRelationships || includePartyRelationshipsByFromParty || includePartyRelationshipsByToParty) {
208                Set<PartyRelationshipTransfer> partyRelationships = new HashSet<>();
209
210                if(includePartyRelationships || includePartyRelationshipsByFromParty) {
211                    partyRelationships.addAll(partyControl.getPartyRelationshipTransfersByFromParty(userVisit, party));
212                }
213
214                if(includePartyRelationships || includePartyRelationshipsByToParty) {
215                    partyRelationships.addAll(partyControl.getPartyRelationshipTransfersByToParty(userVisit, party));
216                }
217
218                employeeTransfer.setPartyRelationships(new ListWrapper<>(new ArrayList<>(partyRelationships)));
219            }
220
221            if(includeEmployments) {
222                employeeTransfer.setEmployments(new ListWrapper<>(employeeControl.getEmploymentTransfersByParty(userVisit, party)));
223            }
224
225            if(includeLeaves) {
226                employeeTransfer.setLeaves(new ListWrapper<>(employeeControl.getLeaveTransfersByParty(userVisit, party)));
227            }
228
229            if(includePartyResponsibilities) {
230                employeeTransfer.setPartyResponsibilities(new ListWrapper<>(employeeControl.getPartyResponsibilityTransfersByParty(userVisit, party)));
231            }
232
233            if(includePartyTrainingClasses) {
234                employeeTransfer.setPartyTrainingClasses(new ListWrapper<>(trainingControl.getPartyTrainingClassTransfersByParty(userVisit, party)));
235            }
236
237            if(includePartySkills) {
238                employeeTransfer.setPartySkills(new ListWrapper<>(employeeControl.getPartySkillTransfersByParty(userVisit, party)));
239            }
240        }
241
242        return employeeTransfer;
243    }
244
245}