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.control.user.workrequirement.server.command;
018
019import com.echothree.control.user.workrequirement.common.form.GetWorkRequirementScopeForm;
020import com.echothree.control.user.workrequirement.common.result.WorkRequirementResultFactory;
021import com.echothree.model.control.core.common.EventTypes;
022import com.echothree.model.control.workeffort.server.control.WorkEffortControl;
023import com.echothree.model.control.workrequirement.server.control.WorkRequirementControl;
024import com.echothree.model.data.user.common.pk.UserVisitPK;
025import com.echothree.util.common.message.ExecutionErrors;
026import com.echothree.util.common.validation.FieldDefinition;
027import com.echothree.util.common.validation.FieldType;
028import com.echothree.util.common.command.BaseResult;
029import com.echothree.util.server.control.BaseSimpleCommand;
030import com.echothree.util.server.persistence.Session;
031import java.util.Arrays;
032import java.util.Collections;
033import java.util.List;
034import javax.enterprise.context.RequestScoped;
035
036@RequestScoped
037public class GetWorkRequirementScopeCommand
038        extends BaseSimpleCommand<GetWorkRequirementScopeForm> {
039    
040    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
041    
042    static {
043        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
044            new FieldDefinition("WorkEffortTypeName", FieldType.ENTITY_NAME, true, null, null),
045            new FieldDefinition("WorkRequirementTypeName", FieldType.ENTITY_NAME, true, null, null),
046            new FieldDefinition("WorkEffortScopeName", FieldType.ENTITY_NAME, true, null, null)
047        ));
048    }
049    
050    /** Creates a new instance of GetWorkRequirementScopeCommand */
051    public GetWorkRequirementScopeCommand() {
052        super(null, FORM_FIELD_DEFINITIONS, true);
053    }
054    
055    @Override
056    protected BaseResult execute() {
057        var workEffortControl = Session.getModelController(WorkEffortControl.class);
058        var result = WorkRequirementResultFactory.getGetWorkRequirementScopeResult();
059        var workEffortTypeName = form.getWorkEffortTypeName();
060        var workEffortType = workEffortControl.getWorkEffortTypeByName(workEffortTypeName);
061        
062        if(workEffortType != null) {
063            var workEffortScopeName = form.getWorkEffortScopeName();
064            var workEffortScope = workEffortControl.getWorkEffortScopeByName(workEffortType, workEffortScopeName);
065            
066            if(workEffortScope != null) {
067                var workRequirementControl = Session.getModelController(WorkRequirementControl.class);
068                var workRequirementTypeName = form.getWorkRequirementTypeName();
069                var workRequirementType = workRequirementControl.getWorkRequirementTypeByName(workEffortType, workRequirementTypeName);
070                
071                if(workRequirementType != null) {
072                    var workRequirementScope = workRequirementControl.getWorkRequirementScope(workEffortScope, workRequirementType);
073                    
074                    result.setWorkRequirementScope(workRequirementControl.getWorkRequirementScopeTransfer(getUserVisit(), workRequirementScope));
075                    
076                    sendEvent(workRequirementType.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
077                } else {
078                    addExecutionError(ExecutionErrors.UnknownWorkRequirementTypeName.name(), workRequirementTypeName);
079                }
080            } else {
081                addExecutionError(ExecutionErrors.UnknownWorkEffortScopeName.name(), workEffortScopeName);
082            }
083        } else {
084            addExecutionError(ExecutionErrors.UnknownWorkEffortTypeName.name(), workEffortTypeName);
085        }
086        
087        return result;
088    }
089    
090}