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.workflow.server.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.security.server.control.SecurityControl;
021import com.echothree.model.control.selector.server.control.SelectorControl;
022import com.echothree.model.control.workflow.common.WorkflowProperties;
023import com.echothree.model.control.workflow.common.transfer.WorkflowTransfer;
024import com.echothree.model.control.workflow.server.control.WorkflowControl;
025import com.echothree.model.data.user.server.entity.UserVisit;
026import com.echothree.model.data.workflow.server.entity.Workflow;
027import com.echothree.util.common.form.TransferProperties;
028import javax.enterprise.context.RequestScoped;
029
030@RequestScoped
031public class WorkflowTransferCache
032        extends BaseWorkflowTransferCache<Workflow, WorkflowTransfer> {
033
034    @Inject
035    SecurityControl securityControl;
036
037    @Inject
038    SelectorControl selectorControl;
039
040    @Inject
041    WorkflowControl workflowControl;
042
043    TransferProperties transferProperties;
044    boolean filterWorkflowName;
045    boolean filterSelectorType;
046    boolean filterSecurityRoleGroup;
047    boolean filterSortOrder;
048    boolean filterDescription;
049    boolean filterEntityInstance;
050
051    /** Creates a new instance of WorkflowTransferCache */
052    protected WorkflowTransferCache() {
053        transferProperties = session.getTransferProperties();
054        if(transferProperties != null) {
055            var properties = transferProperties.getProperties(WorkflowTransfer.class);
056            
057            if(properties != null) {
058                filterWorkflowName = !properties.contains(WorkflowProperties.WORKFLOW_NAME);
059                filterSelectorType = !properties.contains(WorkflowProperties.SELECTOR_TYPE);
060                filterSecurityRoleGroup = !properties.contains(WorkflowProperties.SECURITY_ROLE_GROUP);
061                filterSortOrder = !properties.contains(WorkflowProperties.SORT_ORDER);
062                filterDescription = !properties.contains(WorkflowProperties.DESCRIPTION);
063                filterEntityInstance = !properties.contains(WorkflowProperties.ENTITY_INSTANCE);
064            }
065        }
066        
067        setIncludeEntityInstance(!filterEntityInstance);
068    }
069    
070    public WorkflowTransfer getWorkflowTransfer(UserVisit userVisit, Workflow workflow) {
071        var workflowTransfer = get(workflow);
072        
073        if(workflowTransfer == null) {
074            var workflowDetail = workflow.getLastDetail();
075            var workflowName = filterWorkflowName ? null : workflowDetail.getWorkflowName();
076            var selectorType = filterSelectorType ? null : workflowDetail.getSelectorType();
077            var selectorTypeTransfer = selectorType == null? null: selectorControl.getSelectorTypeTransfer(userVisit, selectorType);
078            var securityRoleGroup = filterSecurityRoleGroup ? null : workflowDetail.getSecurityRoleGroup();
079            var securityRoleGroupTransfer = securityRoleGroup == null? null: securityControl.getSecurityRoleGroupTransfer(userVisit, securityRoleGroup);
080            var sortOrder = filterSortOrder ? null : workflowDetail.getSortOrder();
081            var description = filterDescription ? null : workflowControl.getBestWorkflowDescription(workflow, getLanguage(userVisit));
082            
083            workflowTransfer = new WorkflowTransfer(workflowName, selectorTypeTransfer,
084                    securityRoleGroupTransfer, sortOrder, description);
085            put(userVisit, workflow, workflowTransfer);
086        }
087        
088        return workflowTransfer;
089    }
090    
091}