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