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.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.CoreControl; 023import com.echothree.model.data.core.server.entity.EntityInstance; 024import com.echothree.model.data.user.server.entity.UserVisit; 025import com.echothree.util.common.form.TransferProperties; 026import com.echothree.util.server.persistence.EntityDescriptionUtils; 027import com.echothree.util.server.persistence.EntityNamesUtils; 028import com.echothree.util.server.persistence.Session; 029 030public class EntityInstanceTransferCache 031 extends BaseCoreTransferCache<EntityInstance, EntityInstanceTransfer> { 032 033 CoreControl coreControl = Session.getModelController(CoreControl.class); 034 035 boolean includeEntityAppearance; 036 boolean includeEntityVisit; 037 boolean includeNames; 038 boolean includeKeyIfAvailable; 039 boolean includeGuidIfAvailable; 040 boolean includeUlidIfAvailable; 041 042 TransferProperties transferProperties; 043 boolean filterEntityType; 044 boolean filterEntityUniqueId; 045 boolean filterEntityRef; 046 boolean filterEntityTime; 047 boolean filterDescription; 048 049 /** Creates a new instance of EntityInstanceTransferCache */ 050 public EntityInstanceTransferCache(UserVisit userVisit) { 051 super(userVisit); 052 053 var options = session.getOptions(); 054 if(options != null) { 055 includeEntityAppearance = options.contains(CoreOptions.EntityInstanceIncludeEntityAppearance); 056 includeEntityVisit = options.contains(CoreOptions.EntityInstanceIncludeEntityVisit); 057 includeNames = options.contains(CoreOptions.EntityInstanceIncludeNames); 058 includeKeyIfAvailable = options.contains(CoreOptions.EntityInstanceIncludeKeyIfAvailable); 059 includeGuidIfAvailable = options.contains(CoreOptions.EntityInstanceIncludeGuidIfAvailable); 060 includeUlidIfAvailable = options.contains(CoreOptions.EntityInstanceIncludeUlidIfAvailable); 061 } 062 063 transferProperties = session.getTransferProperties(); 064 if(transferProperties != null) { 065 var properties = transferProperties.getProperties(EntityInstanceTransfer.class); 066 067 if(properties != null) { 068 filterEntityType = !properties.contains(CoreProperties.ENTITY_TYPE); 069 filterEntityUniqueId = !properties.contains(CoreProperties.ENTITY_UNIQUE_ID); 070 filterEntityRef = !properties.contains(CoreProperties.ENTITY_REF); 071 filterEntityTime = !properties.contains(CoreProperties.ENTITY_TIME); 072 filterDescription = !properties.contains(CoreProperties.DESCRIPTION); 073 } 074 } 075 } 076 077 public EntityInstanceTransfer getEntityInstanceTransfer(EntityInstance entityInstance, boolean includeEntityAppearance, 078 boolean includeEntityVisit, boolean includeNames, boolean includeKey, boolean includeGuid, boolean includeUlid) { 079 EntityInstanceTransfer entityInstanceTransfer = get(entityInstance); 080 081 if(entityInstanceTransfer == null) { 082 var entityTypeTransfer = filterEntityType ? null : coreControl.getEntityTypeTransfer(userVisit, entityInstance.getEntityType()); 083 var entityUniqueId = filterEntityUniqueId ? null : entityInstance.getEntityUniqueId(); 084 String key = null; 085 String guid = null; 086 String ulid = null; 087 var componentVendorTransfer = entityTypeTransfer == null ? null : entityTypeTransfer.getComponentVendor(); 088 var componentVendorName = componentVendorTransfer == null ? null : componentVendorTransfer.getComponentVendorName(); 089 var entityTypeName = entityTypeTransfer == null ? null : entityTypeTransfer.getEntityTypeName(); 090 var entityRef = filterEntityRef || componentVendorName == null || entityTypeName == null || entityUniqueId == null ? null : new StringBuilder(componentVendorName).append('.').append(entityTypeName).append('.').append(entityUniqueId).toString(); 091 var entityTime = filterEntityTime ? null : coreControl.getEntityTime(entityInstance); 092 var entityTimeTransfer = entityTime == null ? null : coreControl.getEntityTimeTransfer(userVisit, entityTime); 093 String description = null; 094 095 if(includeKey || includeKeyIfAvailable) { 096 key = entityInstance.getKey(); 097 098 if(includeKey && key == null) { 099 entityInstance = coreControl.ensureKeyForEntityInstance(entityInstance, false); 100 key = entityInstance.getKey(); 101 } 102 } 103 104 if(includeGuid || includeGuidIfAvailable) { 105 guid = entityInstance.getGuid(); 106 107 if(includeGuid && guid == null) { 108 entityInstance = coreControl.ensureGuidForEntityInstance(entityInstance, false); 109 guid = entityInstance.getGuid(); 110 } 111 } 112 113 if(includeUlid || includeUlidIfAvailable) { 114 ulid = entityInstance.getUlid(); 115 116 if(includeUlid && ulid == null) { 117 entityInstance = coreControl.ensureUlidForEntityInstance(entityInstance, false); 118 ulid = entityInstance.getUlid(); 119 } 120 } 121 122 if(!filterDescription) { 123 description = EntityDescriptionUtils.getInstance().getDescription(userVisit, entityInstance); 124 } 125 126 entityInstanceTransfer = new EntityInstanceTransfer(entityTypeTransfer, entityUniqueId, key, guid, ulid, entityRef, 127 entityTimeTransfer, description); 128 put(entityInstance, entityInstanceTransfer); 129 130 if(includeEntityAppearance || this.includeEntityAppearance) { 131 var entityAppearance = coreControl.getEntityAppearance(entityInstance); 132 133 entityInstanceTransfer.setEntityAppearance(entityAppearance == null ? null : coreControl.getEntityAppearanceTransfer(userVisit, entityAppearance)); 134 } 135 136 if(includeEntityVisit || this.includeEntityVisit) { 137 // visitingParty could be null 138 var visitingParty = getUserControl().getPartyFromUserVisit(userVisit); 139 var visitingEntityInstance = visitingParty == null ? null : coreControl.getEntityInstanceByBasePK(visitingParty.getPrimaryKey()); 140 141 // visitingEntityInstance = the entityInstance parameter 142 // entityInstance = the visitedEntityInstance parameter 143 var entityVisit = visitingEntityInstance == null ? null : coreControl.getEntityVisit(visitingEntityInstance, entityInstance); 144 145 entityInstanceTransfer.setEntityVisit(entityVisit == null ? null : coreControl.getEntityVisitTransfer(userVisit, entityVisit)); 146 } 147 148 if(includeNames || this.includeNames) { 149 var entityNamesMapping = EntityNamesUtils.getInstance().getEntityNames(entityInstance); 150 151 entityInstanceTransfer.setEntityNames(entityNamesMapping == null ? null : entityNamesMapping.getEntityNames()); 152 } 153 } 154 155 return entityInstanceTransfer; 156 } 157 158}