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.CreateTrainingClassPageTranslationForm;
020import com.echothree.model.control.core.common.MimeTypeUsageTypes;
021import com.echothree.model.control.core.server.logic.MimeTypeLogic;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.party.server.control.PartyControl;
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.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 java.util.List;
037import javax.enterprise.context.Dependent;
038import javax.inject.Inject;
039
040@Dependent
041public class CreateTrainingClassPageTranslationCommand
042        extends BaseSimpleCommand<CreateTrainingClassPageTranslationForm> {
043    
044    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
045    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
046    
047    static {
048        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
049                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
050                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
051                        new SecurityRoleDefinition(SecurityRoleGroups.TrainingClassPage.name(), SecurityRoles.Translation.name())
052                ))
053        ));
054
055        FORM_FIELD_DEFINITIONS = List.of(
056                new FieldDefinition("TrainingClassName", FieldType.ENTITY_NAME, true, null, null),
057                new FieldDefinition("TrainingClassSectionName", FieldType.ENTITY_NAME, true, null, null),
058                new FieldDefinition("TrainingClassPageName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L),
061                new FieldDefinition("PageMimeTypeName", FieldType.MIME_TYPE, false, null, null),
062                new FieldDefinition("Page", FieldType.STRING, false, null, null)
063        );
064    }
065
066    @Inject
067    PartyControl partyControl;
068
069    @Inject
070    TrainingControl trainingControl;
071
072    @Inject
073    MimeTypeLogic mimeTypeLogic;
074
075    
076    /** Creates a new instance of CreateTrainingClassPageTranslationCommand */
077    public CreateTrainingClassPageTranslationCommand() {
078        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
079    }
080    
081    @Override
082    protected BaseResult execute() {
083        var trainingClassName = form.getTrainingClassName();
084        var trainingClass = trainingControl.getTrainingClassByName(trainingClassName);
085
086        if(trainingClass != null) {
087            var trainingClassSectionName = form.getTrainingClassSectionName();
088            var trainingClassSection = trainingControl.getTrainingClassSectionByName(trainingClass, trainingClassSectionName);
089
090            if(trainingClassSection != null) {
091                var trainingClassPageName = form.getTrainingClassPageName();
092                var trainingClassPage = trainingControl.getTrainingClassPageByName(trainingClassSection, trainingClassPageName);
093
094                if(trainingClassPage != null) {
095                    var languageIsoName = form.getLanguageIsoName();
096                    var language = partyControl.getLanguageByIsoName(languageIsoName);
097
098                    if(language != null) {
099                        var trainingClassPageTranslation = trainingControl.getTrainingClassPageTranslation(trainingClassPage, language);
100
101                        if(trainingClassPageTranslation == null) {
102                            var pageMimeTypeName = form.getPageMimeTypeName();
103                            var page = form.getPage();
104
105                            var pageMimeType = mimeTypeLogic.checkMimeType(this, pageMimeTypeName, page, MimeTypeUsageTypes.TEXT.name(),
106                                    ExecutionErrors.MissingRequiredPageMimeTypeName.name(), ExecutionErrors.MissingRequiredPage.name(),
107                                    ExecutionErrors.UnknownPageMimeTypeName.name(), ExecutionErrors.UnknownPageMimeTypeUsage.name());
108
109                            if(!hasExecutionErrors()) {
110                                var description = form.getDescription();
111
112                                trainingControl.createTrainingClassPageTranslation(trainingClassPage, language, description, pageMimeType, page,
113                                        getPartyPK());
114                            }
115                        } else {
116                            addExecutionError(ExecutionErrors.DuplicateTrainingClassPageTranslation.name(), trainingClassName, trainingClassPageName, languageIsoName);
117                        }
118                    } else {
119                        addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
120                    }
121                } else {
122                    addExecutionError(ExecutionErrors.UnknownTrainingClassPageName.name(), trainingClassName, trainingClassPageName);
123                }
124            } else {
125                addExecutionError(ExecutionErrors.UnknownTrainingClassSectionName.name(), trainingClassName, trainingClassSectionName);
126            }
127        } else {
128            addExecutionError(ExecutionErrors.UnknownTrainingClassName.name(), trainingClassName);
129        }
130
131        return null;
132    }
133    
134}