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.common.transfer; 018 019import com.google.common.base.Splitter; 020import java.io.Serializable; 021import java.util.HashMap; 022import java.util.HashSet; 023import java.util.Map; 024import java.util.Set; 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027 028public class EntityRefs 029 implements Serializable { 030 031 private Log log = LogFactory.getLog(EntityRefs.class); 032 033 private Set<String> componentVendorNames = new HashSet<>(); 034 private Map<String, Set<String>> entityTypeNames = new HashMap<>(); 035 private Map<String, Map<String, Set<String>>> entityRefs = new HashMap<>(); 036 037 private void addComponentVendors(String componentVendorNames) { 038 if(componentVendorNames != null) { 039 for(String splitComponentVendorName : Splitter.on(':').trimResults().omitEmptyStrings().splitToList(componentVendorNames).toArray(new String[0])) { 040 String[] splitComponentVendorNameParts = Splitter.on('.').trimResults().omitEmptyStrings().splitToList(splitComponentVendorName).toArray(new String[0]); 041 042 if(splitComponentVendorNameParts.length == 1) { 043 this.componentVendorNames.add(splitComponentVendorNameParts[0]); 044 } else { 045 log.error("Invalid componentVendorName: " + splitComponentVendorName); 046 } 047 } 048 } 049 } 050 051 private void addEntityTypeNames(String entityTypeNames) { 052 if(entityTypeNames != null) { 053 for(String splitEntityTypeName : Splitter.on(':').trimResults().omitEmptyStrings().splitToList(entityTypeNames).toArray(new String[0])) { 054 String[] splitEntityTypeNameParts = Splitter.on('.').trimResults().omitEmptyStrings().splitToList(splitEntityTypeName).toArray(new String[0]); 055 056 if(splitEntityTypeNameParts.length == 2) { 057 // Check for, and add, the ComponentVendorName. 058 Set<String> entityTypes = this.entityTypeNames.get(splitEntityTypeNameParts[0]); 059 060 if(entityTypes == null) { 061 entityTypes = new HashSet<>(1); 062 this.entityTypeNames.put(splitEntityTypeNameParts[0], entityTypes); 063 } 064 065 // Add the EntityTypeName. 066 entityTypes.add(splitEntityTypeNameParts[1]); 067 } else { 068 log.error("Invalid entityTypeName: " + splitEntityTypeName); 069 } 070 } 071 } 072 } 073 074 private void addEntityRefs(String entityRefs) { 075 if(entityRefs != null) { 076 for(String splitEntityRef : Splitter.on(':').trimResults().omitEmptyStrings().splitToList(entityRefs).toArray(new String[0])) { 077 String[] splitEntityRefParts = Splitter.on('.').trimResults().omitEmptyStrings().splitToList(splitEntityRef).toArray(new String[0]); 078 079 if(splitEntityRefParts.length == 3) { 080 // Check for, and add, the ComponentVendorName. 081 Map<String, Set<String>> entityTypes = this.entityRefs.get(splitEntityRefParts[0]); 082 083 if(entityTypes == null) { 084 entityTypes = new HashMap<>(1); 085 this.entityRefs.put(splitEntityRefParts[0], entityTypes); 086 } 087 088 // Check for, and add, the EntityTypeName. 089 Set<String> entityUniqueIds = entityTypes.get(splitEntityRefParts[1]); 090 091 if(entityUniqueIds == null) { 092 entityUniqueIds = new HashSet<>(1); 093 entityTypes.put(splitEntityRefParts[1], entityUniqueIds); 094 } 095 096 // Add the UniqueEntityId. 097 entityUniqueIds.add(splitEntityRefParts[2]); 098 } else { 099 log.error("Invalid entityRef: " + splitEntityRef); 100 } 101 } 102 } 103 } 104 105 /** Creates a new instance of EntityRefs */ 106 public EntityRefs(String componentVendorNames, String entityTypeNames, String entityRefs) { 107 addComponentVendors(componentVendorNames); 108 addEntityTypeNames(entityTypeNames); 109 addEntityRefs(entityRefs); 110 } 111 112 private boolean containsCompnenentVendorName(String componentVendorName) { 113 return componentVendorNames.contains(componentVendorName); 114 } 115 116 private boolean containsEntityTypeName(String componentVendorName, String entityTypeName) { 117 Set<String> entityTypes = this.entityTypeNames.get(componentVendorName); 118 119 return entityTypes == null ? false : entityTypes.contains(entityTypeName); 120 } 121 122 private boolean containsEntityRef(String componentVendorName, String entityTypeName, String entityUniqueId) { 123 Map<String, Set<String>> entityTypes = this.entityRefs.get(componentVendorName); 124 Set<String> entityUniqueIds = entityTypes == null ? null : entityTypes.get(entityTypeName); 125 126 return entityUniqueIds == null ? false : entityUniqueIds.contains(entityUniqueId); 127 } 128 129 public boolean contains(String entityRef) { 130 boolean result = false; 131 String[] entityRefParts = Splitter.on('.').trimResults().omitEmptyStrings().splitToList(entityRef).toArray(new String[0]); 132 133 if(entityRefParts.length == 3) { 134 String componentVendorName = entityRefParts[0]; 135 String entityTypeName = entityRefParts[1]; 136 String entityUniqueId = entityRefParts[2]; 137 138 result = containsCompnenentVendorName(componentVendorName) || containsEntityTypeName(componentVendorName, entityTypeName) 139 || containsEntityRef(componentVendorName, entityTypeName, entityUniqueId); 140 } else { 141 log.error("Invalid entityRef: " + entityRef); 142 } 143 144 return result; 145 } 146 147}