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