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