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