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.document.server.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.core.server.control.MimeTypeControl;
021import com.echothree.model.control.document.common.DocumentOptions;
022import com.echothree.model.control.document.common.transfer.DocumentTransfer;
023import com.echothree.model.control.document.server.control.DocumentControl;
024import com.echothree.model.data.document.server.entity.Document;
025import com.echothree.model.data.user.server.entity.UserVisit;
026import com.echothree.util.common.persistence.type.ByteArray;
027import javax.enterprise.context.RequestScoped;
028
029@RequestScoped
030public class DocumentTransferCache
031        extends BaseDocumentTransferCache<Document, DocumentTransfer> {
032
033    @Inject
034    DocumentControl documentControl;
035
036    @Inject
037    MimeTypeControl mimeTypeControl;
038
039    boolean includeBlob;
040    boolean includeClob;
041    boolean includeETag;
042    
043    /** Creates a new instance of DocumentTransferCache */
044    protected DocumentTransferCache() {
045        super();
046        
047        var options = session.getOptions();
048        if(options != null) {
049            includeBlob = options.contains(DocumentOptions.DocumentIncludeBlob);
050            includeClob = options.contains(DocumentOptions.DocumentIncludeClob);
051            includeETag = options.contains(DocumentOptions.DocumentIncludeETag);
052        }
053        
054        setIncludeEntityInstance(true);
055    }
056    
057    public DocumentTransfer getDocumentTransfer(UserVisit userVisit, Document document) {
058        var documentTransfer = get(document);
059        
060        if(documentTransfer == null) {
061            var documentDetail = document.getLastDetail();
062            var documentName = documentDetail.getDocumentName();
063            var documentType = documentControl.getDocumentTypeTransfer(userVisit, documentDetail.getDocumentType());
064            var mimeType = mimeTypeControl.getMimeTypeTransfer(userVisit, documentDetail.getMimeType());
065            var pages = documentDetail.getPages();
066            var description = documentControl.getBestDocumentDescription(document, getLanguage(userVisit));
067            ByteArray blob = null;
068            String clob = null;
069            String eTag = null;
070            long eTagEntityId = 0;
071            var eTagSize = 0;
072            
073            if(includeBlob) {
074                var documentBlob = documentControl.getDocumentBlob(document);
075                
076                if(documentBlob != null) {
077                    blob = documentBlob.getBlob();
078                }
079            }
080            
081            if(includeClob) {
082                var documentClob = documentControl.getDocumentClob(document);
083                
084                if(documentClob != null) {
085                    clob = documentClob.getClob();
086                }
087            }
088            
089            if(includeETag && eTagEntityId != 0) {
090                var entityTimeTransfer = documentTransfer.getEntityInstance().getEntityTime();
091                var modifiedTime = entityTimeTransfer.getUnformattedModifiedTime();
092                long maxTime = modifiedTime == null ? entityTimeTransfer.getUnformattedCreatedTime() : modifiedTime;
093
094                // EntityId-Size-ModifiedTime
095                eTag = Long.toHexString(eTagEntityId) + '-' + Integer.toHexString(eTagSize) + '-' + Long.toHexString(maxTime);
096            }
097
098            documentTransfer = new DocumentTransfer(documentName, documentType, mimeType, pages, description, blob, clob, eTag);
099            put(userVisit, document, documentTransfer);
100        }
101        
102        return documentTransfer;
103    }
104    
105}