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.control.user.workflow.server.command;
018
019import com.echothree.control.user.workflow.common.form.DeleteWorkflowDestinationSecurityRoleForm;
020import com.echothree.model.control.party.common.PartyTypes;
021import com.echothree.model.control.party.server.control.PartyControl;
022import com.echothree.model.control.security.common.SecurityRoleGroups;
023import com.echothree.model.control.security.common.SecurityRoles;
024import com.echothree.model.control.security.server.control.SecurityControl;
025import com.echothree.model.control.workflow.server.control.WorkflowControl;
026import com.echothree.model.data.party.server.entity.PartyType;
027import com.echothree.model.data.security.server.entity.SecurityRole;
028import com.echothree.model.data.security.server.entity.SecurityRoleGroup;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.model.data.workflow.server.entity.Workflow;
031import com.echothree.model.data.workflow.server.entity.WorkflowDestination;
032import com.echothree.model.data.workflow.server.entity.WorkflowDestinationPartyType;
033import com.echothree.model.data.workflow.server.entity.WorkflowDestinationSecurityRole;
034import com.echothree.model.data.workflow.server.entity.WorkflowStep;
035import com.echothree.util.common.message.ExecutionErrors;
036import com.echothree.util.common.validation.FieldDefinition;
037import com.echothree.util.common.validation.FieldType;
038import com.echothree.util.common.command.BaseResult;
039import com.echothree.util.server.control.BaseSimpleCommand;
040import com.echothree.util.server.control.CommandSecurityDefinition;
041import com.echothree.util.server.control.PartyTypeDefinition;
042import com.echothree.util.server.control.SecurityRoleDefinition;
043import com.echothree.util.server.persistence.Session;
044import java.util.Arrays;
045import java.util.Collections;
046import java.util.List;
047
048public class DeleteWorkflowDestinationSecurityRoleCommand
049        extends BaseSimpleCommand<DeleteWorkflowDestinationSecurityRoleForm> {
050    
051    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
052    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
053    
054    static {
055        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
058                    new SecurityRoleDefinition(SecurityRoleGroups.WorkflowDestination.name(), SecurityRoles.SecurityRole.name())
059                    )))
060                )));
061
062        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
063                new FieldDefinition("WorkflowName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("WorkflowStepName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("WorkflowDestinationName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("PartyTypeName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("SecurityRoleName", FieldType.ENTITY_NAME, true, null, null)
068                ));
069    }
070    
071    /** Creates a new instance of DeleteWorkflowDestinationSecurityRoleCommand */
072    public DeleteWorkflowDestinationSecurityRoleCommand(UserVisitPK userVisitPK, DeleteWorkflowDestinationSecurityRoleForm form) {
073        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
074    }
075    
076    @Override
077    protected BaseResult execute() {
078        var workflowControl = Session.getModelController(WorkflowControl.class);
079        String workflowName = form.getWorkflowName();
080        var workflow = workflowControl.getWorkflowByName(workflowName);
081        
082        if(workflow != null) {
083            String workflowStepName = form.getWorkflowStepName();
084            var workflowStep = workflowControl.getWorkflowStepByName(workflow, workflowStepName);
085            
086            if(workflowStep != null) {
087                String workflowDestinationName = form.getWorkflowDestinationName();
088                WorkflowDestination workflowDestination = workflowControl.getWorkflowDestinationByName(workflowStep, workflowDestinationName);
089                
090                if(workflowDestination != null) {
091                    var partyControl = Session.getModelController(PartyControl.class);
092                    String partyTypeName = form.getPartyTypeName();
093                    PartyType partyType = partyControl.getPartyTypeByName(partyTypeName);
094                    
095                    if(partyType != null) {
096                        WorkflowDestinationPartyType workflowDestinationPartyType = workflowControl.getWorkflowDestinationPartyType(workflowDestination, partyType);
097                        
098                        if(workflowDestinationPartyType != null) {
099                            SecurityRoleGroup securityRoleGroup = workflow.getLastDetail().getSecurityRoleGroup();
100                            
101                            if(securityRoleGroup != null) {
102                                var securityControl = Session.getModelController(SecurityControl.class);
103                                String securityRoleName = form.getSecurityRoleName();
104                                SecurityRole securityRole = securityControl.getSecurityRoleByName(securityRoleGroup, securityRoleName);
105                                
106                                if(securityRole != null) {
107                                    WorkflowDestinationSecurityRole workflowDestinationSecurityRole = workflowControl.getWorkflowDestinationSecurityRoleForUpdate(workflowDestinationPartyType, securityRole);
108                                    
109                                    if(workflowDestinationSecurityRole != null) {
110                                        workflowControl.deleteWorkflowDestinationSecurityRole(workflowDestinationSecurityRole, getPartyPK());
111                                    } else {
112                                        addExecutionError(ExecutionErrors.UnknownWorkflowDestinationSecurityRole.name(), workflowName,
113                                                workflowStepName, workflowDestinationName, partyTypeName, securityRoleName);
114                                    }
115                                } else {
116                                    addExecutionError(ExecutionErrors.UnknownSecurityRoleName.name(),
117                                            securityRoleGroup.getLastDetail().getSecurityRoleGroupName(), securityRoleName);
118                                }
119                            } else {
120                                addExecutionError(ExecutionErrors.WorkflowMissingSecurityRoleGroup.name(), workflowName);
121                            }
122                        } else {
123                            addExecutionError(ExecutionErrors.UnknownWorkflowDestinationPartyType.name(), workflowName,
124                                    workflowDestinationName, partyTypeName);
125                        }
126                    } else {
127                        addExecutionError(ExecutionErrors.UnknownPartyTypeName.name(), partyTypeName);
128                    }
129                } else {
130                    addExecutionError(ExecutionErrors.UnknownWorkflowDestinationName.name(), workflowName, workflowStepName,
131                            workflowDestinationName);
132                }
133            } else {
134                addExecutionError(ExecutionErrors.UnknownWorkflowStepName.name(), workflowName, workflowStepName);
135            }
136        } else {
137            addExecutionError(ExecutionErrors.UnknownWorkflowName.name(), workflowName);
138        }
139        
140        return null;
141    }
142    
143}