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