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.document.server.transfer;
018
019import com.echothree.model.control.core.common.transfer.EntityTimeTransfer;
020import com.echothree.model.control.core.common.transfer.MimeTypeTransfer;
021import com.echothree.model.control.core.server.control.CoreControl;
022import com.echothree.model.control.document.common.DocumentOptions;
023import com.echothree.model.control.document.common.transfer.DocumentTransfer;
024import com.echothree.model.control.document.common.transfer.DocumentTypeTransfer;
025import com.echothree.model.control.document.server.control.DocumentControl;
026import com.echothree.model.data.document.server.entity.Document;
027import com.echothree.model.data.document.server.entity.DocumentBlob;
028import com.echothree.model.data.document.server.entity.DocumentClob;
029import com.echothree.model.data.document.server.entity.DocumentDetail;
030import com.echothree.model.data.user.server.entity.UserVisit;
031import com.echothree.util.common.persistence.type.ByteArray;
032import com.echothree.util.server.persistence.Session;
033import java.util.Set;
034
035public class DocumentTransferCache
036        extends BaseDocumentTransferCache<Document, DocumentTransfer> {
037    
038    CoreControl coreControl = Session.getModelController(CoreControl.class);
039    boolean includeBlob;
040    boolean includeClob;
041    boolean includeETag;
042    
043    /** Creates a new instance of DocumentTransferCache */
044    public DocumentTransferCache(UserVisit userVisit, DocumentControl documentControl) {
045        super(userVisit, documentControl);
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(Document document) {
058        DocumentTransfer documentTransfer = get(document);
059        
060        if(documentTransfer == null) {
061            DocumentDetail documentDetail = document.getLastDetail();
062            String documentName = documentDetail.getDocumentName();
063            DocumentTypeTransfer documentType = documentControl.getDocumentTypeTransfer(userVisit, documentDetail.getDocumentType());
064            MimeTypeTransfer mimeType = coreControl.getMimeTypeTransfer(userVisit, documentDetail.getMimeType());
065            Integer pages = documentDetail.getPages();
066            String description = documentControl.getBestDocumentDescription(document, getLanguage());
067            ByteArray blob = null;
068            String clob = null;
069            String eTag = null;
070            long eTagEntityId = 0;
071            int eTagSize = 0;
072            
073            if(includeBlob) {
074                DocumentBlob documentBlob = documentControl.getDocumentBlob(document);
075                
076                if(documentBlob != null) {
077                    blob = documentBlob.getBlob();
078                }
079            }
080            
081            if(includeClob) {
082                DocumentClob documentClob = documentControl.getDocumentClob(document);
083                
084                if(documentClob != null) {
085                    clob = documentClob.getClob();
086                }
087            }
088            
089            if(includeETag && eTagEntityId != 0) {
090                EntityTimeTransfer entityTimeTransfer = documentTransfer.getEntityInstance().getEntityTime();
091                Long modifiedTime = entityTimeTransfer.getUnformattedModifiedTime();
092                long maxTime = modifiedTime == null ? entityTimeTransfer.getUnformattedCreatedTime() : modifiedTime;
093
094                // EntityId-Size-ModifiedTime
095                eTag = new StringBuilder(Long.toHexString(eTagEntityId)).append('-').append(Integer.toHexString(eTagSize)).append('-').append(Long.toHexString(maxTime)).toString();
096            }
097
098            documentTransfer = new DocumentTransfer(documentName, documentType, mimeType, pages, description, blob, clob, eTag);
099            put(document, documentTransfer);
100        }
101        
102        return documentTransfer;
103    }
104    
105}