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