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.GetTrainingClassAnswerForm;
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.TrainingClassAnswerLogic;
027import com.echothree.model.control.training.server.logic.TrainingClassLogic;
028import com.echothree.model.control.training.server.logic.TrainingClassQuestionLogic;
029import com.echothree.model.control.training.server.logic.TrainingClassSectionLogic;
030import com.echothree.model.data.training.server.entity.TrainingClassAnswer;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseSingleEntityCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import java.util.List;
039import javax.enterprise.context.Dependent;
040import javax.inject.Inject;
041
042@Dependent
043public class GetTrainingClassAnswerCommand
044        extends BaseSingleEntityCommand<TrainingClassAnswer, GetTrainingClassAnswerForm> {
045    
046    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
047    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
048    
049    static {
050        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
051                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
052                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
053                        new SecurityRoleDefinition(SecurityRoleGroups.TrainingClassAnswer.name(), SecurityRoles.Review.name())
054                ))
055        ));
056
057        FORM_FIELD_DEFINITIONS = List.of(
058                new FieldDefinition("TrainingClassName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("TrainingClassSectionName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("TrainingClassQuestionName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("TrainingClassAnswerName", FieldType.ENTITY_NAME, true, null, null)
062        );
063    }
064
065    @Inject
066    TrainingControl trainingControl;
067
068    @Inject
069    TrainingClassLogic trainingClassLogic;
070
071    @Inject
072    TrainingClassSectionLogic trainingClassSectionLogic;
073
074    @Inject
075    TrainingClassQuestionLogic trainingClassQuestionLogic;
076
077    @Inject
078    TrainingClassAnswerLogic trainingClassAnswerLogic;
079
080    /** Creates a new instance of GetTrainingClassAnswerCommand */
081    public GetTrainingClassAnswerCommand() {
082        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
083    }
084    
085    @Override
086    protected TrainingClassAnswer getEntity() {
087        var trainingClassName = form.getTrainingClassName();
088        var trainingClass = trainingClassLogic.getTrainingClassByName(this, trainingClassName, false);
089        TrainingClassAnswer trainingClassAnswer = null;
090
091        if(!hasExecutionErrors()) {
092            var trainingClassSectionName = form.getTrainingClassSectionName();
093            var trainingClassSection = trainingClassSectionLogic.getTrainingClassSectionByName(this, trainingClass, trainingClassSectionName);
094
095            if(!hasExecutionErrors()) {
096                var trainingClassQuestionName = form.getTrainingClassQuestionName();
097                var trainingClassQuestion = trainingClassQuestionLogic.getTrainingClassQuestionByName(this, trainingClassSection, trainingClassQuestionName);
098
099                if(!hasExecutionErrors()) {
100                    var trainingClassAnswerName = form.getTrainingClassAnswerName();
101                    trainingClassAnswer = trainingClassAnswerLogic.getTrainingClassAnswerByName(this, trainingClassQuestion, trainingClassAnswerName);
102
103                    if(!hasExecutionErrors()) {
104                        sendEvent(trainingClassAnswer.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
105                    }
106                }
107            }
108        }
109
110        return trainingClassAnswer;
111    }
112
113    @Override
114    protected BaseResult getResult(TrainingClassAnswer trainingClassAnswer) {
115        var result = TrainingResultFactory.getGetTrainingClassAnswerResult();
116
117        if(trainingClassAnswer != null) {
118            result.setTrainingClassAnswer(trainingControl.getTrainingClassAnswerTransfer(getUserVisit(), trainingClassAnswer));
119        }
120
121        return result;
122    }
123    
124}