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.control.user.party.server.command.util; 018 019import com.echothree.control.user.party.common.spec.PartySpec; 020import com.echothree.control.user.party.common.spec.PartyTypeSpec; 021import com.echothree.model.control.party.common.PartyTypes; 022import com.echothree.model.control.party.server.control.PartyControl; 023import com.echothree.model.control.security.common.SecurityRoleGroups; 024import com.echothree.util.server.persistence.Session; 025import java.util.HashMap; 026import java.util.Map; 027 028public class PartyAliasUtil { 029 030 private PartyAliasUtil() { 031 super(); 032 } 033 034 private static class PartyAliasUtilHolder { 035 static PartyAliasUtil instance = new PartyAliasUtil(); 036 } 037 038 public static PartyAliasUtil getInstance() { 039 return PartyAliasUtilHolder.instance; 040 } 041 042 private final static Map<String, String> securityRoleGroupNameByPartyTypeName; 043 044 static { 045 securityRoleGroupNameByPartyTypeName = new HashMap<>(); 046 securityRoleGroupNameByPartyTypeName.put(PartyTypes.CUSTOMER.name(), SecurityRoleGroups.CustomerAlias.name()); 047 securityRoleGroupNameByPartyTypeName.put(PartyTypes.EMPLOYEE.name(), SecurityRoleGroups.EmployeeAlias.name()); 048 securityRoleGroupNameByPartyTypeName.put(PartyTypes.VENDOR.name(), SecurityRoleGroups.VendorAlias.name()); 049 securityRoleGroupNameByPartyTypeName.put(PartyTypes.WAREHOUSE.name(), SecurityRoleGroups.WarehouseAlias.name()); 050 } 051 052 public String getSecurityRoleGroupNameByPartyTypeName(String partyTypeName) { 053 String securityRoleGroupName = null; 054 055 if(partyTypeName != null) { 056 securityRoleGroupName = securityRoleGroupNameByPartyTypeName.get(partyTypeName); 057 } 058 059 return securityRoleGroupName; 060 } 061 062 // Caution: no validation of the form has been done at this point. 063 public String getSecurityRoleGroupNameByPartyName(String partyName) { 064 String securityRoleGroupName = null; 065 066 if(partyName != null) { 067 var partyControl = Session.getModelController(PartyControl.class); 068 var party = partyControl.getPartyByName(partyName); 069 070 if(party != null) { 071 securityRoleGroupName = getSecurityRoleGroupNameByPartyTypeName(party.getLastDetail().getPartyType().getPartyTypeName()); 072 } 073 } 074 075 return securityRoleGroupName; 076 } 077 078 public String getSecurityRoleGroupNameByPartySpec(PartySpec spec) { 079 String securityRoleGroupName = null; 080 081 if(spec != null) { 082 var partyName = spec.getPartyName(); 083 084 if(partyName != null) { 085 var partyControl = Session.getModelController(PartyControl.class); 086 var party = partyControl.getPartyByName(partyName); 087 088 if(party != null) { 089 securityRoleGroupName = getSecurityRoleGroupNameByPartyTypeName(party.getLastDetail().getPartyType().getPartyTypeName()); 090 } 091 } 092 } 093 094 return securityRoleGroupName; 095 } 096 097 public String getSecurityRoleGroupNameByPartyTypeSpec(PartyTypeSpec spec) { 098 String securityRoleGroupName = null; 099 100 if(spec != null) { 101 var partyTypeName = spec.getPartyTypeName(); 102 103 if(partyTypeName != null) { 104 var partyControl = Session.getModelController(PartyControl.class); 105 var partyType = partyControl.getPartyTypeByName(partyTypeName); 106 107 if(partyType != null) { 108 securityRoleGroupName = getSecurityRoleGroupNameByPartyTypeName(partyType.getPartyTypeName()); 109 } 110 } 111 } 112 113 return securityRoleGroupName; 114 } 115 116 public String getSecurityRoleGroupNameBySpecs(PartySpec partySpec, PartyTypeSpec partyTypeSpec) { 117 var securityRoleGroupName1 = getSecurityRoleGroupNameByPartySpec(partySpec); 118 var securityRoleGroupName2 = getSecurityRoleGroupNameByPartyTypeSpec(partyTypeSpec); 119 var securityRoleGroupNameCount = (securityRoleGroupName1 == null ? 0 : 1) + (securityRoleGroupName2 == null ? 0 : 1); 120 121 // You may only specify either the PartyName or the PartyTypeName = never both. 122 return securityRoleGroupNameCount == 1 ? (securityRoleGroupName1 == null ? securityRoleGroupName2 : securityRoleGroupName1) : null; 123 } 124 125}