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.CreateTrainingClassSectionTranslationForm;
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.core.server.entity.MimeType;
028import com.echothree.model.data.party.server.entity.Language;
029import com.echothree.model.data.training.server.entity.TrainingClass;
030import com.echothree.model.data.training.server.entity.TrainingClassSection;
031import com.echothree.model.data.training.server.entity.TrainingClassSectionTranslation;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.common.command.BaseResult;
037import com.echothree.util.server.control.BaseSimpleCommand;
038import com.echothree.util.server.control.CommandSecurityDefinition;
039import com.echothree.util.server.control.PartyTypeDefinition;
040import com.echothree.util.server.control.SecurityRoleDefinition;
041import com.echothree.util.server.persistence.Session;
042import java.util.Arrays;
043import java.util.Collections;
044import java.util.List;
045
046public class CreateTrainingClassSectionTranslationCommand
047        extends BaseSimpleCommand<CreateTrainingClassSectionTranslationForm> {
048    
049    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
050    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
051    
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
056                        new SecurityRoleDefinition(SecurityRoleGroups.TrainingClassSection.name(), SecurityRoles.Translation.name())
057                        )))
058                )));
059
060        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
061                new FieldDefinition("TrainingClassName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("TrainingClassSectionName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L),
065                new FieldDefinition("OverviewMimeTypeName", FieldType.MIME_TYPE, false, null, null),
066                new FieldDefinition("Overview", FieldType.STRING, false, null, null),
067                new FieldDefinition("IntroductionMimeTypeName", FieldType.MIME_TYPE, false, null, null),
068                new FieldDefinition("Introduction", FieldType.STRING, false, null, null)
069                ));
070    }
071    
072    /** Creates a new instance of CreateTrainingClassSectionTranslationCommand */
073    public CreateTrainingClassSectionTranslationCommand(UserVisitPK userVisitPK, CreateTrainingClassSectionTranslationForm form) {
074        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
075    }
076    
077    @Override
078    protected BaseResult execute() {
079        var trainingControl = Session.getModelController(TrainingControl.class);
080        String trainingClassName = form.getTrainingClassName();
081        TrainingClass trainingClass = trainingControl.getTrainingClassByName(trainingClassName);
082
083        if(trainingClass != null) {
084            String trainingClassSectionName = form.getTrainingClassSectionName();
085            TrainingClassSection trainingClassSection = trainingControl.getTrainingClassSectionByName(trainingClass, trainingClassSectionName);
086
087            if(trainingClassSection != null) {
088                var partyControl = Session.getModelController(PartyControl.class);
089                String languageIsoName = form.getLanguageIsoName();
090                Language language = partyControl.getLanguageByIsoName(languageIsoName);
091
092                if(language != null) {
093                    TrainingClassSectionTranslation trainingClassSectionTranslation = trainingControl.getTrainingClassSectionTranslation(trainingClassSection, language);
094
095                    if(trainingClassSectionTranslation == null) {
096                        MimeTypeLogic mimeTypeLogic = MimeTypeLogic.getInstance();
097                        String overviewMimeTypeName = form.getOverviewMimeTypeName();
098                        String overview = form.getOverview();
099
100                        MimeType overviewMimeType = mimeTypeLogic.checkMimeType(this, overviewMimeTypeName, overview, MimeTypeUsageTypes.TEXT.name(),
101                                ExecutionErrors.MissingRequiredOverviewMimeTypeName.name(), ExecutionErrors.MissingRequiredOverview.name(),
102                                ExecutionErrors.UnknownOverviewMimeTypeName.name(), ExecutionErrors.UnknownOverviewMimeTypeUsage.name());
103
104                        if(!hasExecutionErrors()) {
105                            String introductionMimeTypeName = form.getIntroductionMimeTypeName();
106                            String introduction = form.getIntroduction();
107
108                            MimeType introductionMimeType = mimeTypeLogic.checkMimeType(this, introductionMimeTypeName, introduction, MimeTypeUsageTypes.TEXT.name(),
109                                    ExecutionErrors.MissingRequiredIntroductionMimeTypeName.name(), ExecutionErrors.MissingRequiredIntroduction.name(),
110                                    ExecutionErrors.UnknownIntroductionMimeTypeName.name(), ExecutionErrors.UnknownIntroductionMimeTypeUsage.name());
111
112                            if(!hasExecutionErrors()) {
113                                var description = form.getDescription();
114
115                                trainingControl.createTrainingClassSectionTranslation(trainingClassSection, language, description, overviewMimeType, overview,
116                                        introductionMimeType, introduction, getPartyPK());
117                            }
118                        }
119                    } else {
120                        addExecutionError(ExecutionErrors.DuplicateTrainingClassSectionTranslation.name(), trainingClassName, trainingClassSectionName, languageIsoName);
121                    }
122                } else {
123                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
124                }
125            } else {
126                addExecutionError(ExecutionErrors.UnknownTrainingClassSectionName.name(), trainingClassName, trainingClassSectionName);
127            }
128        } else {
129            addExecutionError(ExecutionErrors.UnknownTrainingClassName.name(), trainingClassName);
130        }
131
132        return null;
133    }
134    
135}