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
077    /** Creates a new instance of GetTrainingClassCommand */
078    public GetTrainingClassCommand() {
079        super(null, FORM_FIELD_DEFINITIONS, true);
080    }
081
082    @Override
083    protected CommandSecurityDefinition getCommandSecurityDefinition() {
084        return form.getTrainingClassName() == null ? employeeCommandSecurityDefinition : testingCommandSecurityDefinition;
085    }
086
087    @Override
088    protected TrainingClass getEntity() {
089        var trainingClassName = form.getTrainingClassName();
090        var partyTrainingClassName = form.getPartyTrainingClassName();
091        var parameterCount = (trainingClassName == null ? 0 : 1) + (partyTrainingClassName == null ? 0 : 1);
092        TrainingClass trainingClass = null;
093
094        if(parameterCount == 0 || trainingClassName != null) {
095            trainingClass = trainingClassLogic.getTrainingClassByName(this, trainingClassName, true);
096        } else {
097            var partyTrainingClass = partyTrainingClassLogic.getPartyTrainingClassByName(this, partyTrainingClassName);
098
099            if(!hasExecutionErrors()) {
100                partyTrainingClassLogic.checkPartyTrainingClassStatus(this, partyTrainingClass, getPartyPK());
101
102                if(!hasExecutionErrors()) {
103                    trainingClass = partyTrainingClass.getLastDetail().getTrainingClass();
104                }
105            }
106        }
107
108        if(trainingClass != null) {
109            sendEvent(trainingClass.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
110        }
111
112        return trainingClass;
113    }
114
115    @Override
116    protected BaseResult getResult(TrainingClass trainingClass) {
117        var result = TrainingResultFactory.getGetTrainingClassResult();
118
119        if(trainingClass != null) {
120            result.setTrainingClass(trainingControl.getTrainingClassTransfer(getUserVisit(), trainingClass));
121        }
122
123        return result;
124    }
125    
126}