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.transfer.EntityBlobAttributeTransfer;
021import com.echothree.model.control.core.server.control.CoreControl;
022import com.echothree.model.control.core.server.control.EntityInstanceControl;
023import com.echothree.model.control.core.server.control.MimeTypeControl;
024import com.echothree.model.control.party.server.control.PartyControl;
025import com.echothree.model.data.core.server.entity.EntityBlobAttribute;
026import com.echothree.model.data.core.server.entity.EntityInstance;
027import com.echothree.model.data.user.server.entity.UserVisit;
028import com.echothree.util.server.persistence.Session;
029import javax.enterprise.context.RequestScoped;
030
031@RequestScoped
032public class EntityBlobAttributeTransferCache
033        extends BaseCoreTransferCache<EntityBlobAttribute, EntityBlobAttributeTransfer> {
034
035    CoreControl coreControl = Session.getModelController(CoreControl.class);
036    EntityInstanceControl entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
037    MimeTypeControl mimeTypeControl = Session.getModelController(MimeTypeControl.class);
038    PartyControl partyControl = Session.getModelController(PartyControl.class);
039
040    boolean includeBlob;
041    boolean includeETag;
042    
043    /** Creates a new instance of EntityBlobAttributeTransferCache */
044    protected EntityBlobAttributeTransferCache() {
045        super();
046        
047        var options = session.getOptions();
048        if(options != null) {
049            includeBlob = options.contains(CoreOptions.EntityBlobAttributeIncludeBlob);
050            includeETag = options.contains(CoreOptions.EntityBlobAttributeIncludeETag);
051        }
052    }
053    
054    public EntityBlobAttributeTransfer getEntityBlobAttributeTransfer(final UserVisit userVisit, final EntityBlobAttribute entityBlobAttribute, final EntityInstance entityInstance) {
055        var entityBlobAttributeTransfer = get(entityBlobAttribute);
056        
057        if(entityBlobAttributeTransfer == null) {
058            var entityAttribute = entityInstance == null ? coreControl.getEntityAttributeTransfer(userVisit, entityBlobAttribute.getEntityAttribute(), entityInstance) : null;
059            var entityInstanceTransfer = entityInstanceControl.getEntityInstanceTransfer(userVisit, entityBlobAttribute.getEntityInstance(), false, false, false, false);
060            var language = partyControl.getLanguageTransfer(userVisit, entityBlobAttribute.getLanguage());
061            var blobAttribute = includeBlob ? entityBlobAttribute.getBlobAttribute() : null;
062            var mimeType = mimeTypeControl.getMimeTypeTransfer(userVisit, entityBlobAttribute.getMimeType());
063            String eTag = null;
064            
065            if(includeETag) {
066                // Item Descriptions do not have their own EntityTime, fall back on the Item's EntityTime.
067                var entityTimeTransfer = entityInstanceTransfer.getEntityTime();
068                var modifiedTime = entityTimeTransfer.getUnformattedModifiedTime();
069                long maxTime = modifiedTime == null ? entityTimeTransfer.getUnformattedCreatedTime() : modifiedTime;
070                long eTagEntityId = entityBlobAttribute.getPrimaryKey().getEntityId();
071                var eTagSize = entityBlobAttribute.getBlobAttribute().length();
072
073                // EntityId-Size-ModifiedTime
074                eTag = Long.toHexString(eTagEntityId) + '-' + Integer.toHexString(eTagSize) + '-' + Long.toHexString(maxTime);
075            }
076            
077            entityBlobAttributeTransfer = new EntityBlobAttributeTransfer(entityAttribute, entityInstanceTransfer, language, blobAttribute, mimeType, eTag);
078            put(userVisit, entityBlobAttribute, entityBlobAttributeTransfer);
079        }
080        
081        return entityBlobAttributeTransfer;
082    }
083    
084}