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 javax.inject.Inject;
020import com.echothree.model.control.core.common.CoreOptions;
021import com.echothree.model.control.core.common.CoreProperties;
022import com.echothree.model.control.core.common.EntityAttributeTypes;
023import com.echothree.model.control.core.common.transfer.CacheEntryTransfer;
024import com.echothree.model.control.core.server.control.CacheEntryControl;
025import com.echothree.model.control.core.server.control.MimeTypeControl;
026import com.echothree.model.data.core.server.entity.CacheEntry;
027import com.echothree.model.data.user.server.entity.UserVisit;
028import com.echothree.util.common.form.TransferProperties;
029import com.echothree.util.common.persistence.type.ByteArray;
030import com.echothree.util.common.transfer.ListWrapper;
031import javax.enterprise.context.RequestScoped;
032
033@RequestScoped
034public class CacheEntryTransferCache
035        extends BaseCoreTransferCache<CacheEntry, CacheEntryTransfer> {
036
037    @Inject
038    CacheEntryControl cacheEntryControl;
039
040    @Inject
041    MimeTypeControl mimeTypeControl;
042
043    boolean includeBlob;
044    boolean includeClob;
045    boolean includeCacheEntryDependencies;
046    
047    TransferProperties transferProperties;
048    boolean filterCacheEntryKey;
049    boolean filterMimeType;
050    boolean filterCreatedTime;
051    boolean filterUnformattedCreatedTime;
052    boolean filterValidUntilTime;
053    boolean filterUnformattedValidUntilTime;
054
055    /** Creates a new instance of CacheEntryTransferCache */
056    protected CacheEntryTransferCache() {
057        super();
058
059        var options = session.getOptions();
060        if(options != null) {
061            includeBlob = options.contains(CoreOptions.CacheEntryIncludeBlob);
062            includeClob = options.contains(CoreOptions.CacheEntryIncludeClob);
063            includeCacheEntryDependencies = options.contains(CoreOptions.CacheEntryIncludeCacheEntryDependencies);
064        }
065        
066        transferProperties = session.getTransferProperties();
067        if(transferProperties != null) {
068            var properties = transferProperties.getProperties(CacheEntryTransfer.class);
069            
070            if(properties != null) {
071                filterCacheEntryKey = !properties.contains(CoreProperties.CACHE_ENTRY_KEY);
072                filterMimeType = !properties.contains(CoreProperties.MIME_TYPE);
073                filterCreatedTime = !properties.contains(CoreProperties.CREATED_TIME);
074                filterUnformattedCreatedTime = !properties.contains(CoreProperties.UNFORMATTED_CREATED_TIME);
075                filterValidUntilTime = !properties.contains(CoreProperties.VALID_UNTIL_TIME);
076                filterUnformattedValidUntilTime = !properties.contains(CoreProperties.UNFORMATTED_VALID_UNTIL_TIME);
077            }
078        }
079    }
080    
081    public CacheEntryTransfer getCacheEntryTransfer(UserVisit userVisit, CacheEntry cacheEntry) {
082        var cacheEntryTransfer = get(cacheEntry);
083        
084        if(cacheEntryTransfer == null) {
085            var cacheEntryKey = filterCacheEntryKey ? null : cacheEntry.getCacheEntryKey();
086            var mimeType = cacheEntry.getMimeType();
087            var mimeTypeTransfer = filterMimeType ? null : mimeTypeControl.getMimeTypeTransfer(userVisit, mimeType);
088            var unformattedCreatedTime = cacheEntry.getCreatedTime();
089            var createdTime = filterCreatedTime ? null : formatTypicalDateTime(userVisit, unformattedCreatedTime);
090            var unformattedValidUntilTime = cacheEntry.getValidUntilTime();
091            var validUntilTime = filterValidUntilTime ? null : formatTypicalDateTime(userVisit, unformattedValidUntilTime);
092            String clob = null;
093            ByteArray blob = null;
094
095            var entityAttributeTypeName = mimeType.getLastDetail().getEntityAttributeType().getEntityAttributeTypeName();
096            if(entityAttributeTypeName.equals(EntityAttributeTypes.CLOB.name())) {
097                if(includeClob) {
098                    var cacheClobEntry = cacheEntryControl.getCacheClobEntryByCacheEntry(cacheEntry);
099
100                    if(cacheClobEntry != null) {
101                        clob = cacheClobEntry.getClob();
102                    }
103                }
104            } else if(entityAttributeTypeName.equals(EntityAttributeTypes.BLOB.name())) {
105                if(includeBlob) {
106                    var cacheBlobEntry = cacheEntryControl.getCacheBlobEntryByCacheEntry(cacheEntry);
107
108                    if(cacheBlobEntry != null) {
109                        blob = cacheBlobEntry.getBlob();
110                    }
111                }
112            }
113
114            cacheEntryTransfer = new CacheEntryTransfer(cacheEntryKey, mimeTypeTransfer, createdTime,
115                    filterUnformattedCreatedTime ? null : unformattedCreatedTime, validUntilTime,
116                    filterUnformattedValidUntilTime ? null : unformattedValidUntilTime, clob, blob);
117            put(userVisit, cacheEntry, cacheEntryTransfer);
118            
119            if(includeCacheEntryDependencies) {
120                cacheEntryTransfer.setCacheEntryDependencies(new ListWrapper<>(cacheEntryControl.getCacheEntryDependencyTransfersByCacheEntry(userVisit, cacheEntry)));
121            }
122        }
123        
124        return cacheEntryTransfer;
125    }
126    
127}