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.training.server.command;
018
019import com.echothree.control.user.training.common.form.GetTrainingClassForm;
020import com.echothree.control.user.training.common.result.TrainingResultFactory;
021import com.echothree.model.control.core.common.EventTypes;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.security.common.SecurityRoleGroups;
024import com.echothree.model.control.security.common.SecurityRoles;
025import com.echothree.model.control.training.server.control.TrainingControl;
026import com.echothree.model.control.training.server.logic.PartyTrainingClassLogic;
027import com.echothree.model.data.training.server.entity.TrainingClass;
028import com.echothree.model.data.user.common.pk.UserVisitPK;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.server.control.BaseSimpleCommand;
034import com.echothree.util.server.control.CommandSecurityDefinition;
035import com.echothree.util.server.control.PartyTypeDefinition;
036import com.echothree.util.server.control.SecurityRoleDefinition;
037import com.echothree.util.server.persistence.Session;
038import java.util.List;
039import javax.enterprise.context.Dependent;
040
041@Dependent
042public class GetTrainingClassCommand
043        extends BaseSimpleCommand<GetTrainingClassForm> {
044    
045    private final static CommandSecurityDefinition employeeCommandSecurityDefinition;
046    private final static CommandSecurityDefinition testingCommandSecurityDefinition;
047    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
048    
049    static {
050        employeeCommandSecurityDefinition = new CommandSecurityDefinition(List.of(
051                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
052                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
053                        new SecurityRoleDefinition(SecurityRoleGroups.TrainingClass.name(), SecurityRoles.Review.name())
054                        ))
055                ));
056
057        testingCommandSecurityDefinition = new CommandSecurityDefinition(List.of(
058                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
059                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), null)
060                ));
061
062        FORM_FIELD_DEFINITIONS = List.of(
063                new FieldDefinition("TrainingClassName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("PartyTrainingClassName", FieldType.ENTITY_NAME, false, null, null)
065                );
066    }
067    
068    /** Creates a new instance of GetTrainingClassCommand */
069    public GetTrainingClassCommand() {
070        super(null, FORM_FIELD_DEFINITIONS, true);
071    }
072
073    @Override
074    protected CommandSecurityDefinition getCommandSecurityDefinition() {
075        return form.getTrainingClassName() == null ? employeeCommandSecurityDefinition : testingCommandSecurityDefinition;
076    }
077
078    @Override
079    protected BaseResult execute() {
080        var result = TrainingResultFactory.getGetTrainingClassResult();
081        var trainingClassName = form.getTrainingClassName();
082        var partyTrainingClassName = form.getPartyTrainingClassName();
083        var parameterCount = (trainingClassName == null ? 0 : 1) + (partyTrainingClassName == null ? 0 : 1);
084
085        if(parameterCount == 1) {
086            var trainingControl = Session.getModelController(TrainingControl.class);
087            TrainingClass trainingClass = null;
088            var partyPK = getPartyPK();
089
090            if(trainingClassName != null) {
091                trainingClass = trainingControl.getTrainingClassByName(trainingClassName);
092
093                if(trainingClass == null) {
094                    addExecutionError(ExecutionErrors.UnknownTrainingClassName.name(), trainingClassName);
095                }
096            } else {
097                var partyTrainingClass = trainingControl.getPartyTrainingClassByName(partyTrainingClassName);
098
099                if(partyTrainingClass != null) {
100                    PartyTrainingClassLogic.getInstance().checkPartyTrainingClassStatus(this, partyTrainingClass, partyPK);
101
102                    if(!hasExecutionErrors()) {
103                        trainingClass = partyTrainingClass.getLastDetail().getTrainingClass();
104                    }
105                } else {
106                    addExecutionError(ExecutionErrors.UnknownPartyTrainingClassName.name(), partyTrainingClassName);
107                }
108            }
109
110            if(!hasExecutionErrors()) {
111                result.setTrainingClass(trainingControl.getTrainingClassTransfer(getUserVisit(), trainingClass));
112
113                sendEvent(trainingClass.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
114            }
115        } else {
116            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
117        }
118
119        return result;
120    }
121    
122}