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.comment.server.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.comment.common.CommentOptions;
021import com.echothree.model.control.comment.common.transfer.CommentTransfer;
022import com.echothree.model.control.core.server.control.MimeTypeControl;
023import com.echothree.model.control.party.server.control.PartyControl;
024import com.echothree.model.control.workflow.server.control.WorkflowControl;
025import com.echothree.model.data.comment.server.entity.Comment;
026import com.echothree.model.data.user.server.entity.UserVisit;
027import com.echothree.util.common.transfer.ListWrapper;
028import javax.enterprise.context.RequestScoped;
029
030@RequestScoped
031public class CommentTransferCache
032        extends BaseCommentTransferCache<Comment, CommentTransfer> {
033
034    @Inject
035    MimeTypeControl mimeTypeControl;
036
037    @Inject
038    PartyControl partyControl;
039
040    @Inject
041    WorkflowControl workflowControl;
042    
043    boolean includeBlob;
044    boolean includeClob;
045    boolean includeString;
046    boolean includeCommentUsages;
047    boolean includeWorkflowStep;
048    
049    /** Creates a new instance of CommentTransferCache */
050    protected CommentTransferCache() {
051        super();
052
053        var options = session.getOptions();
054        if(options != null) {
055            includeBlob = options.contains(CommentOptions.CommentIncludeBlob);
056            includeClob = options.contains(CommentOptions.CommentIncludeClob);
057            includeString = options.contains(CommentOptions.CommentIncludeString);
058            includeCommentUsages = options.contains(CommentOptions.CommentIncludeCommentUsages);
059            includeWorkflowStep = options.contains(CommentOptions.CommentIncludeWorkflowStep);
060        }
061        
062        setIncludeEntityInstance(true);
063    }
064    
065    public CommentTransfer getCommentTransfer(UserVisit userVisit, Comment comment) {
066        var commentTransfer = get(comment);
067        
068        if(commentTransfer == null) {
069            var commentDetail = comment.getLastDetail();
070            var commentType = commentDetail.getCommentType();
071            var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(comment.getPrimaryKey());
072            
073            commentTransfer = new CommentTransfer();
074            put(userVisit, comment, commentTransfer, entityInstance);
075            
076            commentTransfer.setCommentName(commentDetail.getCommentName());
077            commentTransfer.setCommentType(commentControl.getCommentTypeTransfer(userVisit, commentType));
078            commentTransfer.setCommentedEntityInstance(entityInstanceControl.getEntityInstanceTransfer(userVisit, commentDetail.getCommentedEntityInstance(), false, false, false, false));
079            commentTransfer.setCommentedByEntityInstance(entityInstanceControl.getEntityInstanceTransfer(userVisit, commentDetail.getCommentedByEntityInstance(), false, false, false, false));
080            commentTransfer.setLanguage(partyControl.getLanguageTransfer(userVisit, commentDetail.getLanguage()));
081            commentTransfer.setDescription(commentDetail.getDescription());
082            var mimeType = commentDetail.getMimeType();
083            commentTransfer.setMimeType(mimeType == null? null: mimeTypeControl.getMimeTypeTransfer(userVisit, mimeType));
084            commentTransfer.setEntityInstance(entityInstanceControl.getEntityInstanceTransfer(userVisit, entityInstance, false, false, false, false));
085            
086            if(includeString) {
087                var commentString = commentControl.getCommentString(comment);
088                commentTransfer.setString(commentString == null? null: commentString.getString());
089            }
090            
091            if(includeBlob) {
092                var commentBlob = commentControl.getCommentBlob(comment);
093                commentTransfer.setBlob(commentBlob == null? null: commentBlob.getBlob());
094            }
095            
096            if(includeClob) {
097                var commentClob = commentControl.getCommentClob(comment);
098                commentTransfer.setClob(commentClob == null? null: commentClob.getClob());
099            }
100            
101            if(includeCommentUsages) {
102                commentTransfer.setCommentUsages(new ListWrapper<>(commentControl.getCommentUsageTransfersByComment(userVisit, comment)));
103            }
104            
105            if(includeWorkflowStep) {
106                var workflowEntrance = commentType.getLastDetail().getWorkflowEntrance();
107
108                if(workflowEntrance != null) {
109                    var workflow = workflowEntrance.getLastDetail().getWorkflow();
110
111                    commentTransfer.setCommentStatus(workflowControl.getWorkflowEntityStatusTransferByEntityInstance(userVisit, workflow, entityInstance));
112                }
113            }
114        }
115        
116        return commentTransfer;
117    }
118    
119}