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.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.workrequirement.server.entity.WorkRequirementScope;
025import com.echothree.util.common.command.BaseResult;
026import com.echothree.util.common.message.ExecutionErrors;
027import com.echothree.util.common.validation.FieldDefinition;
028import com.echothree.util.common.validation.FieldType;
029import com.echothree.util.server.control.BaseSingleEntityCommand;
030import java.util.List;
031import javax.enterprise.context.Dependent;
032import javax.inject.Inject;
033
034@Dependent
035public class GetWorkRequirementScopeCommand
036        extends BaseSingleEntityCommand<WorkRequirementScope, GetWorkRequirementScopeForm> {
037    
038    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
039    
040    static {
041        FORM_FIELD_DEFINITIONS = List.of(
042                new FieldDefinition("WorkEffortTypeName", FieldType.ENTITY_NAME, true, null, null),
043                new FieldDefinition("WorkRequirementTypeName", FieldType.ENTITY_NAME, true, null, null),
044                new FieldDefinition("WorkEffortScopeName", FieldType.ENTITY_NAME, true, null, null)
045        );
046    }
047    
048    @Inject
049    WorkEffortControl workEffortControl;
050
051    @Inject
052    WorkRequirementControl workRequirementControl;
053
054    /** Creates a new instance of GetWorkRequirementScopeCommand */
055    public GetWorkRequirementScopeCommand() {
056        super(null, FORM_FIELD_DEFINITIONS, true);
057    }
058    
059    @Override
060    protected WorkRequirementScope getEntity() {
061        WorkRequirementScope workRequirementScope = null;
062        var workEffortTypeName = form.getWorkEffortTypeName();
063        var workEffortType = workEffortControl.getWorkEffortTypeByName(workEffortTypeName);
064
065        if(workEffortType != null) {
066            var workEffortScopeName = form.getWorkEffortScopeName();
067            var workEffortScope = workEffortControl.getWorkEffortScopeByName(workEffortType, workEffortScopeName);
068
069            if(workEffortScope != null) {
070                var workRequirementTypeName = form.getWorkRequirementTypeName();
071                var workRequirementType = workRequirementControl.getWorkRequirementTypeByName(workEffortType, workRequirementTypeName);
072
073                if(workRequirementType != null) {
074                    workRequirementScope = workRequirementControl.getWorkRequirementScope(workEffortScope, workRequirementType);
075
076                    if(workRequirementScope != null) {
077                        sendEvent(workRequirementScope.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
078                    } else {
079                        addExecutionError(ExecutionErrors.UnknownWorkRequirementScope.name(), workEffortTypeName, workEffortScopeName, workRequirementTypeName);
080                    }
081                } else {
082                    addExecutionError(ExecutionErrors.UnknownWorkRequirementTypeName.name(), workRequirementTypeName);
083                }
084            } else {
085                addExecutionError(ExecutionErrors.UnknownWorkEffortScopeName.name(), workEffortScopeName);
086            }
087        } else {
088            addExecutionError(ExecutionErrors.UnknownWorkEffortTypeName.name(), workEffortTypeName);
089        }
090
091        return workRequirementScope;
092    }
093
094    @Override
095    protected BaseResult getResult(WorkRequirementScope workRequirementScope) {
096        var result = WorkRequirementResultFactory.getGetWorkRequirementScopeResult();
097
098        if(workRequirementScope != null) {
099            result.setWorkRequirementScope(workRequirementControl.getWorkRequirementScopeTransfer(getUserVisit(), workRequirementScope));
100        }
101
102        return result;
103    }
104    
105}