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