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
055    /** Creates a new instance of GetWorkRequirementScopeCommand */
056    public GetWorkRequirementScopeCommand() {
057        super(null, FORM_FIELD_DEFINITIONS, true);
058    }
059    
060    @Override
061    protected WorkRequirementScope getEntity() {
062        WorkRequirementScope workRequirementScope = null;
063        var workEffortTypeName = form.getWorkEffortTypeName();
064        var workEffortType = workEffortControl.getWorkEffortTypeByName(workEffortTypeName);
065
066        if(workEffortType != null) {
067            var workEffortScopeName = form.getWorkEffortScopeName();
068            var workEffortScope = workEffortControl.getWorkEffortScopeByName(workEffortType, workEffortScopeName);
069
070            if(workEffortScope != null) {
071                var workRequirementTypeName = form.getWorkRequirementTypeName();
072                var workRequirementType = workRequirementControl.getWorkRequirementTypeByName(workEffortType, workRequirementTypeName);
073
074                if(workRequirementType != null) {
075                    workRequirementScope = workRequirementControl.getWorkRequirementScope(workEffortScope, workRequirementType);
076
077                    if(workRequirementScope != null) {
078                        sendEvent(workRequirementScope.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
079                    } else {
080                        addExecutionError(ExecutionErrors.UnknownWorkRequirementScope.name(), workEffortTypeName, workEffortScopeName, workRequirementTypeName);
081                    }
082                } else {
083                    addExecutionError(ExecutionErrors.UnknownWorkRequirementTypeName.name(), workRequirementTypeName);
084                }
085            } else {
086                addExecutionError(ExecutionErrors.UnknownWorkEffortScopeName.name(), workEffortScopeName);
087            }
088        } else {
089            addExecutionError(ExecutionErrors.UnknownWorkEffortTypeName.name(), workEffortTypeName);
090        }
091
092        return workRequirementScope;
093    }
094
095    @Override
096    protected BaseResult getResult(WorkRequirementScope workRequirementScope) {
097        var result = WorkRequirementResultFactory.getGetWorkRequirementScopeResult();
098
099        if(workRequirementScope != null) {
100            result.setWorkRequirementScope(workRequirementControl.getWorkRequirementScopeTransfer(getUserVisit(), workRequirementScope));
101        }
102
103        return result;
104    }
105    
106}