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.DeleteWorkflowEntranceSecurityRoleForm;
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.WorkflowEntrance;
032import com.echothree.model.data.workflow.server.entity.WorkflowEntrancePartyType;
033import com.echothree.model.data.workflow.server.entity.WorkflowEntranceSecurityRole;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.validation.FieldDefinition;
036import com.echothree.util.common.validation.FieldType;
037import com.echothree.util.common.command.BaseResult;
038import com.echothree.util.server.control.BaseSimpleCommand;
039import com.echothree.util.server.control.CommandSecurityDefinition;
040import com.echothree.util.server.control.PartyTypeDefinition;
041import com.echothree.util.server.control.SecurityRoleDefinition;
042import com.echothree.util.server.persistence.Session;
043import java.util.Arrays;
044import java.util.Collections;
045import java.util.List;
046
047public class DeleteWorkflowEntranceSecurityRoleCommand
048        extends BaseSimpleCommand<DeleteWorkflowEntranceSecurityRoleForm> {
049    
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
052    
053    static {
054        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
055                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
057                    new SecurityRoleDefinition(SecurityRoleGroups.WorkflowEntrance.name(), SecurityRoles.SecurityRole.name())
058                    )))
059                )));
060
061        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
062                new FieldDefinition("WorkflowName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("WorkflowEntranceName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("PartyTypeName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("SecurityRoleName", FieldType.ENTITY_NAME, true, null, null)
066                ));
067    }
068    
069    /** Creates a new instance of DeleteWorkflowEntranceSecurityRoleCommand */
070    public DeleteWorkflowEntranceSecurityRoleCommand(UserVisitPK userVisitPK, DeleteWorkflowEntranceSecurityRoleForm form) {
071        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
072    }
073    
074    @Override
075    protected BaseResult execute() {
076        var workflowControl = Session.getModelController(WorkflowControl.class);
077        String workflowName = form.getWorkflowName();
078        var workflow = workflowControl.getWorkflowByName(workflowName);
079        
080        if(workflow != null) {
081            String workflowEntranceName = form.getWorkflowEntranceName();
082            WorkflowEntrance workflowEntrance = workflowControl.getWorkflowEntranceByName(workflow, workflowEntranceName);
083            
084            if(workflowEntrance != null) {
085                var partyControl = Session.getModelController(PartyControl.class);
086                String partyTypeName = form.getPartyTypeName();
087                PartyType partyType = partyControl.getPartyTypeByName(partyTypeName);
088                
089                if(partyType != null) {
090                    WorkflowEntrancePartyType workflowEntrancePartyType = workflowControl.getWorkflowEntrancePartyType(workflowEntrance, partyType);
091                    
092                    if(workflowEntrancePartyType != null) {
093                        SecurityRoleGroup securityRoleGroup = workflow.getLastDetail().getSecurityRoleGroup();
094
095                        if(securityRoleGroup != null) {
096                            var securityControl = Session.getModelController(SecurityControl.class);
097                            String securityRoleName = form.getSecurityRoleName();
098                            SecurityRole securityRole = securityControl.getSecurityRoleByName(securityRoleGroup, securityRoleName);
099                            
100                            if(securityRole != null) {
101                                WorkflowEntranceSecurityRole workflowEntranceSecurityRole = workflowControl.getWorkflowEntranceSecurityRoleForUpdate(workflowEntrancePartyType, securityRole);
102                                
103                                if(workflowEntranceSecurityRole != null) {
104                                    workflowControl.deleteWorkflowEntranceSecurityRole(workflowEntranceSecurityRole, getPartyPK());
105                                } else {
106                                    addExecutionError(ExecutionErrors.UnknownWorkflowEntranceSecurityRole.name(), workflowName,
107                                            workflowEntranceName, partyTypeName, securityRoleName);
108                                }
109                            } else {
110                                addExecutionError(ExecutionErrors.UnknownSecurityRoleName.name(),
111                                        securityRoleGroup.getLastDetail().getSecurityRoleGroupName(), securityRoleName);
112                            }
113                        } else {
114                            addExecutionError(ExecutionErrors.WorkflowMissingSecurityRoleGroup.name(), workflowName);
115                        }
116                    } else {
117                        addExecutionError(ExecutionErrors.UnknownWorkflowEntrancePartyType.name(), workflowName, workflowEntranceName,
118                                partyTypeName);
119                    }
120                } else {
121                    addExecutionError(ExecutionErrors.UnknownPartyTypeName.name(), partyTypeName);
122                }
123            } else {
124                addExecutionError(ExecutionErrors.UnknownWorkflowEntranceName.name(), workflowEntranceName);
125            }
126        } else {
127            addExecutionError(ExecutionErrors.UnknownWorkflowName.name(), workflowName);
128        }
129        
130        return null;
131    }
132    
133}