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.util.server.persistence.translator; 018 019import com.echothree.model.control.core.server.control.CoreControl; 020import com.echothree.model.control.customer.server.control.CustomerControl; 021import com.echothree.model.control.employee.server.control.EmployeeControl; 022import com.echothree.model.control.party.common.PartyTypes; 023import com.echothree.model.control.sequence.common.SequenceTypes; 024import com.echothree.model.data.core.server.entity.EntityInstance; 025import com.echothree.model.data.party.common.pk.PartyPK; 026import com.echothree.model.data.party.server.entity.Party; 027import com.echothree.model.data.party.server.entity.PartyDetail; 028import com.echothree.model.data.party.server.factory.PartyFactory; 029import com.echothree.util.common.persistence.EntityNames; 030import com.echothree.util.common.persistence.Names; 031import com.echothree.util.common.persistence.Targets; 032import com.echothree.util.common.transfer.MapWrapper; 033import com.echothree.util.server.persistence.EntityPermission; 034import com.echothree.util.server.persistence.Session; 035import java.util.Collections; 036import java.util.HashMap; 037import java.util.Map; 038 039public class PartyNameTranslator 040 implements EntityInstanceTranslator, SequenceTypeTranslator { 041 042 private final static Map<String, String> partyTypesToTargets; 043 private final static Map<String, String> sequenceTypesToTargets; 044 045 static { 046 var partyTypesToTargetsMap = new HashMap<String, String>(); 047 048 partyTypesToTargetsMap.put(PartyTypes.EMPLOYEE.name(), Targets.Employee.name()); 049 partyTypesToTargetsMap.put(PartyTypes.CUSTOMER.name(), Targets.Customer.name()); 050 partyTypesToTargetsMap.put(PartyTypes.COMPANY.name(), Targets.Company.name()); 051 partyTypesToTargetsMap.put(PartyTypes.DIVISION.name(), Targets.Division.name()); 052 partyTypesToTargetsMap.put(PartyTypes.DEPARTMENT.name(), Targets.Department.name()); 053 partyTypesToTargetsMap.put(PartyTypes.VENDOR.name(), Targets.Vendor.name()); 054 partyTypesToTargetsMap.put(PartyTypes.CARRIER.name(), Targets.Carrier.name()); 055 partyTypesToTargetsMap.put(PartyTypes.WAREHOUSE.name(), Targets.Warehouse.name()); 056 057 partyTypesToTargets = Collections.unmodifiableMap(partyTypesToTargetsMap); 058 059 var sequenceTypesToTargetsMap = new HashMap<String, String>(); 060 061 sequenceTypesToTargetsMap.put(SequenceTypes.CUSTOMER.name(), Targets.Customer.name()); 062 sequenceTypesToTargetsMap.put(SequenceTypes.EMPLOYEE.name(), Targets.Employee.name()); 063 064 sequenceTypesToTargets = Collections.unmodifiableMap(sequenceTypesToTargetsMap); 065 } 066 067 private EntityNames getNames(final Map<String, String> targetMap, final String key, final PartyDetail partyDetail) { 068 EntityNames result = null; 069 var target = targetMap.get(key); 070 071 if(target != null) { 072 var names = new MapWrapper<String>(1); 073 074 names.put(Names.PartyName.name(), partyDetail.getPartyName()); 075 076 result = new EntityNames(target, names); 077 } 078 079 return result; 080 } 081 082 @Override 083 public EntityNames getNames(final EntityInstance entityInstance) { 084 var partyDetail = PartyFactory.getInstance().getEntityFromPK(EntityPermission.READ_ONLY, 085 new PartyPK(entityInstance.getEntityUniqueId())).getLastDetail(); 086 var partyTypeName = partyDetail.getPartyType().getPartyTypeName(); 087 088 return getNames(partyTypesToTargets, partyTypeName, partyDetail); 089 } 090 091 @Override 092 public EntityInstanceAndNames getNames(final String sequenceTypeName, final String value, 093 final boolean includeEntityInstance) { 094 EntityInstanceAndNames result = null; 095 var target = sequenceTypesToTargets.get(sequenceTypeName); 096 Party party = null; 097 098 if(target.equals(Targets.Customer.name())) { 099 var customerControl = Session.getModelController(CustomerControl.class); 100 var customer = customerControl.getCustomerByName(value); 101 102 if(customer != null) { 103 party = customer.getParty(); 104 } 105 } else if(target.equals(Targets.Employee.name())) { 106 var employeeControl = Session.getModelController(EmployeeControl.class); 107 var partyEmployee = employeeControl.getPartyEmployeeByName(value); 108 109 if(partyEmployee != null) { 110 party = partyEmployee.getParty(); 111 } 112 } 113 114 if(party != null) { 115 var coreControl = Session.getModelController(CoreControl.class); 116 var entityNames = getNames(sequenceTypesToTargets, sequenceTypeName, party.getLastDetail()); 117 118 result = entityNames == null ? null : new EntityInstanceAndNames(includeEntityInstance? coreControl.getEntityInstanceByBasePK(party.getPrimaryKey()) : null, entityNames); 119 } 120 121 return result; 122 } 123 124} 125