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