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