001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 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.model.control.core.server.transfer; 018 019import com.echothree.model.control.core.common.CoreOptions; 020import com.echothree.model.control.core.common.CoreProperties; 021import com.echothree.model.control.core.common.transfer.EntityInstanceTransfer; 022import com.echothree.model.control.core.server.control.AppearanceControl; 023import com.echothree.model.control.core.server.control.EntityInstanceControl; 024import com.echothree.model.control.core.server.control.EntityTypeControl; 025import com.echothree.model.control.core.server.control.EventControl; 026import com.echothree.model.data.core.server.entity.EntityInstance; 027import com.echothree.model.data.user.server.entity.UserVisit; 028import com.echothree.util.common.form.TransferProperties; 029import com.echothree.util.server.persistence.EntityDescriptionUtils; 030import com.echothree.util.server.persistence.EntityNamesUtils; 031import com.echothree.util.server.persistence.Session; 032import javax.enterprise.context.RequestScoped; 033 034@RequestScoped 035public class EntityInstanceTransferCache 036 extends BaseCoreTransferCache<EntityInstance, EntityInstanceTransfer> { 037 038 EntityInstanceControl entityInstanceControl = Session.getModelController(EntityInstanceControl.class); 039 EntityTypeControl entityTypeControl = Session.getModelController(EntityTypeControl.class); 040 EventControl eventControl = Session.getModelController(EventControl.class); 041 042 boolean includeEntityAppearance; 043 boolean includeEntityVisit; 044 boolean includeNames; 045 boolean includeUuidIfAvailable; 046 047 TransferProperties transferProperties; 048 boolean filterEntityType; 049 boolean filterEntityUniqueId; 050 boolean filterEntityRef; 051 boolean filterEntityTime; 052 boolean filterDescription; 053 054 /** Creates a new instance of EntityInstanceTransferCache */ 055 protected EntityInstanceTransferCache() { 056 super(); 057 058 var options = session.getOptions(); 059 if(options != null) { 060 includeEntityAppearance = options.contains(CoreOptions.EntityInstanceIncludeEntityAppearance); 061 includeEntityVisit = options.contains(CoreOptions.EntityInstanceIncludeEntityVisit); 062 includeNames = options.contains(CoreOptions.EntityInstanceIncludeNames); 063 includeUuidIfAvailable = options.contains(CoreOptions.EntityInstanceIncludeUuidIfAvailable); 064 } 065 066 transferProperties = session.getTransferProperties(); 067 if(transferProperties != null) { 068 var properties = transferProperties.getProperties(EntityInstanceTransfer.class); 069 070 if(properties != null) { 071 filterEntityType = !properties.contains(CoreProperties.ENTITY_TYPE); 072 filterEntityUniqueId = !properties.contains(CoreProperties.ENTITY_UNIQUE_ID); 073 filterEntityRef = !properties.contains(CoreProperties.ENTITY_REF); 074 filterEntityTime = !properties.contains(CoreProperties.ENTITY_TIME); 075 filterDescription = !properties.contains(CoreProperties.DESCRIPTION); 076 } 077 } 078 } 079 080 public EntityInstanceTransfer getEntityInstanceTransfer(final UserVisit userVisit, EntityInstance entityInstance, final boolean includeEntityAppearance, 081 final boolean includeEntityVisit, final boolean includeNames, final boolean includeUuid) { 082 var entityInstanceTransfer = get(entityInstance); 083 084 if(entityInstanceTransfer == null) { 085 var entityTypeTransfer = filterEntityType ? null : entityTypeControl.getEntityTypeTransfer(userVisit, entityInstance.getEntityType()); 086 var entityUniqueId = filterEntityUniqueId ? null : entityInstance.getEntityUniqueId(); 087 String uuid = null; 088 var componentVendorTransfer = entityTypeTransfer == null ? null : entityTypeTransfer.getComponentVendor(); 089 var componentVendorName = componentVendorTransfer == null ? null : componentVendorTransfer.getComponentVendorName(); 090 var entityTypeName = entityTypeTransfer == null ? null : entityTypeTransfer.getEntityTypeName(); 091 var entityRef = filterEntityRef || componentVendorName == null || entityTypeName == null || entityUniqueId == null ? null : componentVendorName + '.' + entityTypeName + '.' + entityUniqueId; 092 var entityTime = filterEntityTime ? null : eventControl.getEntityTime(entityInstance); 093 var entityTimeTransfer = entityTime == null ? null : eventControl.getEntityTimeTransfer(userVisit, entityTime); 094 String description = null; 095 096 if(includeUuid || includeUuidIfAvailable) { 097 uuid = entityInstance.getUuid(); 098 099 if(includeUuid && uuid == null) { 100 entityInstance = entityInstanceControl.ensureUuidForEntityInstance(entityInstance, false); 101 uuid = entityInstance.getUuid(); 102 } 103 } 104 105 if(!filterDescription) { 106 description = EntityDescriptionUtils.getInstance().getDescription(userVisit, entityInstance); 107 } 108 109 entityInstanceTransfer = new EntityInstanceTransfer(entityTypeTransfer, entityUniqueId, uuid, entityRef, 110 entityTimeTransfer, description); 111 put(userVisit, entityInstance, entityInstanceTransfer); 112 113 if(includeEntityAppearance || this.includeEntityAppearance) { 114 var appearanceControl = Session.getModelController(AppearanceControl.class); 115 var entityAppearance = appearanceControl.getEntityAppearance(entityInstance); 116 117 entityInstanceTransfer.setEntityAppearance(entityAppearance == null ? null : appearanceControl.getEntityAppearanceTransfer(userVisit, entityAppearance)); 118 } 119 120 if(includeEntityVisit || this.includeEntityVisit) { 121 // visitingParty could be null 122 var visitingParty = getUserControl().getPartyFromUserVisit(userVisit); 123 var visitingEntityInstance = visitingParty == null ? null : entityInstanceControl.getEntityInstanceByBasePK(visitingParty.getPrimaryKey()); 124 125 // visitingEntityInstance = the entityInstance parameter 126 // entityInstance = the visitedEntityInstance parameter 127 var entityVisit = visitingEntityInstance == null ? null : eventControl.getEntityVisit(visitingEntityInstance, entityInstance); 128 129 entityInstanceTransfer.setEntityVisit(entityVisit == null ? null : eventControl.getEntityVisitTransfer(userVisit, entityVisit)); 130 } 131 132 if(includeNames || this.includeNames) { 133 var entityNamesMapping = EntityNamesUtils.getInstance().getEntityNames(entityInstance); 134 135 entityInstanceTransfer.setEntityNames(entityNamesMapping == null ? null : entityNamesMapping.getEntityNames()); 136 } 137 } 138 139 return entityInstanceTransfer; 140 } 141 142}