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