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.GetTrainingClassSectionsForm;
020import com.echothree.control.user.training.common.result.TrainingResultFactory;
021import com.echothree.model.control.party.common.PartyTypes;
022import com.echothree.model.control.security.common.SecurityRoleGroups;
023import com.echothree.model.control.security.common.SecurityRoles;
024import com.echothree.model.control.training.server.control.TrainingControl;
025import com.echothree.model.control.training.server.logic.PartyTrainingClassLogic;
026import com.echothree.model.data.training.server.entity.TrainingClass;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.server.control.BaseSimpleCommand;
033import com.echothree.util.server.control.CommandSecurityDefinition;
034import com.echothree.util.server.control.PartyTypeDefinition;
035import com.echothree.util.server.control.SecurityRoleDefinition;
036import com.echothree.util.server.persistence.Session;
037import java.util.List;
038import javax.enterprise.context.Dependent;
039
040@Dependent
041public class GetTrainingClassSectionsCommand
042        extends BaseSimpleCommand<GetTrainingClassSectionsForm> {
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.TrainingClassSection.name(), SecurityRoles.List.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    /** Creates a new instance of GetTrainingClassSectionsCommand */
068    public GetTrainingClassSectionsCommand() {
069        super(null, FORM_FIELD_DEFINITIONS, true);
070    }
071    
072    @Override
073    protected CommandSecurityDefinition getCommandSecurityDefinition() {
074        return form.getTrainingClassName() == null ? employeeCommandSecurityDefinition : testingCommandSecurityDefinition;
075    }
076
077    @Override
078    protected BaseResult execute() {
079        var result = TrainingResultFactory.getGetTrainingClassSectionsResult();
080        var trainingClassName = form.getTrainingClassName();
081        var partyTrainingClassName = form.getPartyTrainingClassName();
082        var parameterCount = (trainingClassName == null ? 0 : 1) + (partyTrainingClassName == null ? 0 : 1);
083
084        if(parameterCount == 1) {
085            var trainingControl = Session.getModelController(TrainingControl.class);
086            TrainingClass trainingClass = null;
087            var partyPK = getPartyPK();
088
089            if(trainingClassName != null) {
090                trainingClass = trainingControl.getTrainingClassByName(trainingClassName);
091
092                if(trainingClass == null) {
093                    addExecutionError(ExecutionErrors.UnknownTrainingClassName.name(), trainingClassName);
094                }
095            } else {
096                var partyTrainingClass = trainingControl.getPartyTrainingClassByName(partyTrainingClassName);
097
098                if(partyTrainingClass != null) {
099                    PartyTrainingClassLogic.getInstance().checkPartyTrainingClassStatus(this, partyTrainingClass, partyPK);
100
101                    if(!hasExecutionErrors()) {
102                        trainingClass = partyTrainingClass.getLastDetail().getTrainingClass();
103                    }
104                } else {
105                    addExecutionError(ExecutionErrors.UnknownPartyTrainingClassName.name(), partyTrainingClassName);
106                }
107            }
108
109            if(!hasExecutionErrors()) {
110                result.setTrainingClass(trainingControl.getTrainingClassTransfer(getUserVisit(), trainingClass));
111                result.setTrainingClassSections(trainingControl.getTrainingClassSectionTransfers(getUserVisit(), trainingClass));
112            }
113        } else {
114            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
115        }
116
117
118        return result;
119    }
120    
121}