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