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.CreateTrainingClassAnswerTranslationForm; 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 CreateTrainingClassAnswerTranslationCommand 042 extends BaseSimpleCommand<CreateTrainingClassAnswerTranslationForm> { 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.TrainingClassAnswer.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("TrainingClassQuestionName", FieldType.ENTITY_NAME, true, null, null), 059 new FieldDefinition("TrainingClassAnswerName", FieldType.ENTITY_NAME, true, null, null), 060 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null), 061 new FieldDefinition("AnswerMimeTypeName", FieldType.MIME_TYPE, true, null, null), 062 new FieldDefinition("Answer", FieldType.STRING, true, null, null), 063 new FieldDefinition("SelectedMimeTypeName", FieldType.MIME_TYPE, false, null, null), 064 new FieldDefinition("Selected", FieldType.STRING, false, null, null) 065 ); 066 } 067 068 @Inject 069 PartyControl partyControl; 070 071 @Inject 072 TrainingControl trainingControl; 073 074 @Inject 075 MimeTypeLogic mimeTypeLogic; 076 077 078 /** Creates a new instance of CreateTrainingClassAnswerTranslationCommand */ 079 public CreateTrainingClassAnswerTranslationCommand() { 080 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 081 } 082 083 @Override 084 protected BaseResult execute() { 085 var trainingClassName = form.getTrainingClassName(); 086 var trainingClass = trainingControl.getTrainingClassByName(trainingClassName); 087 088 if(trainingClass != null) { 089 var trainingClassSectionName = form.getTrainingClassSectionName(); 090 var trainingClassSection = trainingControl.getTrainingClassSectionByName(trainingClass, trainingClassSectionName); 091 092 if(trainingClassSection != null) { 093 var trainingClassQuestionName = form.getTrainingClassQuestionName(); 094 var trainingClassQuestion = trainingControl.getTrainingClassQuestionByName(trainingClassSection, trainingClassQuestionName); 095 096 if(trainingClassQuestion != null) { 097 var trainingClassAnswerName = form.getTrainingClassAnswerName(); 098 var trainingClassAnswer = trainingControl.getTrainingClassAnswerByName(trainingClassQuestion, trainingClassAnswerName); 099 100 if(trainingClassAnswer != null) { 101 var languageIsoName = form.getLanguageIsoName(); 102 var language = partyControl.getLanguageByIsoName(languageIsoName); 103 104 if(language != null) { 105 var trainingClassAnswerTranslation = trainingControl.getTrainingClassAnswerTranslation(trainingClassAnswer, language); 106 107 if(trainingClassAnswerTranslation == null) { 108 var answerMimeTypeName = form.getAnswerMimeTypeName(); 109 var answer = form.getAnswer(); 110 111 var answerMimeType = mimeTypeLogic.checkMimeType(this, answerMimeTypeName, answer, MimeTypeUsageTypes.TEXT.name(), 112 ExecutionErrors.MissingRequiredAnswerMimeTypeName.name(), ExecutionErrors.MissingRequiredAnswer.name(), 113 ExecutionErrors.UnknownAnswerMimeTypeName.name(), ExecutionErrors.UnknownAnswerMimeTypeUsage.name()); 114 115 if(!hasExecutionErrors()) { 116 var selected = form.getSelected(); 117 var selectedMimeType = mimeTypeLogic.checkMimeType(this, form.getSelectedMimeTypeName(), selected, MimeTypeUsageTypes.TEXT.name(), 118 ExecutionErrors.MissingRequiredSelectedMimeTypeName.name(), ExecutionErrors.MissingRequiredSelected.name(), 119 ExecutionErrors.UnknownSelectedMimeTypeName.name(), ExecutionErrors.UnknownSelectedMimeTypeUsage.name()); 120 121 if(!hasExecutionErrors()) { 122 trainingControl.createTrainingClassAnswerTranslation(trainingClassAnswer, language, answerMimeType, answer, 123 selectedMimeType, selected, getPartyPK()); 124 } 125 } 126 } else { 127 addExecutionError(ExecutionErrors.DuplicateTrainingClassAnswerTranslation.name(), trainingClassName, trainingClassSectionName, 128 trainingClassQuestionName, trainingClassAnswerName, languageIsoName); 129 } 130 } else { 131 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 132 } 133 } else { 134 addExecutionError(ExecutionErrors.UnknownTrainingClassAnswerName.name(), trainingClassName, trainingClassSectionName, 135 trainingClassQuestionName, trainingClassAnswerName); 136 } 137 } else { 138 addExecutionError(ExecutionErrors.UnknownTrainingClassQuestionName.name(), trainingClassName, trainingClassSectionName, 139 trainingClassQuestionName); 140 } 141 } else { 142 addExecutionError(ExecutionErrors.UnknownTrainingClassSectionName.name(), trainingClassName, trainingClassSectionName); 143 } 144 } else { 145 addExecutionError(ExecutionErrors.UnknownTrainingClassName.name(), trainingClassName); 146 } 147 148 return null; 149 } 150 151}